<template>
  <a-card size="small" :bordered="false" class="salesSlipReportList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="salesSlipReportList-form"
          layout="inline"
          ref="ruleForm"
          :model="queryParam"
          :rules="rules"
          @keyup.enter.native="$refs.table.refresh(true)">
          <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="salesSlipReportList-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="salesSlipReportList-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="销售单号">
                  <a-input id="salesSlipReportList-allocateTypeSn" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入销售单号"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所在区域">
                  <subarea id="salesSlipReportList-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="salesSlipReportList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="salesSlipReportList-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: 3220 }"
        bordered>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
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 { salesBillReportList } from '@/api/salesBillReport'
export default {
  components: { STable, VSelect, rangeDate, subarea, custList },
  data () {
    return {
      spinning: false,
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        time: [],
        beginDate: '',
        endDate: '',
        salesBillNo: '', //  销售单号
        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.join('/') || '--' } },
        { title: '销售单号', dataIndex: 'salesBillNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '省份', dataIndex: 'dealer.provinceName', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'dealer.dealerName', align: 'center', customRender: function (text) { return text || '--' } },
        { 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' }
                },
                {
                  title: '雨刮片',
                  dataIndex: 'jpYgpAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return text ? ('￥' + text) : '0' }
                },
                {
                  title: '喇叭',
                  dataIndex: 'jpLbAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return text ? ('￥' + text) : '0' }
                },
                {
                  title: '刹车片',
                  dataIndex: 'pp4',
                  align: 'jpScpAmount',
                  width: 100,
                  customRender: function (text) { return text ? ('￥' + text) : '0' }
                },
                {
                  title: '感应线',
                  dataIndex: 'jpGyxAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return text ? ('￥' + text) : '0' }
                }
              ]
            },
            {
              title: '冠牌',
              align: 'center',
              children: [
                {
                  title: '滤清器',
                  dataIndex: 'gpLqqAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return text ? ('￥' + text) : '0' }
                },
                {
                  title: '雨刮片',
                  dataIndex: 'gpYgpAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return text ? ('￥' + text) : '0' }
                },
                {
                  title: '刹车片',
                  dataIndex: 'gpScpAmount',
                  align: 'center',
                  width: 100,
                  customRender: function (text) { return text ? ('￥' + text) : '0' }
                }
              ]
            },
            {
              title: '汇箭',
              dataIndex: 'hjAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '德路斯',
              dataIndex: 'dlsAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '稳升',
              dataIndex: 'wsAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: 'TBU',
              dataIndex: 'tbuAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            }
          ] },
        { title: '代理品牌',
          align: 'center',
          children: [
            {
              title: 'NGK',
              dataIndex: 'ngkAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '飞利浦',
              dataIndex: 'flpAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '车仆',
              dataIndex: 'cpAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '瓦尔塔',
              dataIndex: 'wetAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '雷贝斯托',
              dataIndex: 'lbstAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '德尔福',
              dataIndex: 'defAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            },
            {
              title: '巴诺德',
              dataIndex: 'bndAmount',
              align: 'center',
              width: 100,
              customRender: function (text) { return text ? ('￥' + text) : '0' }
            }
          ] },
        { title: '实际销售合计金额', width: 200, dataIndex: 'totalAmount', align: 'center', customRender: function (text) { return text ? ('￥' + text) : '0' } }
      ],
      // 加载数据方法 必须为 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 salesBillReportList(paramsPage).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            const row = data.list[i]
            row.no = no + i + 1
            row.totalAmount = row.jpLqqAmount + row.jpYgpAmount + row.jpLbAmount + row.jpScpAmount
            row.totalAmount += row.jpGyxAmount + row.gpLqqAmount + row.gpYgpAmount + row.gpScpAmount + row.hjAmount
            row.totalAmount += row.dlsAmount + row.wsAmount + row.tbuAmount + row.ngkAmount + row.flpAmount + row.cpAmount
            row.totalAmount += row.wetAmount + row.lbstAmount + row.defAmount + row.bndAmount
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      },
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [] //  区下拉
    }
  },
  methods: {
    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.$refs.rangeDate.resetDate()
      this.queryParam.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.salesBillNo = ''
      this.queryParam.dealer = {
        'dealerSn': undefined,
        'provinceSn': undefined,
        'citySn': undefined,
        'districtSn': undefined,
        'subareaSn': undefined,
        'dealerLevel': undefined
      }
      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>
