couponDialog.vue 8.01 KB
<!--
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2025-02-18 10:33:56
 * @LastEditors: 前端 qianduan01@didihu.com.cn
 * @LastEditTime: 2025-03-11 15:48:38
 * @FilePath: \dj-car-life-system\src\views\residentManage\growUpGrade\components\couponDialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <baseDialog
    :dialog-config="dialogConfig"
    :is-show.sync="visible"
    class="couponDialog"
    :config="{
      appendToBody: true,
      isShowFooter: true,
      cancel_name: '取 消',
      confirm_name: '确 定'
    }"
    @handleClick="handleAddConfirm"
  >
    <div class="dialog-body">
      <div class="dialog-left">
        <el-form :inline="true" :model="searchForm" class="form-inline">
          <el-form-item label="品牌">
            <el-select
              filterable
              v-model="searchForm.brandName"
              placeholder="请选择"
            >
              <el-option
                :label="item.brandName"
                :value="item.brandName"
                v-for="(item, index) in brandList"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="卡券">
            <el-input
              v-model="searchForm.goodsName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item size="small">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
        <d2-table
          ref="table"
          :search-form="searchForm"
          :table-params="tableParams"
          :tableData="couponList"
          @refreshTable="getList"
          :tableTotal="tableTotal"
          @selectionTable="handleSelectionTable"
        >
        </d2-table>
      </div>
      <div class="dialog-right">
        <div class="select-header">
          <span>已选</span>
          <span class="clear" @click="clear">清空</span>
        </div>
        <div class="select-body">
          <div
            class="select-item"
            v-for="(item, index) in selectList"
            :key="index"
          >
            <i class="el-icon-error" @click="removeCoupon(item, index)"></i>
            <div class="select-name">{{ item.goodsName }}</div>
            <el-input-number
              v-model="item.couponCount"
              :min="1"
              :max="100"
            ></el-input-number>
          </div>
        </div>
      </div>
    </div>
  </baseDialog>
</template>
<script>
import { getCouponList, getBrandList } from "@/api/inStorePoints"
import { deepClone } from "@/common/util"
export default {
  name: "CouponDialog",
  props: {
    couponVisible: {
      type: Boolean,
      default: false
    },
    levelType: {
      type: [String, Number]
    },
    couponFormList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogConfig: {
        title: "选择卡券",
        width: "1150px"
      },
      tableTotal: 0,
      brandList: [],
      selectList: [],
      couponList: [],
      tableParams: {
        listName: "list",
        // 行数据的key
        mutiSelectUniqueKey: "id",
        // 多选ref的名称
        mutiSelectName: "list",
        tableColumns: [
          {
            label: "品牌",
            prop: "brandName"
          },
          {
            label: "卡券",
            prop: "goodsName"
          },
          {
            label: "官方价/元",
            prop: "lineationPrice"
          },
          {
            label: "成本价/元",
            prop: "costPrice"
          }
        ]
      },
      searchForm: {}
    }
  },
  computed: {
    visible: {
      get() {
        return this.couponVisible
      },
      set() {
        this.$emit("update:couponVisible", false)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.selectList = deepClone(this.couponFormList)
      this.initStatus = true
      this.getList()
      this.getBrandList()
    })
  },
  methods: {
    clear() {
      this.selectList = []
      this.$refs.table.$refs.listMutiTable.clearSelection()
    },
    getBrandList() {
      getBrandList({ supplier: "HW", pageNum: 1, pageSize: 9999 }).then(
        (res) => {
          this.brandList = res
        }
      )
    },
    getList(isReset) {
      let params = {
        ...this.searchForm,
        appearance: 1,
        status: 2,
        pageNum: this.$refs.table.page.currentPage,
        pageSize: this.$refs.table.page.pageSize
      }
      getCouponList(params).then((res) => {
        this.couponList = res.list
        this.tableTotal = res.total
        this.selectList.forEach((row) => {
          const idx = this.couponList.findIndex((k) => k.id == row.id)
          console.log(idx)
          if (idx != -1) {
            this.toggleRowSelected(row, true)
          }
        })
      })
    },
    removeCoupon(row, i) {
      this.toggleRowSelected(row, false)
      this.selectList.splice(i, 1)
    },
    handleReset() {
      this.searchForm = {}
      this.getList(true)
    },
    handleSearch() {
      this.getList(true)
    },
    handleSelectionTable(val) {
      const currentIdList = this.couponList.map((item) => item.id)
      val.forEach((item) => {
        if (this.selectList.findIndex((k) => k.id == item.id) == -1) {
          this.selectList.push({ ...item, couponCount: 1 })
        }
      })
      currentIdList.forEach((item) => {
        if (
          val.findIndex((k) => k.id == item) == -1 &&
          this.selectList.findIndex((k) => k.id == item) != -1
        ) {
          this.selectList.splice(
            this.selectList.findIndex((k) => k.id == item),
            1
          )
        }
      })
      if (!this.initStatus) {
        return
      }
      this.selectList.forEach((item) => {
        const index = this.couponFormList.findIndex((k) => k.id == item.id)
        if (index != -1) {
          item.couponCount = this.couponFormList[index].couponCount
        }
      })
      setTimeout(() => {
        this.initStatus = false
      }, 300)
    },
    // 删除选中
    toggleRowSelected(List, isSelected) {
      this.$nextTick(() => {
        const i = this.couponList.findIndex((item) => item.id == List.id)
        if (i >= 0) {
          this.$refs.table.$refs.listMutiTable.toggleRowSelection(
            this.couponList[i],
            isSelected
          )
        }
      })
    },
    handleAddConfirm() {
      if (this.levelType == 2 && this.selectList.length > 1) {
        return this.$message.error("只能选择一种卡券")
      }
      this.$emit("update:couponVisible", false)
      this.$emit("handleAddConfirm", this.selectList)
    }
  }
}
</script>
<style scoped lang="scss">
.dialog-body {
  display: flex;
  .dialog-left {
    border: 1px solid #e5e5e5;
    padding: 20px;
    margin-right: 20px;
    width: calc(100% - 360px);
    .form-inline {
      ::v-deep {
        .el-input__inner {
          width: 140px;
        }
      }
    }
  }
  .dialog-right {
    width: 300px;
    border: 1px solid #e5e5e5;
    .select-header {
      height: 44px;
      line-height: 44px;
      border-bottom: 1px solid #e5e5e5;
      display: flex;
      justify-content: space-between;
      padding: 0 16px;
      font-size: 16px;
      color: #333333;
      .clear {
        font-size: 14px;
        color: #2a58ad;
        cursor: pointer;
      }
    }
    .select-body {
      margin-top: 10px;
      .select-item {
        padding: 5px 16px;
        display: flex;
        align-items: center;
        font-size: 14px;
        color: #333333;
        i {
          color: #a6a6a6;
        }
        .select-name {
          width: 125px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          margin-left: 5px;
        }
        .el-input-number--medium {
          width: 125px;
        }
      }
    }
  }
}
</style>