<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="1000px"
      @close="close"
      :visible.sync="visible">
      <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               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'}]"
            >
<!--              <el-select :disabled="method === 'sure'" v-model="inputForm.rosterId" style="width:100%" placeholder="请选择姓名">-->
<!--                <el-option-->
<!--                  v-for="item in supervisorList"-->
<!--                  :key="item.id"-->
<!--                  :label="item.name"-->
<!--                  :value="item.id">-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--              <el-input style="width:100%" v-model="inputForm.rosterId" placeholder="请填写姓名"  disabled  >-->
<!--                <el-button slot="append" @click="openRoster" icon="el-icon-search"></el-button>-->
<!--              </el-input>-->
              <RosterSelectForm :limit='1' :disabled="method==='view'||method==='sure'" :readonly="method==='view'||method==='sure'" :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="method === 'sure'"
                placement="bottom-start"
                style="width: 100%"
                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
                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
                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
                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>
        <el-row v-if="method === 'sure'">
          <el-divider content-position="left"><i class="el-icon-document"></i> 系统调整</el-divider>
          <el-col :span="24">
            <el-form-item label="审批交接给：" prop="handoverId"
                          :rules="[]"
            >
              <RosterSelectForm :limit='1'  :value="inputForm.handoverId" @getValue='(value) => {inputForm.handoverId=value}'></RosterSelectForm>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--        附件-->
      <UpLoadComponent v-if="method === 'sure'" ref="uploadComponent"></UpLoadComponent>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method !== 'view' && method !== 'sure'" @click="doSubmit('1')" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
      <el-button size="small" type="primary" v-if="method === 'sure'" @click="doSubmit('2')" icon="el-icon-circle-check" v-noMoreClick>确认归档</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import DimissionService from '@/api/changes/dimission/DimissionService'
  import RosterService from '@/api/roster/RosterService'
  import UpLoadComponent from '@/views/common/UpLoadComponent'
  import RosterSelectForm from '@/views/common/RosterSelectForm'
  export default {
    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()
    },
    components: {
      UpLoadComponent,
      RosterSelectForm
    },
    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 === 'add') {
          this.title = `办理员工离职`
        } else if (method === 'edit') {
          this.title = '修改'
        } else if (method === 'view') {
          this.title = '离职详情'
        } else if (method === 'sure') {
          this.title = '确认离职'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view' || method === 'sure') { // 修改或者查看
            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 (method === 'sure') {
                this.$refs.uploadComponent.newUpload(method, this.inputForm.changesDimissionFilesDTOList, 'dimission')
              }
              this.loading = false
            })
          }
          if (method !== 'edit' && method !== 'view') {
            if (method === 'sure') {
              this.$refs.uploadComponent.newUpload(method, [], 'dimission')
            }
          }
        })
      },
      // 表单提交
      doSubmit (status) {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.method === 'sure') {
              if (this.$refs.uploadComponent.checkProgress()) {
                this.loading = false
                return
              }
            }
            this.checkMoment()
            if (this.method === 'sure') {
              this.inputForm.changesDimissionFilesDTOList = this.$refs.uploadComponent.getDataList()
            }
            this.inputForm.status = status
            if (status === '2') {
              if (this.inputForm.vettingStatus === '1' || this.inputForm.vettingStatus === '3' || this.inputForm.vettingStatus === '5') {
                this.inputForm.vettingStatus = '2'
              }
            }
            this.dimissionService.save(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.selectData = []
        if (this.method === 'sure') {
          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>
