certificate.vue 7.53 KB
<template>
  <div class="certificate-wrap" v-loading="loading">
    <div class="gl-form-page">
      <el-form ref="configForm" label-width="154px" :model="addForm" :rules="addRules">
        <div class="config-form-item el-form-item">
          <label class="is-require">选择券:</label>
          <el-button size="small" icon="el-icon-plus" @click="handleAdd">选择券</el-button>
        </div>
      </el-form>
    </div>
    <template v-if="tableData.length > 0">
      <div class="merchant-wrap">
        <el-table :data="tableData" border style="width: 100%" :height="tableData.length > 15 ? 600 : auto">
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="couponName" label="券名称" width="200"></el-table-column>
          <el-table-column prop="discountType" label="券类型" width="100">
            <template slot-scope="scope">
              <span>{{ numToCouponText[scope.row.discountType] }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="couponAnticipatedCost" label="预估成本/元"></el-table-column>
          <el-table-column prop="effectiveType" label="有效期" width="100">
            <template slot-scope="scope">
              <span>{{ toLabelText(scope.row) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="residueAmount" label="剩余库存" width="100"></el-table-column>
        </el-table>
      </div>
    </template>
    <select-gift-dialog v-if="isSelectGiftVisible" :isSelectGiftVisible="isSelectGiftVisible" :selectedGiftList="selectedGiftList" @handleGiftConfirm="handleGiftConfirm" @handleGiftClose="handleGiftClose"></select-gift-dialog>
  </div>
</template>
<script>
import url from '@/assets/empty.png'
import selectGiftDialog from './selectGiftDialog.vue'
export default {
  data() {
    return {
      url: url,
      isSelectGiftVisible: false,
      selectedGiftList: [],
      loading: false,
      searchForm: {
        subMerchantId: '',
        subMerchantName: '',
        select: '1',
        selectInput: ''
      },
      openSubscribeList: [],
      tableData: [],
      selectedList: [],
      orderShow: ['service', 'cash', 'discount', 'exchange'],
      numToCoupon: {
        2: 'discount',
        3: 'cash',
        4: 'exchange',
        7: 'service'
      },
      numToCouponText: {
        2: '折扣券',
        3: '代金券',
        4: '兑换券',
        7: '门店服务'
      },
      editForm: {
        couponList: []
      },
      couponObj: {
        // 折扣券
        discount: {
          label: '折扣券',
          discountType: 2,
          list: []
        },
        // 代金券
        cash: {
          label: '代金券',
          discountType: 3,
          list: []
        },
        // 兑换券
        exchange: {
          label: '兑换券',
          discountType: 4,
          list: []
        },
        // 会员服务
        service: {
          label: '会员服务',
          discountType: 7,
          list: []
        }
      }
    }
  },
  components: {
    selectGiftDialog
  },
  props: {
    channelId: {
      type: String,
      default: ''
    },
    topicId: {
      type: String,
      default: ''
    }
  },
  //   components: {
  //     relateStoreDialog
  //   },
  created() {
    this.getCouponconfig()
  },
  methods: {
    toLabelText(row) {
      switch (row.effectiveType) {
        case 3:
          return '永久有效'
        case 2:
          return row.effectiveStartTime ? row.effectiveStartTime.split(' ')[0] + '-' + row.effectiveEndTime.split(' ')[0] : ''
        case 1:
          return row.effectiveDays + '天'
      }
    },
    getCouponconfig() {
      this.$apis.store.getCouponconfig({ topicId: this.topicId }).then(res => {
        if (res.code == 0) {
          res.data.coupons.forEach(item => {
            item.couponAnticipatedCost = (item.couponAnticipatedCost / 100).toFixed(2)
          })
          this.tableData = res.data.coupons
        }
      })
    },
    handleAdd() {
      // console.log(this.couponObj, '')
      this.selectedGiftList = JSON.parse(JSON.stringify(this.couponObj))
      this.handleGiftClose()
    },
    handleGiftClose() {
      this.isSelectGiftVisible = !this.isSelectGiftVisible
    },
    handleGiftConfirm(selectedData, type) {
      // console.log(selectedData, 'selectedData')
      if (type != 'group') {
        this.handleGiftClose()
      }
      this.couponObj = selectedData
      this.recoveryTotalPrice()
      this.recoveryGiftList()
      this.getData()
    },
    getData() {
      this.orderShow.forEach(item => {
        if (this.couponObj[item].list&&this.couponObj[item].list.length) {
          this.tableData = this.couponObj[item].list
        }
      })
    },
    // 计算预估成本
    recoveryTotalPrice() {
      this.totalEstimatedCost = 0
      this.orderShow.forEach(item => {
        this.couponObj[item].list.forEach(subItem => {
          // 失效券不加入预估成本
          if (subItem.status != '3') {
            this.totalEstimatedCost = this.totalEstimatedCost + parseInt(subItem.couponAnticipatedCost || 0) * parseInt(subItem.couponAmount)
          }
        })
      })
      this.totalEstimatedCost = (this.totalEstimatedCost / 100).toFixed(2)
    },
    // 礼包
    recoveryGiftList() {
      this.editForm.couponList = []
      this.orderShow.forEach(item => {
        this.couponObj[item].list.forEach(k => {
          this.editForm.couponList.push(k)
        })
      })
      //   this.hasCouponBlock()
    },
    // hasCouponBlock(isValidate = true) {
    //   var idx = this.editFormData.findIndex(item => item.name == 'couponListBlock')
    //   this.editFormData[idx].isHide = !this.editForm.couponList.length
    //   isValidate && this.$refs.editFormRef.$refs[this.editChildRef].validateField('couponList')
    // },
    handleSave() {
      if (this.tableData.length == 0) {
        this.$message({
          message: '请选择商户',
          type: 'error'
        })
        return
      }
      let couponIds = []
      this.tableData.forEach(item => {
        couponIds.push(item.id)
      })
      let params = {
        topicId: this.topicId,
        couponIds,
        couponNames: this.tableData[0].couponName
      }
      this.$apis.store.configCoupons(params).then(res => {
        if (res.code == 0) {
          this.$router.go(-1)
        }
        this.$message({
          message: res.code == '0' ? '配置成功' : res.message,
          type: res.code == '0' ? 'success' : 'error'
        })
      })
    },
    handleCancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  .gl-form-page {
    flex: 1;
    padding: 20px;
    .config-form-item {
      display: flex;
      margin-top: 20px;
      /deep/.el-form-item__content {
        width: 460px;
      }
      /deep/.el-input--prefix .el-input__inner {
        line-height: 32px;
        height: 32px;
      }
    }
    /deep/.el-input__prefix {
      line-height: 32px;
      .el-input__icon {
        line-height: 32px;
      }
    }
    label {
      font-size: 14px;
      text-align: right;
      color: #666;
      width: 154px;
      // margin-right: 12px;
      height: 36px;
      line-height: 36px;
      img {
        width: 14px;
        height: 14px;
        vertical-align: middle;
        margin-left: 5px;
      }
      &.is-require {
        &::before {
          content: '*';
          color: #f56c6c;
          margin-right: 4px;
        }
      }
    }
  }

  .merchant-wrap {
    width: 700px;
    margin-left: 194px;
    margin-top: 10px;
  }
}
</style>