<template>
  <div class="userd-baseInfo">
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form-model layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="货架名称" prop="shelfSn">
                  <shelfSList v-model="queryParam.shelfSn"></shelfSList>
                </a-form-model-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="下单时间">
                  <rangeDate ref="orderDate" :value="orderTime" @change="orderDateChange" />
                </a-form-item>
              </a-col>
              <!-- <a-col :md="6" :sm="24">
                <a-form-item label="货架取货数量">
                  <a-input-group compact>
                    <a-input-number
                      :precision="0"
                      style=" width: 42%; text-align: center"
                      v-model.trim="queryParam.qhslMin"
                      allowClear
                      placeholder="请输入最小值"/>
                    <a-input
                      style=" width: 16%; pointer-events: none; backgroundColor: #fff"
                      placeholder="~"
                      disabled
                    />
                    <a-input-number
                      :precision="0"
                      :min="queryParam.qhslMin"
                      style=" width: 42%; text-align: center"
                      v-model.trim="queryParam.qhslMax"
                      allowClear
                      placeholder="请输入最大值"/>
                  </a-input-group>
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-item label="销售数量">
                    <a-input-group compact>
                      <a-input-number
                        :precision="0"
                        style=" width: 42%; text-align: center"
                        v-model.trim="queryParam.xsslMin"
                        allowClear
                        placeholder="请输入最小值"/>
                      <a-input
                        style=" width: 16%; pointer-events: none; backgroundColor: #fff"
                        placeholder="~"
                        disabled
                      />
                      <a-input-number
                        :precision="0"
                        :min="queryParam.xsslMin"
                        style=" width: 42%; text-align: center"
                        v-model.trim="queryParam.xsslMax"
                        allowClear
                        placeholder="请输入最大值"/>
                    </a-input-group>
                  </a-form-item>
                </a-col>
              </template> -->
              <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
                <a-button type="primary" @click="$refs.table.refresh(true)" id="userd-refresh">查询</a-button>
                <a-button type="" @click="reset" style="margin-left: 10px;" id="userd-reset">重置</a-button>
                <a-button
                  type="primary"
                  @click="handleExport"
                  :loading="exportLoading"
                  class="button-warning"
                  style="margin-left: 10px;"
                  id="vinAnaly-export-btn">导出</a-button>
                <!-- <a @click="advanced=!advanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </a-col>
            </a-row>
            </a-form>
          </a-form-model></div>
        <div class="table-operator" v-if="statisticsObj">
          <a-alert type="info">
            <div slot="message" class="total-bar">
              <div>
                货架数量：<strong>{{ statisticsObj.shelfCount }}</strong>；
                铺货数量合计：<strong>{{ statisticsObj.totalShelfMaxQty }}</strong>；
                铺货金额合计：<strong>{{ statisticsObj&&(statisticsObj.totalShelfMaxCost||statisticsObj.totalShelfMaxCost==0)?toThousands(statisticsObj.totalShelfMaxCost,2):'--' }}</strong>；
                货架取货数量合计：<strong>{{ statisticsObj.totalOrderQty }}</strong>；
                货架取货金额合计：<strong>{{ statisticsObj&&(statisticsObj.totalOrderAmount||statisticsObj.totalOrderAmount==0)?toThousands(statisticsObj.totalOrderAmount,2):'--' }}</strong>；
              </div>
              <div>
                急送数量合计：<strong>{{ statisticsObj.tempOrderQty }}</strong>；
                急送金额合计：<strong>{{ statisticsObj&&(statisticsObj.tempOrderAmount||statisticsObj.tempOrderAmount==0)?toThousands(statisticsObj.tempOrderAmount,2):'--' }}</strong>；
                销售数量合计：<strong>{{ statisticsObj.saleOrderQty }}</strong>；
                销售金额合计：<strong>{{ statisticsObj&&(statisticsObj.saleOrderAmount||statisticsObj.saleOrderAmount==0)?toThousands(statisticsObj.saleOrderAmount,2) :'--' }}</strong>；
              </div>
            </div>
          </a-alert>
        </div>
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+84.5+'px', wordBreak: 'break-all' }"
          size="small"
          :rowKey="(record)=>record.shelfSn"
          rowKeyName="shelfSn"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <span slot="customTitle">
            <a-popover>
              <template slot="content">
                货架铺货情况为实时数据，<br>与查询条件查询中的下单时间无关
              </template>
              <a-icon type="question-circle" theme="twoTone" />
            </a-popover>
            货架铺货情况
          </span>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import shelfSList from '@/views/common/shelfList'
import { toThousands } from '@/libs/tools.js'
import { downloadExcel } from '@/libs/JGPrint.js'
import { queryShelfOrderReportPage, queryShelfOrderTotalReport, exportShelfOrderReport } from '@/api/shelf'
export default {
  name: 'UserList',
  components: { STable, VSelect, rangeDate, shelfSList },
  data () {
    return {
      toThousands,
      spinning: false,
      // advanced: false,
      tableHeight: 0,
      time: [],
      orderTime: [],
      // 查询参数
      queryParam: {
        shelfSn: undefined,
        beginDate: '',
        endDate: ''
        // qhslMin: undefined,
        // qhslMax: undefined,
        // xsslMin: undefined,
        // xsslMax: undefined
      },
      exportLoading: false,
      showModal: false,
      itemData: null, // 编辑行数据
      openStatusModal: false, // 货架状态弹窗初始状态
      statisticsObj: null,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '货架名称', dataIndex: 'shelfName', width: '30%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        {
          slots: { title: 'customTitle' },
          children: [
            { title: '铺货数量', dataIndex: 'totalShelfMaxQty', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
            { title: '铺货金额', dataIndex: 'totalShelfMaxCost', width: '8%', align: 'right', customRender: function (text) { return (text || text == 0) ? toThousands(text, 2) : '--' } }
          ]
        },
        {
          title: '货架订单',
          children: [
            { title: '取货数量', dataIndex: 'totalOrderQty', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
            { title: '取货金额', dataIndex: 'totalOrderAmount', width: '8%', align: 'right', customRender: function (text) { return (text || text == 0) ? toThousands(text, 2) : '--' } }
          ]
        },
        {
          title: '急送订单',
          children: [
            { title: '急送数量', dataIndex: 'tempOrderQty', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
            { title: '急送金额', dataIndex: 'tempOrderAmount', width: '8%', align: 'right', customRender: function (text) { return (text || text == 0) ? toThousands(text, 2) : '--' } }
          ]
        },
        {
          title: '销售单',
          children: [
            { title: '销售数量', dataIndex: 'saleOrderQty', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
            { title: '销售金额', dataIndex: 'saleOrderAmount', width: '8%', align: 'right', customRender: function (text) { return (text || text == 0) ? toThousands(text, 2) : '--' } }
          ]
        }
      ],
      rules: {
        'shelfSn': [{ required: true, message: '请选择货架', trigger: 'change' }]
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return queryShelfOrderReportPage(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (let i = 0; i < data.list.length; i++) {
              const _item = data.list[i]
              _item.no = no + i + 1
            }
          }
          this.getStatistics(this.queryParam)
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    handleBack () {
      this.$router.go(-1)
    },
    //  下单时间  change
    orderDateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 重置
    reset () {
      this.queryParam = {
        shelfSn: undefined,
        beginDate: '',
        endDate: ''
        // qhslMin: undefined,
        // qhslMax: undefined,
        // xsslMin: undefined,
        // xsslMax: undefined
      }
      this.$refs.orderDate.resetDate()
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 250
    },
    getStatistics (ajaxData) { // 统计
      queryShelfOrderTotalReport(ajaxData).then(res => {
        if (res.status == 200) {
          this.statisticsObj = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 导出
    handleExport () {
      const _this = this
      const params = _this.queryParam
      _this.exportLoading = true
      _this.spinning = true
      exportShelfOrderReport(params).then(res => {
        downloadExcel(res, '客户分析表')
        _this.exportLoading = false
        _this.spinning = false
      })
    }
  },
  watch: {
    // advanced (newValue, oldValue) {
    //   const _this = this
    //   this.$nextTick(() => { // 页面渲染完成后的回调
    //     _this.setTableH()
    //   })
    // },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.reset()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.reset()
    }
  }
}
</script>
<style lang="less">
  .userd-baseInfo{
    .userd-back{
      margin-bottom: 6px;
    }
  }
  .ant-select{
    max-width: 300px !important;
  }
</style>
