<template>
  <div class="app-container">
    <!--    商品销售统计-->
    <!--    搜索框-->
    <div class="card-change">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <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="cvsGoodId">
          <el-select
            v-model="queryParams.cvsGoodId"
            placeholder="请选择品类名称"
            clearable
          >
            <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="">
          <el-date-picker
            v-model="beginTime"
            style="width: 160px"
            type="date"
            placeholder="销售开始日期">
          </el-date-picker>
          至
          <el-date-picker
            v-model="endTime"
            style="width: 160px"
            type="date"
            placeholder="销售结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="float: right;margin-right: 0px">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="dialogVisible = true">导出</el-button>
        </el-form-item>
      </el-form>

      <div style="height: 71vh;overflow: auto">
        <el-table ref="tables" v-loading="loading" :data="list" border
                  @selection-change="handleSelectionChange" :default-sort="defaultSort"
                  @sort-change="handleSortChange">
          <el-table-column type="index" width="60" align="center" label="序号"/>
          <el-table-column label="商品名称" align="center" prop="name"/>
          <el-table-column label="品类名称" align="center" prop="pinLeiName"/>
          <el-table-column label="条形码" align="center" prop="goodsNo"/>
          <el-table-column label="拼音码" align="center" prop="pinyinCode"/>
          <el-table-column label="商品规格" align="center" prop="goodsSpecs"/>
          <el-table-column label="单位" align="center" prop="unit"/>
          <el-table-column label="销售数量" align="center" prop="saleNumber"/>
          <el-table-column label="销售总额" align="center" prop="saleTotal"/>
          <el-table-column label="成本总金额" align="center" prop="costTotal"/>
          <el-table-column label="盈利总额" align="center" prop="profitTotal"/>
          <el-table-column label="统计周期" align="center">
            <template slot-scope="scope">
              <div>{{ beginTime ? parseTime(beginTime) + '~' : '--' }}{{ parseTime(endTime) || '' }}</div>
            </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"
                @click="detail(scope.row)"
              >销售记录
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-drawer
      title="销售记录"
      :visible.sync="table"
      direction="rtl"
      size="60%">
      <div style="width: 96%;margin: 0 auto">
        <div style="display: flex;margin-bottom: 20px">
          <div>商品名称：{{goodsInfo.name || 0}}</div>
          <div style="margin: 0 50px">品类名称：{{goodsInfo.pinLeiName || 0}}</div>
          <div>销售总额：{{goodsInfo.saleTotal || 0}}</div>
          <div style="margin: 0 50px">统计周期：{{ beginTime ? parseTime(beginTime) + '~' : '--' }}{{ parseTime(endTime) || '' }}</div>
        </div>
        <div style="height: 78vh;overflow: auto">
          <el-table ref="tables" border
                    v-loading="loading"
                    :data="detailList">
            <el-table-column label="序号" align="center" type="index"></el-table-column>
            <el-table-column label="商品名称" align="center" prop="name"/>
            <el-table-column label="销售数量" align="center" prop="saleNumber"/>
            <el-table-column label="零售价" align="center" prop="retailPrice"/>
            <el-table-column label="会员价" align="center" prop="memberPrice"/>
            <el-table-column label="成本价" align="center" prop="buyingPrice"/>
            <el-table-column label="盈利金额" align="center" prop="profitTotal"/>
            <el-table-column label="销售总额" align="center" prop="saleTotal"/>
            <el-table-column label="成本总额" align="center" prop="costTotal"/>
            <el-table-column label="订单完成时间" align="center" prop="createTime" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <pagination
          v-show="total1>0"
          :total="total1"
          :page.sync="queryParam.page"
          :limit.sync="queryParam.pageSize"
          @pagination="getDetailList"
        />
      </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="exportSale">
          确 定
          <!--        <a href="/dev-api/business/convenience/stockStatistic/export">确 定</a>-->
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {exportSale, listSale} from "@/api/convenienceStore/salestatistic";
import {addSaleDetail, listSaleDetail} from "@/api/convenienceStore/saledetail";
import {list} from "@/api/convenienceStore/goods";
import {listSuppliers} from "@/api/convenienceStore/supplier";

export default {
  name: 'goodsSale',
  data() {
    return {
      table: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        id: '',
        name: '',
        pinyinCode: '',
        goodsNo: '',
      },
      // 查询参数
      queryParam: {
        page: 1,
        pageSize: 10,
        id: '',
        saleId: '',
      },
      // 商品分类列表
      cvsGoodList: [],
      // 日期范围
      dateRange: [],
      beginTime: "",
      endTime: "",
      dialogVisible: false,
      // 遮罩层
      loading: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 总条数
      total: 0,
      total1: 0,
      // 表格数据
      list: [],
      detailList: [],
      goodsInfo:{}
    }
  },
  created() {
    this.getList()
    this.getQueryList()
  },
  methods: {
    // 查询搜索列表信息
    getQueryList() {
      list().then(response => {
        this.cvsGoodList = response.data
      });
    },
    // 导出信息
    exportSale() {
      this.dateRange = []
      if (this.beginTime && this.endTime) {
        this.dateRange.push(this.beginTime.toLocaleDateString())
        this.dateRange.push(this.endTime.toLocaleDateString())
      }
      exportSale(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        const blob = new Blob([response], {type: 'application/json'})
        this.$download.saveAs(blob, '商品销售统计.xlsx')
      })
      this.dialogVisible = false
    },
    // 弹出框
    detail(data) {
      this.table = true;
      this.queryParam.saleId = data.id;
      this.dateRange = []
      if (this.beginTime && this.endTime) {
        this.dateRange.push(this.beginTime.toLocaleDateString())
        this.dateRange.push(this.endTime.toLocaleDateString())
      }
      this.goodsInfo = data
      listSaleDetail(this.addDateRange(this.queryParam, this.dateRange)).then(response => {
        this.detailList = response.data.records;
        this.total1 = response.data.total;
        this.loading = false;
      })
    },
    // 获取销售统计列表
    getList() {
      this.loading = true;
      this.dateRange = []
      if (this.beginTime && this.endTime) {
        this.dateRange.push(this.beginTime.toLocaleDateString())
        this.dateRange.push(this.endTime.toLocaleDateString())
      }
      listSale(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 获取销售商品详情信息
    getDetailList() {

    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.dateRange = [];
      this.beginTime = ""
      this.endTime = ""
      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 lang="scss" scoped>
.app-container {
  width: 100%;
  height: 100%;
  background: #f6f8f9;
}

.el-form--inline .el-form-item {
  margin-right: 18px;
}

</style>
