<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="角色组" prop="role_id">
          <el-cascader
            v-model="form.model.role_id"
            :options="role.select"
            :props="{
              label: 'name',
              value: 'id',
              checkStrictly: true,
              emitPath: false,
            }"
            placeholder="请选择角色组"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.model.account" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          v-if="modal.params"
          :prop="modal.params.type == 'add' ? 'password' : ''"
        >
          <el-input v-model="form.model.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- TODO√ 默认直接上传 operType  -->
        <el-form-item label="头像" prop="avatar">
          <sa-uploader v-model="form.model.avatar" fileType="image"></sa-uploader>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.model.nickname" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.model.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.model.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.model.status" active-value="normal" inactive-value="disabled">
          </el-switch>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button
        v-if="modal.params.type == 'add'"
        v-auth="'admin.auth.admin.add'"
        type="primary"
        @click="confirm"
        >保存</el-button
      >
      <el-button
        v-if="modal.params.type == 'edit'"
        v-auth="'admin.auth.admin.edit'"
        type="primary"
        @click="confirm"
        >更新</el-button
      >
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import admin from '@/app/admin/api';
  import { cloneDeep } from 'lodash';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });

  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      role_id: null,
      account: '',
      avatar: '',
      email: '',
      nickname: '',
      mobile: '',
      password: '',
      status: 'normal',
    },
    rules: {
      role_id: [{ required: true, message: '请选择角色组', trigger: 'blur' }],
      account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
      password: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur',
        },
      ],
      avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }],
      nickname: [{ required: true, message: '请选择昵称', trigger: 'blur' }],
    },
  });
  const loading = ref(false);
  // 获取详情
  async function getDetail(id) {
    loading.value = true;
    const { error, data } = await admin.auth.admin.detail(id);
    error === 0 && (form.model = data);
    loading.value = false;
  }
  // 角色组
  const role = reactive({
    select: [],
  });
  async function getRoleSelect() {
    const { data } = await admin.auth.role.select();
    role.select = data;
  }

  // 表单关闭时提交
  function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      const { error } =
        props.modal.params.type == 'add'
          ? await admin.auth.admin.add(submitForm)
          : await admin.auth.admin.edit(props.modal.params.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
  async function init() {
    getRoleSelect();
    if (props.modal.params.id) {
      await getDetail(props.modal.params.id);
    }
  }
  onMounted(() => {
    init();
  });
</script>
