<!--
  @description: 专家-评标专业/评标专业弹窗
  @Modifier: 徐媛媛
  @routerPath:/expert/expertEvaluationInfo
  @date: 2020/8/12-13:27
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="organizationDialog"
    :close-on-click-modal="false"
    title="添加评标专业"
    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="specialtyType" />
            </td>
            <td>
              <el-radio-group v-model="formData.specialtyType" @change="selectBuyersShowDialogBtn('clear')">
                <el-radio label="01">
                  主评
                </el-radio>
                <el-radio label="00">
                  辅评
                </el-radio>
              </el-radio-group>
            </td>
          </tr>
          <!--评标专业--->
          <tr>
            <td>
              <el-form-item label="评标专业" prop="specialtyName" />
            </td>
            <td>
              <el-input v-model="formData.specialtyName" style="width:calc(100% - 156px)" />
              <el-button type="primary" @click="selectBuyersShowDialogBtn">
                选择
              </el-button>
              <el-button type="primary" plain @click="selectBuyersShowDialogBtn('clear')">
                清空
              </el-button>
            </td>
          </tr>
          <!--从事起始年--->
          <tr>
            <td>
              <el-form-item label="从事起始年" prop="beginYear" />
            </td>
            <td>
              <el-date-picker
                v-model="formData.beginYear"
                :value-format="elValueFormatDate"
                type="date"
                placeholder="选择日期"
              />
            </td>
          </tr>
          <expertSepcialDialog
            ref="expertSelectworkunitDialog"
            :title="formData.specialtyType"
            :dialog-data="expertDialog"
            @dialogExpertSepcialList="selectBuyersShowDialogBtn"
          />
          <LibraryTreeDialog ref="LibraryTreeDialog" @LibraryTreeInfo="LibraryTreeInfo" />
        </table>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure()">提交</el-button>
      <el-button @click="organizationDialog = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { submitOrUpdateExptExpertEvaluationSpecia } from '@/api/expert/expertEvaluationInfo' // 获取用户任职经历信息 提交
import { saveOrUpdateExpertSepcial } from '@/api/expert/expertChange'// 变更用户任职经历信息 提交
import expertSepcialDialog from './expertSepcial-dialog' // 选择评标专业书弹窗
import LibraryTreeDialog from '@/views/common/tools/LibraryTree-dialog'// 入库专业选择弹框
// import upload from '@comp/common/updownload/upload'
/* 混合开发方法-参数字段*/
import { commonMxin } from '@/mixins/commonMxin'
/* 组件*/
import { mapGetters } from 'vuex'
export default {
  name: 'BidWorkExperienceDialog',
  components: {
    expertSepcialDialog,
    LibraryTreeDialog
    // upload
  },
  mixins: [commonMxin],
  inject: ['type'],
  data() {
    return {
      specialtyTypeList: [], // 列表
      formData: {
        specialtyType: '', // 评标专业类型 01：主，00：辅
        specialtyId: '', // 专业id
        beginYear: '',
        specialtyName: ''
      },
      rules: {
        beginYear: [{ required: true, message: '请选择从业起始年' }], // 证书编号
        specialtyName: [
          { required: true, message: '评标专业不能为空', trigger: 'blur' }
        ], // 评标专业名称
        specialtyType: [
          { required: true, message: '请选择评标专业类型', trigger: 'blur' }
        ] // 评标专业类型
      },
      organizationDialog: false, // 弹框打开状态
      expertDialog: {
        dialog: false // 选择评标专业弹框
      },
      expertId: '' // id
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  methods: {
    LibraryTreeInfo(data) {
    },
    selectBuyersShowDialogBtn(data) {
      if (data === 'clear') { // 清空
        this.formData.specialtyName = ''
        this.lsdata = ''
      } else { // 选择
        this.formData.specialtyName = data.labels
        this.lsdata = data
        this.$refs.expertSelectworkunitDialog.openDialog()
      }
    },
    // 打开弹框
    openOrganizationDialog(typeData, specialtyTypeList, expertId) {
      this.typeData = typeData
      this.index = specialtyTypeList
      this.specialtyTypeList = specialtyTypeList
      this.expertId = expertId
      this.lsdata = this.formData
      if (typeData === 'add') {
        this.formData = {
          beginYear: '', // 从业起始年
          specialtyName: '', // 评标专业名称
          specialtyType: '00' // 评标专业类型 01：主，00：辅
        }
      }
      this.organizationDialog = true // 打开弹框
    },
    // 提交信息
    ensure() {
      this.$refs.form.validate((valid, field) => {
        if (valid) {
          const data = {
            ssList: [],
            expertId: this.expertId
          }
          if (this.lsdata) {
            data.ssList = this.lsdata.labels.split(',').map((item, index) => {
              const data = {}
              data.specialtyId = this.lsdata.keys.split(',')[index]
              data.specialtyName = item
              data.beginYear = this.formData.beginYear
              data.specialtyType = this.formData.specialtyType
              return data
            })
          }

          const ary = this.specialtyTypeList
          const nary = []
          if (ary.length > 0) ary.sort()
          if (nary[0] === this.formData.specialtyType && this.formData.specialtyType === '01') {
            this.$message({
              message: '只允许有一个主评专业',
              type: 'warning'
            })
            return
          }
          if (this.type === 'declarationChange') {
            saveOrUpdateExpertSepcial(data).then(res => {
              this.$emit('dialogDataBuyersInfo', data, this.typeData, this.index)
              this.organizationDialog = false
            })
          } else {
            submitOrUpdateExptExpertEvaluationSpecia(data).then(res => {
              this.$emit('dialogDataBuyersInfo', data, this.typeData, this.index)
              this.organizationDialog = false
            })
          }
        } else {
          this.$message({
            message: '请选择评标专业',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
.el-dialog__body {
  height: 55vh;
}
</style>
