<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useProjectList } from '@/composables/useGetProjectList'
import { useMapStore } from '@/stores/map'
import { useUserStore } from '@/stores/user'

import { addStaff, updateStaff, getStaffDetail } from '../api'
const store = useMapStore()
const userStore = useUserStore()

const ruleFormRef = ref<FormInstance>()
const emit = defineEmits<Emits>()
const props = defineProps({
  isShowStaffDialog: {
    type: Boolean
  },
  title: {
    type: String
  },
  isEdit: {
    type: String
  },
  id: {
    type: String
  }
})
interface Emits {
  (e: 'update:isShowStaffDialog', isShowStaffDialog: boolean): void
  (e: 'create'): void
}
const departmentList: any = useProjectList('', '79806610719731712').departmentList
const sexList = store.getMapValuesByKey('system_gender')
const roleList = store.getMapValuesByKey('teacher_role')
const statusList = store.getMapValuesByKey('system_status')
const form: any = reactive({
  organ_id: '',
  name: '',
  gender: '1',
  mobile: '',
  role: [],
  email: '',
  status: '1'
})

const rules = reactive<FormRules>({
  organ_id: [{ required: true, message: '请选择部门/学校', trigger: 'change' }],

  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  mobile: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1(3|4|5|6|7|8|9)\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  role: [{ required: true, message: '请选择角色类型', trigger: 'change' }],
  email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
  status: [{ required: true, message: '请选择生效状态', trigger: 'change' }]
})
if (userStore.roles[0].name !== '超级管理员') {
  form.organ_id = userStore.organization?.id
}
// 取消
const handleCancel = () => {
  emit('update:isShowStaffDialog', false)
}

// 确定
const handleConfirm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(valid => {
    if (valid) {
      if (props.isEdit === '0') {
        form.role = form.role.toString()
        const params: any = Object.assign({}, form)
        if (userStore.roles[0].name === '部门管理员') {
          params.organ_id = userStore.organization?.id
        }
        addStaff(params).then(() => {
          ElMessage.success('新增教工成功')
          emit('update:isShowStaffDialog', false)
          emit('create')
        })
      } else if (props.isEdit === '1') {
        form.role = form.role.toString()
        const params: any = Object.assign({ id: props.id }, form)
        updateStaff(params).then(() => {
          ElMessage.success('更新教工成功')
          emit('update:isShowStaffDialog', false)
          emit('create')
        })
      } else {
        emit('update:isShowStaffDialog', false)
        emit('create')
      }
    }
  })
}
// 获取详情
if (props.isEdit === '2' || props.isEdit === '1') {
  const params: any = { id: props.id }
  getStaffDetail(params).then((res: any) => {
    Object.keys(form).forEach(key => {
      form[key] = res.data[key]
    })
    form.role = res.data.role.split(',')
  })
}
</script>
<template>
  <el-dialog :model-value="isShowStaffDialog" draggable :title="props.title" :before-close="handleCancel" width="30%">
    <el-form :model="form" label-position="right" label-width="auto" :rules="rules" ref="ruleFormRef" label-suffix="：">
      <el-form-item label="所属部门/学校" prop="organ_id">
        <el-select
          clearable
          v-model="form.organ_id"
          :disabled="props.isEdit === '2' || props.isEdit === '1'"
          placeholder="请选择所属部门/学校"
          style="width: 100%"
          v-if="userStore.roles[0].name === '超级管理员'"
        >
          <el-option v-for="item in departmentList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <el-input :placeholder="userStore.organization?.name" v-model="form.organ_id_name" v-else disabled> </el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" :disabled="props.isEdit === '2' || props.isEdit === '1'"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="form.gender" :disabled="props.isEdit === '2'">
          <el-radio v-for="(item, index) in sexList" :key="index" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" :disabled="props.isEdit === '2' || props.isEdit === '1'"></el-input>
      </el-form-item>
      <el-form-item label="角色类型" prop="role">
        <el-checkbox-group v-model="form.role" :disabled="props.isEdit === '2'">
          <el-checkbox v-for="(item, index) in roleList" :key="index" :label="item.value">{{ item.label }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" :disabled="props.isEdit === '2'" />
      </el-form-item>
      <el-form-item label="生效状态" prop="status">
        <el-radio-group v-model="form.status" :disabled="props.isEdit === '2'">
          <el-radio v-for="(item, index) in statusList" :key="index" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer v-if="props.isEdit !== '2'">
      <span>
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm(ruleFormRef)">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>
