<template>
  <a-modal
    centered
    class="departUser-modal"
    :footer="null"
    :maskClosable="false"
    :title="title+'选择审批人员'"
    v-model="isShow"
    @cancel="isShow=false"
    width="60%">
    <a-spin :spinning="spinning" tip="Loading...">
      <div>
        <div class="departUser-con">
          <a-form-model
            id="departUser-form"
            ref="ruleForm"
            :model="form"
            :rules="rules"
            :label-col="formItemLayout.labelCol"
            :wrapper-col="formItemLayout.wrapperCol">
            <a-form-model-item label="审批人员" prop="approvers">
              <dingDepartUser
                ref="departUserApp"
                @loaded="uploading=false"
                @close="isShow=false"
                id="departUser-approvers"
                :disabled="uploading"
                v-model="form.approvers"
                @change="changeApprovers"
                allowClear
                placeholder="请选择审批人员"
              ></dingDepartUser>
            </a-form-model-item>
            <a-form-model-item label="抄送人" prop="ccList">
              <dingDepartUser
                ref="departUserCc"
                @loaded="uploading=false"
                :disabled="uploading"
                id="departUser-ccList"
                v-model="form.ccList"
                allowClear
                placeholder="请选择抄送人"
              ></dingDepartUser>
            </a-form-model-item>
          </a-form-model>
        </div>
        <div class="btn-cont">
          <a-button id="departUser-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button id="departUser-refash" :loading="uploading" style="margin-right: 15px;" @click="updateUser">刷新人员</a-button>
          <a-button type="primary" id="departUser-save" @click="handleSave">确定</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import dingDepartUser from '@/views/common/dingDepartUser.js'
import { getDefaultCofig } from '@/api/data.js'
import { queryLastFinishDingUser } from '@/api/financeBook.js'
import { getFinishDingUser } from '@/api/pickUp.js'
export default {
  name: 'ChooseDepartUserModal',
  components: { dingDepartUser },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    type: { // 类型
      type: String,
      default: ''
    },
    showDefUser: { // 是否显示默认值
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      uploading: false,
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      title: '',
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      form: {
        approvers: [], // 审批人
        ccList: [] // 抄送人
      },
      rules: {
        approvers: [
          { required: true, type: 'array', message: '请选择审批人', trigger: 'change' }
        ]
        // ccList: [
        //   { required: true, message: '请选择抄送人', trigger: 'change' }
        // ]
      }
    }
  },
  methods: {
    // 刷新人员数据
    updateUser () {
      this.uploading = true
      this.$refs.departUserApp.updateDeptUser()
      // this.$refs.departUserCc.updateDeptUser()
    },
    // 选择人员
    changeApprovers (value) {
      this.form.approvers = value
      this.$refs.ruleForm.clearValidate()
    },
    // 获取人员组织树数据
    getTreeVal (data) {
      const ret = []
      data.map(item => {
        ret.push(item.value)
      })
      return ret.join(',')
    },
    // 确定
    handleSave (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          if (form.approvers) {
            form.approvers = this.getTreeVal(form.approvers)
          }
          if (form.ccList) {
            form.ccList = this.getTreeVal(form.ccList)
          }
          this.$emit('submit', form)
          this.$emit('close')
        } else {
          return false
        }
      })
    },
    // 格式化人员数据
    getFormatUser (list) {
      const ret = []
      list.map(item => {
        ret.push({ label: item.userName, value: item.userId || item.userid })
      })
      return ret
    },
    // 获取默认人员选项
    async getDefAudit () {
      // 费用单时 type = ''
      if (!this.type) {
        // 默认写死的人员
        const data = await getDefaultCofig({ code: 'EXPENSE_ACCOUNT_DING_USER' }).then(res => res.data)
        const ret = JSON.parse(data.value)
        this.form.approvers = this.getFormatUser(ret.approvers)
        this.form.ccList = this.getFormatUser(ret.ccList)
      } else if (this.type && this.type == 'financeOrder') { // 财务单 type = 'financeOrder'
        // 取最新一次审核通过值
        const data = await queryLastFinishDingUser().then(res => res.data)
        if (data.approversTaskList) {
          this.form.approvers = this.getFormatUser(data.approversTaskList)
        }
        if (data.ccTaskList) {
          this.form.ccList = this.getFormatUser(data.ccTaskList)
        }
      } else if (this.type && this.type == 'billOfLading') { // 提货单 type = 'billOfLading'
        // 取最新一次审核通过值
        const data = await getFinishDingUser().then(res => res.data)
        if (data.approversTaskList) {
          this.form.approvers = this.getFormatUser(data.approversTaskList)
        }
        if (data.ccTaskList) {
          this.form.ccList = this.getFormatUser(data.ccTaskList)
        }
      }
    },
    // 重置
    resetForm () {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields()
      })
      this.form = {
        ccList: undefined,
        approvers: undefined
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetForm()
      } else {
        if (this.showDefUser) {
          this.getDefAudit()
        }
      }
    }
  }
}
</script>

<style lang="less">
  .departUser-modal{
    .ant-modal-body {
      padding: 30px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
