<template>
  <el-dialog title="选择商品" :modal-append-to-body="true" :visible.sync="dialogVisible" width="1400px" :before-close="handleClose">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="商品名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择所属供应商" clearable>
          <el-option v-for="(v,i) in supplierList" :key="i" :label="v.supplierName" :value="v.supplierId"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="上架状态" prop="shelvedStatus">
        <el-select v-model="queryParams.shelvedStatus" placeholder="请选择上架状态" clearable>
          <el-option label="已上架" :value="true"></el-option>
          <el-option label="已下架" :value="false"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" v-if="multiple" />
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品图片" width="100" align="center">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px" :src="scope.row.coverImage" :preview-src-list="scope.row.coverImage ? [scope.row.coverImage] : []"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="商品名称" align="center"></el-table-column>
      <el-table-column prop="supplierId" label="所属供应商" align="center">
        <template slot-scope="scope">
          <div>
            {{scope.row.supplier.supplierName}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="categoryId" label="所属分类" align="center">
        <template slot-scope="scope">
          <div>
            {{scope.row.productCategory.categoryName}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="salePrice" label="价格" align="center"></el-table-column>
      <el-table-column prop="realSales" label="实际销量" align="center"></el-table-column>
      <el-table-column prop="points" label="所需兑换豆" align="center"></el-table-column>
      <el-table-column prop="stock" label="库存" align="center"></el-table-column>
      <el-table-column prop="orderIndex" label="排序" width="80" align="center"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="100px"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="!multiple">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="select(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <div style="text-align: center; margin-top: 20px;" v-if="multiple">
      <el-button style="margin-right: 50px;" @click="handleClose()">取消</el-button>
      <el-button type="primary" :disabled="activeList.length<=0" @click="submit">确认选择</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { productPage, supplierList } from '@/api/pointsMall.js'
export default {
  dicts: ['sys_notice_status'],
  data () {
    return {
      dialogVisible: false,
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // // 非多个禁用
      // multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierId: undefined,
        shelvedStatus: undefined,
        title: undefined
      },
      supplierList: [],
      activeList: []
    }
  },
  props: {
    // 多选
    multiple: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    open () {
      this.dialogVisible = true;
      this.getList();
      this.getSupplierList()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.activeList = selection
    },
    getSupplierList () {
      supplierList().then(res => {
        this.supplierList = res.data
      })
    },
    handleClose (done) {
      this.dialogVisible = false;
    },
    select (row) {
      this.$emit("result", row);
      this.handleClose()
    },
    submit () {
      this.$emit("result", this.activeList)
      this.handleClose()
    },
    /** 查询测试单表列表 */
    getList () {
      this.loading = true;
      this.queryParams.shelvedStatus = true
      productPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>

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