<!--
  @description: 项目规则之单一来源采购组件
  @author: gmy
  @date: 2020-8-4 10:21:58
-->
<template>
  <div class="mb16">
    <table class="from_table">
      <!--   紧急采购 报价有效期     -->
      <tr>
        <td>
          <el-form-item ref="isEmergencyProcurement" :rules="rules.isEmergencyProcurement" label="紧急采购" prop="isEmergencyProcurement" />
        </td>
        <td>
          <el-radio-group v-model="dataRule.isEmergencyProcurement">
            <el-radio
              v-for="(item) in emergencyProcurementList"
              :key="item.id"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-radio>
          </el-radio-group>
        </td>
        <td>
          <el-form-item :rules="rules.quoteValidity" label="报价有效期" prop="quoteValidity" />
        </td>
        <td>
          <el-date-picker
            v-model="dataRule.quoteValidity"
            :picker-options="disabledPickerOptions"
            :value-format="elValueFormat"
            type="date"
            placeholder="选择日期时间"
          />
        </td>
        <!-- <td>
          <el-form-item label="首轮报价截止时间" />
        </td>
        <td /> -->
      </tr>
      <!--    报价有效时间  强制上传应答文件       -->
      <tr>
        <td>
          <el-form-item label="强制上传应答文件" prop="isUploadReplyFile" />
        </td>
        <td>
          <el-radio-group v-model="dataRule.isUploadReplyFile">
            <el-radio
              v-for="(item) in uploadReplyFileList"
              :key="item.id"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-radio>
          </el-radio-group>
        </td>
        <td>
          <el-form-item ref=" currency" label="币种" prop="currency" />
        </td>
        <td>
          <el-select v-model="dataRule.currency">
            <el-option
              v-for="(item) in currencyList"
              :key="item.id"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </td>
      </tr>
      <!--    收取标书费 标书费金额 -收取保证金     -->
      <!-- <tr style="height: 50px" >
        <td><el-form-item ref="isReceiveTenderDocumentPrice" :rules="rules.isReceiveTenderDocumentPrice" label="收取标书费" prop="isReceiveTenderDocumentPrice"/></td>
        <td>
          <el-radio-group v-model="dataRule.isReceiveTenderDocumentPrice" :disabled="raidioType">
            <el-radio
              v-for="(item) in documentPrice"
              :key="item.typecode"
              :label="item.typecode">
              {{ item.typename }}
        </el-radio></el-radio-group></td>
        <td>
        <el-form-item ref="isReceiveTenderBailPrice" :rules="rules.isReceiveTenderBailPrice" label="收取保证金" prop="isReceiveTenderBailPrice"/></td>
        <td>
          <el-radio-group v-model="dataRule.isReceiveTenderBailPrice" :disabled="raidioType">
            <el-radio
              v-for="(item) in bailPrice"
              :key="item.id"
              :label="item.typecode">
              {{ item.typename }}
            </el-radio>
          </el-radio-group>
        </td>
      </tr> -->
      <!--    标书费金额 保证金金额   -->
      <!-- <tr v-if="dataRule.isReceiveTenderDocumentPrice==='01'|| dataRule.isReceiveTenderBailPrice==='01'">
        <td v-if="dataRule.isReceiveTenderDocumentPrice==='01'">
          <el-form-item v-for="(item, index) in dataRule.protProjectDocumentPriceList " :key="index" :prop="'protProjectDocumentPriceList.' + index + '.documentPrice'" :rules="rules.documentPrice" :inline-message="true" label="标书费金额(元)"/>
        </td>
        <td v-if="dataRule.isReceiveTenderDocumentPrice==='01'"> -->
      <!--金额组件-->
      <!-- <money :thousands="true" :unit-stust="true" :money-value="dataRule.protProjectDocumentPriceList[0]" name="documentPrice"/>
        </td>
        <td v-if="dataRule.isReceiveTenderDocumentPrice==='00'"/>
        <td v-if="dataRule.isReceiveTenderDocumentPrice==='00'"/>
        <td v-if="dataRule.isReceiveTenderBailPrice==='01'">
          <el-form-item v-for="(item, index) in dataRule.protProjectBailPriceList " :key="index" :prop="'protProjectBailPriceList.' + index + '.bailPrice'" :rules="rules.bailPrice" :inline-message="true" label="保证金金额(元)"/>
        </td>
        <td v-if="dataRule.isReceiveTenderBailPrice==='01'"> -->
      <!--金额组件-->
      <!-- <money :thousands="true" :unit-stust="true" :money-value="dataRule.protProjectBailPriceList[0]" name="bailPrice"/>
        </td>
        <td v-if="dataRule.isReceiveTenderBailPrice==='00'"/>
        <td v-if="dataRule.isReceiveTenderBailPrice==='00'"/>
      </tr> -->
      <!--    币种-付款方式       -->
      <tr>
        <td>
          <el-form-item ref="isContainsFreight" label="是否含运费" prop="isContainsFreight" />
        </td>
        <td>
          <el-radio-group v-model="dataRule.isContainsFreight">
            <el-radio
              v-for="(item) in containsFreightList"
              :key="item.id"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-radio>
          </el-radio-group>
        </td>
        <td>
          <el-form-item ref="quoteType" :rules="rules.quoteType" label="报价要求" prop="quoteType" />
        </td>
        <td>
          <el-radio-group v-model="dataRule.quoteType">
            <el-radio
              v-for="(item) in quoteType"
              :key="item.typecode"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-radio>
          </el-radio-group>
        </td>
      </tr>
      <!--    公示结束时间-物资公司联系人       -->
      <!-- isEmergencyProcurement 00 不是紧急采购 01 紧急采购 -->
      <tr v-if="dataRule.isEmergencyProcurement==='00'">
        <td>
          <el-form-item :rules="rules.publicityEndTime" label="公示结束时间" prop="publicityEndTime" />
        </td>
        <td colspan="3">
          <el-date-picker
            v-model="dataRule.publicityEndTime"
            :picker-options="disabledPickerOptions"
            :format="elValueFormat"
            :value-format="elValueSaveFormat"
            :default-time="defaultTimeDateTime"
            type="datetime"
            placeholder="选择日期时间"
          />
        </td>
      </tr>
      <tr>
        <td>
          <el-form-item ref="reviewMethod" :rules="rules.reviewMethod" label="评审办法" prop="reviewMethod" />
        </td>
        <td v-if="sourceType01A">
          {{ filterDictText(reviewMethod, dataRule.reviewMethod) }}
        </td>
        <td v-else>
          <el-radio-group v-model="dataRule.reviewMethod">
            <el-radio
              v-for="(item) in reviewMethod"
              :key="item.typecode"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-radio>
          </el-radio-group>
        </td>
        <td>
          <el-form-item ref="payType" :rules="rules.payType" label="付款方式" prop="payType" />
        </td>
        <td>
          <el-select
            v-model="dataRule.payType"
            :popper-append-to-body="false"
            popper-class="select-popper"
            filterable
            clearable
            style="width:100%"
          >
            <el-option
              v-for="(item) in payTypelist"
              :key="item.id"
              :title="item.typename"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </td>
      </tr>
      <tr>
        <td>
          <el-form-item ref="isReceiveTenderDocumentPrice" :rules="rules.isReceiveTenderDocumentPrice" label="是否收取标书费" prop="isReceiveTenderDocumentPrice" />
        </td>
        <td :colspan="dataRule.isReceiveTenderDocumentPrice === '01' ? '' : '3'">
          <el-radio-group v-model="dataRule.isReceiveTenderDocumentPrice">
            <el-radio
              v-for="(item) in documentPrice"
              :key="item.typecode"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-radio>
          </el-radio-group>
        </td>
        <td v-if="dataRule.isReceiveTenderDocumentPrice === '01'">
          <el-form-item ref="documentPrice" :rules="rules.documentPrice" label="标书费金额" prop="protProjectDocumentPriceList[0].documentPrice" />
        </td>
        <td v-if="dataRule.isReceiveTenderDocumentPrice === '01'">
          <money
            :money-value="dataRule.protProjectDocumentPriceList[0]"
            width="90%"
            size="mini"
            name="documentPrice"
          />
        </td>
      </tr>
      <tr>
        <td>
          <el-form-item ref="supplierCategory" :rules="rules.supplierCategory" label="供应商分类" prop="supplierCategory" />
        </td>
        <td v-if="sourceType01A">
          {{ filterDictText(supplierCategoryList, dataRule.supplierCategory?dataRule.supplierCategory.join(','):dataRule.supplierCategory) }}
        </td>
        <td v-else>
          <el-checkbox-group v-model="dataRule.supplierCategory">
            <el-checkbox
              v-for="(item, index) in supplierCategoryList"
              :key="index"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-checkbox>
          </el-checkbox-group>
        </td>
        <td>
          <el-form-item ref="supplierAttribute" :rules="rules.supplierAttribute" label="供应商属性" prop="supplierAttribute" />
        </td>
        <td v-if="sourceType01A">
          {{ filterDictText(supplierAttributeList, dataRule.supplierAttribute?dataRule.supplierAttribute.join(','):dataRule.supplierAttribute) }}
        </td>
        <td v-else>
          <el-checkbox-group v-model="dataRule.supplierAttribute">
            <el-checkbox
              v-for="(item, index) in supplierAttributeList"
              :key="index"
              :label="item.typecode"
            >
              {{ item.typename }}
            </el-checkbox>
          </el-checkbox-group>
        </td>
      </tr>
      <!-- isEmergencyProcurement 00 不是紧急采购 01 紧急采购 -->
      <!--      <tr v-if="dataRule.isEmergencyProcurement=== '00'">
        <td>
          <el-form-item :rules="rules.materialContactAddress" label="地址" prop="materialContactAddress" />
        </td>
        <td colspan="3">
          <el-input v-model="dataRule.materialContactAddress" style="width: 96%" maxlength="500" placeholder="最多允许输入500个汉字" clearable />
        </td>
      </tr>-->
      <!-- 拟采购物资、施工或服务的说明 -->
      <!-- isEmergencyProcurement 00 不是紧急采购 01 紧急采购 -->
      <tr>
        <td>
          <el-form-item :rules="rules.purchasingMaterialsExplain" label="采购物资、施工或服务说明" prop="purchasingMaterialsExplain" />
        </td>
        <td colspan="3">
          <el-input
            v-model="dataRule.purchasingMaterialsExplain"
            :autosize="{ minRows: 4, maxRows: 4 }"
            style="width: 96%"
            type="textarea"
            maxlength="500"
            show-word-limit
            clearable
          />
        </td>
      </tr>
      <!-- 采用单一来源的原因及说明 -->
      <!-- isEmergencyProcurement 00 不是紧急采购 01 紧急采购 -->
      <tr>
        <td>
          <el-form-item :rules="rules.singleSourceExplain" label="采用单一来源的原因及说明" prop="singleSourceExplain" />
        </td>
        <td colspan="3">
          <el-input
            v-model="dataRule.singleSourceExplain"
            :autosize="{ minRows: 4, maxRows: 4 }"
            style="width: 96%"
            type="textarea"
            maxlength="500"
            show-word-limit
            clearable
          />
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
/* 接口*/
import { getTime } from '@api/common/tools.js' // 获取服务器时间
import { procurementProjectGroupMixin } from '../../mixins/procurementProjectGroupMixin'// 立项采购-字典
/* 公共方法*/
import { email, phoneAndText, characters1_50, checkMoney, characters1_auto } from '@/assets/js/validate'
import { linkerName } from '@/assets/js/validate'
import { filterDictText } from '@/assets/js/utils'
/* 组件 */
import money from '@comp/common/money/money' // 金额组件
// vuex
import { mapGetters } from 'vuex'
export default {
  name: 'ProcurementProjectAdd',
  components: {
    money // 金额组件
  },
  mixins: [procurementProjectGroupMixin],
  props: {
    dataRule: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      sourceType01A: false,
      confirmLoading: false,
      disflags: true,
      flgsaction: true,
      labellocaltion: 'left',
      status: '', // 采购方式：10公开询价  11邀请询价  12竞争性谈判  13竞争性磋商   14公开竞价  15邀请竞价  16单一来源采购
      amounttenderfeeflg: false, // 标书费金额显示隐藏
      depositflg: false, // 保证金金额显示隐藏
      procurementDocumentsFlg: false, // 标书费收取显示隐藏
      depositflgs: false, // 线下收取
      // 校验输入内容
      rules: {
        quoteEndTime: [{ required: true, message: '请输入', trigger: 'blur' }], // 报价截止时间
        quoteValidity: [{ required: true, message: '请输入', trigger: 'blur' }], // 报价有效期
        payType: [{ required: true, validator: characters1_auto, trigger: 'change' }], // 付款方式
        publicityEndTime: [{ required: true, message: '请输入', trigger: 'blur' }], // 公示时间
        isReceiveTenderBailPrice: [{ required: true, message: '请选择', trigger: 'blur' }], // 收取保证金
        isReceiveTenderDocumentPrice: [{ required: true, message: '请选择', trigger: 'blur' }], // 收取标书费
        isEmergencyProcurement: [{ required: true, message: '请选择', trigger: 'blur' }], // 紧急采购
        bailPrice: [{ required: true, validator: checkMoney, trigger: 'blur' }], // 保证金金额
        documentPrice: [{ required: true, validator: checkMoney, trigger: 'blur' }], // 标书费金额
        isPublic: [{ required: true, message: '请选择', trigger: 'blur' }], // 是否公示
        materialContactPerson: [{ required: true, validator: linkerName, trigger: 'blur' }], // 联系人
        materialContactPhone: [{ required: true, validator: phoneAndText, trigger: 'blur' }], // 联系人手机号
        materialContactEmail: [{ required: true, validator: email, trigger: 'blur' }], // 联系人邮箱
        materialContactAddress: [{ required: true, validator: characters1_50, trigger: 'blur' }], // 地址
        singleSourceExplain: [{ required: true, validator: characters1_50, trigger: 'blur' }], // 采用单一来源的原因及说明
        purchasingMaterialsExplain: [{ required: true, validator: characters1_50, trigger: 'blur' }], //  拟采购物资、施工或服务的说明
        quoteType: [{ required: true, message: '请选择', trigger: 'blur' }], // 报价要求
        reviewMethod: [{ required: true, message: '请选择', trigger: 'blur' }], // 评审办法
        supplierCategory: [{ type: 'array', required: true, message: '请选择', trigger: 'change' }], // 供应商分类
        supplierAttribute: [{ type: 'array', required: true, message: '请选择', trigger: 'change' }] // 供应商属性
      }
    }
  },
  computed: {
    /* language 国际化*/
    ...mapGetters(['language'])
  },
  watch: {
    // 是否为紧急采购或者非紧急采购紧急采购的清空一下字段
    'dataRule.isEmergencyProcurement'(val) {
      // 紧急采购清空公示时间
      if (val === '01') {
        this.dataRule.publicityEndTime = '' // 紧急采购清楚公示时间
        this.dataRule.materialContactPerson = '' // 紧急采购清楚物资公司联系人
        this.dataRule.materialContactPhone = '' // 紧急采购清楚联系人电话
        this.dataRule.materialContactEmail = '' // 紧急采购清楚联系人邮箱
        this.dataRule.materialContactAddress = '' // 紧急采购清楚联系人地址
      }
    },
    // 监听报价有效期首次加载默认为当前时间增加90天后的日期二次进入有值不做处理
    'dataRule.quoteValidity': {
      handler(val) {
        if (!val) {
          this.changeQuoteEndTime()
        }
      }
    }
  },
  created() {
    // 页面初始化
    this.pageInit()
    // 调用项目规则字典方法
    this.initqueryProcurementProjectRuleInitInfo()
  },
  methods: {
    filterDictText, // 引入字典值替换文本通用方法
    // 页面初始化
    pageInit() {
      if (this.$route.query.sourceType.indexOf('01A') !== -1) {
        this.sourceType01A = true
      }
    },
    // 项目规则，报价有效期，默认为当前时间增加90天
    changeQuoteEndTime() {
      const nowTime = this.currentTimeFn() // 获取当前时间
      this.dataRule.quoteValidity = this.$Moment(nowTime).add(90, 'days').format(this.momentDate)
    },
    // 报价截止时间的change事件
    chandehanle(val) {
      // 当前时间
      const presentTime = this.$Moment(this.currentTime)
      // 选择的报价截止时间
      const selectTime = this.$Moment(val)
      // 时间差具体到天
      const diffTime = selectTime.diff(presentTime, 'day')
      // 判断是否紧急采购 isEmergencyProcurement 00 否 01 是
      if (this.dataRule.isEmergencyProcurement === '00') {
        // 非紧急采购报价截止时间-当前时间>=3
        if (diffTime < 3) {
          this.dataRule.quoteValidity = ''
          this.dataRule.quoteEndTime = ''
          this.$message.error('报价截止时间应大于当前时间3天')
          return
        }
      } else if (this.dataRule.isEmergencyProcurement === '01') {
        // 紧急采购报价截止时间-当前时间>=1
        if (diffTime < 1) {
          this.dataRule.quoteValidity = ''
          this.dataRule.quoteEndTime = ''
          this.$message.error('报价截止时间应大于当前时间1天')
          return
        }
      }
      // 获取有效期时间 报价截止时间增加90天
      this.dataRule.quoteValidity = this.$Moment(val).add(90, 'days').format('YYYY-MM-DD')
    },
    // 获取当前时间
    getCurrentTime() {
      getTime().then(res => {
        this.currentTime = res.data.time
      }).catch(() => {
      })
    },
    // 保存或提交时 组装数据然后返回
    assemblyData() {
      const data = {
        id: this.dataRule.id, // 项目规则id
        tenderId: this.$route.query.tenderId, // 项目id
        isEmergencyProcurement: this.dataRule.isEmergencyProcurement, // 紧急采购
        quoteValidity: this.dataRule.quoteValidity ? this.$Moment(this.dataRule.quoteValidity).format(this.momentSaveFormat) : '', // 报价有效期
        isReceiveTenderBailPrice: this.dataRule.isReceiveTenderBailPrice, // 是否收取保证金
        protProjectBailPriceList: this.dataRule.protProjectBailPriceList, // 保证金金额
        publicityEndTime: this.dataRule.publicityEndTime ? this.$Moment(this.dataRule.publicityEndTime).format(this.momentSaveFormat) : '', // 公示结束时间
        materialContactPerson: this.dataRule.materialContactPerson, // 物资公司联系人
        materialContactPhone: this.dataRule.materialContactPhone, // 联系人电话
        materialContactEmail: this.dataRule.materialContactEmail, // 联系人邮箱
        materialContactAddress: this.dataRule.materialContactAddress, // 联系人地址
        isUploadReplyFile: this.dataRule.isUploadReplyFile, // 强制上传应答文件
        isContainsFreight: this.dataRule.isContainsFreight, // 是否含运费
        currency: this.dataRule.currency, // 币种
        payType: this.dataRule.payType, // 付费方式
        quoteType: this.dataRule.quoteType, // 报价要求 默认全部报价
        purchasingMaterialsExplain: this.dataRule.purchasingMaterialsExplain, //  拟采购物资、施工或服务的说明
        singleSourceExplain: this.dataRule.singleSourceExplain, // 采用单一来源的原因及说明
        isReceiveTenderDocumentPrice: this.dataRule.isReceiveTenderDocumentPrice, // 是否收取标书费
        protProjectDocumentPriceList: this.dataRule.protProjectDocumentPriceList, // 标书费金额
        reviewMethod: this.dataRule.reviewMethod, // 评审办法
        supplierCategory: this.dataRule.supplierCategory.join(','), // 供应商分类
        supplierAttribute: this.dataRule.supplierAttribute.join(',') // 供应商属性
      }
      // 返回
      return data
    }
  }
}
</script>

<style lang="less" scoped>

  /deep/.show-word-limit-padding{
    .el-input__inner{
      padding-right: 70px;
    }
  }
  /deep/.select-popper{
    max-width: 100%;
  }
</style>
