<template>
  <view class="content">
    <view class="card">
      <u--form labelPosition="left" :model="form" :rules="rules" ref="form" labelWidth="120px">
        <u-form-item label="照片" prop="userPhotoFile" borderBottom required>
          <u-upload :fileList="form.userPhotoFile" @afterRead="fnAfterRead" @delete="fnDeletePic" :maxCount="1"
                    previewFullImage></u-upload>
        </u-form-item>
        <u-form-item label="姓名" prop="name" borderBottom required>
          <u--input border="none" v-model="form.name"></u--input>
        </u-form-item>
        <u-form-item label="性别" prop="sex" borderBottom required>
          <u--input border="none" v-model="form.sexName" disabled
                    disabledColor="#ffffff"></u--input>
        </u-form-item>
        <u-form-item label="出生年月" prop="dateOfBirth" borderBottom required>
          <u--input border="none" v-model="form.dateOfBirth" disabled disabledColor="#ffffff"></u--input>
        </u-form-item>
        <u-form-item label="年龄" prop="age" borderBottom required>
          <u--input border="none" v-model="form.age" disabled disabledColor="#ffffff"></u--input>
        </u-form-item>
        <u-form-item label="身份证" prop="cardId" borderBottom required>
          <u--input border="none" v-model="form.cardId" @blur="fnCardIdDisassembly"></u--input>
        </u-form-item>
        <u-form-item label="身份证照片(正反面)" prop="userCardIDPhotoFile" borderBottom required labelPosition="top"
                     labelWidth="auto">
          <view class="mt-10 w100">
            <u-upload :fileList="form.userCardIDPhotoFile" @afterRead="fnAttIdPhotoAdd"
                      @delete="fnAttIdPhotoDelete" multiple :maxCount="2" previewFullImage>
            </u-upload>
            <view class="tip">
              温馨提示：用户需上传身份证正反面（身份证照片数量需≥2张），才能进行人员培训
            </view>
          </view>
        </u-form-item>
        <u-form-item label="民族" prop="nationalityName" borderBottom required @click="singleChoiceClick('nationality')">
          <u--input v-model="form.nationalityName" border="none" disabled disabledColor="#ffffff"></u--input>
        </u-form-item>
        <u-form-item label="户口所在地" prop="hklocal" borderBottom required>
          <u--textarea border="none" v-model="form.hklocal" autoHeight></u--textarea>
        </u-form-item>
        <u-form-item label="现住址" prop="address" borderBottom required>
          <u--textarea border="none" v-model="form.address" autoHeight></u--textarea>
        </u-form-item>
        <u-form-item label="联系电话" prop="phone" borderBottom required>
          <u--input border="none" v-model="form.phone" :disabled="true"></u--input>
        </u-form-item>
        <u-form-item label="文化程度" prop="degreeOfEducationName" borderBottom required
                     @click="singleChoiceClick('degreeOfEducation')">
          <u--input v-model="form.degreeOfEducationName" border="none" disabled disabledColor="#ffffff">
          </u--input>
        </u-form-item>
        <view>
          <u-form-item label="婚姻状况" prop="maritalstatusName" borderBottom required
                       @click="singleChoiceClick('maritalstatus')">
            <u--input v-model="form.maritalstatusName" border="none" disabled disabledColor="#ffffff">
            </u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="政治面貌" prop="politicalStatusName" borderBottom required
                       @click="singleChoiceClick('politicalStatus')">
            <u--input v-model="form.politicalStatusName" border="none" disabled disabledColor="#ffffff">
            </u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item v-if="form.politicalStatus === 'zhonggongdangyuan'" label="入党时间" prop="POLITICAL_STATUS"
                       borderBottom required
                       @click="fnShowDatePicke('politicalTime')">
            <u--input v-model="form.politicalTime" border="none" disabled disabledColor="#ffffff">
            </u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="是否缴纳社保" prop="isSocialName" borderBottom required
                       @click="fnShowIsSocial">
            <u--input v-model="form.isSocialName" border="none" disabled disabledColor="#ffffff"
                      inputAlign="right"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item v-if="form.isSocial==='1'" label="社会保障号码" prop="SOCIAL_NUMBER" borderBottom required>
            <u--input border="none" v-model="form.socialNumber"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item v-if="form.isSocial==='1'" label="社会保障卡" prop="socialPhotoFile" borderBottom required
                       labelPosition="top" labelWidth="auto">
            <view class="mt-10 w100">
              <u-upload :fileList="form.socialPhotoFile" @afterRead="fnAttSocialAdd"
                        @delete="fnAttSocialDelete" multiple :maxCount="2" previewFullImage></u-upload>
            </view>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="是否缴纳保险" prop="isBfName" borderBottom required @click="fnShowBf">
            <u--input v-model="form.isBfName" border="none" disabled disabledColor="#ffffff"
                      inputAlign="right"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="是否签订劳动合同" prop="isSignLaborName" borderBottom required
                       @click="fnShowIsSignLabor">
            <u--input v-model="form.isSignLaborName" border="none" disabled disabledColor="#ffffff"
                      inputAlign="right"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item v-if="form.isSignLabor==='1'" label="合同图片" prop="contractFile" borderBottom required
                       labelPosition="top" labelWidth="auto">
            <view class="mt-10 w100">
              <u-upload :fileList="form.contractFile" @afterRead="fnIsSignLaborAdd"
                        @delete="fnIsSignLaborDelete" multiple :maxCount="4" previewFullImage></u-upload>
            </view>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="是否缴纳商业保险" prop="ispayName" borderBottom required @click="fnShowIsPay">
            <u--input v-model="form.ispayName" border="none" disabled disabledColor="#ffffff"
                      inputAlign="right"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item v-if="form.ispay==='1'" label="商业保险单号" prop="ispayNumber" borderBottom required>
            <u--input border="none" v-model="form.ispayNumber"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item v-if="form.ispay==='1'" label="保险图片" prop="insuranceFile" borderBottom required
                       labelPosition="top" labelWidth="auto">
            <view class="mt-10 w100">
              <u-upload :fileList="form.insuranceFile" @afterRead="fnIsPayAdd" @delete="fnIsPayDelete"
                        multiple :maxCount="4" previewFullImage></u-upload>
            </view>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="是否按期缴纳工伤保险" prop="isInjuriesPayName" borderBottom required
                       @click="fnShowEmploymentInjuryInsurance">
            <u--input v-model="form.isInjuriesPayName" border="none" disabled disabledColor="#ffffff"
                      inputAlign="right"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="工伤保险有效期" v-if="form.isInjuriesPay==='1'" prop="isInjuriesPayTime" borderBottom
                       required @click="fnShowInjuriesPayTiemEducation">
            <u--input v-model="form.isInjuriesPayTime" border="none" disabled disabledColor="#ffffff"
                      inputAlign="right"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="上传工伤保险凭证" prop="injuriesPayTiemFile" borderBottom required
                       v-if="form.isInjuriesPay==='1'" labelPosition="top" labelWidth="auto">
            <view class="mt-10 w100">
              <u-upload :fileList="form.injuriesPayTiemFile"
                        @afterRead="fnInjuriesPayTiemAttachmentsAfterRead" @delete="fnInjuriesPayTiemAttachmentsDelete"
                        multiple :maxCount="4" previewFullImage></u-upload>
            </view>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="岗位名称(工种)" prop="postId" borderBottom required>
            <u--textarea border="none" v-model="form.postId" autoHeight></u--textarea>
          </u-form-item>
        </view>
        <view>
          <u-form-item label="是否参加三级安全培训" prop="isLevelThreeName" borderBottom required
                       @click="fnShowPhotosOfLevel">
            <u--input v-model="form.isLevelThreeName" border="none" disabled disabledColor="#ffffff"
                      inputAlign="right"></u--input>
          </u-form-item>
        </view>
        <view>
          <u-form-item v-if="form.isLevelThree === '1'" label="三级安全培训照片" prop="photosOfLevel" borderBottom
                       required labelPosition="top" labelWidth="auto">
            <view class="mt-10 w100">
              <u-upload :fileList="form.photosOfLevel" @afterRead="fnPhotosOfLevelAdd"
                        @delete="fnPhotosOfLevelDelete" multiple :maxCount="4" previewFullImage>
              </u-upload>
            </view>
          </u-form-item>
        </view>
        <u-form-item label="是否流动人员" prop="isflowName" borderBottom required
                     @click="fnShowFloatingPersonnel">
          <u--input v-model="form.isflowName" border="none" disabled disabledColor="#ffffff"
                    inputAlign="right"></u--input>
        </u-form-item>
      </u--form>
      <u-picker ref="uPicker" :show="singleChoice" :columns="singleChoiceColumns" keyName="name"
                @confirm="fnSingleChoiceConfirm" @cancel="fnSingleChoiceCancel"></u-picker>
      <u-datetime-picker :show="datePicker" v-model="dateValue" mode="date" @confirm="fnDateConfirm"
                         @cancel="fnDateCancel"></u-datetime-picker>
      <u-picker :show="showIsSocial" :columns="[[{name: '是', id: '1'}, {name: '否', id: '0'}]]" keyName="name"
                @confirm="fnIsSocialConfirm" @cancel="fnShowIsSocial"></u-picker>
      <u-picker :show="showEmploymentInjuryInsurance" :columns="[[{name: '是', id: '1'}, {name: '否', id: '0'}]]"
                keyName="name" @confirm="fnEmploymentInjuryInsuranceConfirm" @cancel="fnShowEmploymentInjuryInsurance">
      </u-picker>
      <u-picker :show="showFloatingPersonnel" :columns="[[{name: '是', id: '1'}, {name: '否', id: '0'}]]" keyName="name"
                @confirm="fnFloatingPersonnelConfirm" @cancel="fnShowFloatingPersonnel"></u-picker>
      <u-picker :show="showBf" :columns="[[{name: '是', id: '1'}, {name: '否', id: '0'}]]" keyName="name"
                @confirm="fnBFConfirm" @cancel="fnShowBf"></u-picker>
      <u-picker :show="showIsSignLabor" :columns="[[{name: '是', id: '1'}, {name: '否', id: '0'}]]" keyName="name"
                @confirm="fnIsSignLaborConfirm" @cancel="fnShowIsSignLabor"></u-picker>
      <u-picker :show="showPhotosOfLevel" :columns="[[{name: '是', id: '1'}, {name: '否', id: '0'}]]" keyName="name"
                @confirm="fnShowPhotosOfLevelConfirm" @cancel="fnShowPhotosOfLevel"></u-picker>
      <u-picker :show="showIsPay" :columns="[[{name: '是', id: '1'}, {name: '否', id: '0'}]]" keyName="name"
                @confirm="fnShowIsPayConfirm" @cancel="fnShowIsPay"></u-picker>

      <u-datetime-picker :show="showInjuriesPayTiem" v-model="IS_INJURIES_PAY_TIME" mode="date"
                         @confirm="fnShowInjuriesPayTiemConfirm"
                         @cancel="fnShowInjuriesPayTiemEducation"></u-datetime-picker>
      <view class="mt-10">
        <u-button type="primary" text="保 存" @click="$u.debounce(fnSubmit, 1000,true)"></u-button>
      </view>
    </view>
  </view>
</template>

<script>
import {getIDCardDeduplication, getPhoneNumberDeduplication,} from "../../../api/index";
import {getDicLevel, getUserInfoV1, setUserInitV1, uploadFile} from "../../../api/api";

export default {
  data() {
    return {
      showDegreeOfEducation: false,
      singleChoice: false,
      singleChoiceColumns: [],
      changeFrom: '',
      datePicker: false,
      dateValue: new Date().toString(),
      showIsSocial: false,
      showEmploymentInjuryInsurance: false,
      injuriesPayTiemFile: [],
      showInjuriesPayTiem: false,
      IS_INJURIES_PAY_TIME: new Date().toString(),
      EMPLOYMENT_APPLY_MANAGEMENT_ID: '',

      degreeOfEducationColumns: [],
      showPersonWorkType: false,
      showFloatingPersonnel: false,
      showBf: false,
      showIsSignLabor: false,
      showPhotosOfLevel: false,
      showIsPay: false,
      personWorkTypeColumns: [],
      prePhotoId: [],
      form: {
        xgfUserId:'',
        userId: '',
        userPhoto:[],
        userCard:[],
        socSecurity:[],
        contract:[],
        insurance:[],
        empInsurance:[],
        threeLevel:[],
        userPhotoFile: [],
        name: '',
        sex:'',
        sexName:'',
        dateOfBirth:'',
        age:'',
        cardId:'',
        userCardIDPhotoFile: [],
        nationalityName:'',
        nationality:'',
        hklocal:'',
        address:'',
        phone:'',
        degreeOfEducationName: '',
        degreeOfEducation: '',
        maritalstatusName:'',
        maritalstatus:'',
        politicalStatusName:'',
        politicalStatus:'',
        politicalTime:'',
        isSocialName:'',
        isSocial:'',
        socialNumber:'',
        socialPhotoFile: [],
        isBfName:'',
        isBf:'',
        isSignLaborName:'',
        isSignLabor:'',
        contractFile:[],
        ispayName:'',
        ispay:'',
        ispayNumber:'',
        insuranceFile: [],
        isInjuriesPayName:'',
        isInjuriesPay:'',
        isInjuriesPayTime:'',
        injuriesPayTiemFile: [],
        postId:'',
        isLevelThreeName:'',
        isLevelThree:'',
        photosOfLevel: [],
        isflowName:'',
        isflow:'',
        username: '',
        departmentId:'',
        emp:{},
        userDetails:{},
        deleteFileIds: [],
        postMethod: 'application/json'
      },
      rules: {
        userPhotoFile: {
          type: 'array',
          required: true,
          message: '请上传照片',
          trigger: ['change']
        },
        name: {
          required: true,
          message: '请填写姓名',
          trigger: ['blur']
        },
        userCardIDPhotoFile: {
          type: 'array',
          required: true,
          message: '请上传身份证正反照',
          trigger: ['change']
        },
        injuriesPayTiemFile: {
          type: 'array',
          required: true,
          message: '请上传工伤保险凭证',
          trigger: ['change']
        },
        socialPhotoFile: {
          type: 'array',
          required: true,
          message: '请上传社保卡正反照',
          trigger: ['change']
        },
        cardId: [{
          required: true,
          message: '请填写身份证号',
          trigger: ['blur']
        },
          {
            validator: (rule, value, callback) => {
              return uni.$u.test.idCard(value);
            },
            message: '身份证号格式不正确',
            trigger: ['blur'],
          },
        ],
        hklocal: {
          required: true,
          message: '请填写户口所在地',
          trigger: ['blur']
        },
        address: {
          required: true,
          message: '请填写现住址',
          trigger: ['blur']
        },
        phone: [{
          required: true,
          message: '请填写联系电话',
          trigger: ['blur']
        },
          {
            validator: (rule, value, callback) => {
              return uni.$u.test.mobile(value);
            },
            message: '联系电话格式不正确',
            trigger: ['blur'],
          },
        ],
        degreeOfEducationName: {
          required: true,
          message: '请选择文化程度',
          trigger: ['change']
        },
        isflowName: {
          required: true,
          message: '请选择是否为流动人员',
          trigger: ['change']
        },
        isSocialName: {
          required: true,
          message: '请选择是否缴纳社保',
          trigger: ['change']
        },
        isInjuriesPayName: {
          required: true,
          message: '请选择是否按期缴纳工伤保险',
          trigger: ['change']
        },
        isInjuriesPayTime: {
          required: true,
          message: '请选择工伤保险有效期',
          trigger: ['change']
        },
        isBfName: {
          required: true,
          message: '请选择是否是否缴纳保险',
          trigger: ['change']
        },
        isSignLaborName: {
          required: true,
          message: '请选择是否签订劳动合同',
          trigger: ['change']
        },
        contractFile: {
          type: 'array',
          required: true,
          message: '请上传劳动合同',
          trigger: ['change']
        },
        insuranceFile: {
          type: 'array',
          required: true,
          message: '请上传保险图片',
          trigger: ['change']
        },
        postId: {
          required: true,
          message: '请填写岗位',
          trigger: ['change']
        },
        photosOfLevel: {
          type: 'array',
          required: true,
          message: '请上传三级安全培训照片',
          trigger: ['change']
        },
        isLevelThreeName: {
          required: true,
          message: '请选择是否为三级人员',
          trigger: ['change']
        },
        ispayNumber: {
          required: true,
          message: '请填写商业保单号',
          trigger: ['change']
        },
      },
      emp: {
        employmentApplyManagementId: "",
        corpinfoId: "",
        relevantUnitName: "",
        startDate: "",
        endDate: "",
        leaveReason: "",
        applyTime: "",
        userId: "",
        auditState: "",
        departState: "",
        reviewState: "",
        departmentId: "",
        remarks: "",
        auditRemarks: "",
        auditTime: "",
        auditPerson: "",
        corpStartDate: "",
        postId: "",
        entryDate: "",
        isSignLabor: "0",
        isInjuriesPay: "0",
        socialNumber: "",
        ispay: "0",
        isLevelThree: "0",
        isSafetyTell: "0",
        isBodyAdapt: "0",
        isSpecialJob: "0",
        isflow: "0",
        isSocial: "0",
        socialType: "",
        isBf: "0",
      },
      userDetails: {
        userId: "",
        name: "",
        phone: "",
        departState: "0",
        personWorkType: "",
        personWorkTypeName: "",
        age: "",
        hklocal: "",
        address: "",
        degreeOfEducation: "",
        degreeOfEducationName: "",
        corpStartDate: "",
        postId: "",
        postName: "",
        workSign: "",
        joinedDate: "",
        workDate: "",
        photo: "",
        dateOfBirth: "",
        ispay: "0",
        ispayNumber: "",
        isSafetyTell: "0",
        isSafetyTime: "",
        isInjuriesPay: "0",
        isSignLabor: "0",
        sex: "",
        entryDate: "",
        nationality: "",
        nationalityName: "",
        maritalstatus: "",
        politicalTime: "",
        politicalStatus: "",
        politicalStatusName: "",
        isInjuriesPayTime: "",
        isLevelThree: "0",
        isBodyAdapt: "0",
        isSpecialJob: "0",
        belongToCorp: "",
        belongToCorpName: "",
        corpinfoId: "",
        cardId: "",
        departmentId: "",
        departmentName: "",
        isSocial: "0",
        isBf: "0",
        socialNumber: "",
        zzname: "",
        annex: "",
        attorney: "",
        commitmentLetter: "",
      },
    }
  },
  onLoad() {
    this.fnGetData()
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo
    }
  },
  methods: {
    singleChoiceClick(changeFrom) {
      // 设置选中的就是第一个元素
      this.$refs.uPicker.setIndexs([0])
      this.changeFrom = changeFrom
      if (changeFrom === 'nationality') this.fnGetDataDictionary('0a0e406f27f74ee698fe9979d25f62dd')
      if (changeFrom === 'degreeOfEducation') this.fnGetDataDictionary('d7d80f08d73a4accbccf4fd3d8d1d867')
      if (changeFrom === 'maritalstatus') this.singleChoiceColumns = [[{name: '已婚', bianma: '1'}, {
        name: '未婚',
        bianma: '0'
      }]]
      if (changeFrom === 'politicalStatus') this.fnGetDataDictionary('6351efdd12dc4730952e5d195718e252')
      this.singleChoice = true
    },
    // 日期确认
    fnDateConfirm(event) {
      this.$set(this.form, this.changeFrom, uni.$u.timeFormat(event.value, 'yyyy-mm-dd'))
      this.datePicker = false
    },
    // 日期选择器取消
    fnDateCancel() {
      this.datePicker = false
    },
    fnShowDatePicke(changeFrom) {
      this.changeFrom = changeFrom
      this.datePicker = true
    },
    fnSingleChoiceConfirm(event) {
      this.$set(this.form, this.changeFrom, event.value[0].bianma)
      this.$set(this.form, this.changeFrom + 'Name', event.value[0].name)
      this.singleChoice = false
    },
    fnSingleChoiceCancel() {
      this.singleChoice = false
    },
    // 身份证添加
    fnAttIdPhotoAdd(event) {
      for (let i = 0; i < event.file.length; i++) {
        this.form.userCardIDPhotoFile.push(event.file[i])
      }
    },
    // 身份证删除
    fnAttIdPhotoDelete(event) {
      uni.showModal({
        title: '提示',
        content: '确定删除吗',
        success: async (res) => {
          if (res.confirm) {
            console.log('?????')
            if (event.file.id) {
              // 准备删除的数据
              this.prePhotoId.push(event.file.id)
            }
            uni.showToast({
              title: '操作成功',
              duration: 2000
            });
            this.form.userCardIDPhotoFile.splice(event.index, 1)
            this.form.deleteFileIds.push(event.file.id)
          } else if (res.cancel) {
          }
        }
      });
    },
    // 社保卡添加
    fnAttSocialAdd(event) {
      for (let i = 0; i < event.file.length; i++) {
        this.form.socialPhotoFile.push(event.file[i])
      }
    },
    // 社保卡删除
    fnAttSocialDelete(event) {
      uni.showModal({
        title: '提示',
        content: '确定删除吗',
        success: async (res) => {
          if (res.confirm) {
            if (event.file.id) {
              this.prePhotoId.push(event.file.id)
            }
            uni.showToast({
              title: '操作成功',
              duration: 2000
            });
            this.form.socialPhotoFile.splice(event.index, 1)
            this.form.deleteFileIds.push(event.file.id)
          } else if (res.cancel) {
            // console.log('用户点击取消');
          }
        }
      });
    },
    // 合同图片
    fnIsSignLaborAdd(event) {
      for (let i = 0; i < event.file.length; i++) {
        this.form.contractFile.push(event.file[i])
      }
    },
    // 合同图片删除
    fnIsSignLaborDelete(event) {
      uni.showModal({
        title: '提示',
        content: '确定删除吗',
        success: async (res) => {
          if (res.confirm) {
            if (event.file.id) {
              this.prePhotoId.push(event.file.id)
            }
            uni.showToast({
              title: '操作成功',
              duration: 2000
            });
            this.form.contractFile.splice(event.index, 1)
            this.form.deleteFileIds.push(event.file.id)
          } else if (res.cancel) {
            // console.log('用户点击取消');
          }
        }
      });
    },
    // 合同图片
    fnIsPayAdd(event) {
      for (let i = 0; i < event.file.length; i++) {
        this.form.insuranceFile.push(event.file[i])
      }
    },
    // 合同图片删除
    fnIsPayDelete(event) {
      uni.showModal({
        title: '提示',
        content: '确定删除吗',
        success: async (res) => {
          if (res.confirm) {
            if (event.file.id) {
              this.prePhotoId.push(event.file.id)
            }
            uni.showToast({
              title: '操作成功',
              duration: 2000
            });
            this.form.insuranceFile.splice(event.index, 1)
            this.form.deleteFileIds.push(event.file.id)
          } else if (res.cancel) {
            // console.log('用户点击取消');
          }
        }
      });
    },
    // 三级教育培训新增
    fnPhotosOfLevelAdd(event) {
      for (let i = 0; i < event.file.length; i++) {
        this.form.photosOfLevel.push(event.file[i])
      }
    },
    // 三级教育培训删除
    fnPhotosOfLevelDelete(event) {
      uni.showModal({
        title: '提示',
        content: '确定删除吗',
        success: async (res) => {
          if (res.confirm) {
            if (event.file.id) {
              this.prePhotoId.push(event.file.id)
            }
            uni.showToast({
              title: '操作成功',
              duration: 2000
            });
            this.form.photosOfLevel.splice(event.index, 1)
            this.form.deleteFileIds.push(event.file.id)
          } else if (res.cancel) {
            // console.log('用户点击取消');
          }
        }
      });
    },
    async fnGetDataDictionary(dictionariesId) {
      let resData = await getDicLevel({
        dictionariesId:dictionariesId,
        postMethod: 'application/json'
      })
      this.singleChoiceColumns = [resData.list]
    },
    fnShowDegreeOfEducation() {
      this.showDegreeOfEducation = !this.showDegreeOfEducation
    },
    fnDegreeOfEducationConfirm(e) {
      this.form.degreeOfEducation = e.value[0].bianma
      this.form.degreeOfEducationName = e.value[0].name
      this.fnShowDegreeOfEducation()
    },
    fnShowPersonWorkType() {
      this.showPersonWorkType = !this.showPersonWorkType
    },
    fnShowFloatingPersonnel() {
      this.showFloatingPersonnel = !this.showFloatingPersonnel
    },
    fnShowBf() {
      this.showBf = !this.showBf
    },
    fnShowIsSignLabor() {
      this.showIsSignLabor = !this.showIsSignLabor
    },
    fnShowIsPay() {
      this.showIsPay = !this.showIsPay
    },
    fnShowIsPayConfirm(e) {
      this.form.ispay = e.value[0].id
      this.form.ispayNumber = e.value[0].name
      this.fnShowIsPay()
    },
    fnShowPhotosOfLevel() {
      this.showPhotosOfLevel = !this.showPhotosOfLevel
    },
    fnShowPhotosOfLevelConfirm(e) {
      this.form.isLevelThree = e.value[0].id
      this.form.isLevelThreeName = e.value[0].name
      this.fnShowPhotosOfLevel()
    },
    fnFloatingPersonnelConfirm(e) {
      this.form.isflow = e.value[0].id
      this.form.isflowName = e.value[0].name
      this.fnShowFloatingPersonnel()
    },
    fnBFConfirm(e) {
      this.form.isBf = e.value[0].id
      this.form.isBfName = e.value[0].name
      this.fnShowBf()
    },
    fnIsSignLaborConfirm(e) {
      this.form.isSignLabor = e.value[0].id
      this.form.isSignLaborName = e.value[0].name
      this.fnShowIsSignLabor()
    },
    fnShowIsSocial() {
      this.showIsSocial = !this.showIsSocial
    },
    fnIsSocialConfirm(e) {
      this.form.isSocial = e.value[0].id
      this.form.isSocialName = e.value[0].name
      this.fnShowIsSocial()
    },
    fnShowEmploymentInjuryInsurance() {
      this.showEmploymentInjuryInsurance = !this.showEmploymentInjuryInsurance
    },
    fnEmploymentInjuryInsuranceConfirm(e) {
      this.form.isInjuriesPay = e.value[0].id
      this.form.isInjuriesPayName = e.value[0].name
      this.fnShowEmploymentInjuryInsurance()
    },
    //工伤保险有效 凭证
    fnInjuriesPayTiemAttachmentsAfterRead(event) {
      for (let i = 0; i < event.file.length; i++) {
        this.form.injuriesPayTiemFile.push(event.file[i])
      }
    },
    fnInjuriesPayTiemAttachmentsDelete(event) {
      uni.showModal({
        title: '提示',
        content: '确定删除吗',
        success: async (res) => {
          if (res.confirm) {
            if (event.file.id) {
              this.prePhotoId.push(event.file.id)
            }
            uni.showToast({
              title: '操作成功',
              duration: 2000
            });
            this.form.injuriesPayTiemFile.splice(event.index, 1)
            this.form.deleteFileIds.push(event.file.id)
          } else if (res.cancel) {
            // console.log('用户点击取消');
          }
        }
      });
    },
    //工伤保险有效期 IS_INJURIES_PAY_TIME  InjuriesPayTiem
    fnShowInjuriesPayTiemEducation() {
      this.showInjuriesPayTiem = !this.showInjuriesPayTiem
    },
    fnShowInjuriesPayTiemConfirm(event) {
      this.form.isInjuriesPayTime = event.value
      this.fnShowInjuriesPayTiemEducation()
      this.form.isInjuriesPayTime = uni.$u.timeFormat(event.value, 'yyyy-mm-dd')
    },
    async fnIDCardDeduplication(CARD_ID = this.form.cardId) {
      let resData = await getIDCardDeduplication({
        CARD_ID
      })
      if (resData.pd) {
        uni.$u.toast('身份证号重复')
        return true
      }
      return false
    },
    async fnPhoneNumberDeduplication(PHONE = this.form.phone) {
      if (uni.$u.test.mobile(PHONE)) {
        let resData = await getPhoneNumberDeduplication({
          PHONE
        })
        if (resData.pd) {
          uni.$u.toast('联系电话重复')
          return true
        }
        return false
      }
    },
    async fnCardIdDisassembly(event) {
      if (uni.$u.test.idCard(event)) {
        await this.fnIDCardDeduplication()
        this.form.dateOfBirth = event.substring(6, 10) + "-" + event.substring(10, 12) + "-" + event
            .substring(12, 14);
        if (parseInt(event.substring(17, 1)) % 2 === 1) {
          this.form.sex = "0";
          this.form.sexName = "男";
        } else {
          this.form.sex = "1";
          this.form.sexName = "女";
        }
        this.$forceUpdate()
        let myDate = new Date();
        let month = myDate.getMonth() + 1;
        let day = myDate.getDate();
        let age = myDate.getFullYear() - event.substring(6, 10) - 1;
        if (event.substring(10, 12) < month || event.substring(10, 12) === month && event.substring(12,
            14) <= day) {
          age++;
        }
        this.form.age = age;
      }
    },
    async fnGetData() {
      let data = await getUserInfoV1(
          {
            id: this.$store.getters.getUserInfo.USER_ID,
            postMethod: 'application/json'
          }
      )
      this.form.xgfUserId = data.info.userId
      this.form.userId = data.info.userId
      this.form.postId = data.info.userDetails.postId
      this.form.postName = data.info.userDetails.postName
      this.form.name = data.info.userDetails.name
      this.form.username = data.info.username
      this.form.departmentId = data.info.userDetails.departmentId
      if (data.info.userPhotoInfo.length > 0) {
        this.form.userPhotoFile.push({
          id: data.info.userPhotoInfo[0].imgfilesId,
          url: this.$filePath + data.info.userPhotoInfo[0].filepath,
        })
      }
      this.form.maritalstatus = data.info.userDetails.maritalstatus
      this.form.dateOfBirth = data.info.userDetails.dateOfBirth
      this.form.age = data.info.userDetails.age
      this.form.sexName = data.info.userDetails.sex === '0' ? '男' : '女'
      this.form.cardId = data.info.userDetails.cardId
      if (data.info.userCardInfo.length > 0) {
        for (let i = 0; i < data.info.userCardInfo.length; i++){
          this.form.userCardIDPhotoFile.push({
            id: data.info.userCardInfo[i].imgfilesId,
            url: this.$filePath + data.info.userCardInfo[i].filepath
          })
        }
      }
      this.form.nationalityName = data.info.userDetails.nationalityName
      this.form.nationality = data.info.userDetails.nationality
      this.form.hklocal = data.info.userDetails.hklocal
      this.form.address = data.info.userDetails.address
      this.form.phone = data.info.userDetails.phone
      this.form.degreeOfEducationName = data.info.userDetails.degreeOfEducationName
      this.form.degreeOfEducation = data.info.userDetails.degreeOfEducation
      this.form.maritalstatusName = data.info.userDetails.maritalstatus === '1'?'已婚':'未婚'
      this.form.politicalStatusName = data.info.userDetails.politicalStatusName
      this.form.politicalStatus = data.info.userDetails.politicalStatus
      this.form.isSocial = data.info.userDetails.isSocial
      this.form.socialNumber = data.info.userDetails.socialNumber
      this.form.isSocialName = data.info.userDetails.isSocial === '1'?'是':'否'
      if (data.info.socSecurityInfo.length > 0){
        for (let i = 0; i < data.info.socSecurityInfo.length; i++) {
          this.form.socialPhotoFile.push({
            id: data.info.socSecurityInfo[i].imgfilesId,
            url: this.$filePath + data.info.socSecurityInfo[i].filepath
          })
        }
      }
      this.form.isBf = data.info.userDetails.isBf
      this.form.isBfName = data.info.userDetails.isBf === '1'?'是':'否'
      this.form.isSignLabor = data.info.userDetails.isSignLabor
      this.form.isSignLaborName = data.info.userDetails.isSignLabor === '1'?'是':'否'
      if (data.info.contractInfo.length > 0){
        for (let i = 0; i < data.info.contractInfo.length; i++) {
          this.form.contractFile.push({
            id: data.info.contractInfo[i].imgfilesId,
            url: this.$filePath + data.info.contractInfo[i].filepath
          })
        }
      }
      this.form.ispay = data.info.userDetails.ispay
      this.form.ispayName = data.info.userDetails.ispay === '1'?'是':'否'
      this.form.ispayNumber = data.info.userDetails.ispayNumber
      if (data.info.insuranceInfo.length > 0){
        for (let i = 0; i < data.info.insuranceInfo.length; i++) {
          this.form.insuranceFile.push({
            id: data.info.insuranceInfo[i].imgfilesId,
            url: this.$filePath + data.info.insuranceInfo[i].filepath
          })
        }
      }
      this.form.isInjuriesPay = data.info.userDetails.isInjuriesPay
      this.form.isInjuriesPayName = data.info.userDetails.isInjuriesPay === '1'?'是':'否'
      this.form.isInjuriesPayTime = data.info.userDetails.isInjuriesPayTime
      if (data.info.empInsuranceInfo.length > 0){
        for (let i = 0; i < data.info.empInsuranceInfo.length; i++) {
          this.form.injuriesPayTiemFile.push({
            id: data.info.empInsuranceInfo[i].imgfilesId,
            url: this.$filePath + data.info.empInsuranceInfo[i].filepath
          })
        }
      }
      this.form.isLevelThree = data.info.userDetails.isLevelThree
      this.form.isLevelThreeName = data.info.userDetails.isLevelThree === '1'?'是':'否'
      if (data.info.threeLevelInfo.length > 0){
        for (let i = 0; i < data.info.threeLevelInfo.length; i++) {
          this.form.photosOfLevel.push({
            id: data.info.threeLevelInfo[i].imgfilesId,
            url: this.$filePath + data.info.threeLevelInfo[i].filepath
          })
        }
      }
      this.form.isflowName = data.info.emp.isflow === '1'?'是':'否'
      this.form.deleteFileIds = []
    },
    fnAfterRead(event) {
      this.form.userPhotoFile.push(event.file)
    },
    fnDeletePic(event) {
      console.log(event)
      this.form.userPhotoFile.splice(event.index, 1)
      this.form.deleteFileIds.push(event.file.id)
    },
    async fnSubmit() {
      this.$refs.form.validate().then(async () => {
        if (this.form.isSocial === '0' && this.form.isSocial === '0') {
          uni.$u.toast('工商或社保全无人员，不可进入本系统相关方人员管理！');
          return;
        }
        if (await this.fnIDCardDeduplication()) return
        if (await this.fnPhoneNumberDeduplication()) return
        if (this.form.userCardIDPhotoFile.length !== 2) {
          uni.$u.toast('身份证照片需要两张')
          return
        }
        if (this.form.isSocial === '1') {
          if (this.form.socialPhotoFile.length !== 2) {
            uni.$u.toast('社保卡照片需要两张')
            return
          }
        }
        // 头像
        this.form.userPhoto = Array.from(await this.uploadFiles(this.form.userPhotoFile, 3));
        // 身份证
        this.form.userCard = Array.from(await this.uploadFiles(this.form.userCardIDPhotoFile, 3));
        // 社保照片
        this.form.socSecurity = Array.from(await this.uploadFiles(this.form.socialPhotoFile, 3));
        // 劳动合同图片
        this.form.contract = Array.from(await this.uploadFiles(this.form.contractFile, 3));
        // 保险图片
        this.form.insurance = Array.from(await this.uploadFiles(this.form.insuranceFile, 3));
        // 工商保险
        this.form.empInsurance = Array.from(await this.uploadFiles(this.form.injuriesPayTiemFile, 3));
        // 三级培训照片
        this.form.threeLevel = Array.from(await this.uploadFiles(this.form.photosOfLevel, 3));
        // 就职信息更新
        this.form.emp = JSON.parse(JSON.stringify(this.emp));
        this.form.emp.departmentId = this.form.departmentId;
        this.form.emp.isSignLabor = this.form.isSignLabor;
        this.form.emp.isInjuriesPay = this.form.isInjuriesPay;
        this.form.emp.socialNumber = this.form.socialNumber;
        this.form.emp.ispay = this.form.ispay;
        this.form.emp.isLevelThree = this.form.isLevelThree;
        this.form.emp.isSocial = this.form.isSocial;
        this.form.emp.isBf = this.form.isBf;
        this.form.emp.isflow = this.form.isflow;
        this.form.emp.postId = this.form.postId;
        this.form.emp.postName = this.form.postId;
        // 用户信息更新
        this.form.userDetails = JSON.parse(JSON.stringify(this.userDetails));
        this.form.userDetails.name = this.form.name;
        this.form.userDetails.phone = this.form.username;
        this.form.userDetails.age = this.form.age;
        this.form.userDetails.hklocal = this.form.hklocal;
        this.form.userDetails.address = this.form.address;
        this.form.userDetails.degreeOfEducation = this.form.degreeOfEducation;
        this.form.userDetails.degreeOfEducationName = this.form.degreeOfEducationName;
        if (this.form.userPhoto && this.form.userPhoto.length > 0) {
          this.form.userDetails.photo = this.form.userPhoto[0];
        }
        this.form.userDetails.dateOfBirth = this.form.dateOfBirth;
        this.form.userDetails.ispay = this.form.ispay;
        this.form.userDetails.ispayNumber = this.form.ispayNumber;
        this.form.userDetails.isInjuriesPay = this.form.isInjuriesPay;
        this.form.userDetails.isSignLabor = this.form.isSignLabor;
        this.form.userDetails.sex = this.form.sex;
        this.form.userDetails.nationality = this.form.nationality;
        this.form.userDetails.nationalityName = this.form.nationalityName;
        this.form.userDetails.maritalstatus = this.form.maritalstatus;
        this.form.userDetails.politicalTime = this.form.politicalTime;
        this.form.userDetails.politicalStatus = this.form.politicalStatus;
        this.form.userDetails.politicalStatusName = this.form.politicalStatusName;
        this.form.userDetails.isInjuriesPayTime = this.form.isInjuriesPayTime;
        this.form.userDetails.isLevelThree = this.form.isLevelThree;
        this.form.userDetails.isSpecialJob = this.form.isSpecialJob;
        this.form.userDetails.cardId = this.form.cardId;
        this.form.userDetails.isLevelThree = this.form.isLevelThree;
        this.form.userDetails.isSocial = this.form.isSocial;
        this.form.userDetails.isBf = this.form.isBf;
        this.form.userDetails.socialNumber = this.form.socialNumber;
        this.form.userDetails.postId = this.form.postId;
        this.form.userDetails.postName = this.form.postId;
        this.form.postMethod = 'application/json';
        // 保存人员信息
        await setUserInitV1(this.form)
        // 更新token
        let result = await getUserInfoV1({
          id: this.$store.getters.getUserInfo.USER_ID,
          postMethod: 'application/json'
        })
        let photo = result.info.userPhotoInfo[0].filepath
        await this.$store.dispatch('setUserInfo', {
          CORPINFO_ID: result.info.corpinfoId,
          DEPARTMENT_ID: result.info.departmentId,
          NAME: result.info.name,
          USERNAME: result.info.username,
          USER_ID: result.info.userId,
          PHOTO: this.$filePath + photo,
          userPhoto: this.$filePath + photo,
          token: this.userInfo.token
        })
        uni.$u.toast('保存成功')
        setTimeout(() => {
          var pages = getCurrentPages(); // 获取当前页面栈
          var prePage = pages[pages.length - 2]; // 上级页面
          if (prePage.route === 'pages/related_party_units/view') {
            prePage.$vm.reloadx();
          }
          uni.navigateBack()
        }, 2000)
      }).catch((e) => {
        if (e.result === 'out') {
          uni.$u.toast(e.msg)
        } else {
          console.log(e)
          uni.$u.toast('请补全必填项')
        }
      })
    },
    async uploadFiles(list, type){
      const res = [];
      for (let i = 0; i < list.length; i++) {
        if (!list[i].id) {
          const req = await uploadFile({
            filePath: list[i].url,
            name: 'file',
            formData: {
              type: type,
              uuid: Math.random()
            }
          });
          res.push(req.path);
        }
      }
      return res;
    }
  },
}
</script>

<style scoped>
.tip {
  color: red;
  font-size: 12px;
  margin-top: 5px;
}
</style>
