<template>
  <a-modal
    centered
    class="chooseCustomer-modal"
    :footer="null"
    :maskClosable="false"
    title="选择客户"
    v-model="isShow"
    @cancel="isShow=false"
    :width="900">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="chooseCustomer-con">
        <!-- 搜索条件 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="8" :sm="24">
                <a-form-item label="客户名称">
                  <a-input id="chooseCustomer-dealerName" v-model.trim="queryParam.dealerName" allowClear placeholder="请输入客户名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-model-item label="地区">
                    <AreaList id="chooseCustomer-areaList" changeOnSelect ref="areaList" @change="areaChange" defValKey="id"></AreaList>
                </a-form-model-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="promotionRulesList-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="promotionRulesList-reset">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <p style="font-weight: bold;">当前已选：{{ rowSelectionInfo&&rowSelectionInfo.selectedRowKeys.length }}个</p>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.dealerSn"
          rowKeyName="dealerSn"
          :row-selection="{ columnWidth: 40 }"
          @rowSelection="rowSelectionFun"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
        </s-table>
        <!-- 按钮 -->
        <div class="btn-con">
          <a-button
            type="primary"
            id="chooseCustomer-submit"
            size="large"
            class="button-primary"
            style="padding: 0 60px;"
            @click="handleSubmit">确定</a-button>
          <a-button
            id="chooseCustomer-cancel"
            size="large"
            class="button-cancel"
            @click="isShow=false"
            style="padding: 0 60px;margin-left: 15px;">取消</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import AreaList from '@/views/common/areaList.js'
import { dealerQueryList } from '@/api/dealer'
export default {
  name: 'ChooseCustomerModal',
  components: { STable, AreaList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseCust: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: {
        dealerName: '',
        provinceSn: undefined,
        citySn: undefined,
        districtSn: undefined
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '客户名称', dataIndex: 'dealerName', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return dealerQueryList(Object.assign(parameter, this.queryParam)).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
        })
      },
      rowSelectionInfo: null
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    //  重置
    resetSearchForm () {
      this.resetData()
      this.$refs.table.refresh(true)
    },
    resetData () {
      this.queryParam.dealerName = ''
      this.queryParam.provinceSn = undefined
      this.queryParam.citySn = undefined
      this.queryParam.districtSn = undefined
      this.$refs.areaList.clearData()
    },
    areaChange(val){
      this.queryParam.provinceSn = val[0] ? val[0] : ''
      this.queryParam.citySn = val[1] ? val[1] : ''
      this.queryParam.districtSn = val[2] ? val[2] : ''
    },
    //  确定
    handleSubmit () {
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      this.$emit('ok', this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys)
      this.isShow = false
    },
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetData()
      } else {
        const _this = this
        this.$nextTick(() => { // 页面渲染完成后的回调
          _this.$refs.table.refresh(true)
          _this.$refs.table.setTableSelected(_this.chooseCust, []) // 设置表格选中项
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .chooseCustomer-modal{
    .chooseCustomer-con{
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel{
          font-size: 12px;
        }
      }
    }
  }
</style>
