<template>
  <vab-dialog v-model="dialogFormVisible" append-to-body :title="title" width="600px" @close="close">
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-row>
        <el-col v-if="form.parentId !== 0" :span="24">
          <el-form-item label="上级部门" prop="parentId">
            <el-tree-select
              v-model="form.parentId"
              check-strictly
              :data="deptOptions"
              placeholder="选择上级部门"
              :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
              value-key="deptId"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="form.deptName" placeholder="请输入部门名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="leader">
            <el-input v-model="form.leader" maxlength="20" placeholder="请输入负责人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="form.phone" maxlength="11" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" maxlength="50" placeholder="请输入邮箱" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门状态">
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="save">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </template>
  </vab-dialog>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { doAdd, doEdit, getDept, listDept, listDeptExcludeChild } from '/@/api/departmentManagement'

defineOptions({
  name: 'DepartmentManagementEdit2',
})

const { proxy } = getCurrentInstance() as any
const { sys_normal_disable } = proxy.useDict('sys_normal_disable')

const emit = defineEmits(['fetch-data'])

const deptOptions = ref<any>([])
const formRef = ref<FormInstance>()
const form = ref<any>({
  deptId: undefined,
  parentId: undefined,
  deptName: undefined,
  orderNum: 0,
  leader: undefined,
  phone: undefined,
  email: undefined,
  status: '0',
})
const rules = reactive<any>({
  parentId: [{ required: true, message: '上级部门不能为空', trigger: 'blur' }],
  deptName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
  orderNum: [{ required: true, message: '显示排序不能为空', trigger: 'blur' }],
  email: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }],
  phone: [{ pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur' }],
})
const title = ref<string>('')
const dialogFormVisible = ref<boolean>(false)

const showAdd = (row?: any) => {
  reset()
  listDept().then((response) => {
    deptOptions.value = proxy.handleTree(response.data, 'deptId')
  })
  title.value = '添加'
  dialogFormVisible.value = true
  nextTick(() => {
    if (row !== undefined) {
      form.value.parentId = row.deptId
    }
  })
}

const showEdit = (row: any) => {
  reset()
  title.value = '编辑'
  listDeptExcludeChild(row.deptId).then((response) => {
    deptOptions.value = proxy.handleTree(response.data, 'deptId')
  })
  dialogFormVisible.value = true
  nextTick(() => {
    getDept(row.deptId).then((response: any) => {
      form.value = response.data
    })
  })
}

/** 表单重置 */
function reset() {
  form.value = {
    deptId: undefined,
    parentId: undefined,
    deptName: undefined,
    orderNum: 0,
    leader: undefined,
    phone: undefined,
    email: undefined,
    status: '0',
  }
  proxy.resetForm('formRef')
}

const close = () => {
  formRef.value?.clearValidate()
  formRef.value?.resetFields()
  emit('fetch-data')
}

const cancel = () => {
  close()
  dialogFormVisible.value = false
}

const save = () => {
  formRef.value?.validate(async (valid: any) => {
    if (valid) {
      if (form.value.deptId) {
        await doEdit(form.value)
        await $baseMessage('修改成功', 'success', 'hey')
      } else {
        await doAdd(form.value)
        await $baseMessage('添加成功', 'success', 'hey')
      }
      cancel()
    }
  })
}

defineExpose({
  showAdd,
  showEdit,
})
</script>

<style lang="scss" scoped>
:deep() {
  .el-select {
    width: 100%;
  }
}
</style>
