<template>
  <el-dialog v-model="dialog"
             :title="title"
             @closed="emit('close')">
    <el-container>
      <el-form :model="userInfo" :inline="true" label-width="80px" :rules="rules" ref="formRef">
        <el-form-item prop="name" label="姓名">
          <el-input v-model="userInfo.name"></el-input>
        </el-form-item>

        <el-form-item prop="codeRs" label="人事工号">
          <el-input v-model="userInfo.codeRs"></el-input>
        </el-form-item>

        <el-form-item prop="deptCode" label="科室">
          <xc-select :data="peopleAddData.deptData" v-model="userInfo"
                     :name="['deptCode','deptName']"></xc-select>
        </el-form-item>

        <el-form-item prop="empPoCode" label="职务">
          <xc-select :data="peopleAddData.empPosition" v-model="userInfo" :name="['empPoCode','position']"></xc-select>
        </el-form-item>

        <el-form-item prop="empTitCode" label="职称">
          <xc-select :data="peopleAddData.zdEmpTitle" v-model="userInfo" :name="['empTitCode','title']"></xc-select>
        </el-form-item>

        <el-form-item prop="mark" label="在院标志">
          <xc-select :data="peopleAddData.empInmark" v-model="userInfo" :name="['mark','markName']"></xc-select>
        </el-form-item>

        <el-form-item prop="ifcader" label="干工标志">
          <xc-select :data="peopleAddData.zdIfcadre" v-model="userInfo" :name="['ifcadre','ifcadreName']"></xc-select>
        </el-form-item>

        <el-form-item prop="orderYn" label="处方权">
          <xc-select :data="doctorSPrescription" v-model="userInfo" :name="['orderYn','orderYnName']"></xc-select>
        </el-form-item>

        <el-form-item prop="ysjb" label="病历级别">
          <xc-select :data="peopleAddData.ysjbData" v-model="userInfo" :name="['ysjb','ysjbName']"></xc-select>
        </el-form-item>

        <el-form-item prop="ghChargeType" label="挂号号别">
          <xc-select :data="peopleAddData.mzyZdChargeType" v-model="userInfo"
                     :name="['ghChargeType','ghChargeTypeName']"/>
        </el-form-item>
        <el-form-item prop="doctorXzYp" label="限制级别">
          <xc-select :data="peopleAddData.restrictedDrugLevels" v-model="userInfo"
                     :name="['doctorXzYp','doctorXzYpName']"/>
        </el-form-item>
        <el-form-item prop="loginFlag" label="病区就诊">
          <el-switch v-model="userInfo.loginFlag" active-color="#ff4949" active-text="不允许" active-value="2"
                     inactive-color="#13ce66" inactive-text="允许" inactive-value="1">
          </el-switch>
        </el-form-item>
        <el-form-item prop="delFlag" label="停用">
          <el-switch v-model="userInfo.delFlag" active-color="#ff4949" active-text="停用" active-value="1"
                     inactive-color="#13ce66" inactive-text="开启" inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item prop="socialNo" label="身份证">
          <el-input v-model="userInfo.socialNo" clearable></el-input>
        </el-form-item>
        <el-form-item prop="phoneNo" label="手机号">
          <el-input v-model="userInfo.phoneNo" clearable></el-input>
        </el-form-item>
        <el-form-item prop="ybCode" label="医保">
          <el-input v-model="userInfo.ybCode" clearable></el-input>
        </el-form-item>
        <el-form-item label="兼诊科室">
          <el-select v-model="userInfo.partTimeDept"
                     multiple
                     placeholder="兼诊科室"
                     style="width: 240px">
            <el-option
                v-for="item in peopleAddData.deptData"
                :key="item.code"
                :label="item.name"
                :value="item.code"/>
          </el-select>
        </el-form-item>
      </el-form>
    </el-container>
    <template #footer>
      <div>
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="PersonnelInformationEditing">
import {peopleAddData} from "@/components/settings/permissions/EmployeeInformationData";
import {clone} from "@/utils/clone";
import XcSelect from "@/components/xiao-chan/select/XcSelect.vue";
import Sleep from "@/utils/sleep";
import {idVerificationApi} from "@/api/public-api";
import {isValidPhone} from "@/utils/validate";
import {ElMessage} from "element-plus";
import {modifyEmployeeInfo, saveEmployeeInfo} from "@/api/settings/permission-settings";
import {stringIsBlank} from "@/utils/blank-utils";

const props = defineProps({
  userInfo: {
    type: Object,
    default: {
      name: '添加员工'
    }
  },
  index: {
    type: Number,
    default: -1
  },
})
const emit = defineEmits(['close', 'changeTheData'])
const dialog = ref(true)
const title = stringIsBlank(props.userInfo.name) ? '添加员工' : '正在编辑:' + props.userInfo.name
let userInfo = $ref({})

const doctorSPrescription = [
  {code: '1', name: '普通处方权'},
  {code: '2', name: '毒麻药处方权'},
]

const wardVisit = [
  {code: '1', name: '允许'},
  {code: '2', name: '不允许'}
]

const formRef = ref(null)

const idVerification = async (rule, value, callback) => {
  if (value) {
    const check = await idVerificationApi(value)
    if (check) {
      callback()
    } else {
      callback(new Error("请填写正确的身份证号码"))
    }
  } else {
    callback(new Error("请填写身份证"))
  }
}

const phoneNumber = (rule, value, callback) => {
  if (value) {
    if (isValidPhone(value)) {
      callback()
    } else {
      callback(new Error("请填写正确的手机号"))
    }
  } else {
    callback(new Error("该项不能为空"))
  }
}

const rules = reactive({
  name: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  codeRs: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  deptCode: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  empInmark: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  doctorXzYp: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  socialNo: [
    {required: true, message: '该项不能为空', trigger: 'blur'},
    {validator: idVerification, trigger: 'blur'}
  ],
  phoneNo: [
    {required: true, message: '该项不能为空', trigger: 'change'},
    {validator: phoneNumber, trigger: 'blur'}
  ],
})

const confirm = (formRef) => {
  if (!formRef) return
  formRef.validate(async (valid) => {
    if (valid) {
      if (props.index !== -1) {
        await modifyEmployeeInfo(userInfo);
        emit('changeTheData', props.index, userInfo)
      } else {
        await saveEmployeeInfo(userInfo)
        emit('changeTheData', props.index, userInfo)
      }

    } else {
      ElMessage.error("请仔细检查")
    }
  })

}

onMounted(async () => {
  await Sleep(500)
  if (props.userInfo) {
    userInfo = clone(props.userInfo)
    userInfo.oldData = true
    if (userInfo.partTimeDeptCode) {
      userInfo.partTimeDept = userInfo.partTimeDeptCode.split(',')
    }
  } else {
    userInfo.oldData = false
  }
})


</script>

<style scoped>

</style>
