<!--
  @description:集约化评标-添加/编辑屏蔽专家-弹框
  @Modifier: 肖文龙
  @routerPath:
  @date: 2020年3月9日15:22:29
-->
<template>
  <div>
    <el-dialog
      :visible.sync="dialogData.dialog"
      :close-on-click-modal="false"
      title="添加屏蔽单位"
      width="500px"
      custom-class="el-dialog__body-p0"
      top="calc((100vh - 441px) / 2)"
    >
      <section class="main" style="width: 94%;">
        <el-form ref="formData" :model="formData" :rules="rules" label-width="100px">
          <el-row>
            <el-col>
              <!--屏蔽单位-->
              <el-form-item ref="avoidCompany" prop="avoidCompany" label="屏蔽单位">
                <el-input
                  v-model="formData.avoidCompany"
                  disabled
                  show-word-limit
                  maxlength="200"
                  style="width: calc(100% - 156px)"
                  placeholder="请选择"
                />
                <el-button type="primary" @click="expertSpecialtyDialog.dialog=true">
                  选择
                </el-button>
                <el-button type="primary" plain @click="shieldingUnitSelectInfo('clear')">
                  清空
                </el-button>
              </el-form-item>
              <!--屏蔽原因-->
              <el-form-item ref="reason" prop="reason" label="屏蔽原因">
                <el-input
                  v-model="formData.reason"
                  :autosize="{ minRows: 8, maxRows: 8 }"
                  type="textarea"
                  show-word-limit
                  maxlength="4000"
                  placeholder="最多允许输入4000个汉字(8000字符)"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="ensure('formData')">确 定</el-button>
        <el-button @click="dialogData.dialog = false">取 消</el-button>
      </span>
    </el-dialog>
    <!--选择专家专业弹框-->
    <shieldingUnitSelectDialog :dialog-data="expertSpecialtyDialog" @shieldingUnitSelectInfo="shieldingUnitSelectInfo" />
  </div>
</template>

<script>
/* api*/
import { saveOrUpdateExtractCompanyInfo } from '@/api/intensiveeval/intensiveeval'
/* 公共方法*/
import { characters1_50 } from '@/assets/js/validate'// 校验
import { cloneObject, restoreInitialValue } from '@/assets/js/utils'// 深拷贝
/* 混合开发*/
/* 组件*/
import shieldingUnitSelectDialog from './shieldingUnitSelect-dialog'// 选择专家专业弹框
export default {
  name: 'ShieldingUnitDalog',
  components: {
    shieldingUnitSelectDialog
  },
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {
        return {
          dialog: false // 是否展开 布尔值
        }
      }
    },
    // 参数
    data: {
      type: Object,
      default: function() {
        return
      }
    }
  },
  data() {
    return {
    //  选择专家专业弹框- 弹窗展示状态
      expertSpecialtyDialog: {
        dialog: false // 弹窗状态
      },
      // 添加/编辑屏蔽专家信息
      formData: {
        avoidCompany: '', // 屏蔽单位
        avoidOrgcode: '', // 屏蔽单位机构编码
        companyId: '', // 屏蔽单位Id
        reason: '', // 屏蔽原因
        avoidType: '01', // 手动
        juryId: '', // 评委会id
        id: ''// 主键Id
      },
      // 校验
      rules: {
        avoidCompany: [{ required: true, message: '请选择', trigger: 'change' }], // 屏蔽单位
        reason: [{ required: true, validator: characters1_50, trigger: 'blur' }]// 屏蔽原因
      }
    }
  },
  watch: {
    // 监听-弹窗展示状态
    'dialogData.dialog'() {
      if (this.dialogData.dialogType === '01') { // 编辑
        this.formData = cloneObject(this.data)// 评委会id
      } else {
        this.$nextTick(() => {
          this.$refs.formData.resetFields()
        })
      }
      this.formData.juryId = this.$route.query.juryId// 评委会id
    }
  },
  methods: {
    // 屏蔽单位选择/清空
    shieldingUnitSelectInfo(data) {
      if (data === 'clear') { // 清空
        this.formData.avoidCompany = ''// 名称
        this.formData.avoidOrgcode = ''// 编号
        this.formData.companyId = ''// id
      } else { // 选择
        this.formData.avoidCompany = data.name// 名称
        this.formData.avoidOrgcode = data.code// 编号
        this.formData.companyId = data.id// id
      }
    },
    // 确定
    ensure(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveOrUpdateExtractCompanyInfo(cloneObject(this.formData)).then(res => {
            if (res.data === '01') { // 机构未重复
              this.$emit('shieldingUnitInfo', cloneObject(this.formData))
              this.initialize()// 重置数据
            } else if (res.data === '00') { // 机构重复
              this.$message({
                message: '该单位已添加',
                type: 'warning'
              })
            }
          })
        }
      })
    },
    // 取消
    cancel() {
      if (this.dialogData.dialogType === '01') { // 编辑)
        this.initialize()// 重置数据
      }
    },
    // 重置数据
    initialize() {
      // 恢复初始值
      restoreInitialValue(this.formData)
      this.formData.avoidType = '01'// 手动
      this.dialogData.dialog = false// 关闭弹窗
    }
  }
}
</script>

<style scoped lang="less">

</style>
