  <!--
  @description: 新增/编辑-不合格供应商
  @Modifier: haofengxian/xwl
  @routerPath:
  @date: 2020-2-25 13:20:35
-->
<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="dialog"
      :close-on-click-modal="false"
      title="不合格供应商"
      width="700px"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="formData"
        :inline-message="true"
        style="margin-bottom:20px;"
      >
        <table class="from_table">
          <tr>
            <td>
              <el-form-item ref="supplierName" label="供应商名称" prop="supplierName" />
            </td>
            <td>
              <el-input v-model="formData.supplierName" disabled style="width: calc(100% - 156px )" />
              <el-button type="primary" @click="supplierDialog.dialog = true">
                选择
              </el-button>
              <el-button type="primary" plain @click="supplierInfo('clear')">
                清空
              </el-button>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item ref="punishIdea" label="处罚意见" prop="punishIdea" />
            </td>
            <td>
              <el-radio-group v-model="formData.punishIdea" @change="punishIdeaChange">
                <el-radio
                  v-for="(item,index) in supplierPunishIdeaGroup"
                  :key="index"
                  v-model="formData.punishIdea"
                  :label="item.typecode"
                >
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </td>
          </tr>
          <tr>
            <td><el-form-item ref="freezingReasons" label="淘汰原因" prop="freezingReasons" /></td>
            <td>
              <!-- 不合格供应商淘汰原因-->
              <el-select v-if="formData.punishIdea === '01'" v-model="formData.freezingReasons" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in supplierBelowGradeWeedOutReasonGroup"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
              <!--纳入黑名单淘汰原因-->
              <el-select v-if="formData.punishIdea === '02'" v-model="formData.freezingReasons" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in supplierBlackWeedOutReasonGroup"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
          </tr>
          <tr v-if="formData.punishIdea === '02'">
            <td>
              <el-form-item ref="punishTime" label="处罚时间期限(月)" prop="punishTime" />
            </td>
            <td>
              <el-select v-model="formData.punishTime" placeholder="请选择">
                <el-option
                  v-for="item in supplierPunishTimeTermGroup"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="事件说明" prop="remarks" />
            </td>
            <td>
              <el-input
                v-model="formData.remarks"
                :rows="2"
                type="textarea"
                placeholder="请输入内容"
              />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="附件" />
            </td>
            <td>
              <upload
                v-if="dialog"
                ref="upload"
                :data="principalData"
                :auto-delete="true"
                :auto-upload="true"
                title=""
                accept="image/jpg,image/jpeg,image/png"
                tip-text="请上传盖章说明文件（格式jpg、png，pdf）"
              />
            </td>
          </tr>
        </table>
      </el-form>
      <!--底部操作按钮-->
      <span slot="footer" class="dialog-footer">
        <el-button :loading="BtnLoading" type="primary" @click="ensure('form')">提交</el-button>
        <el-button :loading="BtnLoading" @click="dialog = false">取消</el-button>
      </span>
    </el-dialog>
    <!--选择供应商弹框-->
    <supplierListDialog :dialog-data="supplierDialog" @call-back="supplierInfo" />
  </div>
</template>

<script>
/* 接口*/
import { saveBlacklistInfo, queryBlacklistInfo } from '@/api/supplier/supplierUnqualified'
/* 常用方法*/
import { randomData, restoreInitialValue } from '@/assets/js/utils/index'/* 公用方法*/
/* 组件*/
import supplierListDialog from '@/views/common/user/supplierList-dialog' // 选择供应商弹框
import upload from '@comp/common/updownload/upload'// 文件上传组件
import { supplierGroupMixin } from '@/views/supplier/mixins/supplierGroupMixin'
export default {
  name: 'ConsignContractSelectDialog',
  components: {
    supplierListDialog, // 选择供应商弹框
    upload
  },
  mixins: [supplierGroupMixin],
  data() {
    return {
      principalData: {
        bizId: '', // 业务主键Id
        fileType: 'save_blackList_Attr' // 保存黑名单文件类型
      },
      BtnLoading: false,
      dialog: false, // 不合格供应商弹框是否打开
      // 供应商明细-弹窗-展示/隐藏状态 true 展示/false 隐藏
      supplierDialog: {
        dialog: false // 选择供应名称弹框
      },
      formData: {
        freezingReasons: '', // 淘汰原因
        id: '', // 主键
        punishIdea: '01', // 处罚意见 01:暂停合格供应商资格 02:取消合格供应商资格
        punishTime: '', // 处罚时间期限 1-11:1-11月 12:一年 24:两年 36:三年
        remarks: '', // 事件说明
        status: '', // 状态 00保存 01提交
        supplierId: '', // 供应商id
        supplierName: '' // 供应商名称
      },
      // 校验输入内容
      rules: {
        punishIdea: [{ required: true, message: '请选择', trigger: 'change' }], // 处罚意见
        supplierName: [{ required: true, message: '请选择', trigger: 'change' }], // 供应名称
        freezingReasons: [{ required: true, message: '请选择', trigger: 'blur' }], // 淘汰原因
        punishTime: [{ required: true, message: '请选择', trigger: 'change' }], // 处罚期限
        remarks: [{ required: true, message: '请输入', trigger: 'blur' }] // 事件说明
      }
    }
  },
  created() {
    this.initGetBlacklistDictionaries() // 字典
  },
  methods: {
    // 父组件点击"不合格供应商"触发此方法打开弹框
    open(id) {
      this.dialog = true // 打开弹框
      // 初始化
      restoreInitialValue(this.formData)
      this.formData.punishIdea = '01'// 处罚意见 01:暂停合格供应商资格
      // 有id编辑 否则是新增
      if (id) {
        this.formData.id = id
        this.businessLicenseData.bizId = id
        this.getBlacklistInfo(id) // 查询黑名单详情
      } else {
        this.formData.id = randomData()// 随机生成ID
        this.businessLicenseData.bizId = this.formData.id
      }
    },
    // 查询黑名单详情
    getBlacklistInfo(id) {
      queryBlacklistInfo(id).then(res => {
        this.formData = res.data
      }).catch(() => {
      })
    },
    // 监听处罚意见
    punishIdeaChange() {
      this.formData.freezingReasons = '' // 淘汰原因置为空
      this.formData.punishTime = ''// 处罚时间期限
    },
    // 提交
    ensure(formName) {
      this.formData.status = '01'// 提交
      // 校验输入内容
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          saveBlacklistInfo(this.formData).then(res => {
            this.dialog = false
            this.$emit('call-back') // 新增/修改 成功时，触发父组件重载列表
          }).catch(() => {
          })
        }
      })
    },
    // 供应商名称选择信息
    supplierInfo(data) {
      if (data === 'clear') { // 清空
        this.formData.supplierName = ''// 供应名称
        this.formData.supplierId = '' // 供应商id
      } else { // 选择
        this.formData.supplierName = data.supplierName// 供应名称
        this.formData.supplierId = data.supplierId // 供应商id
      }
    }
  }
}
</script>

<style scoped lang="less">

</style>
