<!--
  @description: 招标-招标公告-投标文件的递交
  @Modifier: 肖文龙
  @routerPath:/bidding/projectDrive/bulletin/tabs/tenderDocumentSubmit-add
  @date: 2020/1/2-10:03
-->
<template>
  <div>
    <div style="width: 90%;margin-left: 4%;">
      <el-form ref="form" :model="formData" :rules="rules" label-width="220px">
        <el-row>
          <el-col>
            <!--投标文件递交途径-->
            <el-form-item ref="bidDocumentSubmitChannel" :label="dict.bidDocumentSubmitChannelLabel" prop="bidDocumentSubmitChannel">
              <el-radio-group v-model="formData.bidDocumentSubmitChannel">
                <el-radio v-for="(item,index) in bidFileSubmitMethodList" :key="index" :label="item.typecode">
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col>
            <el-row>
              <!--接收答疑截止时间-->
              <el-col>
                <el-form-item ref="acceptAnsweringQuestionsEndTime" label="接收答疑截止时间" prop="acceptAnsweringQuestionsEndTime">
                  <el-date-picker
                    v-model="formData.acceptAnsweringQuestionsEndTime"
                    :picker-options="disabledPickerOptions"
                    :clearable="false"
                    :format="elValueFormat"
                    :value-format="elValueSaveFormat"
                    :default-time="defaultTimeDateTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    @change="acceptAnsweringQuestionsEndTimeChange('acceptAnsweringQuestionsEndTime')"
                  />
                </el-form-item>
              </el-col>
              <!--投标截止时间-->
              <el-col>
                <el-form-item ref="submitEndTime" :label="dict.submitEndTimeLabel" prop="submitEndTime">
                  <!--
                    tenderBase.tenderMethod 06询比、08谈判、02磋商不需要限制标书结束时间20天之前日期不可选择
                    (tenderBase.tenderMethod==='06'||tenderBase.tenderMethod==='08' || tenderBase.tenderMethod==='02')?disabledPickerOptions:submitEndTimeDisabled
                  -->
                  <el-date-picker
                    v-model="formData.submitEndTime"
                    :picker-options="disabledPickerOptions"
                    :value-format="elValueSaveFormat"
                    :default-time="defaultTimeDateTime"
                    :clearable="false"
                    :format="elValueFormat"
                    type="datetime"
                    placeholder="选择日期时间"
                    @change="acceptAnsweringQuestionsEndTimeChange('submitEndTime')"
                  />
                </el-form-item>
              </el-col>
              <!--投标有效期-->
              <el-col v-if="$store.getters.tenderBase.qualificationMethod == '00'">
                <el-form-item ref="bidValidityPeriod" label="申请有效期">
                  <money
                    :money-value="formData"
                    style="display: inline-block"
                    name="bidValidityPeriod"
                  />
                  <span> 天</span>
                </el-form-item>
              </el-col>
              <!-- <el-col>
                <el-form-item ref="bidValidityPeriod" :label="$store.getters.tenderBase.qualificationMethod == '00' ? '申请有效期' : '投标有效期'">
                  <money
                    :money-value="formData"
                    style="display: inline-block"
                    name="bidValidityPeriod"
                  />
                  <span> 天</span>
                </el-form-item>
              </el-col> -->

              <!--评标办法-->
              <!-- <el-col>
                <el-form-item ref="evaluationMethod" label="评标办法">
                  <gx-input
                    v-model="formData.evaluationMethod"
                    style="width:220px"
                    gx-maxlength="varchar200"
                    placeholder="评标办法"
                  />
                </el-form-item>
              </el-col> -->
            </el-row>
          </el-col>
          <el-col>
            <el-row>
              <!--
            投标文件递交方式
            bidDocumentSubmitChannel===00 电子招标
            -->
              <el-col v-if="formData.bidDocumentSubmitChannel==='00'">
                <el-form-item
                  ref="bidDocumentSubmitDetailAddress"
                  class="fl"
                  :label="dict.bidDocumentSubmitDetailAddressLabel"
                  prop="bidDocumentSubmitDetailAddress"
                >
                  <span>{{ bidDocumentSubmitDetailAddress }}</span>
                </el-form-item>
              </el-col>
              <!--
            投标文件递交地址
            bidDocumentSubmitChannel===01 线下招标
            -->
              <el-col>
                <el-form-item
                  v-if="formData.bidDocumentSubmitChannel==='01'"
                  ref="bidDocumentSubmitAddress"
                  :label="dict.bidDocumentSubmitAddressLabel"
                  prop="bidDocumentSubmitAddress"
                >
                  <!--地区组件-->
                  <cascader :region-text="formData.bidDocumentSubmitAddress" style="width:220px" @regionData="regionData" />
                  <el-input
                    v-model.trim="BTLbidDocumentSubmitDetailAddress"
                    maxlength="500"
                    placeholder="最多允许输入500个汉字(1000字符)"
                    style="width:344px"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- <el-col>
          <el-form-item ref="projectDuration" label="供货时间" prop="projectDuration">
            <el-date-picker
              v-model="formData.projectDuration"
              :picker-options="disabledPickerOptions"
              :default-time="defaultTimeDateTime"
              :value-format="elValueSaveFormat"
              :clearable="false"
              :format="elValueFormat"
              type="datetime"
              placeholder="选择供货时间"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item ref="technicalRequirements" label="简要技术要求/采购项目的性质" prop="technicalRequirements">
            <gx-input
              v-model.trim="formData.technicalRequirements"
              placeholder="请输入简要技术要求/采购项目的性质"
              gx-maxlength="varchar200"
              style="width: calc(100% - 160px)"
            />
          </el-form-item>
        </el-col>

        <el-col>
          <el-form-item ref="implementPolicy" label="需要落实的政府采购政" prop="implementPolicy">
            <gx-input
              v-model.trim="formData.implementPolicy"
              placeholder="需要落实的政府采购政"
              gx-maxlength="varchar200"
              style="width: calc(100% - 160px)"
            />
          </el-form-item>
        </el-col> -->
      </el-form>
    </div>
    <!--保存按钮-->
    <div class="but_footer">
      <div class="but_footer-h" />
      <div class="but_footer-but">
        <el-button @click="prevStep">
          上一步
        </el-button>
        <el-button :loading="confirmLoading" type="primary" @click="nextStep('form')">
          暂存，进入下一步
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
/* 接口*/
import { queryBidFileSubmitInfo, saveBidFileSubmitInfo } from '@/api/bidding/bidBulletin'
/* 公用方法*/
import { characters1_auto } from '@/assets/js/validate'// 校验输入内容
import { formatTimeSecond } from '@/assets/js/utils' // 保存时间初始化00秒

/* 混合开发方法-参数字段*/
import { tenderBulletinInitGroupMixin } from './../mixins/tenderBulletinInitGroupMixin'// 字典信息
import { tenderStageTimeRule } from './../mixins/tenderStageTimeRule'// 招标公告时间规则
import money from '@comp/common/money/money'// 金额组件
/* 组件*/
import cascader from '@comp/common/cascader'// 地区三级联动
export default {
  name: 'TenderDocumentSubmitAdd',
  components: {
    money,
    cascader// 地区三级联动
  },
  mixins: [tenderBulletinInitGroupMixin, tenderStageTimeRule],
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      tenderBase: this.$store.getters.tenderBase, // 项目信息
      tenderId: this.$route.query.id, // 项目Id
      bulletinId: this.$route.query.bulletinId, // 公告Id
      // 回填
      bidDocumentSubmitDetailAddress: '', // 电子招标投标文件递交方式=>字典带回的字段
      BTLbidDocumentSubmitDetailAddress: '', // 线下招标投标文件递交方式
      formData: {
        tenderId: '', // 项目Id
        acceptAnsweringQuestionsEndTime: '', // 接收答疑截止时间
        submitEndTime: '', // 投标截止时间
        bidDocumentSubmitChannel: '', // 投标文件递交途径 00:电子 01:线下
        bidDocumentSubmitAddress: '', // 投标文件递交地址名称
        bidDocumentSubmitAddressCode: '', // 投标文件递交地址编号
        bidDocumentSubmitDetailAddress: '', // 详细地址
        technicalRequirements: '', // 简要技术要求/采购项目的性质
        projectDuration: [], // 供货时间
        implementPolicy: '', // 需要落实的政府采购政策
        bidValidityPeriod: '', // 投标有效时间
        evaluationMethod: '综合评估法' // 评标办法
      },

      // 校验输入内容
      rules: {
        bidDocumentSubmitChannel: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 投标文件递交途径
        acceptAnsweringQuestionsEndTime: [{ required: true, message: '请选择', trigger: 'change' }], // 接收答疑截止时间
        submitEndTime: [{ required: true, message: '请选择', trigger: 'change' }], // 投标截止时间
        bidDocumentSubmitAddress: [{ required: true, message: '请选择', trigger: 'change' }], // 投标文件递交地址
        bidValidityPeriod: [{ required: true, message: '请选择', trigger: 'blur' }], // 投标有效期
        evaluationMethod: [{ required: true, message: '请选择', trigger: 'blur' }] // 评标办法
      }
    }
  },
  watch: {
    /* 设置开标时间和投标截止时间相同-Start*/
    // 开标时间
    'timeRuleInfo.openBidStartTime'(val) {
      //  是否两步开标 00:否 01:是
      if (this.timeRuleInfo.isTwoBidOpening === '00') {
        // 投标截止时间
        if (!this.formData.submitEndTime && val) this.formData.submitEndTime = val
      }
    },
    // 商务技术开标时间(两步开标)
    'timeRuleInfo.technicalOpenBidStartTime'(val) {
      // isTwoBidOpening 是否两步开标 00:否 01:是
      // twoBidOpeningMethod 两步开标方式(开标顺序) 00:先开商务技术后开经济 01:先开经济后开商务技术
      if (this.timeRuleInfo.isTwoBidOpening === '01' && this.timeRuleInfo.twoBidOpeningMethod === '00') {
        // 投标截止时间
        if (!this.formData.submitEndTime && val) this.formData.submitEndTime = val
      }
    },
    // 经济标开标时间(两步开标)
    'timeRuleInfo.economicOpenBidStartTime'(val) {
      // isTwoBidOpening 是否两步开标 00:否 01:是
      // twoBidOpeningMethod 两步开标方式(开标顺序) 00:先开商务技术后开经济 01:先开经济后开商务技术
      if (this.timeRuleInfo.isTwoBidOpening === '01' && this.timeRuleInfo.twoBidOpeningMethod === '01') {
        // 投标截止时间
        if (!this.formData.submitEndTime && val) this.formData.submitEndTime = val
      }
    }
    /* 设置开标时间和投标截止时间相同-End*/
  },
  created() {
    this.getBidDocumentSubmitChannelInitInfo()// 字典信息
    this.initData()// 加载数据
  },
  methods: {
    // 加载数据
    initData() {
      queryBidFileSubmitInfo(this.bulletinId).then((response) => {
        /* 判断投标截止时间是否有值 如果为空 将接口返回的投标截止时间清空在进行合并 */
        if (!response.data.submitEndTime) {
          delete response.data.submitEndTime // 删除接口返回的投标截止时间
        }
        this.formData = {
          ...this.formData,
          ...response.data
        }
        // 解决进入页面调用接口后端返回null 必填校验显示问题
        if (!this.formData.acceptAnsweringQuestionsEndTime) this.formData.acceptAnsweringQuestionsEndTime = ''
        // 投标文件递交途径 初始默认值
        if (!this.formData.bidDocumentSubmitChannel) {
          this.formData.bidDocumentSubmitChannel = '00'
        }
        // 投标文件递交地址
        if (this.formData.bidDocumentSubmitChannel === '01') { // 01 线下招标
          this.BTLbidDocumentSubmitDetailAddress = this.formData.bidDocumentSubmitDetailAddress
        }
      })
    },
    // 投标文件递交地址三级联动组件使用
    regionData(regionName, location) {
      this.formData.bidDocumentSubmitAddress = regionName
      this.formData.bidDocumentSubmitAddressCode = location.join(',')
    },
    /*
     * 接收答疑截止时间||投标截止时间-时间校验规则
     * return 布尔值 true 通过 false 不通过
     */
    acceptAnsweringQuestionsEndTimeChange(type) {
      const data = {
        acceptAnsweringQuestionsEndTime: this.formData.acceptAnsweringQuestionsEndTime, // 接收答疑截止时间
        submitEndTime: this.formData.submitEndTime, // 投标截止时间
        bidValidityPeriod: this.formData.bidValidityPeriod// 投标有效期
      }
      // 投标截止时间
      if (type === 'submitEndTime') {
        this.submitEndTimeRule(data)// 校验公告时间规则
      } else if (type === 'acceptAnsweringQuestionsEndTime') {
        // 接收答疑截止时间
        this.acceptAnsweringQuestionsEndTimeRule(data)// 校验公告时间规则
      } else if (type === 'next') {
        // 点击下一步时
        return this.submitEndTimeRule(data).then(() => { // 校验公告时间规则
          return this.acceptAnsweringQuestionsEndTimeRule(data)// 校验公告时间规则
        })
      }
    },
    // 保存组装数据
    assemblyData() {
      const data = {
        ...this.formData,
        tenderId: this.tenderId, // 项目Id
        bizId: this.bulletinId, // 公告id
        requestSource: '' // 来源 00:资审公告 01:招标公告 02:资格预审通知书 03:资审邀请函 04:后审邀请函
      }
      // 通过资审和后审判断来源
      if (this.tenderBase.stageType === '00') {
        data.requestSource = '00'// 资审公告
      } else if (this.tenderBase.stageType === '01') {
        data.requestSource = '01'// 招标公告
      }
      // 接收答疑截止时间
      if (data.acceptAnsweringQuestionsEndTime) {
        data.acceptAnsweringQuestionsEndTime = formatTimeSecond(data.acceptAnsweringQuestionsEndTime)
      }
      // 投标截止时间
      if (data.submitEndTime) {
        data.submitEndTime = formatTimeSecond(data.submitEndTime)
      }
      // 投标文件递交地址
      if (data.bidDocumentSubmitChannel === '00') { // 00 电子招标
        data.bidDocumentSubmitDetailAddress = this.bidDocumentSubmitDetailAddress// 投标文件递交方式
      } else if (data.bidDocumentSubmitChannel === '01') { // 01 线下招标
        data.bidDocumentSubmitDetailAddress = this.BTLbidDocumentSubmitDetailAddress
      }
      return data
    },
    // 保存数据接口 暂存，下一步
    saveData(type) {
      this.confirmLoading = true// btnLoding
      const data = this.assemblyData()// 保存组装数据
      saveBidFileSubmitInfo(data).then(response => {
        // route携带参数
        const pathData = {
          ...this.$route.query
        }
        if (type === 'draft') return false// 判断是否-点击步骤条切换-如果是则不执行next下一步
        this.$emit('stepPage', 'next', pathData)// 步骤条切换方法 next 下一步
        this.confirmLoading = false// btnLoding
      }).catch(() => {
        this.confirmLoading = false// btnLoding
      })
    },
    // 上一步
    prevStep() {
      // router携带参数
      const pathData = {
        ...this.$route.query
      }
      this.$emit('stepPage', 'prev', pathData)// 步骤条切换方法 prev 上一步
    },
    // 下一步
    nextStep(formName) {
      // 接收答疑截止时间||投标截止时间-时间校验规则
      // this.acceptAnsweringQuestionsEndTimeChange('next').then(() => {
      // 校验输入内容
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          this.saveData()
        } else { // 校验不通过
          this.$confirm('投标文件的递交未完善, 是否进入下一步?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            this.saveData()
          }).catch(() => {
          })
        }
      })
      // })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
