<template>
  <a-modal
    :title="title"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入姓名"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select placeholder="请选择性别" v-decorator="[ 'gender', validatorRules.gender]" >
                <a-select-option value="男">男</a-select-option>
                <a-select-option value="女">女</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="证件类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select placeholder="请选择证件类型" v-decorator="[ 'idType', validatorRules.idType]" >
                <a-select-option value="身份证">身份证</a-select-option>
                <a-select-option value="港澳台胞证">港澳台胞证</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="证件号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'idNo', validatorRules.idNo]" placeholder="请输入证件号码"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="入职日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择入职日期" v-decorator="[ 'inDate', validatorRules.inDate]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tree-select
                v-model="selectedDept"
                style="width: 100%"
                :tree-data="deptTree"
                tree-checkable
                allow-clear
                placeholder="请选择部门"
                @change="handleDeptChange"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="岗位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'post', validatorRules.post]" placeholder="请输入岗位"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="员工类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select placeholder="请选择员工类型" v-decorator="[ 'type', validatorRules.type]" >
                <a-select-option value="正式员工">正式员工</a-select-option>
                <a-select-option value="临时">临时</a-select-option>
                <a-select-option value="实习生">实习生</a-select-option>
                <a-select-option value="劳务工">劳务工</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="办公电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'tel', validatorRules.tel]" placeholder="请输入办公电话"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="移动电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'mobile', validatorRules.mobile]" placeholder="请输入移动电话"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="邮件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'email', validatorRules.email]" placeholder="请输入邮件"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="员工状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select placeholder="请选择员工状态" v-decorator="[ 'status', validatorRules.status]" >
                <a-select-option value="在职">在职</a-select-option>
                <a-select-option value="离职">离职</a-select-option>
                <a-select-option value="退休">退休</a-select-option>
                <a-select-option value="辞退">辞退</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="工资开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'bank', validatorRules.bank]" placeholder="请输入工资开户行"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="工资账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'bankAccount', validatorRules.bankAccount]" placeholder="请输入工资账号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="公积金号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'cpf', validatorRules.cpf]" placeholder="请输入公积金号"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="社保缴纳地" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'ssPlace', validatorRules.ssPlace]" placeholder="请输入社保缴纳地"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="社保号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'ssAccount', validatorRules.ssAccount]" placeholder="请输入社保号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="岗位职能要求" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea  v-decorator="[ 'requirement', validatorRules.requirement]" placeholder="请输入岗位职能要求" :rows="4"></a-textarea >
            </a-form-item>
          </a-col>
        </a-row>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import { queryDepartTreeList } from '@/api/api'

  export default {
    name: "StaffModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {rules: [
            { required: true, message: '请输入姓名!' }
          ]},
          gender: {rules: [
          ]},
          idType: {rules: [
          ]},
          idNo: {rules: [
          ]},
          inDate: {rules: [
          ]},
          dept: {rules: [
          ]},
          deptId: {rules: [
          ]},
          post: {rules: [
          ]},
          type: {rules: [
          ]},
          tel: {rules: [
          ]},
          mobile: {rules: [
          ]},
          email: {rules: [
          ]},
          requirement: {rules: [
          ]},
          accesory: {rules: [
          ]},
          bank: {rules: [
          ]},
          bankAccount: {rules: [
          ]},
          cpf: {rules: [
          ]},
          ssPlace: {rules: [
          ]},
          ssAccount: {rules: [
          ]},
          status: {rules: [
          ]},
        },
        url: {
          add: "/oa/staff/add",
          edit: "/oa/staff/edit",
        },
        deptTree: [],
        selectedDept:[],
        selectedDeptName:'',
      }
    },
    mounted () {
      this.initTreeData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','gender','idType','idNo','inDate','dept','post','type','tel','mobile','email','requirement','accesory','bank','bankAccount','cpf','ssPlace','ssAccount','status','deptId'))
        })
        if(this.model){
          this.selectedDept = this.model.deptId==undefined||this.model.deptId.length==0?[]:this.model.deptId.split(",")
          this.selectedDeptName = this.model.dept
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData.deptId = this.selectedDept.join(',')
            formData.dept = this.selectedDeptName
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','gender','idType','idNo','inDate','dept','post','type','tel','mobile','email','requirement','accesory','bank','bankAccount','cpf','ssPlace','ssAccount','status','deptId'))
      },

      initTreeData() {
        queryDepartTreeList().then((res)=>{
          if (res.success) {
            this.deptTree = res.result
          } else {
            this.$message.warn(res.message)
          }
        })
      },
      handleDeptChange(value, label, extra) {
        if(!label){
          this.selectedDept = []
          this.selectedDeptName = ''
        } else if (label instanceof Array) {
          this.selectedDept = value
          this.selectedDeptName = label.join(',')
        } else {
          this.selectedDept = value 
          this.selectedDeptName = label 
        }
      },
    }
  }
</script>