index.vue 9.27 KB
<template>
  <div class="table-wrap">
    <table>
      <tr class="th-wrap">
        <th rowspan="2">规格</th>
        <th rowspan="2">价格区间(万元)</th>
        <th rowspan="2">是否计算</th>
        <th rowspan="2">价格系数</th>
        <th colspan="2" v-for="(item, index) in tableData.salePeriodData" :key="index">{{ item.servicePeriod }}年版</th>
        <th rowspan="2" style="white-space: nowrap">操作</th>
      </tr>
      <tr>
        <template v-for="(item, index) in tableData.salePeriodData">
          <th :key="index">
            上架成本
            <!-- <el-tooltip class="item" effect="dark" content="提示待运营提供" placement="top">
                  <img class="tip" :src="require('@/assets/tip.png')" />
                </el-tooltip> -->
          </th>
          <th :key="index">
            嘀加成本
            <!-- <el-tooltip class="item" effect="dark" content="提示待运营提供" placement="top">
                  <img class="tip" :src="require('@/assets/tip.png')" />
                </el-tooltip> -->
          </th>
        </template>
      </tr>
      <tr>
        <td :rowspan="tableData.gradeConfigList.length + 2">{{ tableData.serviceGrade }}</td>
        <td>标准</td>
        <td>-</td>
        <td>-</td>
        <template v-for="(item, index) in tableData.salePeriodData">
          <td :key="index"><el-input :placeholder="placeholder" @input="changePrice($event, item, 'standardShelfCost')" :disabled="disabled" v-model="item.standardShelfCost"></el-input></td>
          <td :key="index"><el-input :placeholder="placeholder" @input="changePrice($event, item, 'standardSalesCost')" :disabled="disabled" v-model="item.standardSalesCost"></el-input></td>
        </template>
        <td></td>
      </tr>
      <tr v-for="(item, index) in tableData.gradeConfigList" :key="index">
        <td class="price">
          <el-input :placeholder="placeholder" @input="changePrice($event, item, 'minPrice')" :disabled="disabled" v-model="item.minPrice"></el-input>
          <span class="sign">-</span>
          <el-input :placeholder="placeholder" @input="changePrice($event, item, 'maxPrice')" :disabled="disabled" v-model="item.maxPrice"></el-input>
        </td>
        <td><el-switch v-model="item.computeFlag" :active-value="0" @change="changeComputeFlag($event, item, index)" :inactive-value="1" :disabled="disabled"></el-switch></td>
        <td>
          <span v-if="item.computeFlag == 1">-</span>
          <el-input v-else :placeholder="placeholder" @input="changePrice($event, item, 'priceCoefficient', index)" :disabled="disabled" v-model="item.priceCoefficient" @blur="formatPrice($event, item, 'priceCoefficient', 100)"></el-input>
        </td>
        <template v-for="(ele, eIdx) in tableData.salePeriodData">
          <template v-if="item.computeFlag == 0">
            <td :key="eIdx">{{ calcPrice(ele.standardShelfCost, item.priceCoefficient) }}</td>
            <td :key="eIdx">{{ calcPrice(ele.standardSalesCost, item.priceCoefficient) }}</td>
          </template>
          <template v-if="item.computeFlag == 1">
            <td :key="eIdx"><el-input v-model="item.childSalePeriodData[eIdx].shelfCost" :placeholder="placeholder" @input="changePrice($event, item.childSalePeriodData[eIdx], 'shelfCost')" :disabled="disabled"></el-input></td>
            <td :key="eIdx"><el-input v-model="item.childSalePeriodData[eIdx].salesCost" :placeholder="placeholder" @input="changePrice($event, item.childSalePeriodData[eIdx], 'salesCost')" :disabled="disabled"></el-input></td>
          </template>
        </template>
        <td class="del-btn"><span v-if="index > 2 && !disabled" class="btn" @click="delSection(index)">删除</span></td>
      </tr>
      <tr>
        <td class="add-btn" :colspan="tableData.salePeriodData.length * 2 + 4"><span v-if="!disabled" class="btn" @click="addSection">新增价格区间</span></td>
      </tr>
      <tr class="desc-wrap">
        <td class="desc-td" :colspan="tableData.salePeriodData.length * 2 + 5">
          <div class="desc">
            <label for="">服务条款概述:</label>
            <Editor :disabled="disabled" :editorOption="overviewOption" v-model="tableData.serviceOverview"></Editor>
          </div>
        </td>
      </tr>
      <tr class="desc-wrap">
        <td class="desc-td" :colspan="tableData.salePeriodData.length * 2 + 5">
          <div class="desc">
            <label for="">规格详细描述:</label>
            <Editor :disabled="disabled" :editorOption="DetailOption" v-model="tableData.serviceDetail"></Editor>
          </div>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
import Editor from '@/components/Editor'
export default {
  name: 'ServicesTable',
  components: {
    Editor
  },
  props: {
    name: {
      type: String,
      default: ''
    },
    tableData: {
      type: String,
      default: () => {
        return {}
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    url: {
      type: String,
      default: ''
    },
    key: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      placeholder: '请输入',
      overviewOption: {
        placeholder: '请输入服务条款概述',
        modules: {
          toolbar: false,
          clipboard: {
            // 粘贴版,处理粘贴时候的自带样式
            matchers: [[Node.ELEMENT_NODE, this.HandleCustomMatcher]]
          }
        }
      },
      DetailOption: {
        placeholder: '请输入规格详细描述',
        modules: {
          toolbar: false,
          clipboard: {
            // 粘贴版,处理粘贴时候的自带样式
            matchers: [[Node.ELEMENT_NODE, this.HandleCustomMatcher]]
          }
        }
      }
    }
  },
  methods: {
    changePrice(value, item, key) {
      const decimal = ['standardShelfCost', 'standardSalesCost', 'minPrice', 'maxPrice'].includes(key) ? 2 : 16
      item[key] = value
        ? value
            .replace(/^0\d/g, '0')
            .replace(/[^\d.]/g, '')
            .replace(/^\./g, '')
            .replace(/^\d{6}\d+(\.\d+)?/g, value.substr(0, 6))
            .replace(/\.{2,}/g, '')
            .replace('.', '$#$')
            .replace(/\./g, '')
            .replace('$#$', '.')
            .replace(new RegExp(`^(\\-)*(\\d+)\\.(\\d{${decimal}}).*`, 'g'), '$1$2.$3')
        : ''
    },
    changeComputeFlag(e, item) {
      if (e == 1) {
        item?.childSalePeriodData?.forEach(ele => {
          ele.shelfCost = ''
          ele.salesCost = ''
        })
      }
    },
    formatPrice(e, item, key, maxNum) {
      let num = e.target.value
      if (num > maxNum) {
        item[key] = maxNum
      }
    },
    calcPrice(price1, price2) {
      price1 = price1 || 0
      price2 = price2 || 0
      return Number((price1 * price2)?.toFixed(2))
    },
    HandleCustomMatcher(node, Delta) {
      // 文字、图片等,从别处复制而来,清除自带样式,转为纯文本
      let ops = []
      Delta.ops.forEach(op => {
        if (op.insert && typeof op.insert === 'string') {
          ops.push({
            insert: op.insert
          })
        }
      })
      Delta.ops = ops
      return Delta
    },
    addSection() {
      // 获取最后一个的最大价格赋值到新增的最小价格
      const minPrice = this.tableData.gradeConfigList[this.tableData.gradeConfigList.length - 1].maxPrice
      const arr = this.tableData.salePeriodData.map(item => {
        return { servicePeriod: item.servicePeriod, standardShelfCost: '', standardSalesCost: '', shelfCost: '', salesCost: '' }
      })
      let sectionItem = {
        servicePeriod: '',
        maxPrice: '',
        minPrice,
        computeFlag: 0,
        priceCoefficient: '',
        standardShelfCost: '',
        standardSalesCost: '',
        childSalePeriodData: arr
      }
      this.tableData.gradeConfigList.push(sectionItem)
    },
    delSection(idx) {
      const minPrice = this.tableData.gradeConfigList[idx - 1].maxPrice
      this.tableData.gradeConfigList.splice(idx, 1)
      if (this.tableData.gradeConfigList.length > idx) {
        this.$set(this.tableData.gradeConfigList[idx], 'minPrice', minPrice)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.table-wrap {
  .th-wrap {
    white-space: nowrap;
  }
  .del-btn {
    color: #5f5efb;
  }
  .btn {
    cursor: pointer;
  }
  .add-btn {
    color: #5f5efb;
    text-align: left;
  }
  .tip {
    width: 14px;
    vertical-align: middle;
    margin-bottom: 2px;
    cursor: pointer;
  }
  /deep/ .el-input__inner {
    padding: 0 2px;
    text-align: center;
  }
  th,
  td {
    border: 1px solid #e5e5e5;
    padding: 0 16px;
    vertical-align: middle;
    text-align: center;
  }
  th {
    background: #f4f6fc;
    font-weight: 600;
    font-size: 14px;
    color: #333333;
  }
  td {
    font-weight: 400;
    height: 56px;
    font-size: 14px;
    color: #666666;
  }
  .price {
    width: 136px;
    .sign {
      margin: 0 6px;
    }
    .el-input {
      width: 50px;
    }
  }
  .desc-wrap {
    height: 88px;
    .desc-td {
      padding: 12px 16px;
    }
    .desc {
      display: flex;
      height: 100%;
      label {
        white-space: nowrap;
      }
      /deep/ .editor {
        width: 100%;
        text-align: left;
        border-radius: 4px;
        .w-e-text-container {
          border-radius: 4px;
        }
      }
    }
  }
}
</style>