<template>
  <a-card size="small" :bordered="false" class="customerSalesDetailsReportList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="customerSalesDetailsReportList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          :rules="rules"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          @keyup.enter.native="handleSearch" >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="审核日期" prop="time">
                <rangeDate ref="rangeDate" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户名称">
                <a-input id="customerSalesDetailsReportList-salesTargetName" v-model.trim="queryParam.salesTargetName" allowClear placeholder="请输入客户名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="customerSalesDetailsReportList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="customerSalesDetailsReportList-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_customerSalesDetailsReport_export')"
                id="customerSalesDetailsReportList-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.targetSn"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        bordered>
        <template slot="footer">
          <a-row>
            <a-col span="2">合计：</a-col>
            <a-col span="22">
              <a-row>
                <a-col span="4">滤清器：{{ (totalData && (totalData.lqqAmount || totalData.lqqAmount==0)) ? totalData.lqqAmount : '--' }}</a-col>
                <a-col span="4">雨刮片：{{ (totalData && (totalData.ygpAmount || totalData.ygpAmount==0)) ? totalData.ygpAmount : '--' }}</a-col>
                <a-col span="4">刹车片：{{ (totalData && (totalData.scpAmount || totalData.scpAmount==0)) ? totalData.scpAmount : '--' }}</a-col>
                <a-col span="4">蓄电池：{{ (totalData && (totalData.xdcAmount || totalData.xdcAmount==0)) ? totalData.xdcAmount : '--' }}</a-col>
                <a-col span="4">润滑油：{{ (totalData && (totalData.rhyAmount || totalData.rhyAmount==0)) ? totalData.rhyAmount : '--' }}</a-col>
                <a-col span="4">火花塞：{{ (totalData && (totalData.hhsAmount || totalData.hhsAmount==0)) ? totalData.hhsAmount : '--' }}</a-col>
                <a-col span="4">点火线圈：{{ (totalData && (totalData.dhxqAmount || totalData.dhxqAmount==0)) ? totalData.dhxqAmount : '--' }}</a-col>
                <a-col span="4">LED灯：{{ (totalData && (totalData.ledAmount || totalData.ledAmount==0)) ? totalData.ledAmount : '--' }}</a-col>
                <a-col span="4">其他：{{ (totalData && (totalData.otherAmount || totalData.otherAmount==0)) ? totalData.otherAmount : '--' }}</a-col>
                <a-col span="4">合计：{{ (totalData && (totalData.totalAmount || totalData.totalAmount==0)) ? totalData.totalAmount : '--' }}</a-col>
              </a-row>
            </a-col>
          </a-row>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { downloadExcel } from '@/libs/JGPrint.js'
import { reportCustomerSalesBillDetailList, reportCustomerSalesBillDetailCount, reportCustomerSalesBillDetailExport } from '@/api/reportData'
export default {
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      spinning: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      tableHeight: 0,
      queryParam: { //  查询条件
        salesTargetName: '',
        time: [],
        beginDate: '',
        endDate: ''
      },
      rules: {
        'time': [{ required: true, message: '请选择审核日期', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      columns: [
        { title: '客户名称', dataIndex: 'salesTargetName', width: '16%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '滤清器', dataIndex: 'lqqAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '雨刮片', dataIndex: 'ygpAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '刹车片', dataIndex: 'scpAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '蓄电池', dataIndex: 'xdcAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '润滑油', dataIndex: 'rhyAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '火花塞', dataIndex: 'hhsAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '点火线圈', dataIndex: 'dhxqAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: 'LED灯', dataIndex: 'ledAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '其他', dataIndex: 'otherAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '合计', dataIndex: 'totalAmount', width: '8.4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, this.queryParam)
        this.spinning = true
        delete params.time
        return reportCustomerSalesBillDetailList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 总计
            this.getCount(params)
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      linkageGroupData: [],
      totalData: null // 合计
    }
  },
  methods: {
    //  创建时间  change
    dateChange (date) {
      this.queryParam.time = date
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 查询总计
    getCount (params) {
      reportCustomerSalesBillDetailCount(params).then(res => {
        if (res.status == 200 && res.data) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.salesTargetName = ''
      this.$refs.rangeDate.resetDate()
      this.queryParam.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.totalData = null
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.exportLoading = true
          _this.spinning = true
          reportCustomerSalesBillDetailExport(params).then(res => {
            downloadExcel(res, '客户销售明细报表')
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    },
    pageInit () {}
  },
  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>
