<template>
  <div class="app-container">
    <!--    搜索框-->
    <el-card >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="商品分类" prop="cvsGoodId">
          <el-cascader v-model="cvsGoodId"
                       :options="cvsGoodOptions"
                       :props="{ checkStrictly: true }"
                       clearable></el-cascader>
<!--          <el-select-->
<!--            v-model="queryParams.cvsGoodId"-->
<!--            placeholder="全部"-->
<!--            clearable-->
<!--            style="width: 300px"-->
<!--          >-->
<!--            <el-option v-for="item in cvsGoodList" :key="item.id" :label="item.name" :value="item.id"/>-->
<!--          </el-select>-->
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select
            v-model="queryParams.supplierId"
            placeholder="全部"
            clearable
            style="width: 300px"
          >
            <el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入商品名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="拼音码" prop="pinyinCode">
          <el-input
            v-model="queryParams.pinyinCode"
            placeholder="请输入拼音码"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="条形码" prop="goodsNo">
          <el-input
            v-model="queryParams.goodsNo"
            placeholder="请输入商品条形码"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="货架号" prop="shelfNumber">
          <el-input
            v-model="queryParams.shelfNumber"
            placeholder="请输入商品所在货架号"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="等级状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="全部"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </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-card>

    <!--     列表-->
    <el-card style="margin-top: 20px" >
      <div style="display: flex;justify-content: space-between">
        <div style="font-size: 18px">列表</div>
        <el-button type="primary" @click="dialogVisible = true">导出</el-button>
      </div>

      <el-table ref="tables" v-loading="loading" :data="list"
                @selection-change="handleSelectionChange" :default-sort="defaultSort"
                @sort-change="handleSortChange">
        <el-table-column label="ID" align="center" prop="id"/>
        <el-table-column label="供应商" align="center" prop="supplierId">
          <template slot-scope="scope">
            <span>{{ getName(supplierList, scope.row.supplierId) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分类名称" align="center" prop="cvsGoodId">
          <template slot-scope="scope">
            <span>{{ getName(cvsGoodList, scope.row.cvsGoodId) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="name" width="160" />
        <el-table-column label="条码" align="center" prop="goodsNo" width="160" />
        <el-table-column label="货架号" align="center" prop="shelfNumber" />
        <el-table-column label="零售价" align="center" prop="retailPrice" />
        <el-table-column label="单位" align="center" prop="unit" />
        <el-table-column label="当前库存" align="center" prop="stock" />
        <el-table-column label="当前成本价" align="center" prop="buyingPrice" />
        <el-table-column label="成本金额" align="center" prop="costAmount" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status=='qy'">启用</el-tag>
            <el-tag type="info" v-if="scope.row.status=='jy'">禁用</el-tag>
<!--            <dict-tag :options="dict.type.zhzt" :value="scope.row.status"/>-->
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="200">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="trace(scope.row)"
            >库存跟踪</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>


    <el-drawer
      title="库存跟踪"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
      :before-close="handleClose">
      <div style="display: grid; place-items: center;">
        <el-card class="box-card" style="background-color: #f5f5f5">
          <div slot="header" class="clearfix">
            <div style="position: relative;">
              <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
                <h2>{{form.name}}</h2>
              </div>
            </div>
            <br>
            <div style="display: flex; justify-content: space-between;">
              <span>拼音码：</span>
              <span>{{  form.pinyinCode }}</span>
            </div>
          </div>
          <div >
            <div  style="margin-top:-5px;display: flex; justify-content: space-between;">
              <h3>库存数量</h3>
              <h3>{{form.stock}}{{form.unit}}</h3>
            </div>
            <div style="display: flex; justify-content: space-between;">
              <span>更新时间</span>
              <span>{{ form.updateTime }}</span>
            </div>
          </div>
        </el-card>
        <el-card class="box-card" style="margin-top: 15px">
          <el-table ref="tables"
                    v-loading="loading"
                    :data="trackList"
                    :default-sort="defaultSort">
            <el-table-column
              lable="#"
              type="index"
            >
            </el-table-column>
            <el-table-column label="库存更新时间" align="center" prop="createTime" width="200">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="单据" align="center" prop="document" />
            <el-table-column label="单号" align="center" prop="oddNumber" />
            <el-table-column label="变动数量" align="center" prop="changeNumber" />
          </el-table>
          <pagination
            v-show="total1>0"
            :total="total1"
            :page.sync="queryParam.page"
            :limit.sync="queryParam.pageSize"
            @pagination="getTrackList"
          />
        </el-card>
      </div>
    </el-drawer>

    <el-dialog
      title="导出状态"
      :visible.sync="dialogVisible"
      width="20%"
      style="margin-top: 200px"
      :before-close="handleClose">
      <span>导出文档已生成,请点击下载按钮进行下载</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="exportStock">
          确 定
<!--        <a href="/dev-api/business/convenience/stockStatistic/export">确 定</a>-->
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {cvsGoodsTree, selectTree} from "@/api/convenienceStore/goods";
import {listSupplier} from "@/api/convenienceStore/supplier";
import {exportStocks, getStock, listStock} from "@/api/convenienceStore/stockstatistic";
import {listTrack} from "@/api/convenienceStore/stocktrack";

export default {
  name: "oilStatistics",
  dicts: ['zhzt'],

  data() {
    return {
      // 商品分类id
      cvsGoodId:"",
      cvsGoodOptions:[],
      dialogVisible: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        id: '',
        cvsGoodId: '',
        supplierId: '',
        name: '',
        pinyinCode: '',
        goodsNo: '',
        shelfNumber: '',
        status: '',
      },
      queryParam: {
        page: 1,
        pageSize: 10,
        id: '',
        stockId:'',
      },
      // 商品分类列表
      cvsGoodList:[],
      // 供应商列表
      supplierList:[],
      // 表格数据
      list: [],
      trackList: [],
      // 总条数
      total: 0,
      total1: 0,
      // 遮罩层
      loading: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 弹出层
      drawer: false,
      form:{},
    }
  },
  created() {
    this.getList();
    this.getQueryList();
    this.getTree();
  },
  methods: {
    // 获取分类菜单
    getTree(){
      cvsGoodsTree().then(response => {
        this.cvsGoodOptions = response.data
      })
    },
    // 导出信息
    exportStock(){
      exportStocks(this.queryParams).then(response => {
        const blob = new Blob([response], { type: 'application/json' })
        this.$download.saveAs(blob,'商品-库存统计.xlsx')
      })
      this.dialogVisible = false
    },
    // 获取库存跟踪信息
    getTrackList(){
      this.loading = true;
      listTrack(this.queryParam).then(response => {
        this.trackList = response.data.records;
        this.total1 = response.data.total;
        this.loading = false;
      })
    },
    // 库存跟踪
    trace(data) {
      this.drawer=true
      getStock(data.id).then(response => {
        this.form = response.data
      })
      this.queryParam.stockId = data.id;
      this.getTrackList();
    },
    // 查询列表
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      if (this.cvsGoodId.length==1){
        this.queryParams.cvsGoodId = this.cvsGoodId[0]
      }else {
        this.queryParams.cvsGoodId = this.cvsGoodId[1]
      }
      this.getList();
    },
    // 查询搜索列表信息
    getQueryList(){
      selectTree().then(response => {
        this.cvsGoodList = response.data.records
      });
      listSupplier().then(response => {
        this.supplierList = response.data.records
      })
    },
    // 重置按钮操作
    resetQuery() {
      this.cvsGoodId = ""
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
      this.handleQuery();
    },
    // 是否关闭弹框
    handleClose(done) {
      done();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    // 排序触发事件
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
  }
}
</script>

<style scoped>
  .app-container{
    width: 100%;
    height: 100vh;
    background: #f6f8f9;
  }
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 90%;
  }
</style>
