<!--
  @description: 专家-回避单位/添加回避单位弹窗
  @Modifier: 徐媛媛
  @routerPath:
  @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"
        @current-change="currentChange"
      >
        <table key="index" class="from_table">
          <!--单位名称--->
          <tr>
            <td>
              <el-form-item label="工作单位" prop="comName" />
            </td>
            <td>
              <el-input v-model="formData.comName" style="width:calc(100% - 172px)" />
              <el-button type="primary" @click="selectBuyersShowDialogBtn()">
                选择
              </el-button>
              <el-button @click="clearUnit">
                清空
              </el-button>
            </td>
          </tr>
          <!--机构代码--->
          <tr>
            <td>
              <el-form-item label="统一社会信用代码" prop="comCode" />
            </td>
            <td>
              <el-input
                v-model.trim="formData.comCode"
                placeholder="统一社会信用代码"
                class="show-word-limit-padding"
              />
            </td>
          </tr>
          <!--回避原因-->
          <tr>
            <td>
              <el-form-item label="回避原因 " prop="reason" />
            </td>
            <td>
              <el-radio-group v-model="formData.reason">
                <el-radio label="00">
                  项目业主
                </el-radio>
                <el-radio label="01">
                  行政监督单位
                </el-radio>
                <el-radio label="02">
                  投标单位
                </el-radio>
                <el-radio label="03">
                  代理机构
                </el-radio>
                <el-radio label="04">
                  行政主管单位
                </el-radio>
                <el-radio label="05">
                  其他原因
                </el-radio>
              </el-radio-group>
            </td>
          </tr>
        </table>
      </el-form>
      <expertSelectworkunitDialog
        ref="expertSelectworkunitDialog"
        @dialogDataBuyersInfo="dialogDataBuyersInfo"
      />
    </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 { saveOrUpdateExpertAchievement } from '@/api/expert/expertAvoidanceUnit' // 获取用户任职经历信息 提交
import { saveOrUpdateTBExpertAvoidCompanyHistory } from '@/api/expert/expertChange.js' //  新增或修改专家某个回避单位
import { upLoadFileUrl } from '@/api/common/uploadCenter'
// import upload from '@comp/common/updownload/upload'
/* 混合开发方法-参数字段*/
import { organizationCode } from '@/assets/js/validate'
/* 组件*/
import expertSelectworkunitDialog from '@views/expert/expertInfo/expertWorkPerience/selectworkunit-dialog' // 选择工作单位弹窗
import { mapGetters } from 'vuex'
export default {
  name: 'BidWorkExperienceDialog',
  components: {
    expertSelectworkunitDialog
    // upload
  },
  inject: ['type'],
  data() {
    return {
      formData: {
        comName: '', // 单位名称
        comCode: '', // 机构编码
        reason: '', // 回避原因 1 项目业主 2行政监督单位 3投标单位 4 代理机构 5行政主管单位 6其他原因
        expertId: '', // 专家id
        projectBuyersList: [],
        id: '',
        flag: ''
      },
      rules: {
        comName: [
          { required: true, message: '请填写工作单位', trigger: 'blur' }
        ], // 所在部门
        comCode: [
          { required: true, validator: organizationCode, trigger: 'blur' }
        ], // 组织机构代码
        reason: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ] // 回避原因
      },
      loading: true,
      idImportFileUrl: upLoadFileUrl,
      organizationDialog: false, // 弹框打开状态
      title: '', // 标题
      // 搜索信息
      searchForm: {
        comCode: '',
        departName: ''
      },
      openOrganizationData: {
        type: '',
        index: '',
        data: ''
      },
      list: [], // 列表数据
      currentData: {} // 当前选择的信息
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  methods: {
    selectBuyersShowDialogBtn() {
      this.$refs.expertSelectworkunitDialog.openOrganizationDialog(true)
    },
    dialogDataBuyersInfo(currentData) {
      this.formData.comName = currentData.comName
      this.formData.comCode = currentData.comCode
    },
    // 清空工作单位
    clearUnit() {
      this.ruleForm.comName = ''
      this.ruleForm.comCode = ''
      this.ruleForm.comId = ''
    },
    // 当前选择的信息
    currentChange(val) {
      this.currentData = val
    },
    // 打开弹框
    openOrganizationDialog(type, index) {
      this.openOrganizationData.type = type
      this.openOrganizationData.index = index
      if (type === 'add') {
        this.formData = {
          comName: '', // 单位名称
          comCode: '', // 机构编码
          reason: '', // 回避原因
          expertId: '', // 专家id
          projectBuyersList: [],
          id: ''
        }
      }
      this.organizationDialog = true // 打开弹框
    },
    // 提交信息
    ensure() {
      this.$refs.form.validate((valid, field) => {
        if (valid) {
          const userId = this.info.id
          this.formData.userId = userId
          this.formData.flag = '01'
          const data = { ...this.formData }
          this.$emit('dialogDataApplyInfo', data)
          if (this.type === 'declarationChange') {
            saveOrUpdateTBExpertAvoidCompanyHistory(data).then(res => {
              this.$emit('dialogDataBuyersInfo', res.data, this.openOrganizationData.type, this.openOrganizationData.index)
              this.organizationDialog = false
            })
          } else {
            const expertId = this.$route.query.id
            if (expertId) {
              data.expertId = expertId
              delete data.userId
            }
            saveOrUpdateExpertAchievement(data).then(res => {
              this.$emit('dialogDataBuyersInfo', res.data, this.openOrganizationData.type, this.openOrganizationData.index)
              this.organizationDialog = false
            })
          }
        } else {
          this.$message({
            message: '请选择回避单位',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

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