<template>
  <a-card size="small" :bordered="false" class="actualSalesReportList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="actualSalesReportList-form"
          layout="inline"
          ref="ruleForm"
          :model="queryParam"
          :rules="rules"
          @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="客户名称">
                <custList id="actualSalesReportList-allocateTypeSn" ref="custList" @change="custChange"></custList>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户类型">
                <v-select
                  v-model="queryParam.dealer.dealerLevel"
                  ref="dealerLevel"
                  id="actualSalesReportList-allocateTypeSn"
                  code="DEALER_LEVEL"
                  placeholder="请选择客户类型"
                  allowClear></v-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所在区域">
                  <subarea id="actualSalesReportList-allocateTypeSn" v-model="queryParam.dealer.subareaSn"></subarea>
                </a-form-model-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-row>
                  <a-form-model-item label="地区">
                    <a-col span="7">
                      <a-form-model-item prop="dealerProvinceSn" style="margin: 0;">
                        <a-select v-model="queryParam.dealer.provinceSn" allowClear @change="getCityList" placeholder="请选择省">
                          <a-select-option v-for="item in addrProvinceList" :value="item.id" :key="item.id + 'a'">{{ item.name }}</a-select-option>
                        </a-select>
                      </a-form-model-item>
                    </a-col>
                    <a-col span="7" offset="1">
                      <a-form-model-item prop="dealerCitySn" style="margin: 0;">
                        <a-select v-model="queryParam.dealer.citySn" allowClear @change="getAreaList" placeholder="请选择市">
                          <a-select-option v-for="item in addrCityList" :value="item.id" :key="item.id + 'b'">{{ item.name }}</a-select-option>
                        </a-select>
                      </a-form-model-item>
                    </a-col>
                    <a-col span="7" offset="1">
                      <a-form-model-item prop="dealerCountySn" style="margin: 0;">
                        <a-select v-model="queryParam.dealer.districtSn" allowClear placeholder="请选择区/县">
                          <a-select-option v-for="item in addrDistrictList" :value="item.id" :key="item.id + 'c'">{{ item.name }}</a-select-option>
                        </a-select>
                      </a-form-model-item>
                    </a-col>
                  </a-form-model-item>
                </a-row>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="actualSalesReportList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="actualSalesReportList-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ 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"
        :scroll="{ x: 3120 }"
        bordered>
        <template slot="footer">
          <a-row>
            <a-col span="2">合计：</a-col>
            <a-col span="22">
              <a-row>
                <a-col span="4">箭牌滤清器：{{ (totalData && (totalData.jpLqqAmount || totalData.jpLqqAmount==0)) ? totalData.jpLqqAmount : '--' }}</a-col>
                <a-col span="4">箭牌雨刮片：{{ (totalData && (totalData.jpYgpAmount || totalData.jpYgpAmount==0)) ? totalData.jpYgpAmount : '--' }}</a-col>
                <a-col span="4">箭牌喇叭：{{ (totalData && (totalData.jpLbAmount || totalData.jpLbAmount==0)) ? totalData.jpLbAmount : '--' }}</a-col>
                <a-col span="4">箭牌刹车片：{{ (totalData && (totalData.jpScpAmount || totalData.jpScpAmount==0)) ? totalData.jpScpAmount : '--' }}</a-col>
                <a-col span="4">箭牌感应线：{{ (totalData && (totalData.jpGyxAmount || totalData.jpGyxAmount==0)) ? totalData.jpGyxAmount : '--' }}</a-col>
                <a-col span="4">冠牌滤清器：{{ (totalData && (totalData.gpLqqAmount || totalData.gpLqqAmount==0)) ? totalData.gpLqqAmount : '--' }}</a-col>
                <a-col span="4">冠牌雨刮片：{{ (totalData && (totalData.gpYgpAmount || totalData.gpYgpAmount==0)) ? totalData.gpYgpAmount : '--' }}</a-col>
                <a-col span="4">冠牌刹车片：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">汇箭：{{ (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">TBU：{{ (totalData && (totalData.tbuAmount || totalData.tbuAmount==0)) ? totalData.tbuAmount : '--' }}</a-col>
                <a-col span="4">NGK：{{ (totalData && (totalData.ngkAmount || totalData.ngkAmount==0)) ? totalData.ngkAmount : '--' }}</a-col>
                <a-col span="4">飞利浦：{{ (totalData && (totalData.flpAmount || totalData.flpAmount==0)) ? totalData.flpAmount : '--' }}</a-col>
                <a-col span="4">车仆：{{ (totalData && (totalData.cpAmount || totalData.cpAmount==0)) ? totalData.cpAmount : '--' }}</a-col>
                <a-col span="4">瓦尔塔：{{ (totalData && (totalData.wetAmount || totalData.wetAmount==0)) ? totalData.wetAmount : '--' }}</a-col>
                <a-col span="4">雷贝斯托：{{ (totalData && (totalData.lbstAmount || totalData.lbstAmount==0)) ? totalData.lbstAmount : '--' }}</a-col>
                <a-col span="4">德尔福：{{ (totalData && (totalData.defAmount || totalData.defAmount==0)) ? totalData.defAmount : '--' }}</a-col>
                <a-col span="4">巴诺德：{{ (totalData && (totalData.bndAmount || totalData.bndAmount==0)) ? totalData.bndAmount : '--' }}</a-col>
                <a-col span="6">实售销售合计金额：{{ (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 getDate from '@/libs/getDate.js'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import custList from '@/views/common/custList.vue'
import subarea from '@/views/common/subarea.js'
import { getArea } from '@/api/data'
import { salesBillRealAmountQueryPageList, salesBillRealReportCount } from '@/api/salesBillReport'
export default {
  components: { STable, VSelect, rangeDate, subarea, custList },
  data () {
    return {
      spinning: false,
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        time: [
          getDate.getCurrMonthDays().starttime,
          getDate.getCurrMonthDays().endtime
        ],
        beginDate: getDate.getCurrMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        dealer: {
          'dealerSn': undefined,
          'provinceSn': undefined,
          'citySn': undefined,
          'districtSn': undefined,
          'subareaSn': undefined,
          'dealerLevel': undefined
        }
      },
      rules: {
        'time': [{ required: true, message: '请选择销售日期', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '区域', dataIndex: 'dealer.subareaNameSet', width: 220, align: 'center', customRender: function (text) { return (text && text.length > 0) ? text.join('/') : '--' } },
        { title: '省份', dataIndex: 'dealer.provinceName', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'dealer.dealerName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户类型', dataIndex: 'dealer.dealerLevelDictValue', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌',
          align: 'center',
          children: [
            {
              title: '箭牌',
              align: 'center',
              children: [
                {
                  title: '滤清器',
                  dataIndex: 'jpLqqAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '雨刮片',
                  dataIndex: 'jpYgpAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '喇叭',
                  dataIndex: 'jpLbAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '刹车片',
                  dataIndex: 'pp4',
                  align: 'jpScpAmount',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '感应线',
                  dataIndex: 'jpGyxAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                }
              ]
            },
            {
              title: '冠牌',
              align: 'center',
              children: [
                {
                  title: '滤清器',
                  dataIndex: 'gpLqqAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '雨刮片',
                  dataIndex: 'gpYgpAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '刹车片',
                  dataIndex: 'gpScpAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                }
              ]
            },
            {
              title: '汇箭',
              dataIndex: 'hjAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '德路斯',
              dataIndex: 'dlsAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '稳升',
              dataIndex: 'wsAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: 'TBU',
              dataIndex: 'tbuAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            }
          ] },
        { title: '代理品牌',
          align: 'center',
          children: [
            {
              title: 'NGK',
              dataIndex: 'ngkAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '飞利浦',
              dataIndex: 'flpAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '车仆',
              dataIndex: 'cpAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '瓦尔塔',
              dataIndex: 'wetAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '雷贝斯托',
              dataIndex: 'lbstAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '德尔福',
              dataIndex: 'defAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '巴诺德',
              dataIndex: 'bndAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            }
          ] },
        { title: '实售销售合计金额', width: 200, dataIndex: 'totalAmount', align: 'center', customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 380
        }
        const paramsPage = Object.assign(this.queryParam, { pageNo: parameter.pageNo, pageSize: parameter.pageSize }) //  有分页
        return salesBillRealAmountQueryPageList(paramsPage).then(res => {
          this.getCount(paramsPage)
          const data = res.data
          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,
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [] //  区下拉
    }
  },
  methods: {
    // 总计
    getCount (params) {
      salesBillRealReportCount(params).then(res => {
        if (res.status == 200 && res.data) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    handleSearch () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$refs.table.refresh(true)
        } else {
          return false
        }
      })
    },
    //  创建时间  change
    dateChange (date) {
      this.queryParam.time = date
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    custChange (val) {
      this.queryParam.dealer.dealerSn = val.key
    },
    //  重置
    resetSearchForm () {
      this.queryParam.time = [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ]
      this.$refs.rangeDate.resetDate(this.queryParam.time)
      this.queryParam.beginDate = getDate.getCurrMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.dealer = {
        'dealerSn': undefined,
        'provinceSn': undefined,
        'citySn': undefined,
        'districtSn': undefined,
        'subareaSn': undefined,
        'dealerLevel': undefined
      }
      this.totalData = null
      this.$refs.rangeDate.resetDate()
      this.$refs.custList.resetForm()
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.queryParam.dealerCitySn = undefined
      this.queryParam.dealerCountySn = undefined
      this.getArea('city', val)
    },
    // 获取区县列表
    getAreaList (val) {
      this.addrDistrictList = []
      this.queryParam.dealerCountySn = undefined
      this.getArea('district', val)
    },
    //  省/市/区
    getArea (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) {
          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 = []
          }
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getArea('province')
    })
  }
}
</script>
