<template>
  <div class="product-select" >
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="药品名称" prop="keyword">
        <el-input
          style="width:200px"
          v-model="queryParams.keyword"
          placeholder="请输入药品名称或首字母"
          clearable
          size="small"
          @input="handleInput"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="list">
      <el-table-column label="药品图片" align="center" width="100">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.image" width="80">
            <img :src="scope.row.image" style="max-width: 80px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="药品名称" show-overflow-tooltip align="center" prop="productName" />
      <el-table-column label="药品类型" align="center" prop="productType" width="100px">
          <template slot-scope="scope">
            <dict-tag :options="productTypeOptions" :value="scope.row.productType"/>
          </template>
        </el-table-column>
      <el-table-column label="商品规格" align="center" prop="sku" />

      <el-table-column label="单价" align="center" prop="price" />

      <el-table-column label="操作" align="center" width="100px" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleSelect(scope.row)"
          >选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      style="padding-bottom:10px;"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getStoreProductList } from "@/api/storeProduct";

function debounce(fn, delay) {
  let timer = null;
  return function (...args) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
}
export default {
  name: "selectProduct",
  data() {
    return {
      loading: true,
      productTypeOptions:[],
      list:[],
      total: 0,
      queryParams: {
        handleQuery:"",
        pageNum: 1,
        pageSize: 10,
        isPackage:0,
        storeId:null,
      }
    };
  },

  created() {
    this.getDicts("sys_product_type").then(response => {
      this.productTypeOptions = response.data;
    });
  },
  methods: {
    handleSelect(row){
      this.$emit('selectProduct',row);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getDetails(storeId){
      this.queryParams.storeId=storeId;
      this.getList();
    },
    getList(){
      this.loading = true;
      getStoreProductList(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    handleInput: debounce(function () {
      this.queryParams.pageNum = 1;
      this.getList();
    }, 500)
  }
};

</script>
<style scoped>

.product-select{
  padding-bottom: 15px;
}
</style>
