<template>
  <a-spin :spinning="spinning" tip="Loading...">
    <div class="productInfoList-wrap">
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.no"
        rowKeyName="no"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        :scroll="{ y: tableHeight-120 }"
        :defaultLoadData="false"
        bordered>
        <template slot="footer">
          <a-row>
            <a-col span="24">
              <a-row>
                <a-col span="3">实时数量（非促）：{{ (totalData && (totalData.notGiftQty || totalData.notGiftQty==0)) ? totalData.notGiftQty : '--' }}</a-col>
                <a-col span="3">实时数量（促）：{{ (totalData && (totalData.giftQty || totalData.giftQty==0)) ? toThousands(totalData.giftQty) : '--' }}</a-col>
                <a-col span="3">实时实售金额：{{ (totalData && (totalData.totalRealAmount || totalData.totalRealAmount==0)) ?totalData.totalRealAmount : '--' }}</a-col>
                <a-col span="3">实时开单金额：{{ (totalData && (totalData.totalAmount || totalData.totalAmount==0)) ? toThousands(totalData.totalAmount) : '--' }}</a-col>
                <a-col span="3">实时成本金额：{{ (totalData && (totalData.totalRealCost || totalData.totalRealCost==0)) ? totalData.totalRealCost : '--' }}</a-col>
                <a-col span="4">实时损失费用（成本）：{{ (totalData && (totalData.lossCost || totalData.lossCost==0)) ? toThousands(totalData.lossCost) : '--' }}</a-col>
                <a-col span="5">实时损失费用（开单）：{{ (totalData && (totalData.lossAmount || totalData.lossAmount==0)) ? toThousands(totalData.lossAmount) : '--' }}</a-col>
              </a-row>
            </a-col>
          </a-row>
        </template>
      </s-table>
    </div>
  </a-spin>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { salesPromoRealReportList, salesPromoRealReportCount } from '@/api/reportData'
import { STable, VSelect } from '@/components'
export default {
  name: 'QueryPart',
  mixins: [commonMixin],
  components: { STable, VSelect },
  props: {
    queryParam: {
      type: Object,
      default: function () {
        return {}
      }
    },
    tableHeight: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      spinning: false,
      newQueryParam: {}, // 查询条件
      totalData: null, //  合计
      columns: [
        { title: '促销名称', dataIndex: 'promotion.title', width: '160px', align: 'center', customRender: function (text) { return text || '--' }, fixed: 'left', ellipsis: true },
        { title: '促销时间', scopedSlots: { customRender: 'promotionTime' }, width: '100px', align: 'center', fixed: 'left' },
        { title: '费用所属部门', dataIndex: 'departmentName', width: '180px', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true, fixed: 'left' },
        { title: '促销类型', dataIndex: 'promotionRule.promotionRuleTypeDictValue', width: '80px', align: 'center', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '规则', dataIndex: 'description', width: '80px', align: 'center', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '销售单号', dataIndex: 'salesBillNo', width: '80px', align: 'center', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: '80px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: '80px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '地区', dataIndex: 'provinceName', width: '120px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'buyerName', width: '100px', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户级别', dataIndex: 'buyerLevelDictValue', width: '80px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售审核日期', dataIndex: 'auditDate', width: '120px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '实时数量(非促)', dataIndex: 'notGiftQty', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '实时数量(促)', dataIndex: 'giftQty', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '实时实售金额', dataIndex: 'totalRealAmount', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '实时开单金额', dataIndex: 'totalAmount', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '实时成本金额', dataIndex: 'totalRealCost', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '实时损失成本', dataIndex: 'lossCost', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '实时损失费用', dataIndex: 'lossAmount', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        const params = Object.assign(parameter, this.newQueryParam)
        return salesPromoRealReportList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.length; i++) {
              data[i].no = i + 1
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 统计
    getCount (params) {
      salesPromoRealReportCount(params).then(res => {
        if (res.status == 200) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    // 刷新当前页面
    resetCurForm () {
      const params = JSON.parse(JSON.stringify(this.queryParam))
      delete params.salesDate
      this.newQueryParam = params
      this.$nextTick(() => {
        this.getCount(this.newQueryParam)
        this.$refs.table.refresh()
      })
    }
  }
}
</script>
<style lang="less" scoped>
  .productInfoList-wrap{
    .ellipsisCon{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .ellipsisText{
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        display: box;
        display: -webkit-box;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
      }
    }
  }
</style>
