<script setup lang="ts">
import md5 from 'js-md5'
import { ElMessage } from 'element-plus'
import { reuls_validator } from './relus'
import { GlobalStore } from '@/stores/index'
import Drawer from '@/components/Drawer/index.vue'
import { User_Power_List } from '@/api/role/index'
import { ref, reactive, onMounted, nextTick } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { User_Post_List, User_Add, User_Edit } from '@/api/user/index'

type Fn = () => void
interface PropsType {
  action: boolean
}

const validate_checkPass = (rule: any, value: any, callback: any) => {
  if (checkPass.value === '') {
    callback(new Error('请再输入一次密码'))
  } else if (checkPass.value !== form.value.T_pass) {
    callback(new Error('两次密码校验不一致'))
  } else {
    callback()
  }
}

const checkPass = ref('')
const formRef = ref()
const _PASS = '******'
let userPowerList: any = []
let userPostList = ref<any[]>([])
const globalStore = GlobalStore()
const formLabelWidth = ref('105px')
const drawerRef = ref<InstanceType<typeof Drawer> | null>(null)
const rules = reactive<FormRules>({
  ...reuls_validator,
  checkPass: [{ required: true, validator: validate_checkPass, trigger: 'blur' }]
})

let form = ref({
  T_power: '',
  T_name: '',
  T_user: '',
  T_pass: '',
  T_dept: '',
  T_post: '',
  T_sex: '',
  T_id_card: '',
  T_nation: '',
  T_school: '',
  T_major: '',
  T_education: '',
  T_marry: '',
  T_phone: '',
  T_entry_time: '',
  T_spouse_name: '',
  T_entry_type: '',
  T_contract_start_time: '',
  T_contract_end_time: '',
  T_remark: '',
  T_positive_time: '',
  T_spouse_phone: '',
  T_expire: '',
  T_dept_leader: ''
})

const getUserPowerList = async () => {
  const params = {
    User_tokey: globalStore.GET_User_tokey,
    page: 1,
    page_z: 9999
  }
  const res: any = await User_Power_List(params)
  userPowerList = res.Data.Data
}

const changeDept = async (val: number) => {
  const res: any = await User_Post_List({ T_dept: val })
  userPostList.value = res.Data
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  checkPass.value = ''
}

const callbackDrawer = (done: Fn) => {
  done()
  nextTick(() => {
    resetForm(formRef.value)
  })
}

const openDrawer = () => {
  drawerRef.value?.openDrawer()
}
const AddUser = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async valid => {
    if (valid) {
      let res: any = {}
      if (props.action) {
        form.value.T_pass = md5(form.value.T_pass)
        res = await User_Add(form.value)
      } else {
        form.value.T_pass === _PASS && (form.value.T_pass = '')
        res = await User_Edit(form.value)
      }
      if (res.Code === 200) {
        ElMessage.success(`${props.action ? '添加' : '编辑'}成功！`)
        nextTick(() => {
          drawerRef.value?.closeDrawer()
          emit('onTableList')
          resetForm(formRef.value)
          emit('onaction', true)
        })
      }
    } else {
      return false
    }
  })
}

const DataEcho = async (row: any) => {
  row.T_entry_type = +row.T_entry_type
  row.T_pass = _PASS
  const res: any = await User_Post_List({ T_dept: row.T_dept })
  userPostList.value = res.Data
  nextTick(() => {
    form.value = { ...row }
  })
}

onMounted(() => {
  if (userPowerList.length <= 0) {
    getUserPowerList()
  }
})

const emit = defineEmits<{ (event: 'onTableList'): void; (event: 'onaction', val: boolean): void }>()
const props = defineProps<PropsType>()
defineExpose({
  openDrawer,
  callbackDrawer,
  DataEcho
})
</script>
<template>
  <Drawer ref="drawerRef" :handleClose="callbackDrawer">
    <template #header="{ params }">
      <h4 :id="params.titleId" :class="params.titleClass">{{ action ? '添加' : '编辑' }} - 账户管理</h4>
    </template>
    <el-form :model="form" ref="formRef" :rules="rules">
      <el-form-item label="角色权限：" :label-width="formLabelWidth" prop="T_power">
        <el-select v-model="form.T_power" placeholder="请选择角色">
          <el-option v-for="item in userPowerList" :key="item.T_id" :label="item.T_name" :value="item.T_id" />
        </el-select>
      </el-form-item>
      <el-form-item label="名称：" :label-width="formLabelWidth" prop="T_name">
        <el-input v-model="form.T_name" autocomplete="off" placeholder="名称" />
      </el-form-item>
      <el-form-item label="负责人：" :label-width="formLabelWidth" prop="T_dept_leader">
        <el-radio-group v-model="form.T_dept_leader">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="账号：" :label-width="formLabelWidth" prop="T_user">
        <el-input v-model="form.T_user" autocomplete="off" placeholder="账号" />
      </el-form-item>
      <el-form-item label="密码：" :label-width="formLabelWidth" prop="T_pass">
        <el-input v-model="form.T_pass" type="password" autocomplete="off" placeholder="密码" />
      </el-form-item>
      <el-form-item label="确认密码：" :label-width="formLabelWidth" prop="checkPass">
        <el-input v-model="checkPass" type="password" autocomplete="off" placeholder="确认密码" />
      </el-form-item>
      <el-form-item label="部门：" :label-width="formLabelWidth" prop="T_dept">
        <el-select v-model="form.T_dept" placeholder="请选择部门" @change="changeDept">
          <el-option v-for="item in globalStore.GET_Dept_List" :key="item.Id" :label="item.T_name" :value="item.Id" />
        </el-select>
      </el-form-item>
      <el-form-item label="岗位：" :label-width="formLabelWidth" prop="T_post">
        <el-select v-model="form.T_post" placeholder="请选择岗位">
          <el-option v-for="item in userPostList" :key="item.Id" :label="item.T_name" :value="item.Id" />
        </el-select>
      </el-form-item>
      <el-form-item label="性别：" :label-width="formLabelWidth" prop="T_sex">
        <el-radio-group v-model="form.T_sex" class="ml-4">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="身份证号：" :label-width="formLabelWidth" prop="T_id_card">
        <el-input v-model="form.T_id_card" autocomplete="off" placeholder="身份证号" />
      </el-form-item>
      <el-form-item label="民族：" :label-width="formLabelWidth" prop="T_nation">
        <el-input v-model="form.T_nation" autocomplete="off" placeholder="民族" />
      </el-form-item>
      <el-form-item label="毕业院校：" :label-width="formLabelWidth" prop="T_school">
        <el-input v-model="form.T_school" autocomplete="off" placeholder="毕业院校" />
      </el-form-item>
      <el-form-item label="专业：" :label-width="formLabelWidth" prop="T_major">
        <el-input v-model="form.T_major" autocomplete="off" placeholder="专业" />
      </el-form-item>
      <el-form-item label="学历：" :label-width="formLabelWidth" prop="T_education">
        <el-input v-model="form.T_education" autocomplete="off" placeholder="学历" />
      </el-form-item>
      <el-form-item label="联系电话：" :label-width="formLabelWidth" prop="T_phone">
        <el-input v-model="form.T_phone" autocomplete="off" placeholder="联系电话" />
      </el-form-item>
      <el-form-item label="婚否：" :label-width="formLabelWidth" prop="T_marry">
        <el-radio-group v-model="form.T_marry" class="ml-4">
          <el-radio :label="0">未婚</el-radio>
          <el-radio :label="1">已婚</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入职时间：" :label-width="formLabelWidth" prop="T_entry_time">
        <el-date-picker
          style="width: 100%"
          class="my-date-picker"
          v-model="form.T_entry_time"
          type="date"
          placeholder="入职时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="转正时间：" :label-width="formLabelWidth" prop="T_positive_time">
        <el-date-picker
          style="width: 100%"
          class="my-date-picker"
          v-model="form.T_positive_time"
          type="date"
          placeholder="转正时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="配偶姓名：" label-width="120px">
        <el-input v-model="form.T_spouse_name" autocomplete="off" placeholder="配偶姓名" />
      </el-form-item>
      <el-form-item label="配偶联系电话：" label-width="120px">
        <el-input v-model="form.T_spouse_phone" autocomplete="off" placeholder="配偶联系电话" />
      </el-form-item>
      <el-form-item label="入职类型：" :label-width="formLabelWidth" prop="T_entry_type">
        <el-radio-group v-model="form.T_entry_type" class="ml-4">
          <el-radio :label="1">全职</el-radio>
          <el-radio :label="2">兼职</el-radio>
          <el-radio :label="3">实习生</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="劳动合同开始时间：" label-width="160px" prop="T_contract_start_time">
        <el-date-picker
          style="width: 100%"
          class="my-date-picker"
          v-model="form.T_contract_start_time"
          type="date"
          placeholder="劳动合同开始时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="劳动合同结束时间：" label-width="160px" prop="T_contract_end_time">
        <el-date-picker
          style="width: 100%"
          class="my-date-picker"
          v-model="form.T_contract_end_time"
          type="date"
          placeholder="劳动合同结束时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="是否到期：" :label-width="formLabelWidth" prop="T_expire">
        <el-radio-group v-model="form.T_expire" class="ml-4">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注：" :label-width="formLabelWidth">
        <el-input v-model="form.T_remark" type="textarea" autocomplete="off" placeholder="备注" />
      </el-form-item>

      <el-form-item :label-width="formLabelWidth">
        <el-button v-if="action" color="#626aef" @click="AddUser(formRef)">添加</el-button>
        <el-button v-else color="#626aef" @click="AddUser(formRef)">编辑</el-button>
      </el-form-item>
    </el-form>
  </Drawer>
</template>
<style scoped lang="scss"></style>
