<template>
  <a-modal
    centered
    class="lookUpCustomers-modal"
    :footer="null"
    :maskClosable="false"
    title="参与经销商"
    v-model="isShow"
    @cancel="isShow = false"
    width="60%">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-card size="small" :bordered="false">
        <!-- 筛选条件 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" id="lookUpCustomers-form" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="地区">
                  <AreaList id="lookUpCustomers-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="区域/分区">
                  <subarea ref="subarea" id="lookUpCustomers-subarea" @change="subareaChange"></subarea>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="经销商名称">
                  <a-input id="lookUpCustomers-purchaseBillNo" v-model.trim="queryParam.dealerName" allowClear placeholder="请输入经销商名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="lookUpCustomers-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="lookUpCustomers-reset">重置</a-button>
                <a-button
                  type="primary"
                  style="margin-left: 10px"
                  id="lookUpCustomers-export"
                  class="button-warning"
                  @click="handleExport"
                  :disabled="disabled"
                  :loading="exportLoading">导出</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          :scroll="{ y: 450 }"
          :rowClassName="(record, index) => record.checkProfitLossQty < 0 ? 'redBg-row':''"
          bordered>
          <!-- 地区 -->
          <template slot="address" slot-scope="text, record" >
            <div v-if="queryParam.bannerSn">
              <span v-if="record.dealerEntity&&record.dealerEntity.provinceName">{{ record.dealerEntity.provinceName }}/{{ record.dealerEntity.cityName }}/{{ record.dealerEntity.districtName }}</span>
              <span v-else>--</span>
            </div>
            <div v-else>
              <span v-if="record.provinceName">{{ record.provinceName }}/{{ record.cityName }}/{{ record.districtName }}</span>
              <span v-else>--</span>
            </div>
          </template>
          <!-- 商户类型 -->
          <template slot="businessType" slot-scope="text, record" >
            <div v-if="queryParam.bannerSn">
              <span v-if="record.dealerEntity&&record.dealerEntity.dealerTypeName1">{{ record.dealerEntity.dealerTypeName1 }}{{ record.dealerEntity.dealerTypeName1?'>'+record.dealerEntity.dealerTypeName2:record.dealerEntity.dealerTypeName2 }}</span>
              <span v-else>--</span>
            </div>
            <div v-else>
              {{ record.dealerTypeName1 }}{{ record.dealerTypeName1?'>'+record.dealerTypeName2:record.dealerTypeName2 }}
            </div>
          </template>
          <!-- 商户级别 -->
          <template slot="businessLevel" slot-scope="text, record" >
            <div v-if="queryParam.bannerSn">
              <span v-if="record.dealerEntity&&record.dealerEntity.dealerLevelDictValue">{{ record.dealerEntity.dealerLevelDictValue }}</span>
              <span v-else>--</span>
            </div>
            <div v-else>
              {{ record.dealerLevelDictValue||'--' }}
            </div>
          </template>
        </s-table>
      </a-card>
      <div class="btn-cont">
        <a-button id="lookUpCustomers-modal-close" @click="isShow = false">关闭</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { hdExportExcel } from '@/libs/exportExcel'
// 组件
import { STable } from '@/components'
import ProductType from '@/views/common/productType.js'
import subarea from '@/views/common/subarea.js'
import AreaList from '@/views/common/areaList.js'
// 接口
import { dealerExport } from '@/api/promoTerminal'
import { shopBannerSeeDealer, shopBannerExport } from '@/api/shopBanner'
import { queryDealerPageList } from '@/api/dealer'
export default {
  name: 'LookUpCustomersModal',
  components: { STable, ProductType, subarea, AreaList },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, // 是否打开弹框
      disabled: false, // 查询、重置按钮是否可操作
      advanced: false, // 高级搜索 展开/关闭
      exportShow: false, // 导出弹窗显示
      exportLoading: false, // 导出按钮加载状态
      // 查询条件
      queryParam: {
        // 区域分区
        subareaArea: {
          subareaSn: undefined, // 区域
          subareaAreaSn: undefined// 分区
        },
        provinceSn: undefined, // 省sn
        citySn: undefined, // 市sn
        districtSn: undefined, // 区sn
        dealerName: undefined// 经销商名称
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        const ajaxName = this.queryParam.bannerSn ? shopBannerSeeDealer : queryDealerPageList
        return ajaxName(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
        })
      },
      snObj: undefined, // 经销商列表
      columns: [// 表头
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '地区', scopedSlots: { customRender: 'address' }, width: '20%', align: 'center', ellipsis: true },
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '经销商名称', dataIndex: 'dealerName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商户类型', scopedSlots: { customRender: 'businessType' }, width: '20%', align: 'center' },
        { title: '商户级别', scopedSlots: { customRender: 'businessLevel' }, width: '13%', align: 'center' }
      ]
    }
  },
  methods: {
    // 地区
    areaChange (val) {
      this.queryParam.provinceSn = val[0] ? val[0] : ''
      this.queryParam.citySn = val[1] ? val[1] : ''
      this.queryParam.districtSn = val[2] ? val[2] : ''
    },
    // 区域分区
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    // 初始化
    pageInit (data) {
      this.snObj = data
      this.queryParam = Object.assign(this.queryParam, data)
      this.$refs.table.refresh(true)
    },
    //  重置
    resetSearchForm () {
      this.queryParam = Object.assign({
        provinceSn: undefined,
        citySn: undefined,
        districtSn: undefined,
        subareaArea: {
          subareaSn: undefined,
          subareaAreaSn: undefined
        }
      }, this.snObj)
      this.queryParam.dealerName = undefined
      this.$refs.areaList.clearData()
      this.$refs.subarea.clearData()
      this.$refs.table.refresh(true)
    },
    //  导出
    handleExport () {
      const _this = this
      _this.exportLoading = true
      _this.spinning = true
      const ajaxExportName = this.queryParam.bannerSn ? shopBannerExport : dealerExport
      const exportName = this.queryParam.bannerSn ? '首页轮播图参与经销商列表导出' : '促销活动参与经销商列表导出'
      hdExportExcel(ajaxExportName, _this.queryParam, exportName, function () {
        _this.exportLoading = false
        _this.spinning = false
        _this.showExport = true
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetSearchForm()
      }
    }
  }
}
</script>
<style lang="less" scoped>
  .lookUpCustomers-modal{
    .ant-modal-body{
      padding: 0 24px 24px;
    }
    .redBg-row{
      background-color: #f5cdc8;
    }
    .btn-cont {
      text-align: center;
      margin: 5px 0 10px;
    }
    .table-page-search-wrapper{
      margin-bottom:6px;
      /deep/.ant-select-selection{
        border:0 !important;
        box-shadow: none !important;
      }
      .ant-form.ant-form-inline .ant-form-item{
        border:1px solid #dadada;
        border-radius: 3px;
        overflow: hidden;
        margin-bottom: 10px!important;
        /deep/.ant-form-item-label{
          padding-left: 11px !important;
          padding-right: 0!important;
        }
      }
    }
  }
</style>
