index.vue 8.95 KB
<!-- 关联门店弹窗 -->
<template>
  <d2-dialog title="规格组合管理" :is-visible.sync="relateStoreVisible" width="40%" btn-ok-label="确定" @confirmDialog="handleRelateStoreConfirm" @closeDialog="handleRelateStoreClose" class="relateSku">
    <el-form :model="searchForm" ref="relate" :inline="true">
      <el-form-item class="formItem" :label="`${item.specificationName}:`" v-for="(item, index) in specificationDetailList" :key="index">
        <el-select collapse-tags multiple v-model="searchForm[index]" placeholder="请选择规格" value-key="specificationValue">
          <el-option v-for="(options, childIndex) in item.specificationValueList" :key="childIndex" :label="options.specificationValue" :value="options.specificationValue"></el-option>
          <!-- <el-option :label="'全部'" :value="0"></el-option> -->
        </el-select>
      </el-form-item>
      <el-button type="primary" @click="handleCurrentChange(1)">搜索</el-button>
    </el-form>
    <div class="tableHeader">
      <div class="title">规格组合列表</div>
      <div class="btn">
        <el-button @click="handleRelateStart">批量启用</el-button>
        <el-button @click="handleRelateStop">批量禁用</el-button>
      </div>
    </div>
    <!-- <div class="tableCon"> -->
    <div class="tableBox" v-if="specificationDetailList.length">
      <!-- height="464px" -->
      <el-table ref="seletingTable" :data="tableData" class="table" border @select="handleChange" @select-all="handleAll" height="380px">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" width="70" align="center"></el-table-column>
        <!-- width="150" -->
        <el-table-column :label="`${tableItem.specificationName}`" v-for="(tableItem, tableIndex) in specificationDetailList" :key="tableIndex" align="center">
          <template slot-scope="scope">
            {{ scope.row.specsValueList[tableIndex] }}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="150px">
          <template slot-scope="scope">
            <el-switch class="elSwitch" v-model="scope.row.useStatus" active-text="启用" inactive-text="禁用" :active-value="0" :inactive-value="1" @input="clickSwitch(scope)"></el-switch>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 10px">
      共
      <span class="tip-text">{{ tableData.length }}</span>
      件规格商品。
    </div>
    <!-- </div> -->
  </d2-dialog>
</template>

<script>
import { deepClone } from '@/common/constant'
import D2Dialog from '@/components/d2-dialog'
export default {
  name: 'skuDialog',
  components: {
    D2Dialog
  },
  data() {
    return {
      selectedData: [],
      tableData: [],
      // 分页
      // total: 0,
      // currentPage: 1,
      // pageSize: 20,
      searchForm: []
      // tableData: []
    }
  },

  props: {
    relateStoreVisible: {
      type: Boolean,
      default: false
    },
    specificationDetailList: {
      type: Array,
      default: () => []
    },
    goodsSpecificationList: {
      type: Array,
      default: () => []
    }
  },
  filters: {
    filterRow(row, index) {
      console.log(row)
      // return row.specsValueList[index]
    }
  },
  computed() {
    return {
      // goodsSpecificationListTable() {
      //   return this.goodsSpecificationList.filter(item => {
      //     return item
      //   })
      // }
    }
  },

  mounted() {
    console.log(this.specificationDetailList, 'specificationDetailList')
    this.specificationDetailList.forEach((item, index) => {
      this.searchForm.push([])
    })
    this.tableData = deepClone(this.goodsSpecificationList)
    // this.$nextTick(() => {
    //   console.log(this.$refs.seletingTable, ' this.$refs.seletingTable')
    //   this.$refs.seletingTable.doLayout()
    // }, 4000)
    console.log(this.tableData, 'tableData')
  },

  methods: {
    handleCurrentChange() {
      let tableData = deepClone(this.goodsSpecificationList)
      tableData.forEach((item, idx) => {
        this.searchForm.forEach((subItem, index) => {
          // if (item.flag)
          let flag = false
          if (subItem.length > 0) {
            if (subItem.includes(item.specsValueList[index])) {
              flag = true
            }
          } else {
            flag = true
          }
          // return subItem.includes(item.specsValueList[index])

          item.flag = index == 0 ? flag : flag && item.flag
        })
      })
      console.log(tableData, 'tableData')
      this.tableData = tableData.filter(item => {
        return item.flag
      })
      console.log(this.tableData, 'this.tableData')
      this.selectedData = [] //清空勾选
    },
    handleRelateStart() {
      if (!this.selectedData.length) {
        return this.$message.error('请勾选规格')
      }
      this.tableData.forEach(item => {
        this.selectedData.forEach(subItem => {
          if (JSON.stringify(subItem.specsValueList) === JSON.stringify(item.specsValueList)) {
            item.useStatus = 0
          }
        })
      })
      this.$message.success('启用成功')
    },
    handleRelateStop() {
      if (!this.selectedData.length) {
        return this.$message.error('请勾选规格')
      }
      this.tableData.forEach(item => {
        this.selectedData.forEach(subItem => {
          if (JSON.stringify(subItem.specsValueList) === JSON.stringify(item.specsValueList)) {
            item.useStatus = 1
          }
        })
      })
      this.$message.success('禁用成功')
    },
    clickSwitch(row, val) {},
    recoverySelection() {
      var exitIds = this.selectedData.map(item => JSON.stringify(item.specsValueList))
      this.tableData.forEach(item => {
        exitIds.includes(JSON.stringify(item.specsValueList)) && this.$refs.seletingTable.toggleRowSelection(item, true)
      })
    },
    handleChange(selection, row) {
      var addOrDelete = !!selection.find(item => JSON.stringify(item.specsValueList) === JSON.stringify(row.specsValueList))
      addOrDelete ? this.selectedData.push(row) : (this.selectedData = this.selectedData.filter(item => JSON.stringify(item.specsValueList) !== JSON.stringify(row.specsValueList)))
      // console.log(this.selectedData, ' this.selectedData')
    },
    handleAll(selection) {
      this.tableData.forEach(item => {
        var isExit = this.selectedData.findIndex(subItem => JSON.stringify(subItem.specsValueList) === JSON.stringify(item.specsValueList))
        if (selection.length && isExit === -1) {
          this.selectedData.push(item)
        } else if (!selection.length && isExit !== -1) {
          this.selectedData.splice(isExit, 1)
        }
      })
      // console.log(this.selectedData, ' this.selectedData')
    },
    handleDelete(row) {
      this.selectedData = this.selectedData.filter(item => JSON.stringify(item.specsValueList) !== JSON.stringify(row.specsValueList))
      const obj = this.tableData.find(v => JSON.stringify(v.specsValueList) == JSON.stringify(row.specsValueList))
      this.$refs.seletingTable.toggleRowSelection(obj)
    },
    handleRelateStoreConfirm() {
      this.$emit('handleRelateSkuConfirm', this.tableData)
    },
    handleRelateStoreClose() {
      this.$emit('handleSkuClose')
    }
  }
}
</script>
<style lang="scss" >
.tableHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  .title {
    font-weight: 600;
    font-size: 16px;
    color: #333333;
  }
}
.relateSku {
  .formItem {
    margin-bottom: 10px;
  }
}
.relateSku .table {
  // margin: 0 auto 20px !important;
}
// .relateSku .tableCon {
//   margin-top: 20px;
//   width: 100%;
// }
.service-store .relateSku .el-table .cell,
.d2-form .relateSku .el-table .cell {
  line-height: 26px !important;
}
.service-store .relateSku .el-table--medium td,
.d2-form .relateSku .el-table--medium td {
  padding: 6px 8px !important;
}
.relateSku .red {
  color: red;
}
.relateSku .tip-icon {
  position: absolute;
  display: inline-block;
  height: 10px;
  width: 10px;
  line-height: 10px;
  color: #ffffff;
  background-color: red;
  border: 1px solid red;
  border-radius: 50%;
  cursor: pointer;
  font-size: 6px;
  margin-left: 2px;
}
.pagination {
  .el-pager {
    line-height: 20px;
  }
}
.elSwitch {
  .el-switch {
    // width: 38px;
    // height: 20px;
    width: 44px;
    height: 22px;
    margin-left: 40px;
  }

  .el-switch__label {
    position: absolute;
    display: none;
    color: #606266;
  }

  /*打开时文字位置设置*/
  .el-switch__label--right {
    z-index: 1;
    left: 40px;
    // right: 40px;
  }

  /*关闭时文字位置设置*/
  .el-switch__label--left {
    z-index: 1;
    // right: 30px;
    left: 40px;
  }

  .el-switch__label * {
    line-height: 1.5;
    font-size: 16px;
    display: inline-block;
  }

  /*显示文字*/
  .el-switch__label.is-active {
    display: block;
    width: 40px;
  }

  .el-switch .el-switch__core,
  .el-switch .el-switch__label {
    width: 50px !important;
  }
}
</style>