<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <div>
      <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="formReadOnly"
               label-width="125px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col :span="12">
            <el-form-item label="姓名" prop="rosterId"
                          :rules="[{required: true, message: '姓名不能为空', trigger: 'blur'},{required: true, message: '姓名不能为空', trigger: 'change'}]"
            >
              <RosterSelectForm :limit='1' :disabled="true" :readonly="true" :value="inputForm.rosterId" @getValue='(value) => {inputForm.rosterId=value}'></RosterSelectForm>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请日期" prop="requestDate"
                          :rules="[{required: true, message: '申请日期不能为空', trigger: 'blur'}]">
              <el-date-picker
                :disabled="true"
                placement="bottom-start"
                style="width: 100%"
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="inputForm.requestDate"
                type="date"
                placeholder="选择申请日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离职类型" prop="type"
                          :rules="[{required: true, message: '离职类型不能为空', trigger: 'blur'}
                 ]">
              <el-radio v-for="item in $dictUtils.getDictList('dimission_type')" v-model="inputForm.type"
                        :label="item.value" :value="item.value" :key="item.value" size="mini" style="margin-right: 0px" border>
                {{item.label}}</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离职原因" prop="reaTypeList"
                          :rules="[{required: true, message: '离职原因不能为空', trigger: 'blur'}
                 ]">
              <el-select v-model="inputForm.reaTypeList" multiple placeholder="请选择离职原因"  style="width:100%;">
                <el-option
                  v-for="item in $dictUtils.getDictList('dimission_reason')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="原因说明" prop="reasonDesc"
                          :rules="[
                 ]">
              <el-input type="textarea" style="width:100%" maxlength="1000" v-model="inputForm.reasonDesc" placeholder="请填写原因说明"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离职日期" prop="dimissionDate"
                          :rules="[{required: true, message: '离职日期不能为空', trigger: 'blur'}
                 ]">
              <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                placement="bottom-start"
                style="width: 100%"
                v-model="inputForm.dimissionDate"
                type="date"
                placeholder="选择离职日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最后工作日期" prop="lastWorkDate"
                          :rules="[{required: true, message: '最后工作日期不能为空', trigger: 'blur'}
                 ]">
              <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                placement="bottom-start"
                style="width: 100%"
                v-model="inputForm.lastWorkDate"
                type="date"
                placeholder="选择最后工作日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="薪资结算日期" prop="payrollSettlementDate"
                          :rules="[{required: true, message: '薪资结算日期不能为空', trigger: 'blur'}
                 ]">
              <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                placement="bottom-start"
                style="width: 100%"
                v-model="inputForm.payrollSettlementDate"
                type="date"
                placeholder="选择薪资结算日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" style="width:100%" maxlength="1000" v-model="inputForm.remarks" placeholder="请填写备注"     ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="method === 'sure'">
          <el-divider content-position="left"><i class="el-icon-document"></i> 福利缴纳</el-divider>
          <el-col :span="12">
            <el-form-item label="社保最后缴纳月：" prop="socialSecurityLast"
                          :rules="[]"
            >
              <el-date-picker
                v-model="inputForm.socialSecurityLast"
                type="month"
                value-format="yyyy-MM"
                placement="bottom-start"
                placeholder="选择月">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公积金后缴纳月：" prop="providentFundLast"
                          :rules="[]"
            >
              <el-date-picker
                v-model="inputForm.providentFundLast"
                type="month"
                value-format="yyyy-MM"
                placement="bottom-start"
                placeholder="选择月">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      <!--        附件-->
      <UpLoadComponent ref="uploadComponent"></UpLoadComponent>
        <el-row style="margin-top: 40px;margin-bottom: 20px">
          <el-divider content-position="left"><i class="el-icon-document"></i> 审批流程</el-divider>
          <el-col :span="24" style="margin-top: 10px">
            <el-form-item label="选择审批人：" prop="handoverId"
                          :rules="[{required: true, message: '审批人不能为空', trigger: 'blur'},{required: true, message: '审批人不能为空', trigger: 'change'}]"
            >
              <UserSelect :disabled="formReadOnly" :limit='1' :value="inputForm.handoverId" @getValue='(value) => {inputForm.handoverId = value}'></UserSelect>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
  </div>
</template>

<script>
  import DimissionService from '@/api/changes/dimission/DimissionService'
  import RosterService from '@/api/roster/RosterService'
  import UpLoadComponent from '@/views/common/UpLoadComponent'
  import UserSelect from '@/components/userSelect'
  import RosterSelectForm from '@/views/common/RosterSelectForm'
  export default {
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          remarks: '',
          rosterId: '',
          lastWorkDate: '',
          dimissionDate: '',
          reasonDesc: '',
          type: '',
          vettingStatus: '',
          handoverStatus: '',
          status: '',
          payrollSettlementDate: '',
          requestDate: '',
          reaTypeList: [],
          changesDimissionFilesDTOList: [],
          handoverId: '',
          socialSecurityLast: '',
          providentFundLast: ''
        },
        supervisorList: []
      }
    },
    dimissionService: null,
    rosterService: null,
    created () {
      this.dimissionService = new DimissionService()
      this.rosterService = new RosterService()
    },
    watch: {
      'businessId': {
        handler (newVal) {
          if (this.businessId) {
            if (this.businessId !== 'false') {
              this.rosterService = new RosterService()
              this.init('audit', this.businessId)
            }
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields()
            })
          }
        },
        immediate: true,
        deep: false
      }
    },
    components: {
      UpLoadComponent,
      RosterSelectForm,
      UserSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.queryAllSupervisor()
        this.inputForm = {
          id: '',
          createBy: '',
          remarks: '',
          rosterId: '',
          lastWorkDate: '',
          dimissionDate: '',
          reasonDesc: '',
          type: '',
          vettingStatus: '',
          handoverStatus: '',
          status: '',
          payrollSettlementDate: '',
          requestDate: '',
          reaTypeList: [],
          changesDimissionFilesDTOList: [],
          handoverId: '',
          socialSecurityLast: '',
          providentFundLast: ''
        }
        this.inputForm.id = id
        if (method === 'audit') {
          this.title = '离职审批'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.loading = true
          this.dimissionService.queryById(this.inputForm.id).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data)
            this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
            if (this.formReadOnly === true) {
              method = 'view'
            }
            this.$refs.uploadComponent.newUpload(method, this.inputForm.changesDimissionFilesDTOList, 'dimission')
            this.loading = false
          })
        })
      },
      saveForm (callback) {
        this.doSubmit('save', callback)
      },
      startForm (callback) {
        this.doSubmit('start', callback)
      },
      agreeForm (callback) {
        this.doSubmit('agree', callback)
      },
      // 表单提交
      doSubmit (status, callback) {
        console.log(this.inputForm)
        if (status === 'save') {
          // 暂存
          this.inputForm.vettingStatus = '3'
          this.loading = true
          if (this.$refs.uploadComponent.checkProgress()) {
            this.loading = false
            return
          }
          // this.checkMoment()
          this.inputForm.changesDimissionFilesDTOList = this.$refs.uploadComponent.getDataList()
          this.dimissionService.saveCheck(this.inputForm).then(({data}) => {
            this.inputForm.assignee = this.inputForm.handoverId
            callback(data.businessTable, data.businessId, this.inputForm)
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
          return
        } else if (status === 'start') {
          // 送审  待审核
          this.inputForm.vettingStatus = '4'
        } else if (status === 'agree') {
          // 审核同意
          this.inputForm.vettingStatus = '6'
        }
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.$refs.uploadComponent.checkProgress()) {
              this.loading = false
              return
            }
            // this.checkMoment()
            this.inputForm.changesDimissionFilesDTOList = this.$refs.uploadComponent.getDataList()
            this.dimissionService.saveCheck(this.inputForm).then(({data}) => {
              this.inputForm.assignee = this.inputForm.handoverId
              callback(data.businessTable, data.businessId, this.inputForm)
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      updateStatusById (type) {
        if (type === 'agree') {
          // 同意
          this.inputForm.vettingStatus = '6'
        }
        if (type === 'reject') {
          // 驳回
          this.inputForm.vettingStatus = '5'
        }
        if (type === 'reback') {
          // 撤回
          this.inputForm.vettingStatus = '7'
        }
        this.dimissionService.updateVettingStatusById(this.inputForm)
      },
      close () {
        this.selectData = []
        this.$refs.uploadComponent.clearUpload()
        this.$refs.inputForm.resetFields()
        this.visible = false
      },
      checkMoment () {
        this.inputForm.lastWorkDate = this.moment(this.inputForm.lastWorkDate).format('YYYY-MM-DD HH:mm:ss')
        this.inputForm.dimissionDate = this.moment(this.inputForm.dimissionDate).format('YYYY-MM-DD HH:mm:ss')
        this.inputForm.payrollSettlementDate = this.moment(this.inputForm.payrollSettlementDate).format('YYYY-MM-DD HH:mm:ss')
        this.inputForm.requestDate = this.moment(this.inputForm.requestDate).format('YYYY-MM-DD HH:mm:ss')
      },
      queryAllSupervisor () {
        this.rosterService.list({
          'current': '1',
          'size': '-1'
        }).then(({data}) => {
          this.supervisorList = JSON.parse(JSON.stringify(data.records))
        })
      }
    }
  }
</script>
