GoodsInfo.vue 10 KB
<!-- 编辑 - 上架商品及测算 -->
<template>
  <div class="edit-page goods-info">
    <div class="title">
      <span>上架商品</span>
      <!-- <span class="addBtnSpan"
        ><el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="addPop"
          >新增</el-button
        ></span
      > -->
    </div>
    <div class="check-tab">
      <el-tabs v-model="tabsValue" type="card" class="my-tab-pane">
        <el-tab-pane v-for="(item, idx) in goodTabs" :key="item.name" :name="item.name">
          <span slot="label"><i class="el-icon-warning car-error-txt" v-if="item.valiadError == 'Y'" />{{ item.title ?
            item.title + (idx + 1) : item.goodsName }}
          </span>
          <span>
            <good-tab :info="item.info" :tab-idx="item.tabIndex" />
          </span>
        </el-tab-pane>
        <el-tab-pane v-for="(item, idx) in packageTabs" :key="item.name" :label="item.title" :name="item.name">
          <span slot="label"><i class="el-icon-warning car-error-txt" v-if="item.valiadError == 'Y'" />{{ item.title ?
            item.title + (idx + 1) : item.goodsName }}
          </span>
          <span>
            <package-tab :info="item.info" :tab-idx="item.tabIndex" @packageEditInfo="getPackageEditInfo_notice" />
          </span>
        </el-tab-pane>
        <!-- <el-tab-pane key="add"
                     name="add"> -->
        <!-- //这是增加的按钮 -->
        <!-- <span slot="label"
                style="padding: 8px; font-size: 20px; font-weight: bold">+</span>
        </el-tab-pane> -->
      </el-tabs>
    </div>
    <!-- <div style="padding: 20px;">
      <div>啦啦啦</div>
    </div> -->
    <d2-dialog title="选择商品或套餐" v-if="chooseVisible" :is-visible="chooseVisible" @closeDialog="handleSignClose"
      @confirmDialog="handleConfirmSign">
      <glsx-combi-form :is-block="true" class="edit-form-container" ref="editFormRef" label-width="220px"
        default-width="400px" :form-obj="integralForm" :form-data="integralFormData">
      </glsx-combi-form>
    </d2-dialog>
  </div>
</template>

<script>
import { getGoodsDetail } from "@api/shelfManage.js"
import goodTab from "./common/goodTab.vue"
import packageTab from "./common/packageTab.vue"
import { deepClone } from "@/common/util"
export default {
  name: "GoodsInfo",
  props: {
    recordId: {
      type: Number,
      default: null
    }
  },
  components: {
    goodTab,
    packageTab
  },
  data() {
    // 这里存放数据
    return {
      chooseVisible: false,
      integralForm: {
        goodItem: "1"
      },
      integralFormData: [
        {
          label: "新增选择",
          name: "goodItem",
          type: "radio",
          optionId: "confId",
          optionLabel: "confName",
          optionList: [
            { confId: "1", confName: "商品" },
            { confId: "2", confName: "套餐" }
          ],
          handleRadioChange: (val) => { }
        }
      ],

      tabsValue: "1",
      currentIndex: 1,
      addIndex: 1,
      tabIndex: 1,
      goodTabs: [],
      packageTabs: [],
      goodInfoList: [],
      packageInfoList: [],
      goodsInfo: {}
    }
  },
  computed: {
    routeInfo() {
      return this.$route.params
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    submitParamsPackages() {
      return this.goodsInfo.packageGoodsList
    },
    getDetail() {
      console.log("getGoodsDetail")
      // this.recordId
      getGoodsDetail({ id: this.routeInfo.id })
        .then((res) => {
          console.log(res, "getGoodsDetail")
          this.goodsInfo = res
          const goodsList = deepClone(res.goodsList)
          const packageGoodsList = deepClone(res.packageGoodsList)
          goodsList.forEach((item, idx) => {
            const length = this.goodTabs.length
            const tabIndex =
              length == 0 ? length : this.goodTabs[length - 1].tabIndex + 1
            this.goodTabs.push({
              goodsName: item.goodsName,
              name: "good_" + tabIndex,
              tabIndex: tabIndex,
              // goodId: item.id,
              info: item
            })
            this.tabsValue = "good_" + tabIndex
          })
          packageGoodsList.forEach((item, idx) => {
            const length = this.packageTabs.length
            const tabIndex =
              length == 0 ? length : this.packageTabs[length - 1].tabIndex + 1
            this.packageTabs.push({
              goodsName: item.packageName,
              name: "package_" + tabIndex,
              tabIndex: tabIndex,
              // goodId: item.id,
              info: item
            })
            this.tabsValue = "package_" + tabIndex
          })
          this.packageNotice()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    addPop() {
      this.chooseVisible = true
    },
    handleSignClose() {
      this.chooseVisible = false
    },
    handleConfirmSign() {
      this.chooseVisible = false
      this.addTab(this.integralForm.goodItem)
    },

    /* 活动标签切换时触发 */
    beforeLeave(currentName, oldName) {
      // console.log("currentName", currentName)
      // 通过tab标签的切换事件触发增加的函数
      // 如果name是add,则调用增加tab的函数
      // if (currentName == "add") {
      //   // this.addTab()
      //   this.addPop()
      //   return false
      // } else {
      this.currentIndex = currentName
      // }
    },
    addTab(goodType) {
      // 根据已有的tab,再增减
      if (goodType == 1) {
        // 商品
        const length = this.goodTabs.length
        // this.tabIndex = length + 1
        // this.addIndex = length + 1
        // this.goodTabs = this.goodTabs.map((item, index) => {
        //   item.name = index + 1 + ''
        //   return item
        // })
        // const newTabIndex = ++this.tabIndex + ""
        const tabIndex =
          length == 0 ? length : this.goodTabs[length - 1].tabIndex + 1
        this.goodTabs.push({
          // title: "商品名称" + this.addIndex++,
          title: "商品名称",
          // name: 'good_' + newTabIndex,
          name: "good_" + tabIndex,
          tabIndex: tabIndex,
          info: {}
        })
        // this.tabsValue = 'good_' + newTabIndex
        // this.currentIndex = 'good_' + newTabIndex
        this.tabsValue = "good_" + tabIndex
      } else {
        // 套餐
        const length = this.packageTabs.length
        // this.tabIndex = length + 1
        // this.addIndex = length + 1

        // const newTabIndex = ++this.tabIndex + ""
        const tabIndex =
          length == 0 ? length : this.packageTabs[length - 1].tabIndex + 1
        this.packageTabs.push({
          // title: "套餐名称" + this.addIndex++,
          title: "套餐名称",
          name: "package_" + tabIndex,
          tabIndex: tabIndex,
          info: null
        })
        // this.tabsValue = 'package_' + newTabIndex
        // this.currentIndex = 'package_' + newTabIndex
        this.tabsValue = "package_" + tabIndex
      }
    },
    confirmRemove(targetName) {
      // 删除tab
      this.$confirm("确认删除标签页吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnClickModal: false // 是否可以点击空白处关闭弹窗
      })
        .then(() => {
          // 确认切换标签页
          this.removeTab(targetName)
        })
        .catch(() => {
          // 取消切换标签页
          this.$message({
            type: "info",
            message: "已取消删除"
          })
        })
    },
    removeTab(targetName) {
      const self = this
      let activeName = self.tabsValue
      let tabs
      if (targetName.split("_")[0] == "good") {
        tabs = self.goodTabs
      } else {
        tabs = self.packageTabs
      }
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            const nextTab = tabs[index + 1] || tabs[index - 1]
            console.log("tab.name", tab.name)
            console.log("nextTab", nextTab)
            if (nextTab) {
              activeName = nextTab.name
            } else {
              if (tab.name.split("_")[0] == "good") {
                if (
                  self.packageTabs.length > 0 &&
                  self.packageTabs[self.packageTabs.length - 1].name
                ) {
                  activeName =
                    self.packageTabs[self.packageTabs.length - 1].name
                }
              } else {
                if (
                  self.goodTabs.length > 0 &&
                  self.goodTabs[self.goodTabs.length - 1].name
                ) {
                  activeName = self.goodTabs[self.goodTabs.length - 1].name
                }
              }
            }
          }
        })
      }
      this.tabsValue = activeName
      if (targetName.split("_")[0] == "good") {
        this.goodTabs = tabs.filter((tab) => tab.name !== targetName)
      } else {
        this.packageTabs = tabs.filter((tab) => tab.name !== targetName)
      }
      this.packageNotice()
    },

    // 填写套餐名称时通知
    getPackageEditInfo_notice(obj) {
      console.log("obj", obj)
      this.packageTabs.find((item) => item.tabIndex == obj.idx).info = obj.info
      this.packageNotice()
    },

    // 初始化、填写套餐名称时、删除套餐时通知
    packageNotice() {
      this.packageInfoList = []
      this.packageTabs.forEach((item) => {
        if (item.info) {
          console.log(item)
          const o = {
            name: item.info.val1 || item.info.packageName,
            virtualCode: item.info.timeId,
            id: item.info.id,
            packageCode: item.info.packageCode
          }
          this.packageInfoList.push(o)
        }
      })
      console.log("this.packageInfoList", this.packageInfoList)
      this.$EventBus.$emit("packageInfoList", this.packageInfoList)
    }
  }
}
</script>
<style lang="scss" scoped>
@import "../../style/editdetailCon.scss";

.addBtnSpan {
  float: right;
}
</style>