<!--
 * @Descripttion:
 * @version:
 * @Author: kBank
 * @Date: 2022-11-02 14:15:48
-->
<template>
  <div class="">
    <div class="">
      <div class="maintop">
        <div class="mainbtn">
          <el-button style="background-color: #3181E5;border-color: #3181E5"
                     @click="openDig"
                     class="addBtn">{{'添加'}}</el-button>
          <el-button @click="delTableList"
                     class="delBtn"> {{ '删除' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table :data="tableData"
                  style="width: 100%"
                  :header-cell-style="{ background: '#EEEEEE' }"
                  :row-class-name="tableRowClassName"
                  @selection-change="tableChange">
          <el-table-column type="selection"
                           width="55"> </el-table-column>
          <el-table-column align="center"
                           prop="cover"
                           :label="'产品主图'">
            <template slot-scope="scope">
              <img class="bgImg"
                   :src="scope.row.cover"
                   alt="" />
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="productName"
                           :label="'产品名称'">
          </el-table-column>
          <el-table-column align="center"
                           prop="productCode"
                           :label="'产品编号'">
          </el-table-column>
          <el-table-column align="center"
                           prop="num"
                           :label="'产品数量'">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.quantity"
                               @change="getData"
                               size="medium"
                               :min="1"
                               label=""></el-input-number>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="isGift"
                           width="200"
                           :label="'销售类型'">
            <template slot-scope="scope">
              <el-select clearable
                         v-model="scope.row.isGift"
                         @change="getData">
                <el-option v-for="item in agreeList"
                           :key="item.val"
                           :label="item.text"
                           :value="item.val"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="time"
                           width="120"
                           :label="'操作'"
                           fixed="right">
            <template slot-scope="scope">
              <el-button @click="handleDelete(scope.row)"
                         type="text"
                         size="small"
                         style="color: #c73030">
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog :title="'添加'"
               :visible.sync="dialogVisible"
               :close-on-click-modal="false"
               width="45%"
               center
               :before-close="handleClose">
      <div class="cpHeight">
        <el-input v-model="select.productName"
                  :placeholder="'请输入'"
                  @keyup.enter.native="getDataList"
                  prefix-icon="el-icon-search"></el-input>
        <div style="height:10px"></div>
        <el-table :data="dialogList"
                  ref="lessonTableRef"
                  style="width: 100%"
                  :row-key="(row)=>{ return row.pkProduct}"
                  :header-cell-style="{ background: '#EEEEEE' }"
                  :row-class-name="tableRowClassName"
                  @selection-change="dialogChange">
          <el-table-column type="selection"
                           :reserve-selection="true"
                           width="55"> </el-table-column>
          <el-table-column align="center"
                           prop="cover"
                           :label="'产品主图'">
            <template slot-scope="scope">
              <img class="bgImg"
                   :src="scope.row.cover"
                   alt="" />
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="productName"
                           :label="'产品名称'">
          </el-table-column>
          <el-table-column align="center"
                           prop="productCode"
                           :label="'产品编号'">
          </el-table-column>
        </el-table>
        <pagination v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getDataList" />
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="handleClose">{{'取消'}}</el-button>
        <el-button type="primary"
                   @click="onSubmit('form')">{{'确认'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/configManage.js'
import { productList } from '@/api/product'
import { getToken } from '@/utils/auth'
export default {
  name: 'Bzpz',
  filters: {
    isAgree(val) {
      if (!val) {
        return '允许'
      } else {
        return '禁止'
      }
    },
  },
  data() {
    return {
      select: {},
      // 折叠搜索框
      isActive: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      total: 0,
      dialogVisible: false,
      // 表单数组
      tableData: [],
      // 弹框数组
      dialogList: [],
      // 选中弹框临时的数组
      lsArr: [],
      // 选中临时的数组
      tableArr: [],
      agreeList: [
        {
          text: '赠品',
          val: 0,
        },
        {
          text: '销售',
          val: 1,
        },
      ],
    }
  },
  mounted() {},
  methods: {
    // table选中
    tableChange(val) {
      this.tableArr = val
    },
    handleDelete(val) {
      this.tableData.splice(this.tableData.indexOf(val), 1)
       // 判断重复
     let arr = this.tableData
      let isTrue = false
      for (var i = 0; i < arr.length; i++) {
        // 首次遍历数组
        for (var j = i + 1; j < arr.length; j++) {
          // 再次遍历数组
          if (arr[i].productCode == arr[j].productCode) {
            if (arr[i].isGift == arr[j].isGift) {
              isTrue = true
            }
          }
        }
      }
      this.$emit('getTableList', this.tableData,isTrue)
    },
    // 删除指定数组
    delTableList() {
      this.tableArr.forEach((item) => {
        this.tableData.splice(this.tableData.indexOf(item), 1)
      })
    },
    // 内容变化
    getData() {
     // 判断重复
     let arr = this.tableData
      let isTrue = false
      for (var i = 0; i < arr.length; i++) {
        // 首次遍历数组
        for (var j = i + 1; j < arr.length; j++) {
          // 再次遍历数组
          if (arr[i].productCode == arr[j].productCode) {
            if (arr[i].isGift == arr[j].isGift) {
              isTrue = true
            }
          }
        }
      }
      this.$emit('getTableList', this.tableData,isTrue)
    },
    handleClose() {
      this.dialogVisible = false
    },
    openDig() {
      this.select = {}
      productList(Object.assign({isPutOn:0}, this.queryParams)).then((res) => {
        res.rows.forEach((item) => {
          item.quantity = 1
          item.pkProduct = item.pkProduct
          item.isGift = 0
        })
        this.dialogList = res.rows
        // this.tableArr.forEach((info) => {
        //   this.dialogList.forEach((row) => {
        //     if (row.pkProduct == info.pkProduct) {
        //       this.$refs.lessonTableRef.toggleRowSelection(row, true)
        //     }
        //   })
        // })
        this.total = res.total
      })
      this.dialogVisible = true
    },
    getDataList() {
      productList(Object.assign({isPutOn:0}, this.queryParams, this.select)).then(
        (res) => {
          res.rows.forEach((item) => {
            item.quantity = 1
            item.pkProduct = item.pkProduct
            item.isGift = 0
          })
          this.dialogList = res.rows
          // this.tableArr.forEach((info) => {
          //   this.dialogList.forEach((row) => {
          //     if (row.pkProduct == info.pkProduct) {
          //       this.$refs.lessonTableRef.toggleRowSelection(row, true)
          //     }
          //   })
          // })
          this.total = res.total
        }
      )
    },
    dialogChange(val) {
      this.lsArr = val
    },
    onSubmit() {
      // this.tableData = []
      this.lsArr.forEach((item) => {
        this.tableData.push(item)
      })
      // this.tableData = this.clearArr(this.tableData)
      this.dialogVisible = false
      this.dialogList.forEach((row) => {
        this.$refs.lessonTableRef.toggleRowSelection(row, false)
      })
      // 判断重复
      let arr = this.tableData
      let isTrue = false
      for (var i = 0; i < arr.length; i++) {
        // 首次遍历数组
        for (var j = i + 1; j < arr.length; j++) {
          // 再次遍历数组
          if (arr[i].productCode == arr[j].productCode) {
            if (arr[i].isGift == arr[j].isGift) {
              isTrue = true
            }
          }
        }
      }

      this.$emit('getTableList', this.tableData,isTrue)
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    },
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.page {
  // padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .maintop {
      display: flex;
      padding: 10px 20px;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
        }
        .thebtn2 {
          background: #c73030;
          color: #ffffff;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
  .tem {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
  .bgImg {
    width: 48px;
    height: 48px;
  }
}
.openClose {
  text-align: right;
  margin-right: 10px;
  color: #3181e5;
}
.cpHeight {
  height: 60vh;
  overflow-y: auto;
}
::v-deep .el-input__prefix {
  top: -3px;
}
</style>
