<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="商品分类" prop="cateId">
         <treeselect style="width:205.4px"  v-model="queryParams.cateId"   :options="categoryOptions" :normalizer="normalizer" placeholder="请选择分类" />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input

          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="productType">
        <el-select  v-model="queryParams.productType" placeholder="请选择商品类型" clearable size="small" >
         <el-option
                v-for="item in productTypeOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="租户" prop="tenantId">
        <tenant-select v-model="queryParams.tenantId" @change="handleQuery" />
      </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-tabs type="card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="出售中" name="1"></el-tab-pane>
      <el-tab-pane label="待上架" name="0"></el-tab-pane>
    </el-tabs>
    <el-table height="500" border  v-loading="loading" :data="storeProductList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="productId" />
      <el-table-column label="商品图片" align="center" width="120">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.imgUrl" width="100">
            <img :src="scope.row.imgUrl" style="max-width: 150px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" show-overflow-tooltip align="center" prop="productName" />
      <el-table-column label="所属公司" align="center" prop="companyName" width="160" show-overflow-tooltip />
      <el-table-column label="分类" align="center" prop="cateName" />
      <el-table-column label="售价" align="center" prop="price" >
          <template slot-scope="scope" >
              <span v-if="scope.row.price!=null">{{scope.row.price.toFixed(2)}}</span>
          </template>
      </el-table-column>
      <el-table-column label="原价" align="center" prop="otPrice" >
          <template slot-scope="scope" >
              <span v-if="scope.row.otPrice!=null">{{scope.row.otPrice.toFixed(2)}}</span>
          </template>
      </el-table-column>
      <el-table-column label="销量" align="center" prop="sales" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="商品类型" align="center" prop="productType" >
        <template slot-scope="scope">
           <el-tag prop="status" v-for="(item, index) in productTypeOptions"    v-if="scope.row.productType==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isShow" >
          <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in isShowOptions"    v-if="scope.row.isShow==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="120px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
            v-hasPermi="['store:storeProduct:query']"
          >查看</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"
    />
    <el-drawer
     size="50%"
      :title="title" :visible.sync="open"
      >
      <storeProductDetails  ref="storeProductDetails" />
    </el-drawer>
  </div>
</template>

<script>
import {  listStoreProduct  } from "@/api/store/storeProduct";
import { getAllStoreProductCategory } from "@/api/store/storeProductCategory";
import storeProductDetails from "../components/storeProductDetails";
import Treeselect from "@riophae/vue-treeselect";
import TenantSelect from '@/components/TenantSelect/index'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "StoreProduct",
  components: {
    Treeselect,
    storeProductDetails,
    TenantSelect
  },
  data() {
    return {
      activeName:"1",
      isShowOptions:[],
      productTypeOptions:[],
      categoryOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      storeProductList: [],
      // 弹出层标题
      title: "商品详情",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productType: null,
        isShow: "1",
        tenantId: null,
      },
    };
  },
  created() {
    this.getDicts("sys_product_type").then(response => {
         this.productTypeOptions = response.data;
       });
    this.getDicts("sys_spec_show").then(response => {
          this.isShowOptions = response.data;
        });
    this.getTreeselect();
    this.getList();
  },
  methods: {
    handleDetails(row){
      this.open=true;
      setTimeout(() => {
           this.$refs.storeProductDetails.getDetails(row.productId,row.cateName,row.storeName);
      }, 1);

    },
    handleClick(tab, event) {
      this.queryParams.isShow=tab.name;
      this.getList();
    },
    /** 转换商品分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.cateId,
        label: node.cateName,
        children: node.children
      };
    },
    getTreeselect() {
      getAllStoreProductCategory().then(response => {
        this.categoryOptions = [];
        const data = this.handleTree(response.data, "cateId", "pid");
        this.categoryOptions=data;
      });
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listStoreProduct(this.$withTenant(this.queryParams)).then(response => {
        this.storeProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
  }
};
</script>
