<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="680"
    :visible="visible"
    :confirm-loading="loading"
    :title="'基本信息'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    :footer="null"
  >
    <a-form
      :label-col="{ md: { span: 5 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 19 }, sm: { span: 24 } }"
    >
      <a-form-item label="账号">
        <span class="ele-text">{{ user.username }}</span>
      </a-form-item>
      <a-form-item label="昵称">
        <span class="ele-text">{{ user.nickname }}</span>
      </a-form-item>
      <a-form-item label="性别">
        <span class="ele-text">{{ user.sexName }}</span>
      </a-form-item>
      <a-form-item label="手机号">
        <span class="ele-text">{{ user.phone }}</span>
      </a-form-item>
      <a-form-item label="角色">
        <a-tag v-for="item in user.roles" :key="item.roleId" color="blue">
          {{ item.roleName }}
        </a-tag>
      </a-form-item>
      <a-form-item label="创建时间">
        <span class="ele-text">{{ user.createTime }}</span>
      </a-form-item>
      <a-form-item label="状态">
        <a-badge
          v-if="typeof user.status === 'number'"
          :status="(['processing', 'error'][user.status] as any)"
          :text="['正常', '冻结'][user.status]"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import type { User } from '@/api/system/user/model';

  const useForm = Form.useForm;

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: User | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 用户信息
  const user = reactive<User>({
    username: '',
    nickname: '',
    sexName: '',
    phone: '',
    roles: [],
    createTime: undefined,
    status: undefined
  });

  // 请求状态
  const loading = ref(true);

  const { resetFields } = useForm(user);

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          assignObject(user, props.data);
        }
      } else {
        resetFields();
      }
    }
  );
</script>
