packageRecommend.vue 11 KB
<template>
  <div class="package-form">
    <div class="content">
      <table
        cellspacing="0"
        cellpadding="0"
        class="table-box"
      >
        <tr>
          <th>序号</th>
          <th><span class="required">*</span>套餐类型</th>
          <th><span class="required">*</span>套餐名称</th>
          <th><span class="required">*</span>套餐内容</th>
          <th><span class="required">*</span>价格/元</th>
          <th><span class="required">*</span>套餐简介</th>
          <th><span class="required">*</span>话术介绍</th>
          <th>操作</th>
        </tr>
        <tr v-for="(pItem, pindex) in infoList" :key="pindex">
          <td>{{ pindex + 1 }}</td>
          <td>
            <el-select class="type-select" v-model="pItem.type" placeholder="请选择">
              <el-option
                v-for="item in packageTypeOption"
                :key="item.confId"
                :label="item.confName"
                :value="item.confId">
              </el-option>
            </el-select>
          </td>
          <td>
            <el-input v-model.trim="pItem.name" class="textarea-input" rows="5" type="textarea" maxlength="15" placeholder="请输入(15字以内)" />
          </td>
          <td>
            <el-input
              type="textarea"
              rows="5"
              class="textarea-input"
              v-model="pItem.content"
              maxlength="200"
              placeholder="请输入(200字以内)">
            </el-input>
          </td>
          <td class="input-td">
            <div>
              <span class="label">市场价</span>
              <el-input
                class="price-input"
                @input="priceInput($event, pindex, 'marketPrice')"
                v-model="pItem.marketPrice"
                placeholder="请输入"
              ></el-input>
            </div>
            <div>
              <span class="label">套餐价</span>
              <el-input
                class="price-input"
                @input="priceInput($event, pindex, 'packagePrice')"
                v-model="pItem.packagePrice"
                placeholder="请输入"
              ></el-input>
            </div>
            <div>
              <span class="label">优惠金额</span>
              <el-input
                disabled
                class="price-input"
                :value="calculateDiscount(pItem)"
              ></el-input>
            </div>
          </td>
          <td>
            <el-input
              type="textarea"
              class="textarea-input"
              rows="5"
              v-model="pItem.briefIntroduction"
              maxlength="20"
              placeholder="请输入(20字以内)"
            ></el-input>
          </td>
          <td>
            <el-input
              type="textarea"
              rows="5"
              class="textarea-input"
              v-model="pItem.scriptIntroduction"
              maxlength="200"
              placeholder="对驻店套餐话术介绍(200字以内)"
            ></el-input>
          </td>
          <td style="width: 90px;">
            <el-button
              type="text"
              @click="handleDel(pindex)"
            >
              删除
            </el-button>
            <el-button type="text" @click="handlePinToTop(pindex)" :disabled="pItem.topFlag == 'Y'">{{ pItem.topFlag == 'Y' ? '已置顶' : '置顶' }}</el-button>
          </td>
        </tr>
      </table>
      <div class="package-add">
        <el-button
          icon="el-icon-plus"
          type="text"
          v-if="infoList.length < 10"
          @click="handleAdd(infoList.length - 1)"
        >
          新增套餐
        </el-button>
      </div>
    </div>
    <div class="footer">
      <el-button type="primary" @click="handleSave">提交</el-button>
      <el-button type="primary" plain @click="handleReset">重置</el-button>
      <el-button class="width_100 cancel-btn" @click="handleCancel"
        >取消</el-button
      >
    </div>
  </div>
</template>
<script>
import { limitPrice } from '@/common/format'
import BigNumber from 'bignumber.js'
import { packageRecommendDetail, updatePackageRecommend } from '@api/contentManager'
import { deepClone } from "@/common/util"
export default{
  name: 'PackageRecommend',
  props: {
    pageQuery: {
      type: Object
    }
  },
  data() {
    return {
      packageTypeOption: [{
        confId: 1,
        confName: '必推'
      }, {
        confId: 2,
        confName: '可选'
      }],
      infoList: [],
      initObj: {
        type: '',
        name: '',
        content: '',
        marketPrice: '',
        packagePrice: '',
        briefIntroduction: '',
        scriptIntroduction: ''
      }
    }
  },
  mounted() {
    // 获取套餐推荐配置详情数据
    console.log(this.pageQuery)
    this.getPackageDetail(this.pageQuery?.merchantId)
  },
  methods: {
    getPackageDetail(merchantId) {
      packageRecommendDetail({ merchantId }).then(res => {
        if (res?.length) {
          this.infoList = res || []
        } else {
          this.initFormData()
        }
      })
    },
    initFormData() {
      this.infoList = [deepClone(this.initObj)]
    },
    priceInput(newValue, index, key) {
      newValue = newValue.replace(/[^\d.]/g, '')
      let oldValue = `${this.infoList[index][`old${key}`]}`
      this.$set(this.infoList[index], key, limitPrice(newValue, oldValue, 6, 2, 0, 100000))
      this.$set(this.infoList[index], `old${key}`, this.infoList[index][key])
    },
    // 计算优惠金额
    calculateDiscount(pItem) {
      const marketPrice = Number(pItem.marketPrice) || 0
      const packagePrice = Number(pItem.packagePrice) || 0
      return pItem.marketPrice && pItem.packagePrice ? new BigNumber(marketPrice).minus(packagePrice).toNumber() : ''
    },
    // 新增一行
    handleAdd(i) {
      if (i < 0) return this.infoList.push(deepClone(this.initObj))
      if (this.infoList?.length >= 10) return this.$message.error('最多支持新增10个套餐')
      const result = this.validateForm(i)
      if (result) this.infoList.push(result)
    },
    // 置顶
    handlePinToTop(index) {
      if (this.infoList[index]?.topFlag == 'Y') return
      const newList = this.infoList.map((item, idx) => ({
        ...item,
        topFlag: idx === index ? 'Y' : 'N'
      }))
      
      this.infoList = newList
    },
    validateForm(i) {
      const keys = Object.keys(this.infoList[i])
      let validateList = {
        type: "请选择套餐类型",
        name: "请输入套餐名称",
        content: "请输入套餐内容",
        marketPrice: "请输入市场价",
        packagePrice: "请输入套餐价",
        briefIntroduction: "请输入套餐简介",
        scriptIntroduction: "请输入话术介绍"
      }
      for (let k in validateList) {
        if (keys.includes(k) && !this.infoList[i][k]) {
          this.$message.error("请校验并输入套餐信息必填项")
          return false
        }
      }
      if (this.infoList[i].marketPrice == 0) {
        this.$message.error(`${this.infoList[i]?.name}市场价不能为0`)
        return false
      }
      if (this.infoList[i]?.packagePrice == 0) {
        this.$message.error(`${this.infoList[i]?.name}套餐价不能为0`)
        return false
      }
      if (this.calculateDiscount(this.infoList[i]) <= 0) {
        this.$message.error(`${this.infoList[i]?.name}优惠金额≤0,请核实后重新录入`)
        return false
      }
      return deepClone(this.initObj)
    },
    handleDel(i) {
      this.infoList.splice(i, 1)
    },
    // 提交
    handleSave() {
      let list = []
      this.infoList?.forEach((val, index) => {
        if (!this.validateForm(index)) throw new Error("请完善信息")
        val.discountPrice = new BigNumber(Number(val.marketPrice)).minus(Number(val.packagePrice)).toNumber()
        list.push({
          type: val.type,
          name: val.name,
          content: val.content,
          marketPrice: val.marketPrice,
          packagePrice: val.packagePrice,
          discountPrice: val.discountPrice,
          briefIntroduction: val.briefIntroduction,
          scriptIntroduction: val.scriptIntroduction,
          topFlag: val.topFlag == 'Y' ? 'Y' : 'N'
        })
      })
      const params = {
        merchantConfigId: this.pageQuery.id,
        list
      }
      // 请求保存接口
      updatePackageRecommend(params).then(res => {
        this.notice.okTips()
        this.handleCancel()
      })
    },
    // 重置 将内容置为默认门店的数据
    handleReset() {
      this.$confirm("是否对该门店重置配置?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.getPackageDetail()
          this.isUpStatus = true
          this.$message.success("重置成功")
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 取消按钮返回列表页
    handleCancel() {
      this.$router.push({
        name: "packageConfig",
        params: {
          tabPane: '1',
          tabLabel: 'UpgradeLightly'
        }
      })
    },
  }
}
</script>
<style lang="scss">
.package-form {
  .content {
    background: #FFFFFF;
    min-height: 64vh;
    padding: 20px 20px 0;
    .table-box {
      border: 1px solid #ebeef5;
      width: 100%;
      color: #666666;
      font-size: 14px;
      tr {
        line-height: 60px;
        &:first-child {
          line-height: 30px;
          background: #f5f7fa;
        }
        th,
        td {
          padding: 10px 20px;
          text-align: center;
          border-bottom: 1px solid #ebeef5;
          .el-textarea {
            vertical-align: middle;
            ::v-deep.el-textarea__inner {
              min-height: 80px !important;
            }
          }
          .required {
            color: #ff0000;
          }
          .el-icon-edit-outline {
            cursor: pointer;
            color: #185efe;
          }
          .flex {
            display: flex;
            justify-content: center;
            &.disabled {
              cursor: not-allowed;
              pointer-events: none;
            }
            ::v-deep.photo-activity {
              position: static;
            }
            ::v-deep.activity-img-box {
              display: block;
              margin-bottom: 6px;
            }
          }
        }
        .input-td {
          width: 180px;
          div{
            line-height: 45px;
          }
        }
      }
      .type-select {
        width: 120px;
      }
      .label {
        display: inline-block;
        width: 70px;
        margin-right: 4px;
        text-align: right;
      }
      .price-input {
        width: 105px;
        height: 45px;
      }
    }
  }
  .footer {
    position: fixed;
    bottom: 0;
    left: 50%;
    text-align: center;
    padding: 10px;
  }
  .package-add {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #e5e5e5;
    border-top: 0;
  }
}
</style>