<!--
  @description: 组件委员会-添加专家
  @Modifier: yf/xwl
  @routerPath:
  @date: 2020/7/7-21:16
-->
<template>
  <el-dialog
    v-dialogDrag
    v-loading="loading"
    :visible.sync="dialogData.dialog"
    title="录入专家"
    width="500px"
    custom-class="el-dialog__body-p0"
  >
    <div class="main mb10">
      <el-form
        ref="fromData"
        :model="expertFormData"
        :rules="rules"
        :inline-message="true"
      >
        <table class="from_table">
          <tr>
            <td>
              姓名
            </td>
            <td>
              <el-form-item prop="name" label="">
                <el-input v-model="expertFormData.name" />
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td> <el-form-item prop="idCard" label="身份证号" /></td>
            <td> <el-input v-model="expertFormData.idCard" /></td>
          </tr>
          <tr>
            <td><el-form-item prop="mobilePhone" label="手机号" /></td>
            <td> <el-input v-model="expertFormData.mobilePhone" /></td>
          </tr>
          <tr>
            <td><el-form-item label="性别" prop="sex" /></td>
            <td>
              <el-radio
                v-for="(item, index) in sexList"
                :key="index"
                v-model="expertFormData.sex"
                :label="item.typecode"
              >
                {{ item.typename }}
              </el-radio>
            </td>
          </tr>
          <tr>
            <td><el-form-item label="专家类型" prop="type" /></td>
            <td>
              <el-radio
                v-for="(item, index) in personTypeList"
                :key="index"
                v-model="expertFormData.type"
                :label="item.typecode"
              >
                {{ item.typename }}
              </el-radio>
            </td>
          </tr>
          <tr>
            <td><el-form-item label="专家类别" prop="expertType" /></td>
            <td>
              <el-checkbox-group v-model="expertFormData.expertType">
                <el-checkbox
                  v-for="(item, index) in expertTypeList"
                  :key="index"
                  :label="item.typecode"
                >
                  {{ item.typename }}
                </el-checkbox>
              </el-checkbox-group>
            </td>
          </tr>
        </table>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer" style="text-align: center">
      <el-button type="primary" @click="entry">
        确 定
      </el-button>
      <el-button @click="dialogData.dialog=false">
        取 消
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
/* api */
import { saveExpertInfo } from '@/api/bidding/bidCommittee.js'

/* 公用方法*/
import { restoreInitialValue, cloneObject } from '@/assets/js/utils'// 常用方法
import { linkerName, checkLinktel, makeSureId } from '@assets/js/validate'// 校验规则
/* 混合开发方法-参数字段*/
import { bidCommitteeMixin } from './../mixins/bidCommitteeMixin'// 组建评标委员会-公共方法
import { bidCommitteeGroupMixin } from './../mixins/bidCommitteeGroupMixin'// 组建评标委员会-字典信息
export default {
  mixins: [bidCommitteeMixin, bidCommitteeGroupMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {
        return {
          dialog: false,
          rowData: {} // 专家信息
        }
      }
    }
  },
  data() {
    return {
      loading: false, //
      // 录入专家-直接直接指定
      rules: {
        name: [{ required: true, validator: linkerName, trigger: 'blur' }], // 姓名
        mobilePhone: [{ required: true, validator: checkLinktel, trigger: 'blur' }], // 联系方式
        idCard: [{ required: true, validator: makeSureId, trigger: 'blur' }], // 身份证
        type: [{ required: true, message: '请选择', trigger: 'blur' }], // 专家类型
        expertType: [{ required: true, message: '请选择', trigger: 'blur' }], // 专家类别
        sex: [{ required: true, message: '请选择', trigger: 'blur' }] // 性别
      }
    }
  },
  watch: {
    // 弹框打开状态
    'dialogData.dialog'(val) {
      if (val) {
        if (this.dialogData.rowData && this.dialogData.rowData.id) {
          this.expertFormData = cloneObject(this.dialogData.rowData)
          this.expertFormData.expertType = this.expertFormData.expertType.split(',')// this.expertFormData
        }
      } else {
        // 清空 value
        this.dialogData.rowData = {}
        restoreInitialValue(this.expertFormData)
        this.expertFormData.type = '00' // 专家类型(00专家 01招标代表人)
        this.expertFormData.expertType = ['00']// 专家类别(00商务 01技术 02经济)
        this.expertFormData.sex = '00' // 性别（00男 01女）
      }
    }
  },
  methods: {
    // 提交
    entry() {
      this.$refs.fromData.validate(valid => {
        if (valid) {
          const data = {
            ...this.expertFormData,
            expertType: this.expertFormData.expertType.join(','), // 专家类别
            stageId: this.$store.getters.packInfo.stageId // 标段id
          }
          this.loading = true
          saveExpertInfo(data).then(res => {
            this.loading = false
            this.dialogData.dialog = false
            this.$emit('change')
          }).catch(() => {
            this.loading = false
            this.$emit('change')
          })
        }
      })
    }
  }
}
</script>
