<template>
  <div class="flow-form" v-loading="loading">
    <div class="com-title">
      <h1>销假申请</h1>
      <span class="number">单据号：{{ dataForm.billNo }}</span>
    </div>
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="110px" :disabled="setting.readonly">
      <el-row>
        <el-col v-if="judgeShow('flowTitle')" :span="12">
          <el-form-item label="流程标题" prop="flowTitle">
            <el-input v-model="dataForm.flowTitle" placeholder="流程标题" :disabled="judgeWrite('flowTitle')" />
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('flowUrgent')" :span="12">
          <el-form-item label="紧急程度" prop="flowUrgent">
            <el-select v-model="dataForm.flowUrgent" placeholder="选择紧急程度" :disabled="judgeWrite('flowUrgent')">
              <el-option v-for="item in flowUrgentOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('applyUser')" :span="12">
          <el-form-item label="申请人员" prop="applyUser">
            <el-input v-model="dataForm.applyUser" readonly :disabled="judgeWrite('applyUser')" />
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('applyDate')" :span="12">
          <el-form-item label="申请日期" prop="applyDate">
            <el-date-picker v-model="dataForm.applyDate" type="date" value-format="timestamp" format="yyyy-MM-dd"
              :editable="false" readonly :disabled="judgeWrite('applyDate')" />
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('applyDept')" :span="12">
          <el-form-item label="申请部门" prop="applyDept">
            <el-input v-model="dataForm.applyDept" readonly :disabled="judgeWrite('applyDept')" />
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('applyPost')" :span="12">
          <el-form-item label="申请职位" prop="applyPost">
            <el-input v-model="dataForm.applyPost" readonly :disabled="judgeWrite('applyPost')" />
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('attendanceRecordId')" :span="24">
          <el-form-item label="销假记录" prop="attendanceRecordId">
            <el-select v-model="dataForm.attendanceRecordId" filterable placeholder="请选择待销假的考勤记录" style="width:100%"
              :disabled="judgeWrite('attendanceRecordId') || candidateLoading">
              <el-option v-for="c in leaveCandidates" :key="c.id" :label="c.attendanceDate + ' · ' + c.statusText"
                :value="c.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('cancelReason')" :span="24">
          <el-form-item label="销假原因" prop="cancelReason">
            <el-input v-model="dataForm.cancelReason" type="textarea" :rows="3" placeholder="请说明销假原因"
              :disabled="judgeWrite('cancelReason')" />
          </el-form-item>
        </el-col>
        <el-col v-if="judgeShow('fileJson')" :span="24">
          <el-form-item label="相关附件" prop="fileJson">
            <NCC-UploadFz v-model="fileList" type="workFlow" :disabled="judgeWrite('fileJson')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import comMixin from '../../workFlowForm/mixin'
import { Info, Create, Update } from '@/api/workFlow/workFlowForm'
import { getLeaveCancelCandidates } from '@/api/extend/attendanceRecord'

export default {
  name: 'LeaveCancelApplyForm',
  mixins: [comMixin],
  data() {
    return {
      billEnCode: 'WF_LeaveCancelApplyNo',
      formApiKey: 'leaveCancelApply',
      candidateLoading: false,
      leaveCandidates: [],
      dataForm: {
        id: '',
        flowId: '',
        billNo: '',
        flowTitle: '',
        flowUrgent: 1,
        applyUser: '',
        applyDate: '',
        applyDept: '',
        applyPost: '',
        attendanceRecordId: '',
        cancelReason: '',
        fileJson: '',
        description: ''
      },
      dataRule: {
        flowTitle: [{ required: true, message: '流程标题不能为空', trigger: 'blur' }],
        flowUrgent: [{ required: true, message: '紧急程度不能为空', trigger: 'change' }],
        attendanceRecordId: [{ required: true, message: '请选择待销假记录', trigger: 'change' }],
        cancelReason: [{ required: true, message: '销假原因不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    selfInit() {
      this.dataForm.applyDate = new Date().getTime()
      this.dataForm.flowTitle = this.userInfo.userName + '的销假申请'
      this.dataForm.applyUser = this.userInfo.userName + '/' + this.userInfo.userAccount
      this.dataForm.applyDept = this.userInfo.organizeName
      if (this.userInfo.positionIds && this.userInfo.positionIds.length) {
        this.dataForm.applyPost = this.userInfo.positionIds.map(o => o.name).join(',')
      }
      this.loadLeaveCandidates()
    },
    loadLeaveCandidates() {
      this.candidateLoading = true
      getLeaveCancelCandidates({}).then(res => {
        this.leaveCandidates = res.data || []
      }).finally(() => {
        this.candidateLoading = false
      })
    },
    selfGetInfo() {
      Info(this.formApiKey, this.setting.id).then(res => {
        this.dataForm = res.data
        if (res.data.fileJson) {
          this.fileList = JSON.parse(res.data.fileJson)
        }
        this.loadLeaveCandidates()
        this.$emit('setPageLoad')
      })
    },
    exist() {
      return true
    },
    selfSubmit() {
      this.dataForm.status = this.eventType === 'submit' ? 0 : 1
      if (this.eventType === 'save') return this.selfHandleRequest()
      this.$confirm('确定提交销假申请？审批通过后将自动执行考勤销假。', '提示', { type: 'warning' }).then(() => {
        this.selfHandleRequest()
      }).catch(() => { })
    },
    selfHandleRequest() {
      if (!this.dataForm.id) delete this.dataForm.id
      if (this.eventType === 'save') this.$emit('setLoad', true)
      const formMethod = this.dataForm.id ? Update : Create
      formMethod(this.formApiKey, this.dataForm).then(res => {
        this.$message({
          message: res.msg, type: 'success', duration: 1500, onClose: () => {
            if (this.eventType === 'save') this.$emit('setLoad', false)
            this.$emit('close', true)
          }
        })
      }).catch(() => {
        if (this.eventType === 'save') this.$emit('setLoad', false)
      })
    }
  }
}
</script>
