reservationDialog.vue 10.2 KB
<template>
  <div>
    <GLDialog
      bigTitle="预约时间"
      :visible.sync="dialogShow"
      class="detail-content-warn"
      width="620px"
      mode="big"
      :close-on-click-modal="false"
      :before-close="handleClose"
      @onOk="handleAddConfirm"
      @onCancel="dialogShow = false"
    >
      <div v-loading="loading">
        <div class="form_div">
          <div class="tipsDiv">
            <div class="tipsText">
              {{ paramsObj.customer.customerName + ' | ' + paramsObj.customer.customerPhone }}
            </div>
          </div>
          <el-form
            label-width="100px"
            :model="infoForm"
            ref="editRef"
            :rules="formRules"
          >
            <el-form-item label="流量有效性:">
              <el-form-item>
                <el-select
                  v-model="infoForm.trafficEffectiveness"
                  placeholder="请选择"
                  class="select-width"
                  :disabled="trafficEffectivenessDisabled"
                >
                  <el-option
                    v-for="(item, key) in trafficEffectivenessOpt"
                    :key="key"
                    :label="item.confValue"
                    :value="item.confId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form-item>
            <el-form-item
              label="预约时间:"
              required
            >
              <el-col :span="10">
                <el-form-item prop="dateTime">
                  <el-date-picker
                    v-model="infoForm.dateTime"
                    :clearable="false"
                    type="date"
                    class="default-width"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    @change="inviteStatMerchantList(true)"
                    :picker-options="datePickerOptions"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="1"></el-col>
              <el-col :span="13">
                <el-form-item prop="times">
                  <el-select
                    v-model="infoForm.times"
                    filterable
                    placeholder="请选择"
                    class="default-width"
                  >
                    <el-option
                      v-for="(item, key) in timesOpt"
                      :key="key"
                      :label="item.confValue"
                      :value="item.confId"
                      :disabled="item.disabled"
                    ></el-option>
                  </el-select>
                  <el-button
                    type="text"
                    size="small"
                    class="timesSle"
                    v-if="paramsObj.inviteDate"
                    @click="cancelInvite"
                  >取消预约</el-button>
                </el-form-item>
              </el-col>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </GLDialog>
  </div>
</template>

<script>
import * as dayjs from 'dayjs'
export default {
  name: 'ReservationDialog',
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    paramsObj: {
      type: Object,
      default: null
    }
  },
  computed: {
    dialogShow: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('dialogClose', { flag: val })
      }
    },
    trafficEffectivenessDisabled () {
      let { orderStatus } = this.paramsObj
      //已支付4、已施工、已失效11、已撤销9、已评价6、已关闭10、战败2 不支持编辑
      return [4, 11, 9, 6, 10, 2].includes(orderStatus)
    }
  },
  mounted () {
    this.loading = false
    console.log('this.paramsObj', this.paramsObj)
    this.inviteDatePush()
  },
  data () {
    // var validateDateTime = {
    //   validator: (rule, value, callback) => {
    //     if (!value) {
    //       return callback(new Error(`请选择日期`))
    //     }

    //     const times = this.infoForm.times
    //     if (!times) {
    //       return callback(new Error(`请选择时间段`))
    //     }

    //     callback() // 验证通过
    //   },
    //   trigger: 'change'
    // }
    return {
      infoForm: {
        dateTime: '',
        times: '',
        trafficEffectiveness: ''
      },
      editChildRef: 'editChildRef',
      formRules: {
        // dateTime: [validateDateTime]
        dateTime: [{ required: true, message: '请选择日期', trigger: 'change' }],
        times: [{ required: true, message: '请选择时间段', trigger: 'change' }]
      },
      datePickerOptions: {
        disabledDate: time => {
          const today = new Date()
          // 设置时间到当天的开始 (00:00:00.000),避免时分秒影响比较
          today.setHours(0, 0, 0, 0)

          // 计算一个月后的日期 (今天 + 30天)
          const oneMonthLater = new Date(today)
          oneMonthLater.setDate(today.getDate() + 30) // 粗略按30天算一个月
          // 或者更精确地处理月份 (考虑不同月份天数差异)
          // oneMonthLater.setMonth(today.getMonth() + 1);

          // 禁用规则:
          // 1. 禁用早于今天(包含今天00:00:00之前)的日期
          // 2. 禁用晚于一个月后(包含一个月后23:59:59之后)的日期
          return time.getTime() < today.getTime() || time.getTime() > oneMonthLater.getTime()
        }
      },
      loading: true,
      baseData: {
        technicianList: []
      },
      timesOpt: [
        { confId: '09:00', confValue: '09:00', disabled: false },
        { confId: '10:30', confValue: '10:30', disabled: false },
        { confId: '13:00', confValue: '13:00', disabled: false },
        { confId: '14:30', confValue: '14:30', disabled: false },
        { confId: '16:00', confValue: '16:00', disabled: false }
      ],
      trafficEffectivenessOpt: [
        { confId: 1, confValue: "有效客户" },
        { confId: 2, confValue: "无效客户" },
      ],
    }
  },
  methods: {
    inviteDatePush () {
      let { trafficEffectiveness, inviteDate } = this.paramsObj
      if (trafficEffectiveness) this.infoForm.trafficEffectiveness = trafficEffectiveness
      if (inviteDate) {
        const dateTimeStr = dayjs(inviteDate).format('YYYY-MM-DD HH:mm:ss')
        // 截取年月日(空格前)
        this.infoForm.dateTime = dateTimeStr.split(' ')[0]

        // 截取时分(去掉秒)
        this.infoForm.times = dateTimeStr.split(' ')[1].substring(0, 5) // "09:00"
        this.inviteStatMerchantList(false)
      }
    },

    inviteStatMerchantList (flag) {
      if (flag) {
        this.infoForm.times = ''
      }
      var params = {}
      params.merchantId = this.paramsObj.yqMerchantId
      params.startDate = this.infoForm.dateTime
      params.endDate = this.infoForm.dateTime
      console.log('params', params)
      this.$apis.invitation.inviteStatMerchantList(params).then(res => {
        const saturdayFlag = res[0].saturdayFlag
        const sundayFlag = res[0].sundayFlag
        const timeStats = res[0].timeStats
        const day = new Date(this.infoForm.dateTime).getDay()

        if (saturdayFlag === 'N' && day === 6) {
          this.$message.warning('工位设置不允许周六施工,请选择其他时间')
          this.infoForm.dateTime = ''
          return
        } else if (sundayFlag === 'N' && day === 0) {
          this.$message.warning('工位设置不允许周日施工,请选择其他时间')
          this.infoForm.dateTime = ''
          return
        }
        timeStats.forEach(item => {
          this.timesOpt.forEach(optItem => {
            if (item.inviteTime === optItem.confValue) {
              if (item.inviteNum < item.maxNum) {
                optItem.disabled = false
              } else {
                optItem.disabled = true
              }
            }
          })
        })
        console.log('this.timesOpt', this.timesOpt)
      })
    },

    handleAddConfirm () {
      this.$refs.editRef.validate(valid => {
        if (valid) {
          var params = this.$common.utils.deepClone(this.infoForm)
          params.id = this.paramsObj.id
          params.inviteType = this.paramsObj.customerCoupon.couponType
          params.inviteDateStr = params.dateTime + ' ' + params.times + ':00'
          delete params.dateTime
          delete params.times
          const targetTime = new Date(params.inviteDateStr).getTime() // 转为时间戳
          const now = new Date().getTime() // 当前时间戳
          if (targetTime < now) {
            this.$message.warning('预约时间不能选择已过去的时间')
            return
          }
          this.$apis.invitation.dateUpdate(params).then(res => {
            this.$notify({
              type: 'success',
              title: '操作成功'
            })
            this.dialogShow = true
          })
        }
      })
    },

    cancelInvite () {
      this.$confirm('请确认是否要执行当前操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      })
        .then(() => {
          var params = {}
          params.id = this.paramsObj.id
          this.$apis.invitation.cancelInvite(params).then(res => {
            this.$notify({
              type: 'success',
              title: '操作成功'
            })
            this.dialogShow = true
          })
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.detail-content-warn {
  .div-title {
    width: 1040px;
    height: 30px;
    border-bottom: 1px solid #e5e5e5;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    margin: 10px 0 20px 0;
  }

  .el-icon-circle-close {
    color: #ffffff;
  }

  .form_div {
    color: #333333;

    .tipsDiv {
      height: 28px;
      margin-bottom: 20px;

      .tipsText {
        padding-left: 20px;
        font-size: 14px;
      }
    }
  }

  .default-width {
    width: 170px;
  }

  .timesSle {
    padding-left: 10px;
  }
  .select-width {
    width: calc(100% - 110px);
  }
}
</style>