<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入商品编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="关键词" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="请输入关键词" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品品牌" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入商品品牌" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品状态" prop="state">
        <el-input v-model="queryParams.state" placeholder="请输入商品状态" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品类型" prop="category">
        <el-input v-model="queryParams.category" placeholder="请输入商品类型" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="上架时间" prop="upTime">
        <el-date-picker v-model="queryParams.upTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </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">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['supplier:goods-detail:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['supplier:goods-detail:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-row :gutter="10" class="mb8">

    </el-row>
    <!-- 列表 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="4">
        <span>商品分类</span>
        <el-tree :data="categoryOptions" :props="defaultProps" :expand-on-click-node="false"
          :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick"
          style="overflow: auto;" />
      </el-col>
      <el-col :span="20">
        <el-table v-loading="loading" :data="list">
          <el-table-column label="序号" align="center" prop="id" />
          <el-table-column label="商品id" align="center" prop="goodsId" />
          <el-table-column label="商品编号" align="center" prop="code" />
          <el-table-column label="商品名称" align="center" prop="name" />
          <el-table-column label="关键词" align="center" prop="keyword" />
          <el-table-column label="单位" align="center" prop="unit" />
          <el-table-column label="市场价" align="center" prop="marketPrice" />
          <el-table-column label="平台价" align="center" prop="platPrice" />
          <el-table-column label="商品品牌" align="center" prop="brand" />
          <el-table-column label="库存" align="center" prop="num" />
          <el-table-column label="商品状态" align="center" prop="state" />
          <el-table-column label="商品类型" align="center" prop="category" />
          <el-table-column label="销售数量" align="center" prop="saleNum" />
          <el-table-column label="付款策略" align="center" prop="payMethod" />
          <el-table-column label="售后策略" align="center" prop="aftersaleStrategy" />
          <el-table-column label="价格策略" align="center" prop="priceStrategy" />
          <el-table-column label="上架方式" align="center" prop="upMethod" />
          <el-table-column label="上架数量" align="center" prop="upNum" />
          <el-table-column label="上架时间" align="center" prop="upTime" width="180">
            <template v-slot="scope">
              <span>{{ parseTime(scope.row.upTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否置顶" align="center" prop="sticky" />
          <el-table-column label="商品详情" align="center" prop="productDetail" />
          <el-table-column label="商品封面" align="center" prop="productPhoto" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template v-slot="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['supplier:goods-detail:update']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['supplier:goods-detail:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>


    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键词" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="市场价" prop="marketPrice">
          <el-input v-model="form.marketPrice" placeholder="请输入市场价" />
        </el-form-item>
        <el-form-item label="平台价" prop="platPrice">
          <el-input v-model="form.platPrice" placeholder="请输入平台价" />
        </el-form-item>
        <el-form-item label="商品品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入商品品牌" />
        </el-form-item>
        <el-form-item label="库存" prop="num">
          <el-input v-model="form.num" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="商品状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入商品状态" />
        </el-form-item>
        <el-form-item label="商品类型" prop="category">
          <el-input v-model="form.category" placeholder="请输入商品类型" />
        </el-form-item>
        <el-form-item label="销售数量" prop="saleNum">
          <el-input v-model="form.saleNum" placeholder="请输入销售数量" />
        </el-form-item>
        <el-form-item label="付款策略" prop="payMethod">
          <el-input v-model="form.payMethod" placeholder="请输入付款策略" />
        </el-form-item>
        <el-form-item label="售后策略" prop="aftersaleStrategy">
          <el-input v-model="form.aftersaleStrategy" placeholder="请输入售后策略" />
        </el-form-item>
        <el-form-item label="价格策略" prop="priceStrategy">
          <el-input v-model="form.priceStrategy" placeholder="请输入价格策略" />
        </el-form-item>
        <el-form-item label="上架方式" prop="upMethod">
          <el-input v-model="form.upMethod" placeholder="请输入上架方式" />
        </el-form-item>
        <el-form-item label="上架数量" prop="upNum">
          <el-input v-model="form.upNum" placeholder="请输入上架数量" />
        </el-form-item>
        <el-form-item label="上架时间" prop="upTime">
          <el-date-picker clearable v-model="form.upTime" type="date" value-format="timestamp" placeholder="选择上架时间" />
        </el-form-item>
        <el-form-item label="是否置顶" prop="sticky">
          <el-input v-model="form.sticky" placeholder="请输入是否置顶" />
        </el-form-item>
        <el-form-item label="商品详情" prop="productDetail">
          <el-input v-model="form.productDetail" placeholder="请输入商品详情" />
        </el-form-item>
        <el-form-item label="商品封面" prop="productPhoto">
          <el-input v-model="form.productPhoto" placeholder="请输入商品封面" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createGoodsDetail,
  updateGoodsDetail,
  deleteGoodsDetail,
  getGoodsDetail,
  getGoodsDetailPage,
  exportGoodsDetailExcel
} from "@/api/supplier/goodsDetail";
import { getGoodsCategoryListAll, } from "@/api/supplier/goodsCategory";
import Treeselect from '@riophae/vue-treeselect'

export default {
  name: "GoodsDetail",
  components: {
    Treeselect,
  },
  data() {
    return {
      categoryOptions: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品详情列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        goodsId: null,
        code: null,
        name: null,
        keyword: null,
        brand: null,
        state: null,
        category: null,
        upTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    getGoodsCategoryListAll().then(response => {
      this.categoryOptions = this.handleTree(response.data, "id");
    });
  },
  methods: {
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    handleNodeClick(data) {
      this.reset();
      this.queryParams.category = data.id
      this.getList()
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getGoodsDetailPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        goodsId: undefined,
        code: undefined,
        name: undefined,
        keyword: undefined,
        unit: undefined,
        marketPrice: undefined,
        platPrice: undefined,
        brand: undefined,
        num: undefined,
        state: undefined,
        category: undefined,
        saleNum: undefined,
        payMethod: undefined,
        aftersaleStrategy: undefined,
        priceStrategy: undefined,
        upMethod: undefined,
        upNum: undefined,
        upTime: undefined,
        sticky: undefined,
        productDetail: undefined,
        productPhoto: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getGoodsDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateGoodsDetail(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createGoodsDetail(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除商品详情编号为"' + id + '"的数据项?').then(function () {
        return deleteGoodsDetail(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有商品详情数据项?').then(() => {
        this.exportLoading = true;
        return exportGoodsDetailExcel(params);
      }).then(response => {
        this.$download.excel(response, '商品详情.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>
