<!--
  @description: 专家-教育经历/添加教育经历弹窗
  @Modifier: 徐媛媛
  @routerPath:
  @date: 2020/8/12-13:27
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    :title="dialogTitle"
    width="650px"
    append-to-body
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <div>
      <el-form ref="form" :model="formData" :rules="rules" :inline-message="true">
        <table class="from_table">
          <!--毕业院校--->
          <tr>
            <td>
              <el-form-item label="毕业院校" prop="graduateSchool" />
            </td>
            <td>
              <el-input
                v-model="formData.graduateSchool"
                placeholder="毕业院校"
              />
            </td>
          </tr>
          <!--所学专业--->
          <tr>
            <td>
              <el-form-item label="所学专业" prop="specialty" />
            </td>
            <td>
              <el-input
                v-model.trim="formData.specialty"
                placeholder="所学专业"
                class="show-word-limit-padding"
              />
            </td>
          </tr>
          <!--学历-->
          <tr>
            <td>
              <el-form-item label="学历" prop="educationDvCode" />
            </td>
            <td>
              <el-select v-model="formData.educationDvCode" placeholder="请选择" style="width:100%">
                <el-option
                  v-for="item in EducationList"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
          </tr>
          <!--入学时间-->
          <tr>
            <td>
              <el-form-item label="入学时间" prop="startDate" />
            </td>
            <td>
              <el-date-picker
                v-model="formData.startDate"
                :value-format="elValueFormatDate"
                type="date"
                placeholder="选择日期"
              />
            </td>
          </tr>
          <!--毕业时间-->
          <tr>
            <td>
              <el-form-item label="毕业时间 " prop="endDate" />
            </td>
            <td>
              <el-date-picker
                v-model="formData.endDate"
                :value-format="elValueFormatDate"
                type="date"
                placeholder="选择日期"
              />
            </td>
          </tr>
          <!--证明文件-->
          <tr>
            <td>
              <el-form-item label="证明文件" prop="credentialsId" />
            </td>
            <td>
              <upload
                :on-change="photoIdFile"
                :data="photoIdType"
                :auto-delete="true"
                :auto-upload="true"
                :limit="1"
                file-ref="fileRefs"
                but-text="图片上传"
                title=""
                accept="image/jpg,image/jpeg,image/png"
                tip-text="上传附件格式(*.png;*.jpg;*.jpeg;*.pdf)"
              />
            </td>
          </tr>
        </table>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure()">提交</el-button>
      <el-button @click="dialogData.dialog = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { submitOrUpdateExptExpertEducation } from '@/api/expert/expertInfo' // 获取用户任职经历信息 提交y
import { queryTBExpertEducationById } from '@/api/expert/expertInfo'
import { queryTBExpertEducationInfoById, saveOrUpdateTBExpertEducationInfoById } from '@/api/expert/expertChange'//  教育信息变更—查询，新增
import { upLoadFileUrl } from '@/api/common/uploadCenter'
import upload from '@comp/common/updownload/upload'
/* 组件*/
import { mapGetters } from 'vuex'
import { commonMxin } from '@/mixins/commonMxin'
import { expertInfo } from '../mixins/expertInfo'
import { filterDictText } from '@/assets/js/utils'// 常用方法
import { randomData, restoreInitialValue } from '@assets/js/utils'// 清空value值
export default {
  name: 'BidWorkExperienceDialog',
  components: {
    upload
  },
  mixins: [commonMxin, expertInfo],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {
        return {
          dialog: false, // 展示状态
          id: '', // 编辑时才会有财务状况id
          oldId: '' // 修改时用的Id
        }
      }
    }
  },
  data() {
    return {
      type: this.$route.meta.type,
      dialogTitle: '',
      formData: {
        specialty: '', // 所学专业
        educationDvCode: '', // 学历
        startDate: '', // 入学时间
        graduateSchool: '', // 学校
        endDate: '', // 毕业时间
        credentialsId: '', // 证明文件
        id: ''
      },
      openOrganizationData: {
        type: '',
        index: '',
        data: ''
      },
      photoIdType: {
        bizId: '', // 业务主键Id
        fileType: 'EXPERT_UPLOAD_EDUCATION_PROVE' // 文件类型
      },
      rules: {
        specialty: [{ required: true, message: '请填写所学专业' }], // 所学专业
        educationDvCode: [
          { required: true, message: '学历不能为空', trigger: 'blur' }
        ], // 学历
        startDate: [{ required: true, message: '请填写入学时间', trigger: 'blur' }], // 入学时间
        endDate: [
          { required: true, message: '毕业时间不能为空', trigger: 'blur' }
        ], // 毕业时间
        graduateSchool: [
          { required: true, message: '学校不能为空', trigger: 'blur' }
        ] // 学校
      },
      loading: true,
      idImportFileUrl: upLoadFileUrl,
      organizationDialog: false // 弹框打开状态
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    // 生成附件bizId
    if (this.dialogData.id === '') {
      this.dialogTitle = '添加教育经历'
      this.formData.id = randomData()// 主键id
      this.photoIdType.bizId = this.formData.id// 财务审计报告扫描件
    } else {
      this.dialogTitle = '编辑教育经历'
      const data = {
        id: this.dialogData.id
      }
      this.seeData(data)
    }
  },
  methods: {
    filterDictText,
    //  证件扫描件
    photoIdFile(file, fileList) {
      if (fileList.length > 0) {
        this.$set(this.formData, 'businessLicenseType', 'true')// 上传了
      } else {
        this.$set(this.formData, 'businessLicenseType', '')// 未上传
      }
    },
    seeData(data) {
      if (this.type === 'declarationChange') {
        queryTBExpertEducationInfoById(data).then(res => {
          this.formData = res.data
          this.photoIdType.bizId = this.formData.id
        })
      } else {
        const expertId = this.$route.query.id
        if (expertId) {
          data.expertId = expertId
        }
        queryTBExpertEducationById(data).then(res => {
          this.formData = res.data
          this.photoIdType.bizId = this.formData.id
        })
      }
    },
    // 恢复初始值
    restoreInitial() {
      restoreInitialValue(this.formData)
    },
    // 提交信息
    ensure() {
      this.$refs.form.validate((valid, field) => {
        if (valid) {
          const userId = this.info.id
          this.formData.userId = userId
          const data = { ...this.formData }
          if (this.type === 'declarationChange') {
            saveOrUpdateTBExpertEducationInfoById(data)
              .then(res => {
                this.$emit('dialogDataBuyersInfo', this.formData)
                this.dialogData.dialog = false
              })
          } else {
            const expertId = this.$route.query.id
            if (expertId) {
              data.expertId = expertId
              delete data.userId
            }
            submitOrUpdateExptExpertEducation(data)
              .then(res => {
                this.$emit('dialogDataBuyersInfo', this.formData)
                this.dialogData.dialog = false
              })
          }
        } else {
          this.$message({
            message: '请选择任职信息',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.el-dialog__body {
  height: 55vh;
}
</style>
