<template>
  <el-dialog width="60%" title="选择商品" :visible.sync="innerVisible" append-to-body closed="closeShop">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" :size="ELsize" label-width="auto">
      <el-form-item label="商品名称" prop="title">
        <el-input v-model="queryParams.title" :maxlength="20" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" ref="goodsTab" :data="list" @selection-change="handleSelectionChange"
      :border="TableBorder" :size="ELTableSize">
      <el-table-column type="selection" width="55" :selectable="selected">
      </el-table-column>
      <el-table-column align="center" type="index" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="coverImage" header-align="center" align="center" label="商品图片">
        <template slot-scope="{ row }">
          <el-image style="width: 50px; height: 50px" :src="row.coverImage"
            :preview-src-list="row.coverImage ? [row.coverImage] : []"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="商品名称">
      </el-table-column>
      <el-table-column prop="price" header-align="center" align="center" label="商品价格（元）">
        <template slot-scope="scope">
          <span>{{
            scope.row.minSalePrice == scope.row.maxSalePrice
              ? scope.row.minSalePrice
              : scope.row.minSalePrice + '~' + scope.row.maxSalePrice
          }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <span slot="footer" class="dialog-footer">
      <el-button :size="ELsize" @click="close">取 消</el-button>
      <el-button :size="ELsize" type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  shopProductPage,
} from "@/api/goods.js";
export default {
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shelvedStatus: true,
        auditStatus: 10,
        productPaymentMode: 0,
        productUseType: 0,
      },
      daterangeCreateTime: [],
      innerVisible: false,
      temGoods: [],
      selGoods: [],
    }
  },
  methods: {
    open(selGoods) {
      this.selGoods = selGoods;
      this.innerVisible = true;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    close() {
      this.$refs.goodsTab.clearSelection()
      this.innerVisible = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    selected(row, index) {
      let bool = this.selGoods.some((k) => {
        return k.productId == row.productId
      })
      return !bool
    },
    getList() {
      this.loading = true;
      this.queryParams["startTime"] = undefined;
      this.queryParams["endTime"] = undefined;
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams["startTime"] = this.daterangeCreateTime[0];
        this.queryParams["endTime"] = this.daterangeCreateTime[1];
      }
      shopProductPage(this.queryParams).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.$nextTick(() => {
          for (let i = 0; i < this.list.length; i++) {
            for (let j = 0; j < this.selGoods.length; j++) {
              if (this.list[i].productId === this.selGoods[j].productId) {
                this.$refs.goodsTab.toggleRowSelection(this.list[i], true)
                break
              }
            }
          }
        })
      }).finally(() => {
        this.loading = false;
      });
    },
    // 选择商品
    handleSelectionChange(e) {
      this.temGoods = []
      e.forEach((v) => {
        let bool = this.selGoods.some((k) => {
          return k.productId == v.productId
        })
        if (!bool) {
          this.temGoods.push(v)
        }
      })
    },
    // 选择店铺 确定
    confirm() {
      let arr = JSON.parse(JSON.stringify(this.temGoods))
      this.selGoods.push(...arr)
      // this.form.adaptShopIds = this.selGoods.map((v) => {
      //   return v.productId
      // })
      this.$emit("result", this.selGoods)
      this.innerVisible = false
    },
  },
}
</script>

<style lang='scss' scoped></style>