<template>
  <div class="">
    <div class="tit">{{ '规则设置' }}</div>
    <div class="xian"></div>
    <div class="tabList"></div>
    <div class="table">
      <!-- 批量操作 -->
      <table>
        <tr>
          <th colspan="10" class="tableLeft">
            <el-button type="primary" @click="addNewGoods" size="mini"
              >{{ '添加' }}
            </el-button>
          </th>
          <th colspan="2" style="padding-bottom: 0">
            <!-- <div class="tableFlex">
              <div class="tableFlex_i">
                <el-button type="primary" @click="toPlcz" size="mini">{{
                  '批量操作'
                }}</el-button>
              </div>
              <div class="tableFlex_i">
                <el-button size="mini" @click="reset">
                  {{ '重置' }}</el-button
                >
              </div>
            </div> -->
          </th>
        </tr>
      </table>
      <table class="table_b">
        <tr style="border-top: 0">
          <td style="border: 0"></td>
          <td colspan="5" style="padding-bottom: 0">
            <div class="tableFlex">
              <div class="tableFlex_i">{{ $t("商品") }}</div>
              <div class="tableAdd"></div>
            </div>
          </td>
          <td colspan="2">{{ $t("商品数量") }}</td>
          <td colspan="2">{{ $t("赠送提货数量") }}</td>

          <td colspan="2" style="border: 0"></td>
        </tr>
        <tr v-for="(item, index) in mcrList" :key="index">
          <td style="border: 0"></td>
          <td colspan="5" style="padding-bottom: 0">
            <div
              class="tableFlex"
              v-show="item.rppcList.length > 0"
              v-for="(stem, sndex) in item.rppcList"
              :key="sndex"
            >
              <div class="tableFlex_i">
                <el-select
                  v-model="stem.pkWares"
                  size="medium"
                  filterable
                  clearable
                  :placeholder="'请选择'"
                >
                  <el-option
                    v-for="item in allGoods"
                    :key="item.pkId"
                    :label="item.waresName"
                    :value="item.pkId"
                  >
                    <span style="float: left">{{ item.waresName }}</span>
                    <span
                      style="float: right; color: #8492a6; font-size: 13px"
                      >{{ item.waresCode }}</span
                    >
                  </el-option>
                </el-select>
              </div>
              <div class="tableAdd">
                <img
                  src="@/assets/images/iconpop.png"
                  @click="reduceProduct(index, sndex)"
                  alt=""
                />
                <img
                  src="@/assets/images/iconadd.png"
                  @click="addProduct(index)"
                  alt=""
                />
              </div>
            </div>
            <div class="tableAddPro" v-show="item.rppcList.length == 0">
              {{ '添加产品' }}
              <img
                src="@/assets/images/iconadd.png"
                @click="addProduct(index)"
                alt=""
              />
            </div>
          </td>
          <td colspan="2">
            <el-input
              placeholder="商品数量"
              size="medium"
              v-model="item.waresQuantity"
            ></el-input>
          </td>
          <td colspan="2">
            <el-input
              placeholder="赠送提货数量"
              size="medium"
              v-model="item.giftQuantity"
            ></el-input>
          </td>
          <td colspan="2" style="border: 0">
            <div class="tableAdd">
              <img
                src="@/assets/images/iconpop.png"
                @click="reduceRow(index)"
                alt=""
              />
            </div>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
import * as api from "@/api/giftGoods.js";
import topBar from "@/components/topBar";
import { isLocals } from "../../../utils/numberToCurrency";
export default {
  components: {
    topBar,
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      total: 0,
      moren: "setGiftGoodsInfo",
      topList: [
        {
          name: '基本信息',
          path: "pickproductInfo",
        },
        {
          name: '规则设置',
          path: "pickproductSetConsumptionInfo",
        },
      ],
      allData: [], //全部数据
      areaList: [], //全部专区
      allGoods: [], //全部商品
      isArea: "",
      plcz: {},
      select: {}, //产品筛选
      dialogList: [], //产品数组
      selectData: [], //选中
      skuList: [], //规格数组
      selSkuList: "",
      selSkuNameList: "",
      selIndex: "",
      showCndex: "",
      showIndex: "",
      pageData: {},
      queryData: [],
      gradeList: [],
      mcrList: [],
    };
  },
  watch: {
    allGoods: {
      handler(n) {
        console.log(n, "nnn");
      },
    },
  },
  mounted() {
    this.getData();
  },
  methods: {
    isLocals,
    getPage(allData) {
      const aggregatedData = {
        waresQuantity: "",
        giftQuantity: "",
        rppcList: [],
      };
      if (allData.length > 0) {
        aggregatedData.waresQuantity = allData[0].waresQuantity || "";
        aggregatedData.giftQuantity = allData[0].giftQuantity || "";
      }
      allData.forEach((item) => {
        aggregatedData.rppcList.push({
          pkWares: item.pkWares,
        });
      });
      this.mcrList = [aggregatedData];
    },
    getData() {
      api.gradeList().then((res) => {
        this.gradeList = res.rows;
      });
      api.waresList().then((res) => {
        this.allGoods = res.data;
      });
    },

    toPlcz() {
      this.mcrList.forEach((ctem) => {
        ctem.rppcList.forEach((stem) => {
          stem.quantity = this.plcz.quantity;
        });
      });
    },
    reset() {
      this.plcz = {};
    },

    addProduct(rowIndex) {
      this.mcrList[rowIndex].rppcList.push({
        pkWares: "",
      });
    },
    reduceProduct(cndex, sndex) {
      this.mcrList[cndex].rppcList.splice(sndex, 1);
    },
    addNewGoods() {
      if (this.mcrList.length >= 1) {
        return;
      }
      this.mcrList.push({
        waresQuantity: "",
        giftQuantity: "",
        rppcList: [
          {
            pkWares: "",
          },
        ],
      });
    },
    reduceRow(index) {
      this.mcrList.splice(index, 1);
    },

    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>

<style lang="scss" scoped>
.main_a {
  // height: calc(100vh - 124px);
  // display: flex;
  // flex-direction: column;
  // height: calc(100vh - 164px);
  // height: 100%;
}
.main {
  background: #ffffff;
  border-radius: 8px;
  height: calc(100vh - 164px);
  padding-bottom: 20px;
  box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
}
::v-deep .el-select {
  width: 100%;
}
// }
.footer {
  height: 68px;
  background: #ffffff;
  box-shadow: 0px -3px 20px 0px rgba(204, 204, 204, 0.5);
  // margin: 0 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  .thebtn1 {
    background: #3181e5;
    color: #ffffff;
    padding: 9px 45px;
  }
  .thebtn2 {
    background: #cccccc;
    color: #ffffff;
    padding: 9px 45px;
  }
}
.tit {
  font-size: 14px;
  font-weight: bold;
  color: #333333;
  padding: 0 20px;
  border-left: 5px solid #c8161d;
  margin: 20px 0 10px 0;
}
.xian {
  height: 1px;
  background: rgba(0, 0, 0, 0.1);
}
.demo-ruleForm {
  padding: 20px 0;
}
::v-deep .el-date-editor--datetimerange {
  width: auto !important;
}
.flexed {
  display: flex;
}
.flexed_l {
  margin-right: 20px;
}
.table {
  padding: 0 20px;
  margin-top: 1px;
}

table {
  width: 100%;
  background: #fff;
  border-collapse: collapse;
  border: 1px solid #cccccc;
  border-spacing: 0;
  table-layout: fixed;
  border-collapse: collapse;
  //   合并内外边距
  // (去除表格单元格默认的2个像素内外边距*/
}
.table_b {
  border-top: 0;
}
th {
  padding: 10px 10px;
  text-align: center;
  border: 1px solid #cccccc;
  overflow: hidden;
  border-top: 0;
}
td {
  padding: 10px 10px;
  text-align: center;
  border: 1px solid #cccccc;
  border-top: 0;
  overflow: hidden;
}
.tableFlex {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  .tableFlex_i {
    flex: 1;
    ::v-deep .el-input__inner {
      width: 100%;
    }
  }
}
.tableAdd {
  width: 50px;
  img {
    width: 19px;
    height: 19px;
  }
}
.tableAddPro {
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 19px;
    height: 19px;
    margin-left: 10px;
  }
}
.tableLeft {
  text-align: left;
}
.tabList {
  display: flex;
  align-items: center;
  padding: 20px 0px 0 20px;
  .tabList_i {
    padding: 10px;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    cursor: pointer;
  }
  .tabAct {
    background: #108ee9;
    color: #fff;
  }
}
.clearBtm {
  border-bottom: 0;
}
.cpHeight {
  height: 50vh;
  overflow-y: auto;
}
.blueCor {
  color: #589ff8;
  cursor: pointer;
}
::v-deep .el-input__icon {
  line-height: 33px;
}
::v-deep .el-button {
  margin-top: 0;
}
</style>
