<template>
  <!-- 对话框(添加 / 修改) -->
  <el-drawer
    :title="drawTitle"
    size="40%"
    :visible.sync="drawVisible"
    @close="cancel()"
  >
    <div style="padding: 0 40px;margin-bottom: 70px">
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="120px">
        <el-form-item label="资质证书类型" prop="qualsType">
          <el-select v-model="formData.qualsType"  placeholder="请选择">
            <el-option v-for="dict in qualsTypeDictDatas" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="资质证书编号" prop="qualsNo">
          <el-input v-model="formData.qualsNo" placeholder="请输入资质证书编号"/>
        </el-form-item>
        <el-form-item label="资质证书名称" prop="qualsName">
          <el-input v-model="formData.qualsName" placeholder="请输入资质证书名称"/>
        </el-form-item>
        <el-form-item label="有效起始日期" prop="startDate">
          <el-date-picker clearable v-model="formData.startDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="选择有效起始日期"
          />
        </el-form-item>
        <el-form-item label="有效截止日期" prop="endDate">
          <el-date-picker clearable v-model="formData.endDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="选择有效截止日期"
          />
        </el-form-item>
        <el-form-item label="资质证书" prop="fileUrl">
          <ImageUpload v-model="formData.fileUrl" :limit="1"/>
        </el-form-item>
      </el-form>
    </div>
    <div class="dl-draw-footer" >
      <div class="demo-drawer__footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submitForm" :disabled="formLoading">保 存</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import * as CompanyQualsApi from '@/api/base/company'
import ImageUpload from '@/components/ImageUpload';
import {DICT_TYPE, getDictDatas} from "@/utils/dict";

export default {
  name: 'EditQualsDraw',
  components: {
    ImageUpload,
  },
  props:{
    corpId:{
      type:String,
      default:""
    }
  },
  data() {
    return {
      // 弹出层标题
      drawTitle:"",
      // 是否显示弹出层
      drawVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        qualsNo: undefined,
        corpId: undefined,
        qualsType: undefined,
        qualsName: undefined,
        fileUrl: undefined,
        startDate: undefined,
        endDate: undefined
      },
      // 表单校验
      formRules: {
        qualsType: [{ required: true, message: '请选择资质证书类型', trigger: 'blur' }],
        qualsNo: [{ required: true, message: '资质证书编号不能为空', trigger: 'blur' }],
        qualsName: [{ required: true, message: '资质证书名称不能为空', trigger: 'blur' }],
        startDate: [{ required: true, message: '请选择有效起始日期', trigger: 'blur' }],
        endDate: [{ required: true, message: '请选择有效截止日期', trigger: 'blur' }],
        fileUrl: [{ required: true, message: '请上传资质证书', trigger: 'blur' }],
      },
      // 数据字典
      qualsTypeDictDatas: getDictDatas(DICT_TYPE.BASE_QUALS_TYPE),
    }
  },
  methods: {

    /** 打开弹窗 */
    async open(id) {
      this.drawVisible = true
      this.drawTitle = "新增资质"
      this.reset()
      // 修改时，设置数据
      if (id) {
        this.drawTitle = "编辑资质"
        this.formLoading = true
        try {
          const res = await CompanyQualsApi.getCompanyQuals(id)
          this.formData = res.data
        } finally {
          this.formLoading = false
        }
      }
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs['formRef'].validate()
      this.formLoading = true
      try {
        const data = this.formData
        data.corpId = this.corpId
        // 修改的提交
        if (data.id) {
          await CompanyQualsApi.updateCompanyQuals(data)
          this.$modal.msgSuccess('修改成功')
          this.drawVisible = false
          this.$emit('success')
          return
        }
        // 添加的提交
        await CompanyQualsApi.createCompanyQuals(data)
        this.$modal.msgSuccess('新增成功')
        this.drawVisible = false
        this.$emit('success')
      } finally {
        this.formLoading = false
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        qualsNo: undefined,
        corpId: undefined,
        qualsType: undefined,
        qualsName: undefined,
        fileUrl: undefined,
        startDate: undefined,
        endDate: undefined
      }
      this.resetForm('formRef')
    },
    /** 取消 */
    cancel(){
      this.drawVisible = false
      this.reset()
    }
  }
}
</script>
<style>
.dl-draw-footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  right: 0;
  padding: 5px 20px 10px 0;
  z-index: 999;
  background: white;
  text-align: right
}
</style>
