<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="state">
        <el-select v-model="queryParams.state" placeholder="请选择商品状态" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.GOODS_STATE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-popover placement="bottom" width="300" trigger="click">
          <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="category">
            <el-cascader v-model="queryParams.category" :options="categoryOptions" :show-all-levels="false"
              :props="{ label: 'name', value: 'id', children: 'children', checkStrictly: true }" />
          </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="keyword">
            <el-input v-model="queryParams.keyword" placeholder="请输入关键词" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="商品状态" prop="state">
            <el-select v-model="queryParams.state" placeholder="请选择商品状态" clearable @keyup.enter.native="handleQuery">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.GOODS_STATE)" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </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-button slot="reference" style="margin-right: 10px;">复合查询</el-button>
        </el-popover>
        <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-tabs type="border-card" value="0" @tab-click="handleClick">
          <el-tab-pane label="全部" name="0">
            <el-table v-loading="loading" :data="list" :show-header="false">
              <el-table-column label="商品封面" align="center" prop="cover" width="250">
                <template v-slot="scope">
                  <img :src="scope.row.cover" width="200px" />
                </template>
              </el-table-column>
              <el-table-column label="商品详情" align="center" width="680">
                <template v-slot="scope">
                  <el-descriptions :title="`商品名称：` + scope.row.name">
                    <el-descriptions-item label="商品编号">{{ scope.row.code }}</el-descriptions-item>
                    <el-descriptions-item label="上架数量">{{ scope.row.upNum }}</el-descriptions-item>
                    <el-descriptions-item label="市场价">{{ scope.row.marketPrice }}</el-descriptions-item>
                    <el-descriptions-item label="计量单位">{{ scope.row.unit }}</el-descriptions-item>
                    <el-descriptions-item label="库存数量">{{ scope.row.num }}</el-descriptions-item>
                    <el-descriptions-item label="平台价">{{ scope.row.platPrice }}</el-descriptions-item>
                    <el-descriptions-item label="商品品牌">{{ scope.row.brand }}</el-descriptions-item>
                    <el-descriptions-item label="销售数量">{{ scope.row.saleNum }}</el-descriptions-item>
                    <el-descriptions-item label="商品状态">
                      <dict-tag :type="DICT_TYPE.GOODS_STATE" :value="scope.row.state" />
                    </el-descriptions-item>
                    <el-descriptions-item label="商品类型">
                      <el-tag> {{ getCategoryName(scope.row.category) }}</el-tag>
                    </el-descriptions-item>
                    <el-descriptions-item label="搜索关键词">
                      <el-tag v-for="item in scope.row.keyword"> {{ item }}</el-tag>
                    </el-descriptions-item>
                  </el-descriptions>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template v-slot="scope">
                  <el-button class="tableButton" size="small" type="success"
                    @click="handleDetail(scope.row)">详情</el-button>
                  <el-button class="tableButton" size="small" type="primary"
                    @click="handleUpdate(scope.row)">修改</el-button>
                  <el-button class="tableButton" size="small" type="warning" @click="handleOn(scope.row)">补货</el-button>
                  <el-button class="tableButton" size="small" type="danger"
                    @click="handleDown(scope.row)">下架</el-button>
                  <el-button class="tableButton" size="small" type="info"
                    @click="handleOnLog(scope.row)">补货记录</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-tab-pane>
          <el-tab-pane label="在售中" name="1">
            <el-table v-loading="loading" :data="list" :show-header="false">
              <el-table-column label="商品封面" align="center" prop="cover" width="250">
                <template v-slot="scope">
                  <img :src="scope.row.cover" width="200px" />
                </template>
              </el-table-column>
              <el-table-column label="商品详情" align="center" width="680">
                <template v-slot="scope">
                  <el-descriptions :title="`商品名称：` + scope.row.name">
                    <el-descriptions-item label="商品编号">{{ scope.row.code }}</el-descriptions-item>
                    <el-descriptions-item label="上架数量">{{ scope.row.upNum }}</el-descriptions-item>
                    <el-descriptions-item label="市场价">{{ scope.row.marketPrice }}</el-descriptions-item>
                    <el-descriptions-item label="计量单位">{{ scope.row.unit }}</el-descriptions-item>
                    <el-descriptions-item label="库存数量">{{ scope.row.num }}</el-descriptions-item>
                    <el-descriptions-item label="平台价">{{ scope.row.platPrice }}</el-descriptions-item>
                    <el-descriptions-item label="商品品牌">{{ scope.row.brand }}</el-descriptions-item>
                    <el-descriptions-item label="销售数量">{{ scope.row.saleNum }}</el-descriptions-item>
                    <el-descriptions-item label="商品状态">
                      <dict-tag :type="DICT_TYPE.GOODS_STATE" :value="scope.row.state" />
                    </el-descriptions-item>
                    <el-descriptions-item label="商品类型">
                      <el-tag> {{ getCategoryName(scope.row.category) }}</el-tag>
                    </el-descriptions-item>
                    <el-descriptions-item label="搜索关键词">
                      <el-tag v-for="item in scope.row.keyword"> {{ item }}</el-tag>
                    </el-descriptions-item>
                  </el-descriptions>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template v-slot="scope">
                  <el-button class="tableButton" size="small" type="success"
                    @click="handleDetail(scope.row)">详情</el-button>
                  <el-button class="tableButton" size="small" type="primary"
                    @click="handleUpdate(scope.row)">修改</el-button>
                  <el-button class="tableButton" size="small" type="warning" @click="handleOn(scope.row)">补货</el-button>
                  <el-button class="tableButton" size="small" type="danger"
                    @click="handleDown(scope.row)">下架</el-button>
                  <el-button class="tableButton" size="small" type="info"
                    @click="handleOnLog(scope.row)">上架记录</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-tab-pane>
          <el-tab-pane label="售罄" name="2">
            <el-table v-loading="loading" :data="list" :show-header="false">
              <el-table-column label="商品封面" align="center" prop="cover" width="250">
                <template v-slot="scope">
                  <img :src="scope.row.cover" width="200px" />
                </template>
              </el-table-column>
              <el-table-column label="商品详情" align="center" width="680">
                <template v-slot="scope">
                  <el-descriptions :title="`商品名称：` + scope.row.name">
                    <el-descriptions-item label="商品编号">{{ scope.row.code }}</el-descriptions-item>
                    <el-descriptions-item label="上架数量">{{ scope.row.upNum }}</el-descriptions-item>
                    <el-descriptions-item label="市场价">{{ scope.row.marketPrice }}</el-descriptions-item>
                    <el-descriptions-item label="计量单位">{{ scope.row.unit }}</el-descriptions-item>
                    <el-descriptions-item label="库存数量">{{ scope.row.num }}</el-descriptions-item>
                    <el-descriptions-item label="平台价">{{ scope.row.platPrice }}</el-descriptions-item>
                    <el-descriptions-item label="商品品牌">{{ scope.row.brand }}</el-descriptions-item>
                    <el-descriptions-item label="销售数量">{{ scope.row.saleNum }}</el-descriptions-item>
                    <el-descriptions-item label="商品状态">
                      <dict-tag :type="DICT_TYPE.GOODS_STATE" :value="scope.row.state" />
                    </el-descriptions-item>
                    <el-descriptions-item label="商品类型">
                      <el-tag> {{ getCategoryName(scope.row.category) }}</el-tag>
                    </el-descriptions-item>
                    <el-descriptions-item label="搜索关键词">
                      <el-tag v-for="item in scope.row.keyword"> {{ item }}</el-tag>
                    </el-descriptions-item>
                  </el-descriptions>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template v-slot="scope">
                  <el-button class="tableButton" size="small" type="success"
                    @click="handleDetail(scope.row)">详情</el-button>
                  <el-button class="tableButton" size="small" type="primary"
                    @click="handleUpdate(scope.row)">修改</el-button>
                  <el-button class="tableButton" size="small" type="warning" @click="handleOn(scope.row)">补货</el-button>
                  <el-button class="tableButton" size="small" type="danger"
                    @click="handleDown(scope.row)">下架</el-button>
                  <el-button class="tableButton" size="small" type="info"
                    @click="handleOnLog(scope.row)">上架记录</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-tab-pane>
          <el-tab-pane label="已下架" name="3">
            <el-table v-loading="loading" :data="list" :show-header="false">
              <el-table-column label="商品封面" align="center" prop="cover" width="250">
                <template v-slot="scope">
                  <img :src="scope.row.cover" width="200px" />
                </template>
              </el-table-column>
              <el-table-column label="商品详情" align="center" width="680">
                <template v-slot="scope">
                  <el-descriptions :title="`商品名称：` + scope.row.name">
                    <el-descriptions-item label="商品编号">{{ scope.row.code }}</el-descriptions-item>
                    <el-descriptions-item label="上架数量">{{ scope.row.upNum }}</el-descriptions-item>
                    <el-descriptions-item label="市场价">{{ scope.row.marketPrice }}</el-descriptions-item>
                    <el-descriptions-item label="计量单位">{{ scope.row.unit }}</el-descriptions-item>
                    <el-descriptions-item label="库存数量">{{ scope.row.num }}</el-descriptions-item>
                    <el-descriptions-item label="平台价">{{ scope.row.platPrice }}</el-descriptions-item>
                    <el-descriptions-item label="商品品牌">{{ scope.row.brand }}</el-descriptions-item>
                    <el-descriptions-item label="销售数量">{{ scope.row.saleNum }}</el-descriptions-item>
                    <el-descriptions-item label="商品状态">
                      <dict-tag :type="DICT_TYPE.GOODS_STATE" :value="scope.row.state" />
                    </el-descriptions-item>
                    <el-descriptions-item label="商品类型">
                      <el-tag> {{ getCategoryName(scope.row.category) }}</el-tag>
                    </el-descriptions-item>
                    <el-descriptions-item label="搜索关键词">
                      <el-tag v-for="item in scope.row.keyword"> {{ item }}</el-tag>
                    </el-descriptions-item>
                  </el-descriptions>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template v-slot="scope">
                  <el-button class="tableButton" size="small" type="success"
                    @click="handleDetail(scope.row)">详情</el-button>
                  <el-button class="tableButton" size="small" type="primary"
                    @click="handleUpdate(scope.row)">修改</el-button>
                  <el-button class="tableButton" size="small" type="warning" @click="handleOn(scope.row)">补货</el-button>
                  <el-button class="tableButton" size="small" type="danger"
                    @click="handleDown(scope.row)">下架</el-button>
                  <el-button class="tableButton" size="small" type="info"
                    @click="handleOnLog(scope.row)">上架记录</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-tab-pane>
        </el-tabs>
      </el-col>
    </el-row>


    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="93px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品编号" prop="code">
              <el-input v-model="form.code" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="form.name" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款方式" prop="payMethod">
              <el-select v-model="form.payMethod" placeholder="请选择付款方式" multiple>
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PAY_METHOD)" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="售后策略" prop="aftersaleStrategy">
              <el-select v-model="form.aftersaleStrategy" placeholder="请选择售后策略" clearable>
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.AFTERSALE_STRATEGY)" :key="parseInt(dict.value)"
                  :label="dict.label" :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格策略" prop="priceStrategy">
              <el-select v-model="form.priceStrategy" placeholder="请选择价格策略" clearable>
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PRICE_STRATEGY)" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置顶" prop="sticky">
              <el-select v-model="form.sticky" placeholder="请选择是否置顶" clearable>
                <el-option v-for="dict in [{ label: '否', value: 0 }, { label: '是', value: 1 }]" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 阶梯价格策略 -->
          <el-col :span="24" v-if="form.priceStrategy == 2" style="margin-bottom: 20px;">
            <el-table :data="priceStrategyTableData" style="width: 100%">
              <el-table-column prop="level" align="center" label="阶梯" />
              <el-table-column prop="condition" align="center" label="数量条件" />
              <el-table-column prop="unit" align="center" label="单位" />
              <el-table-column prop="category" align="center" label="策略类型" />
              <el-table-column prop="price" align="center" label="价格/RMB（元）" />
            </el-table>
          </el-col>
          <el-col :span="24">
            <el-form-item label="自定义策略" prop="customStrategy">
              <el-input type="textarea" v-model="form.customStrategy" placeholder="请输入自定义策略" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上架价格" prop="marketPrice">
              <el-input-number v-model="form.marketPrice" :precision="2" :step="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平台价" prop="platPrice">
              <el-input-number v-model="form.platPrice" :precision="2" :step="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上架方式" prop="upMethod">
              <el-select v-model="form.upMethod" placeholder="请选择上架方式" clearable>
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.ON_SOURCE)" :key="parseInt(dict.value)"
                  :label="dict.label" :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上架数量" prop="upNum">
              <el-input-number :precision="0" :min="0" v-model="form.upNum" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="立即上架" prop="upNow">
              <el-select v-model="form.upNow" placeholder="请选择是否立即上架" clearable>
                <el-option v-for="dict in [{ label: '否', value: 0 }, { label: '是', value: 1 }]" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上架时间" prop="upTime" v-if="form.upNow == 0">
              <el-date-picker v-model="form.upTime" type="datetime" placeholder="选择上架时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发货地址" prop="unit" v-if="form.upNow == 0">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退货地址" prop="unit" v-if="form.upNow == 0">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退货联系人" prop="unit" v-if="form.upNow == 0">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商品详情" prop="productDetail">
              <editor v-model="form.productDetail" :min-height="192" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商品封面" prop="cover">
              <imageUpload v-model="form.cover" :limit='1' />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商品图片" prop="cover">
              <img v-for="item in form.picture" :src="item" class="avatar" width="200" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="buttonDisable">确 定</el-button>
        <el-button @click="cancel" :disabled="buttonDisable">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 对话框(补货) -->
    <el-dialog title="商品补货" :visible.sync="openAdd" width="600px" v-dialogDrag append-to-body>
      <el-form ref="formAdd" :model="formAdd" :rules="rulesAdd" label-width="93px">
        <el-form-item label="商品编号" prop="code">
          <el-input v-model="formAdd.code" disabled />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="formAdd.name" disabled />
        </el-form-item>
        <el-form-item label="补货数量" prop="upNum">
          <el-input-number :precision="0" :min="0" v-model="formAdd.upNum" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAdd" :disabled="buttonDisable">确 定</el-button>
        <el-button @click="cancelAdd" :disabled="buttonDisable">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 对话框(补货记录) -->
    <el-dialog title="补货记录" :visible.sync="openAddLog" width="700px" v-dialogDrag append-to-body>
      <el-table v-loading="loadingAddLog" :data="listAddLog">
        <el-table-column label="商品编号" align="center" prop="goodsCode" />
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="补货数量" align="center" prop="upNum" />
        <el-table-column label="补货时间" align="center" prop="createTime">
          <template v-slot="scope">
            {{ parseTime(scope.row.updateTime) }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

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

export default {
  name: "GoodsDetail",
  components: {
    Editor,
    ImageUpload,
    Treeselect,
  },
  data() {
    return {
      buttonDisable: false,
      categoryOptions: [],
      categoryList: [],
      listAddLog: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 遮罩层
      loading: true,
      loadingAddLog: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品详情列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAdd: false,
      openAddLog: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        goodsId: null,
        code: null,
        name: null,
        keyword: null,
        brand: null,
        state: null,
        category: null,
        upTime: [],
      },
      // 表单参数
      form: {},
      formAdd: {},
      // 表单校验
      rules: {
      },
      rulesAdd: {
        upNum: [{ required: true, message: "补货数量不能为空", trigger: "blur" }],
      },
      priceStrategyTableData: [
        { level: '1', condition: '0 ≤ 数量 ≤ 1', unit: '个', category: '固定价格', price: '￥100' },
        { level: '2', condition: '2 ≤ 数量', unit: '个', category: '折扣/固定价格', price: '80%/￥90' },
      ]
    };
  },
  created() {
    this.getList();
    getGoodsCategoryListAll().then(response => {
      this.categoryList = response.data;
      this.categoryOptions = this.handleTree(response.data, "id");
    });
  },
  methods: {
    /** 商品补货 */
    submitFormAdd() {
      this.buttonDisable = true;
      this.$refs["formAdd"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.formAdd.id != null) {
          addGoodsDetailUpNum(this.formAdd).then(response => {
            this.$modal.msgSuccess("补货成功");
            this.openAdd = false;
            this.buttonDisable = false;
            this.getList();
          });
          return;
        }
      })
    },
    /** 取消补货 */
    cancelAdd() {
      this.openAdd = false;
      this.buttonDisable = false;
      this.resetAdd();
    },
    /** tab查询 */
    handleClick(tab, event) {
      if (tab.name == '0') {//全部
        this.queryParams.state = null;
      }
      if (tab.name == '1') {//在售中
        this.queryParams.state = 1;
      }
      if (tab.name == '2') {//售罄
        this.queryParams.state = 2;
      }
      if (tab.name == '3') {//已下架
        this.queryParams.state = 3;
      }
      this.getList();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    handleNodeClick(data) {
      this.reset();
      this.queryParams.category = data.id
      this.getList()
    },
    //获得商品类型名称
    getCategoryName(category) {
      for (const item of this.categoryList) {
        if (item.id === category) {
          return item.name;
        }
      }
      return "未知类型";
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getGoodsDetailPage(this.queryParams).then(response => {
        this.list = response.data.list;
        for (let i = 0; i < this.list.length; i++) {
          if (this.list[i].keyword) {
            this.list[i].keyword = this.list[i].keyword.split(",");
          }
        }
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.buttonDisable = 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,
        cover: undefined,
        onTitle: undefined,
        upNow: undefined,
        customStrategy: undefined,
      };
      this.resetForm("form");
    },
    resetAdd() {
      this.formAdd = {
        code: undefined,
        name: undefined,
        upNum: undefined,
      }
      this.resetForm("formAdd");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加上架记录";
    },
    /** 详情 */
    handleDetail(row) {
      this.$router.push({
        path: "/supplier/goodsDetail/detail",
        query: { id: row.id },
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.buttonDisable = false;
      const id = row.id;
      getGoodsDetail(id).then(response => {
        this.form = response.data;
        if (this.form.payMethod) {
          this.form.payMethod = this.form.payMethod.split(",");
        }
        if (this.form.picture) {
          this.form.picture = this.form.picture.split(",");
        }
        this.open = true;
        this.title = "修改上架记录";
      });
    },
    /** 补货 */
    handleOn(row) {
      this.resetAdd();
      this.formAdd.id = row.id;
      this.formAdd.code = row.code;
      this.formAdd.name = row.name;
      this.openAdd = true;
    },
    /** 下架 */
    handleDown(row) {
      this.$modal.confirm('是否确认下架商品名称为"' + row.name + '",编号为"' + row.code + '"的商品吗?').then(function () {
        return changeGoodsDetailStatus(row.id, 3);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("下架成功");
      }).catch(() => { });
    },
    /** 补货记录 */
    handleOnLog(row) {
      const id = row.id;
      this.loadingAddLog = true;
      getAddAgainLog(id).then(res => {
        this.listAddLog = res.data;
        this.openAddLog = true;
        this.loadingAddLog = false;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.buttonDisable = true;
        if (!valid) {
          this.buttonDisable = false;
          return;
        }
        if (this.form.payMethod != null) {
          let temp = "";
          for (let i = 0; i < this.form.payMethod.length; i++) {
            if (!temp) {
              temp = this.form.payMethod[i];
            } else {
              temp = temp + "," + this.form.payMethod[i];
            }
          }
          this.form.payMethod = temp;
        }
        if (this.form.picture != null) {
          let temp = "";
          for (let i = 0; i < this.form.picture.length; i++) {
            if (!temp) {
              temp = this.form.picture[i];
            } else {
              temp = temp + "," + this.form.picture[i];
            }
          }
          this.form.picture = temp;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateGoodsDetail(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.buttonDisable = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createGoodsDetail(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.buttonDisable = 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>
<style scoped>
.tableButton {
  margin-top: 5px;
  margin-left: 10px;
  width: 88%;
}
</style>
