selectCouponDialog.vue 12.3 KB
<template>
  <GLDialog bigTitle="选择商品优惠券" mode="big" :visible.sync="isVisible" :show-close="true" :before-close="handleClose" :close-on-click-modal="false" @onOk="onOk" @onCancel="onCancel">
    <div class="dialog_body">
      <div class="table_body">
        <GLCombiForm :rowNumber="1" class="dialog_search" ref="searchRef" :formObj="dialogSearchForm" :formData="dialogFormData" @handleOk="handleOk" @handleRest="handleRest"></GLCombiForm>
        <div class="dialog_table">
          <GLTable  v-loading="loading" :tableTotal="tableTotal" :pageSizes="pageSizes" ref="tableRef" :pageConf="pageConf" :searchForm="dialogSearchForm" :tableParams="tableParams" :tableData="tableData" @refreshTable="getCouponList" @selectionTable="selectionTable">
            <div class="good_name" slot="goodName" slot-scope="scope">
              <img v-if="scope.scope.appearance == 1" src="@/assets/coupon.png" alt="" />
              <img v-else src="@/assets/directCharge.png" alt="" />
              <!-- {{ scope.scope.appearance == 2 ? scope.scope.goodsName + (scope.scope.denomination || '') : scope.scope.goodsName }} -->
              {{ scope.scope.goodsName }}
            </div>
          </GLTable>
        </div>
      </div>
      <div class="selected_list">
          <p class="list_title">已选({{ selectedSum }})</p>
          <div class="list_gifts">
            <div v-for="(val, key) in signList" :key="key">
              <div class="gift_box" v-if="val && val.length">
                <img v-if="key == 'couponList'" class="gift_label" src="@/assets/coupon.png" alt="" />
                <img v-else class="gift_label" src="@/assets/directCharge.png" alt="" />
                <div class="gift_content">
                  <div class="gift_item" v-for="(item, index) in val" :key="index">
                    <img class="muti_icon" src="@/assets/muti-selected.png" @click="handleGiftDelete(key, item, index)" />
                    <!-- <img class="muti_icon" src="@/assets/muti-no-select.png" @click="handleGiftDelete(item, index)" /> -->
                    <el-tooltip effect="dark" placement="top" :disabled="item.goodsName.length <= 7">
                      <p :class="['gift_coupon_name', 'ellipsis-1', item.status == '3' ? 'txt-grey' : '']">{{ item.goodsName.length > 7 ? item.goodsName.slice(0, 7) + '...' : item.goodsName }}</p>
                      <div slot="content">{{ item.goodsName }}</div>
                    </el-tooltip>
                    <div class="gift_sum">
                      <el-input-number :disabled="item.status == '3'" class="sum_input" v-model="item.couponAmount" :min="1" :max="9999" size="mini" label="描述文字"></el-input-number>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
      </div>
    </div>
  </GLDialog>
</template>

<script>
import { utils } from 'glsx-common'
export default {
  name: 'index',
  props: ['isVisible', 'selectedGoodsList','templateId'],
  data() {
    return {
      copyList: [],
      firstOpen: true,
      resetTable: false,
      loading: false,
      signList : {
        couponList: [],
        directChargeList: []
      },
      giftList: [],
      pageSizes: [10, 20, 30, 40, 50],
      dialogSearchForm: {
        brandName: '',
        goodsName: ''
      },
      dialogFormData: [
        {
          label: '商品品牌',
          name: 'brandName',
          type: 'select',
          optionList: [],
          optionId: 'brandName',
          optionLabel: 'brandName',
          isClear: true
        },
        {
          label: '商品名称',
          name: 'goodsName',
          type: 'input',
          isClear: true
        }
      ],
      getListApi: this.$apis.goodsCouponApi.getCouponsList,
      tableData: [],
      tableTotal: 0,
      pageConf: { pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'total' },
      tableParams: {
        listName: 'list',
        mutiSelectName: 'goodsIndex',
        mutiSelectUniqueKey: 'id',
        tableOption: false,
        tableColumns: [
          {
            label: '品牌',
            prop: 'brandName'
          },
          {
            label: '商品名称',
            minWidth: 220,
            addHtml: 'goodName'
          },
          {
            label: '券面额/元',
            width: 110,
            formatter: cell => {
              return cell.denomination ? Number((Number(cell.denomination) / 100).toFixed(2)) : 0
            }
          },
          {
            label: '有效期/天',
            width: 110,
            prop: 'effectiveDays'
          }
        ]
      }
    }
  },
  computed:{
    selectedSum(){
      return  this.signList?.couponList?.length + this.signList?.directChargeList?.length
    }
  },
  created() {
    this.getAllBrandList()
  },
  mounted(){
    this.getListByTemplateId()
  },  
  methods: {
    getListByTemplateId(){
      const params = {
        pageNum: 1,
        pageSize: 9999,
        templateId: this.templateId
      }
      console.log(params)
      this.$apis.goodsCouponApi.getCouponById(params).then(res => {
        if(res){
          res?.list?.map(item => item.id = item.couponId)
          this.filterCoupon(res.list)
          this.getCouponList(false)
        }
      })
    },
    getCouponList(type){
      if (this?.$refs?.tableRef?.page && type=='reset') {
        this.$refs.tableRef.page.pageNum = 1
        this.$refs.tableRef.page.pageSize = 10
      }
      let {pageSize=10, pageNum=1} = this?.$refs?.tableRef?.page || {}
      let params = {
        ...this.dialogSearchForm,
        pageSize,
        pageNum
      }
      this.loading = true
      this.$apis.goodsCouponApi.getCouponsList(params).then(res => {
        this.loading = false
        if(res){
         this.tableData = res?.list || []
         this.tableTotal = res?.total || 0
         this.setTableFinishFun()
        }
      })
    },
    getAllBrandList() {
      this.$apis.orderApi.getAllBrandList().then(res => {
        const item = this.dialogFormData.find(item => item.name === 'brandName')
        item.optionList = [...res.data]
      })
    },
    handleOk() {
      this.resetTable = true
      this.getCouponList('reset')
    },
    handleRest() {
      this.dialogSearchForm = {
        brandName: '',
        goodsName: ''
      }
      this.handleOk()
    },
    onOk() {
      this.$emit('confirmDialog', [...this.signList.directChargeList, ...this.signList.couponList])
      this.handleClose()
    },
    onCancel() {
      this.handleClose()
    },
    handleClose() {
      this.$emit('closeDialog')
    },
    // 点击选中卡券
    selectionTable(e){
      if(this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable']?.data?.length){
        this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable']?.data?.forEach(item => {
          const selectTarget = e?.find(row =>  item.id == row.id)
          const idx = this.signList[item.appearance==2 ? 'directChargeList': 'couponList']?.findIndex(row => item.id == row.id)
          if(selectTarget && idx==-1){
            let obj = utils.deepClone(item)
            this.$set(obj, 'couponAmount', 1)
            this.signList[item.appearance==2 ? 'directChargeList': 'couponList'].push(obj)
          }
          if(!selectTarget && idx>-1){
            this.signList[item.appearance==2 ? 'directChargeList': 'couponList'].splice(idx, 1)
          }
        })
      }
      e?.forEach(item => {
        const target = this.signList[item.appearance==2 ? 'directChargeList': 'couponList']?.find(row => item.id == row.id)
        if(!target){
          this.signList[item.appearance==2 ? 'directChargeList': 'couponList'].push(item)
        }
      })
    },
    // 选中的优惠券分成卡券和直充
    filterCoupon(list){
      this.signList.directChargeList = list.filter(item => item.appearance==2)
      this.signList.couponList = list.filter(item => item.appearance==1)
    },
    //删除选中行
    delSelectedRow(row){
      this.$nextTick(() => {
        let idx = this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].data.findIndex(item => item.id == row.id)
        if (idx > -1) {
          try {
            this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].toggleRowSelection(this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].data[idx], false)
          } catch (err) {
            console.log(err)
          }
        }
      })
    },
    // 点击删除
    handleGiftDelete(key, item, idx){
      this.signList[key].splice(idx, 1)
      this.delSelectedRow(item)
    },
    //根据右边已选框设置选中
    setTableFinishFun() {
      this.$nextTick(()=>{
        let list = [...this.signList.directChargeList, ...this.signList.couponList]
        this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable']?.data?.forEach((row,rowIdx) => {
          let idx = list.findIndex(item => item.id == row.id)
          try {
            if(idx>-1){
              this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].toggleRowSelection(this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].data[rowIdx], true)
            }else{
              this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].toggleRowSelection(this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].data[rowIdx], false)
            }
          } catch (err) {
            console.log(err)
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  /deep/ .table-header {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
    .el-button {
      min-width: 0;
    }
  }
  .good_name {
    img {
      width: 40px;
      height: 20px;
      vertical-align: text-bottom;
    }
  }
  .dialog_body{
    display: flex;
  }
  .table_body {
    border: 1px solid #e5e5e5;
    margin-right: 10px;
    .dialog_search {
      padding: 20px 20px 0;
      /deep/ .el-form-item {
        width: 270px;
        &:last-child{
          width: 130px;
          .el-button{
            vertical-align: middle;
          }
        }
      }
    }
    .coupon_type {
      width: 100%;
      display: inline-block;
      border-bottom: 1px solid #eee;
      .type {
        display: inline-block;
        height: 40px;
        width: 70px;
        line-height: 40px;
        font-weight: 500;
        text-align: center;
        font-size: 16px;
        cursor: pointer;
      }
      .active_type {
        color: #5850ff;
        border-bottom: 2px solid #5850ff;
      }
    }
    .dialog_table {
      padding: 0 20px 20px;
    }
  }
  .selected_list {
    width: 280px;
    border-radius: 2px;
    border: 1px solid #eeeeee;
    flex-shrink: 0;
    .list_title {
      height: 54px;
      line-height: 54px;
      font-size: 16px;
      font-weight: 500;
      color: #333333;
      border-bottom: 1px solid #f1f1f1;
      padding: 0 17px;
    }
    .list_gifts {
      height: 540px;
      padding: 0 16px;
      overflow-y: scroll;
      .gift_box{
        padding: 16px 0 20px;
      }
      .gift_label {
        width: 40px;
        height: 20px;
        margin-bottom: 10px;
        vertical-align: text-bottom;
      }
      .gift_content {
        &:last-child {
          border-bottom: 0px;
        }
        border-bottom: 1px solid #e5e5e5;
        .gift_item {
          &:last-child {
            margin-bottom: 0;
          }
          margin-bottom: 20px;
          display: inline-flex;
          align-items: center;
          .muti_icon {
            cursor: pointer;
          }
          .gift_coupon_name {
            display: inline-block;
            width: 109px;
            margin: 0 5px 0 7px;
          }
          /deep/ .gift_sum {
            display: inline-block;
            width: 82px;
            text-align: right;
            .el-input-number--mini .el-input__inner {
              padding: 0 10px;
            }
            .sum_input {
              display: inline-block;
              width: 82px;
            }
            .el-input--mini .el-input__inner {
              border: 0px;
            }
            .el-input-number--mini .el-input-number__decrease,
            .el-input-number--mini .el-input-number__increase {
              width: 20px !important;
              border: 0px;
              background-color: #fff;
            }
          }
        }
      }
    }
  }
}
</style>