<template>
  <a-card size="small">
    <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">
        <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="审核时间" prop="time">
                <rangeDate ref="rangeDate" :value="queryParam.time" @change="dateChange"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="销售状态" prop="salesStatusType">
                <a-select v-model="queryParam.salesStatusType" allowClear placeholder="请选择销售状态">
                  <a-select-option v-for="item in salesStatusTypeList" :value="item.code" :key="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-model-item>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24" v-if="activeKey==0">
              <a-form-model-item label="所在区域">
                <subarea id="returnGoodsPresentationList-subareaSn" v-model="queryParam.subareaSn"></subarea>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced && activeKey==0">
              <a-col :md="6" :sm="24">
                <a-form-model-item prop="dealerProvinceSn" style="margin: 0;" label="地区">
                  <a-select v-model="queryParam.dealerProvinceSn" allowClear placeholder="请选择省">
                    <a-select-option v-for="item in addrProvinceList" :value="item.areaSn" :key="item.areaSn + 'a'">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <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_regionTypeSalesReportExport')"
                id="returnGoodsPresentationList-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px" v-if="activeKey==0">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :defaultLoadData="false"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        :scroll="{ x: 1550 }"
        bordered>
      </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 { getArea } from '@/api/data'
import subarea from '@/views/common/subarea.js'
import { hdExportExcel } from '@/libs/exportExcel'
import reportModal from '@/views/common/reportModal.vue'
import { realReportSalesByProvince, realReportSalesByProvinceExport, realReportSalesBySubArea, realReportSalesBySubAreaExport } from '@/api/reportData.js'
export default {
  name: 'RegionTypeSalesReportList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, subarea, reportModal },
  data () {
    return {
      spinning: false,
      tabsList: [{ id: 0, name: '各省品类实售明细' }, { id: 1, name: '各区品类实售明细' }],
      salesStatusTypeList: [{ code: 'SALES', name: '实售销售' }, { code: 'SALES_RETURN', name: '实售退货' }, { code: 'REAL_SALES', name: '实际销售' }],
      activeKey: 0,
      advanced: true, // 高级搜索 展开/关闭
      showExport: false,
      queryParam: {
        time: [],
        beginDate: '',
        endDate: '',
        dealerProvinceSn: undefined,
        subareaSn: undefined,
        salesStatusType: 'REAL_SALES'
      },
      addrProvinceList: [], //  省下拉
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      rules: {
        'time': [{ required: true, message: '请选择审核时间', trigger: 'change' }],
        'salesStatusType': [{ required: true, message: '请选择销售状态', trigger: 'change' }]
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const paramsPage = Object.assign(this.queryParam) //  无分页
        const jkUrl = this.activeKey == 0 ? realReportSalesByProvince : realReportSalesBySubArea
        return jkUrl(paramsPage).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '区域',
          dataIndex: 'subareaNames',
          width: 50,
          align: 'center',
          customRender: function (text) { return text || '--' }
        },
        { title: '销售状态', dataIndex: 'salesStatus', width: 50, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '箭牌滤清器', dataIndex: 'jpLqqAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '冠牌滤清器', dataIndex: 'gpLqqAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '滤清器合计', dataIndex: 'lqqAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '箭牌雨刮片', dataIndex: 'jpYgpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '冠牌雨刮片', dataIndex: 'gpYgpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '雨刮片合计', dataIndex: 'ygpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '箭牌刹车片', dataIndex: 'jpScpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '箭牌感应线', dataIndex: 'jpGyxAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '冠牌刹车片', dataIndex: 'gpScpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: 'TBU刹车片', dataIndex: 'tbuScpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: 'TBU刹车油', dataIndex: 'tbuScyAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: 'TBU感应线', dataIndex: 'tbuGyxAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '刹车片合计', dataIndex: 'scpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: 'TBU-P', dataIndex: 'tbupAmount', align: 'center', width: 50, customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '德路斯机油', dataIndex: 'dlsJyAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '德路斯刹车油', dataIndex: 'dlsScyAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '德路斯变速箱油', dataIndex: 'dlsBsxyAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '德路斯汽车养护产品', dataIndex: 'dlsQcyhAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '德路斯合计', dataIndex: 'dlsAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '稳升点火线圈', dataIndex: 'wsDhxqAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '稳升灯泡', dataIndex: 'wsDpAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '稳升合计', dataIndex: 'wsAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '汇箭', dataIndex: 'hjAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '布瑞斯', dataIndex: 'brsAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '箭牌喇叭', dataIndex: 'jpLbAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '品牌合计', dataIndex: 'ppAmount', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: 'NGK', dataIndex: 'ngkAmount', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '品牌+NGK总合计', dataIndex: 'ppAndNgkAmount', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.activeKey == 0) {
        arr.splice(1, 0, { title: '省份', dataIndex: 'dealerProvinceName', width: 50, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      return arr
    }
  },
  methods: {
    callback (key) {
      this.activeKey = key
      this.resetSearchForm()
      // this.queryParam.time = []
      // this.$refs.rangeDate.resetDate(this.queryParam.time)
      // this.$refs.ruleForm.resetFields()
      // this.$refs.table.clearTable()
      console.log(key, this.activeKey)
    },
    // 查询省
    getProvinceData (leve, sn) {
      let params
      if (leve == 'province') {
        params = { type: '2' }
      } else {
        params = { parentId: sn, type: leve == 'city' ? '3' : '4' }
      }
      getArea(params).then(res => {
        if (res.status == 200) {
          console.log(res, '=============地区')
          if (leve == 'province') {
            this.addrProvinceList = res.data || []
          } else if (leve == 'city') {
            this.addrCityList = res.data || []
          } else if (leve == 'district') {
            this.addrDistrictList = res.data || []
          }
        } else {
          if (leve == 'province') {
            this.addrProvinceList = []
          } else if (leve == 'city') {
            this.addrCityList = []
          } else if (leve == 'district') {
            this.addrDistrictList = []
          }
        }
      })
    },
    //  创建时间  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
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.ruleForm.resetFields()
      this.queryParam.time = []
      this.$refs.rangeDate.resetDate(this.queryParam.time)
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.dealerProvinceSn = undefined
      this.queryParam.subareaSn = undefined
      this.queryParam.salesStatusType = 'REAL_SALES'
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      const jkUrl = _this.activeKey == 0 ? realReportSalesByProvinceExport : realReportSalesBySubAreaExport
      const excelTitle = _this.activeKey == 0 ? '各省品类实售明细报表' : '各区品类实售明细报表'
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.showExport = true
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(jkUrl, params, excelTitle, function () {
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    },
    pageInit () {
      this.activeKey = 0
      this.getProvinceData('province')
      this.resetSearchForm()
    }
  },
  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">
  .regionTypeSalesReportList-wrap{

  }
</style>
