<!--
  @description: 招标-邀请函-招标文件的获取
  @Modifier: 肖文龙/haoFx
  @routerPath:/bidding/projectDrive/invitation/tabs/tenderDocumentPrice-add
  @date: 2019/12/31-18:32
-->
<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="tenderDocumentReceiveTime"
              :label="dict.tenderDocumentReceiveTimeLabel"
              prop="tenderDocumentReceiveTime"
            >
              <el-date-picker
                v-model="formData.tenderDocumentReceiveTime[0]"
                :clearable="false"
                :picker-options="disabledPickerOptions"
                :format="elValueFormat"
                :default-time="defaultTimeDateTime"
                :value-format="elValueSaveFormat"
                type="datetime"
                placeholder="开始日期"
                @change="documentReceiveTimeChange('start')"
              />
              至
              <el-date-picker
                v-model="formData.tenderDocumentReceiveTime[1]"
                :clearable="false"
                :picker-options="disabledPickerOptions"
                :format="elValueFormat"
                :default-time="defaultTimeDateTime"
                :value-format="elValueSaveFormat"
                type="datetime"
                placeholder="结束日期"
                @change="documentReceiveTimeChange('end')"
              />
              <span style="padding-left: 10px;">共
                <span v-if="tenderDocumentReceiveDays===''" style="padding-left: 16px" />
                <span style="padding: 0 4px">{{ tenderDocumentReceiveDays }}</span>天
              </span>
            </el-form-item>
            <!--标书获取方式-->
            <el-form-item ref="tenderDocumentReceiveMethod" :label="dict.tenderDocumentReceiveMethodLabel" prop="tenderDocumentReceiveMethod">
              <el-radio-group v-model="formData.tenderDocumentReceiveMethod">
                <el-radio v-for="(item,index) in receiveMethodList" :key="index" :label="item.typecode">
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col>
            <!--
            标书购买详细地址
            tenderDocumentReceiveMethod===01 线下招标
            线下转账需要屏蔽（张慢慢修改）
            -->
            <el-form-item
              v-if="false"
              ref="tenderDocumentReceiveAddress"
              :label="dict.tenderDocumentReceiveAddressLabel"
              prop="tenderDocumentReceiveAddress"
            >
              <!--地区组件-->
              <cascader :region-text="formData.tenderDocumentReceiveAddress" style="width:220px" @regionData="regionData" />
              <el-input
                v-model.trim="formData.tenderDocumentReceiveDetailAddress"
                maxlength="500"
                placeholder="最多允许输入500个汉字(1000字符)"
                style="width:344px"
              />
            </el-form-item>
          </el-col>
          <el-col>
            <!--标书金额设置-->
            <el-form-item ref="tenderDocumentSaleMethod" :label="dict.tenderDocumentSaleMethodLabel" prop="tenderDocumentSaleMethod">
              <el-radio-group v-model="formData.tenderDocumentSaleMethod" :disabled="formData.tenderDocumentSaleMethodChange==='00'">
                <el-radio v-for="(item,index) in documentMethodList" :key="index" :label="item.typecode">
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
              <el-button
                size="mini"
                style="color: red;margin-left: 16px"
                type="text"
              >
                注：金额为0或不填即为免费
              </el-button>
            </el-form-item>
            <!--
            售卖金额
            ·-->
            <template>
              <!--按标段售卖 tenderDocumentSaleMethod===00-->
              <el-form-item v-if="formData.tenderDocumentSaleMethod==='00'" class="form_item_table">
                <div>
                  <el-table :data="formData.tenderDocumentPriceInfoDto">
                    <el-table-column type="index" label="序号" width="60" align="center" />
                    <el-table-column header-align="center" width="200px" label="标段编号" prop="packNo" align="center" />
                    <el-table-column header-align="center" label="标段名称" prop="packName" />
                    <el-table-column header-align="center" width="220px" prop="documentPrice" :label="dict.documentPriceTitle">
                      <template slot-scope="scope">
                        <money :money-value="scope.row" name="documentPrice" />
                      </template>
                    </el-table-column>
                  </el-table>
                </div>

                <el-button
                  size="mini"
                  class="fr"
                  type="text"
                  @click="msetKeyMoney"
                >
                  批量设置金额
                </el-button>
              </el-form-item>
              <!--按项目售卖 tenderDocumentSaleMethod===01-->
              <el-form-item v-if="formData.tenderDocumentSaleMethod==='01'">
                <!--
                  tenderDocumentSaleMethodChange===00 不可编辑
                -->
                <money :disabled="formData.tenderDocumentSaleMethodChange==='00'" :unit-stust="true" :money-value="formData" name="documentPrice" />
              </el-form-item>
            </template>
          </el-col>
        </el-row>
      </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>
/* api*/
import { queryTenderDocumentPriceInfo, saveTenderDocumentPriceInfo } from '@/api/bidding/bidInvitation'
/* 公用方法*/
import { moneyRegular, characters1_auto } from '@/assets/js/validate'// 校验输入内容
import { formatTimeSecond } from '@/assets/js/utils' // 保存时间初始化00秒
/* 混合开发方法-参数字段*/
import { tenderInvitationInitGroupMixin } from './../mixins/tenderInvitationInitGroupMixin'// 字典信息
import { tenderStageTimeRule } from '@/views/bidding/bulletin/mixins/tenderStageTimeRule.js'// 招标公告-时间规则
/* 组件*/
import cascader from '@comp/common/cascader'// 地区三级联动
import money from '@comp/common/money/money'// 金额
export default {
  name: 'TenderDocumentPriceAdd',
  components: {
    cascader, // 地区三级联动
    money// 金额
  },
  mixins: [tenderInvitationInitGroupMixin, tenderStageTimeRule],
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      tenderBase: this.$store.getters.tenderBase, // 项目信息
      tenderId: this.$route.query.id, // 项目Id
      stageId: this.$store.getters.groupId.stageId, // 阶段Id
      invitationId: this.$route.query.invitationId, // 邀请函Id
      // 回填
      tenderDocumentReceiveDays: '', // 标书获取开始时间-标书获取截止时间时间差 天数
      formData: {
        invitationType: '', // 邀请函类型
        requestSource: '', // 来源 00:资审公告 01:招标公告 02:资格预审通知书 03:邀请函 04:后审邀请函
        tenderDocumentReceiveTime: [], // 标书获取时间 =》前端展示，不需要传给后端
        tenderDocumentReceiveStartTime: '', // 标书获取开始时间
        tenderDocumentReceiveEndTime: '', // 标书获取截止时间
        tenderDocumentReceiveMethod: '00', // 标书获取方式 00 电子招标 01 线下招标
        tenderDocumentSaleMethodChange: '00', // 标书售卖方式 是否可以变更 01:可以变更 00:不可以变更
        tenderDocumentSaleMethod: '', // 标书金额设置 00 按标段售卖 01 按项目售卖
        documentPrice: '', // 按項目售卖金额-标书费
        tenderDocumentReceiveAddress: '', // 标书购买地址名称
        tenderDocumentReceiveAddressCode: '', // 标书购买地址编号
        tenderDocumentReceiveDetailAddress: '', // 标书购买详细地址
        tenderDocumentPriceInfoDto: []// 标书费集合
      },
      // 校验输入内容
      rules: {
        tenderDocumentReceiveTime: [{ required: true, message: '请选择', trigger: 'change' }], // 标书获取时间
        buyersName: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 标书获取方式
        tenderDocumentReceiveAddress: [{ required: true, message: '请选择', trigger: 'change' }], // 标书购买详细地址
        tenderDocumentSaleMethod: [{ required: true, validator: characters1_auto, trigger: 'blur' }] // 标书金额设置
      }
    }
  },
  watch: {
    // 标书获取时间
    'formData.tenderDocumentReceiveTime'(val) {
      if (val[0] && val[1]) {
        this.tenderDocumentReceiveDays = this.momentDiff('days', val[0], val[1])// 时间差
      } else {
        this.tenderDocumentReceiveDays = ''
      }
    }
  },
  created() {
    this.getTenderDocumentInitInfo()// 招标文件获取-获取标书费相关字典信息
    this.initData() // 加载数据
  },
  methods: {
    // 加载数据
    initData() {
      if (!this.invitationId) return
      const data = {
        invitationType: this.tenderBase.invitationType, // 邀请函类型
        tenderId: this.tenderId, // 项目Id
        bizId: this.invitationId, // 邀请函Id
        stageId: this.stageId, // 阶段id
        requestSource: '' // 来源 00:资审公告 01:招标公告 02:资格预审通知书 03:资审邀请函 04:后审邀请函
      }
      // 通过资审和后审判断来源
      if (this.tenderBase.stageType === '00') {
        data.requestSource = '03'// 资审邀请函
      } else if (this.tenderBase.stageType === '01') {
        data.requestSource = '04'// 后审邀请函
      }
      this.formData.invitationType = data.invitationType // 邀请函类型
      this.formData.requestSource = data.requestSource // 来源
      // 接口
      queryTenderDocumentPriceInfo(data).then((response) => {
        this.formData = Object.assign(this.formData, response.data)
        this.formData.tenderDocumentPriceInfoDto = [...this.formData.tenderDocumentPriceInfoVo]// 物料
        delete this.formData.tenderDocumentPriceInfoVo
        // 标书获取时间
        this.formData.tenderDocumentReceiveTime = [
          this.formData.tenderDocumentReceiveStartTime, // 开始时间
          this.formData.tenderDocumentReceiveEndTime // 结束时间
        ]
        if (!this.formData.tenderDocumentReceiveMethod) {
          // 邀请函管理-规则管理，默认线下转账
          this.formData.tenderDocumentReceiveMethod = '01'// 标书获取方式 00 电子招标 01 线下招标
        }
        if (!this.formData.tenderDocumentSaleMethod) {
          // 为空时设置默认
          this.formData.tenderDocumentSaleMethod = '00'// 标书金额设置 00 按标段售卖 01 按项目售卖
        }
      })
    },
    // 标书购买详细地址三级联动组件使用
    regionData(regionName, location) {
      this.formData.tenderDocumentReceiveAddress = regionName
      this.formData.tenderDocumentReceiveAddressCode = location.join(',')
    },
    /*
      * 标书获取时间
      * type start 开始时间 end结束时间
      * return 布尔值 true 通过 false 不通过
     */
    documentReceiveTimeChange(type) {
      // 标书获取开始时间
      this.formData.tenderDocumentReceiveStartTime = this.formData.tenderDocumentReceiveTime[0] && this.$Moment(this.formData.tenderDocumentReceiveTime[0]).format(this.momentSaveFormat)
      // 标书获取截止时间
      this.formData.tenderDocumentReceiveEndTime = this.formData.tenderDocumentReceiveTime[1] && this.$Moment(this.formData.tenderDocumentReceiveTime[1]).format(this.momentSaveFormat)
      const data = {
        tenderDocumentReceiveStartTime: this.formData.tenderDocumentReceiveStartTime, // 标书获取开始时间
        tenderDocumentReceiveEndTime: this.formData.tenderDocumentReceiveEndTime// 标书获取截止时间
      }
      let isTimeRule = true
      if (type === 'start') {
        // 开始时间触发
        isTimeRule = this.tenderDocumentReceiveStartTimeRule(data)// 标书获取开始时间
      } else if (type === 'end') {
        // 截止时间触发
        isTimeRule = this.tenderDocumentReceiveEndTimeRule(data)// 标书获取结束时间
      } else {
        // 点击下一步时触发
        // 保存时触发
        if (!this.formData.tenderDocumentReceiveStartTime) {
          isTimeRule = this.tenderDocumentReceiveStartTimeRule(data)// 标书获取开始时间
          return false
        }
        if (isTimeRule) isTimeRule = this.tenderDocumentReceiveEndTimeRule(data)// 标书获取结束时间
      }
      return isTimeRule
    },
    // 分标段时-批量设置金额
    msetKeyMoney() {
      this.$prompt('批量设置金额（元）', '批量设置标书费', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        inputPattern: moneyRegular,
        inputErrorMessage: '请输入正确的金额'
      }).then(({ value }) => {
        // value 标书金额
        this.formData.tenderDocumentPriceInfoDto.map(item => {
          item.documentPrice = value
        })
      }).catch(() => {
      })
    },
    // 保存组装数据
    assemblyData() {
      const data = {
        ...this.formData,
        tenderId: this.tenderId, // 项目Id
        bizId: this.invitationId // 邀请函Id
      }
      // 标书获取开始时间
      if (data.tenderDocumentReceiveStartTime) {
        data.tenderDocumentReceiveStartTime = formatTimeSecond(data.tenderDocumentReceiveStartTime)
      }
      // 标书获取截止时间
      if (data.tenderDocumentReceiveEndTime) {
        data.tenderDocumentReceiveEndTime = formatTimeSecond(data.tenderDocumentReceiveEndTime)
      }
      // 删除不需要的参数
      delete data.tenderDocumentReceiveTime // 标书获取截止时间
      delete data.tenderDocumentSaleMethodChange// 标书售卖方式
      delete data.tenderDocumentPriceInfoVo// 公告已制作标段包
      return data
    },
    // 保存数据接口 暂存，下一步
    saveData(type) {
      this.confirmLoading = true// btnLoding
      const data = this.assemblyData()// 保存组装数据
      saveTenderDocumentPriceInfo(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) {
      const isVali = this.documentReceiveTimeChange()// 标书获取时间规则校验
      if (!isVali) return
      let isPrice = false //  false 未设置金额 true 已设置金额
      if (this.formData.tenderDocumentSaleMethod === '00') { // 00 按标段售卖
        this.formData.tenderDocumentPriceInfoDto.map(item => {
          if (Number(item.documentPrice)) {
            isPrice = true
          }
        })
      } else if (this.formData.tenderDocumentSaleMethod === '01') { // 01 按项目售卖
        if (Number(this.formData.documentPrice)) {
          isPrice = true
        }
      }

      const { tenderDocumentSaleMethod, tenderDocumentSaleMethodChange } = this.formData
      // true 已设置金额 或者 01 为按项目售卖时 && 00不可编辑时
      // true 已设置金额 或者 01 为按项目售卖时 && 00不可编辑时
      if (isPrice || tenderDocumentSaleMethod === '01' && tenderDocumentSaleMethodChange === '00') { // 已设置金额
        this.valiDate(formName)// 校验输入框必填项
      } else { // 未设置金额
        this.valiPrice()// 金额未填写提示
      }
    },
    // 金额未填写提示
    valiPrice() {
      this.$confirm('确认标书费免费吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.saveData()
      }).catch(() => {
      })
    },
    // 校验输入框必填项
    valiDate(formName) {
      // 校验输入内容
      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>

