<template>
  <div>
    <a-card size="small" :bordered="false" class="tireSalesReportList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper" ref="tableSearch">
        <a-form-model
          id="tireSalesReportList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          @keyup.enter.native="handleSearch">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="下推时间" prop="time">
                <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="区域/分区">
                <subarea id="allocateBillList-subarea" ref="subarea" @change="subareaChange"></subarea>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="区域负责人">
                <BizUser v-model="queryParam.bizUserSn"></BizUser>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button
                style="margin-left: 5px"
                type="primary"
                @click="handleSearch"
                :disabled="disabled"
                id="salesOrderTotalList-refresh">查询</a-button>
              <a-button
                style="margin-left: 8px"
                @click="resetSearchForm"
                :disabled="disabled"
                id="salesOrderTotalList-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_tireSalesAreaReportExport')"
                id="salesOrderTotalList-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :rowKey="(record) => record.salesBillNo"
          :style="{ height: tableHeight+70+'px' }"
          rowKeyName="salesBillNo"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight-30 }"
          :pageSize="30"
          :defaultLoadData="false"
          bordered>
          <template slot="footer">
            <a-row :gutter="15">
              <a-col :md="4" :sm="24">采购数量：{{ (totalData && (totalData.totalbuyAmount || totalData.totalbuyAmount==0)) ? totalData.totalbuyAmount : '--' }}</a-col>
              <a-col :md="4" :sm="24">库存数量：{{ (totalData && (totalData.totalstoreAmount || totalData.totalsellAmount==0)) ? totalData.totalsellAmount: '--' }}</a-col>
              <a-col :md="4" :sm="24">售出数量：{{ (totalData && (totalData.totalsellAmount || totalData.totalsellAmount==0)) ?totalData.totalsellAmount: '--' }}</a-col>
              <a-col :md="4" :sm="24">已绑质保单数量：{{ (totalData && (totalData.totalwarrantyAmount || totalData.totalwarrantyAmount==0)) ?totalData.totalwarrantyAmount: '--' }}</a-col>
            </a-row>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import getDate from '@/libs/getDate.js'
import { STable } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import subarea from '@/views/common/subarea.js'
import BizUser from '@/views/common/bizUser.js'
import { hdExportExcel } from '@/libs/exportExcel'
import { querySaleAreaCount, excelSaleAreaOnlineExport, querySaleAreaPage } from '@/api/reportData'
export default {
  name: 'TireSalesSubareaList',
  mixins: [commonMixin],
  components: { STable, rangeDate, subarea, BizUser },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      time: [
        getDate.getThreeMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ],
      queryParam: { //  查询条件
        beginDate: getDate.getThreeMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        subareaSn: undefined,
        subareaAreaSn: undefined,
        bizUserSn: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return querySaleAreaPage(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getCount(params)
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      totalData: null,
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '区域', dataIndex: 'subareaName', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaAreaName', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '区域负责人', dataIndex: 'userNameQyfzrs', width: '16%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '采购数量', dataIndex: 'buyAmount', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '库存数量', dataIndex: 'storeAmount', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '售出数量', dataIndex: 'sellAmount', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '已绑质保单数量', dataIndex: 'warrantyAmount', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
    }
  },
  methods: {
    // 统计
    getCount (params) {
      querySaleAreaCount(params).then(res => {
        if (res.status == 200 && res.data) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    handleSearch () {
      this.$refs.table.refresh(true)
    },
    //  下推时间  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.time = date
      } else {
        this.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    subareaChange (val) {
      this.queryParam.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaAreaSn = val[1] ? val[1] : undefined
    },
    //  重置
    resetSearchForm () {
      this.time = [
        getDate.getThreeMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ]
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = getDate.getThreeMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.subareaSn = undefined
      this.queryParam.subareaAreaSn = undefined
      this.queryParam.bizUserSn = undefined
      this.totalData = null
      this.$refs.subarea.clearData()
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      const params = _this.queryParam
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(excelSaleAreaOnlineExport, params, '轮胎销售报表（区域/分区）', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 245
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
