<template>
  <a-card>
    <a-spin :spinning="spinning" tip="Loading...">
      <a-tabs v-model="activeKey" type="card" @change="callback">
        <a-tab-pane :key="item.id" :tab="item.name" v-for="item in tabsList">
        </a-tab-pane>
      </a-tabs>

      <div class="table-page-search-wrapper searchBoxNormal">
        <a-form-model
          id="returnGoodsPresentationList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :rules="rules"
          :model="queryParam"
          @keyup.enter.native="handleSearch">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="所在区域">
                <subarea id="returnGoodsPresentationList-subareaSn" ref="subarea" @change="subareaChange"></subarea>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24" v-if="activeKey == 0">
              <a-form-model-item prop="dealerProvinceSn" style="margin: 0;" label="地区">
                <Area id="returnGoodsPresentationList-shippingAddrProvinceSn" v-model="queryParam.dealer.provinceSn" placeholder="请选择省"></Area>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="returnGoodsPresentationList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="returnGoodsPresentationList-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_returnGoodsPresentationExport')"
                id="returnGoodsPresentationList-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :defaultLoadData="false"
        :rowKey="(record) => record.id"
        :columns="columns"
        :showPagination="false"
        :data="loadData"
        :scroll="{ x: 2370 }"
        bordered>
        <template slot="footer">
          <a-row>
            <a-col span="2">合计：</a-col>
            <a-col span="22">
              <a-row>
                <a-col span="4">1月：{{ (totalData && (totalData.jpYgpAmount || totalData.jpYgpAmount==0)) ? totalData.jpYgpAmount : '--' }}</a-col>
                <a-col span="4">2月：{{ (totalData && (totalData.jpLbAmount || totalData.jpLbAmount==0)) ? totalData.jpLbAmount : '--' }}</a-col>
                <a-col span="4">3月：{{ (totalData && (totalData.jpScpAmount || totalData.jpScpAmount==0)) ? totalData.jpScpAmount : '--' }}</a-col>
                <a-col span="4">4月：{{ (totalData && (totalData.jpGyxAmount || totalData.jpGyxAmount==0)) ? totalData.jpGyxAmount : '--' }}</a-col>
                <a-col span="4">5月：{{ (totalData && (totalData.gpYgpAmount || totalData.gpYgpAmount==0)) ? totalData.gpYgpAmount : '--' }}</a-col>
                <a-col span="4">6月：{{ (totalData && (totalData.gpLqqAmount || totalData.gpLqqAmount==0)) ? totalData.gpLqqAmount : '--' }}</a-col>
                <a-col span="4">7月：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">8月：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">9月：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">10月：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">11月：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">12月：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">1-12月费用：{{ (totalData && (totalData.hjAmount || totalData.hjAmount==0)) ? totalData.hjAmount : '--' }}</a-col>
                <a-col span="4">实际销售额：{{ (totalData && (totalData.dlsAmount || totalData.dlsAmount==0)) ? totalData.dlsAmount : '--' }}</a-col>
                <a-col span="4">费用占比(%)：{{ (totalData && (totalData.wsAmount || totalData.wsAmount==0)) ? totalData.wsAmount : '--' }}</a-col>
                <a-col span="4">正常配送品费用：{{ (totalData && (totalData.wsAmount || totalData.wsAmount==0)) ? totalData.wsAmount : '--' }}</a-col>
                <a-col span="4">正常促销品费用：{{ (totalData && (totalData.wsAmount || totalData.wsAmount==0)) ? totalData.wsAmount : '--' }}</a-col>
                <a-col span="4">特殊申请配送品费用：{{ (totalData && (totalData.tbuAmount || totalData.tbuAmount==0)) ? totalData.tbuAmount : '--' }}</a-col>
                <a-col span="4">特殊申请促销品费用：{{ (totalData && (totalData.tbupAmount || totalData.tbupAmount==0)) ? totalData.tbupAmount : '--' }}</a-col>
              </a-row>
            </a-col>
          </a-row>
        </template>
      </s-table>
    </a-spin>
    <!-- 导出提示框 -->
    <reportModal :visible="showExport" @close="showExport=false"></reportModal>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import reportModal from '@/views/common/reportModal.vue'
import Area from '@/views/common/area.js'
import subarea from '@/views/common/subarea.js'
import moment from 'moment'
import reportData from '@/libs/reportData.js'
export default {
  name: 'AllCountryCostReportList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, subarea, Area, moment, reportModal },
  data () {
    return {
      spinning: false,
      tabsList: [{ id: 0, name: '全国客户费用统计' }, { id: 1, name: '区域促销费用统计' }],
      activeKey: 0,
      open: false,
      year: '',
      showExport: false, // 导出弹窗初始值
      queryParam: {
        time: [],
        beginDate: '',
        endDate: '',
        subareaArea:{
          subareaSn: undefined,
          subareaAreaSn: undefined
        },
        dealer: {
          dealerName: '',
          provinceSn: undefined,
          citySn: undefined,
          districtSn: undefined,
          dealerLevel: undefined
        }
      },
      addrProvinceList: [], //  省下拉
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      rules: {
        'time': [{ required: true, message: '请选择月份', trigger: 'change' }]
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const _this = this
        _this.disabled = true
        _this.spinning = true
        return new Promise(function (resolve, reject) {
          const data = {
            list: reportData.nationalCustomerCostStatisticsList
          }
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = i + 1
          }
          _this.spinning = false
          _this.disabled = false
          resolve(data)
        })
      },
      totalData: null
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
        {
          title: '客户名称',
          dataIndex: '',
          align: 'center',
          customRender: function (text) {
            return text || '--'
          },
		  width: 120,
          ellipsis: true
        },
        {
          title: '费用类型',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '1月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '2月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '3月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '4月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '5月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '6月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '7月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '8月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '9月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '10月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '11月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '12月',
          dataIndex: '',
          width: 120,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '1-12月费用合计',
          dataIndex: '',
          width: 140,
          align: 'right',
          customRender: function (text) {
            return ((text || text == 0) ? _this.toThousands(text) : '--')
          }
        },
        {
          title: '实际销售额合计',
          dataIndex: '',
          width: 140,
          align: 'right',
          customRender: (value, row, index) => {
            const obj = {
              children: _this.toThousands(value),
              attrs: {}
            }
            if (index === 0) {
              obj.attrs.rowSpan = 5
            }
            if (index === 1) {
              obj.attrs.rowSpan = 0
            }
            if (index === 2) {
              obj.attrs.rowSpan = 0
            }
            if (index === 3) {
              obj.attrs.rowSpan = 0
            }
            if (index === 4) {
              obj.attrs.rowSpan = 0
            }
            if (index === 5) {
              obj.attrs.rowSpan = 5
            }
            if (index === 6) {
              obj.attrs.rowSpan = 0
            }
            if (index === 7) {
              obj.attrs.rowSpan = 0
            }
            if (index === 8) {
              obj.attrs.rowSpan = 0
            }
            if (index === 9) {
              obj.attrs.rowSpan = 0
            }
            if (index === 10) {
              obj.attrs.rowSpan = 5
            }
            if (index === 11) {
              obj.attrs.rowSpan = 0
            }
            if (index === 12) {
              obj.attrs.rowSpan = 0
            }
            if (index === 13) {
              obj.attrs.rowSpan = 0
            }
            if (index === 14) {
              obj.attrs.rowSpan = 0
            }
            return obj
          }
        },
        {
          title: '费用占比(%)',
          dataIndex: '',
          width: 140,
          align: 'center',
          customRender: function (text) {
            return ((text || text == 0) ? text : '--')
          }
        },
        {
          title: '正常配送品费用',
          dataIndex: '',
          width: 120,
          align: 'right',
          customRender: function (text) {
            return ((text || text == 0) ? _this.toThousands(text) : '--')
          }
        },
        {
          title: '正常促销品费用',
          dataIndex: '',
          width: 120,
          align: 'right',
          customRender: function (text) {
            return ((text || text == 0) ? _this.toThousands(text) : '--')
          }
        },
        {
          title: '特殊申请配送品费用',
          dataIndex: '',
          width: 120,
          align: 'right',
          customRender: function (text) {
            return ((text || text == 0) ? _this.toThousands(text) : '--')
          }
        },
        {
          title: '特殊申请促销品费用',
          dataIndex: '',
          width: 120,
          align: 'right',
          customRender: function (text) {
            return ((text || text == 0) ? _this.toThousands(text) : '--')
          }
        }
      ]
      if (this.activeKey == 0) {
        arr.splice(1, 0, { title: '省份', dataIndex: 'province', width: 90, align: 'center', customRender: function (text) { return text || '--' } })
      }
      return arr
    }
  },
  methods: {
    callback (key) {
      this.activeKey = key
      console.log(key, this.activeKey)
    },
    onChange (date, dateString) {
      console.log(date, dateString)
    },
    openChange (status) {
      if (status) {
        this.open = true
      } else {
        this.open = false
      }
    },
    panelChange (value) {
      this.year = value
      this.open = false
    },
    // 禁止选择的年份
    disabledDate (currentDate) {
      console.log(currentDate)
      return currentDate && currentDate < moment().endOf('year')
    },
    //  创建时间  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    // 查询
    handleSearch () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$refs.table.refresh(true)
        } else {
          return false
        }
      })
    },
    subareaChange(val){
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    //  重置
    resetSearchForm () {
      this.queryParam.time = []
      this.$refs.rangeDate.resetDate(this.queryParam.time)
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.dealer = {
        dealerName: '',
        provinceSn: undefined,
        citySn: undefined,
        districtSn: undefined,
        dealerLevel: undefined
      }
      this.queryParam.subareaArea = {
          subareaSn: undefined,
          subareaAreaSn: undefined
      }
      this.totalData = null
      this.$refs.subarea.clearData()
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.showExport = true
          const params = _this.queryParam
          _this.$store.state.app.curActionPermission = 'B_returnGoodsPresentationExport'
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(reportRebateExport, params, '全国各省品类实际销售明细报表', function () {
            _this.exportLoading = false
            _this.spinning = false
            _this.$store.state.app.curActionPermission = ''
          })
        } else {
          return false
        }
      })
    },
    pageInit () {
      this.activeKey = 0
      this.getProvinceData('province')
    }
  },
  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>

<style style="less">
   
</style>
