<template>
  <div>
    <a-card size="small" :bordered="false" class="customerCountList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper" ref="tableSearch">
        <a-form-model
          id="customerCountList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户名称">
                <dealerSubareaScopeList ref="dealerSubareaScopeList" id="customerCountList-dealerName" @change="custChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户级别">
                <v-select
                  v-model="queryParam.dealerLevel"
                  ref="dealerLevel"
                  id="customerCountList-dealerLevel"
                  code="DEALER_LEVEL"
                  placeholder="请选择客户级别"
                  allowClear></v-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="所在区域">
                <subarea id="customerCountList-subarea" ref="subarea" @change="subareaChange"></subarea>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="区域负责人">
                  <BizUser id="customerCountList-bizUserSn" v-model="queryParam.subareaArea.bizUserSn"></BizUser>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="地区">
                  <AreaList id="customerCountList-areaList" changeOnSelect ref="areaList" @change="areaChange" defValKey="id"></AreaList>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="客服">
                  <customerService ref="customerName" id="customerCountList-customerName" v-model="queryParam.subareaArea.kfSn"></customerService>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="handleSearch"
                :disabled="disabled"
                id="customerCountList-refresh">查询</a-button>
              <a-button
                style="margin-left: 8px"
                @click="resetSearchForm"
                :disabled="disabled"
                id="customerCountList-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_newCustomSearchExport')"
                id="customerCountList-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :style="{ height: tableHeight+70+'px' }"
          :columns="columns"
          :data="loadData"
          :scroll="{y: tableHeight}"
          bordered>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { hdExportExcel } from '@/libs/exportExcel'
// 组件
import { STable, VSelect } from '@/components'
import subarea from '@/views/common/subarea.js'
import AreaList from '@/views/common/areaList.js'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import BizUser from '@/views/common/bizUser.js'
import customerService from '@/views/common/customerService.vue'
// 接口
import { dealerNewLogPageList, dealerNewLogExport } from '@/api/dealearNewLog'
export default {
  name: 'CustomerCountList',
  mixins: [commonMixin],
  components: { STable, VSelect, subarea, AreaList, dealerSubareaScopeList, BizUser, customerService },
  data () {
    const _this = this
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0, // 表格高度
      exportLoading: false, // 导出按钮加载状态
      //  查询条件
      queryParam: {
        dealerLevel: undefined, // 客户等级
        dealerSn: undefined, // 客户sn
        dealerName: undefined, // 客户名称
        provinceSn: undefined, // 省
        citySn: undefined, // 市
        districtSn: undefined, // 区
        subareaArea: {
          subareaSn: '', // 区域
          subareaAreaSn: '', // 分区
          bizUserSn: undefined, // 区域负责人
          kfSn: undefined// 客服
        }
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '区域负责人', dataIndex: 'subareaArea.bizUserName', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '省份', dataIndex: 'provinceName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'dealerName', width: '17%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '客户级别', dataIndex: 'dealerLevelDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '年份', dataIndex: 'year', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '月份', dataIndex: 'month', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '第一笔时间', dataIndex: 'firstDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '最后一笔时间', dataIndex: 'lastDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '首笔订单数', dataIndex: 'billCount', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '总开单金额', dataIndex: 'totalAmount', width: '8%', align: 'right', customRender: function (text) { return _this.toThousands(text) || '--' } },
        { title: '总实售金额', dataIndex: 'totalRealAmount', width: '8%', align: 'right', customRender: function (text) { return _this.toThousands(text) || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        // 获取列表数据  有分页
        return dealerNewLogPageList(params).then(res => {
          let data
          if (res.status == 200) {
            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
        })
      }
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  methods: {
    // 查询
    handleSearch () {
      this.$refs.table.refresh(true)
    },
    // 客户名称 change
    custChange (val) {
      this.queryParam.dealerName = val.name
      this.queryParam.dealerSn = val.key
    },
    // 地区
    areaChange (val) {
      this.queryParam.provinceSn = val[0] ? val[0] : undefined
      this.queryParam.citySn = val[1] ? val[1] : undefined
      this.queryParam.districtSn = val[2] ? val[2] : undefined
    },
    // 区域分区  change
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : ''
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : ''
    },
    //  重置
    resetSearchForm () {
      this.queryParam.provinceSn = undefined
      this.queryParam.citySn = undefined
      this.queryParam.districtSn = undefined
      this.queryParam.dealerName = undefined
      this.queryParam.dealerSn = undefined
      this.queryParam.subareaArea.subareaSn = ''
      this.queryParam.subareaArea.subareaAreaSn = ''
      this.queryParam.subareaArea.bizUserSn = undefined
      this.queryParam.subareaArea.kfSn = undefined
      this.queryParam.dealerLevel = undefined
      if (this.advanced) {
        this.$refs.subarea.clearData()
        this.$refs.dealerSubareaScopeList.resetForm()
        this.$refs.areaList.clearData()
      }
      this.$refs.table.clearTable()
      this.$refs.ruleForm.resetFields()
      this.$refs.table.refresh(false)
    },
    // 导出
    handleExport () {
      const _this = this
      const params = JSON.parse(JSON.stringify(_this.queryParam))
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(dealerNewLogExport, params, '新客户查询', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    // 初始化
    pageInit () {
      this.$nextTick(() => { // 页面渲染完成后的回调
        this.setTableH()
        this.resetSearchForm()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 200
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
  }
}
</script>
<style lang="less" scoped>
  .monthBox{
    width: 100%;
    /deep/.ant-calendar-picker-icon{
      display:none !important;
    }
    /deep/.ant-calendar-picker input{
      text-align:center;
    }
  }
</style>
