<template>
  <a-spin :spinning="spinning" tip="Loading...">
    <!-- 搜索条件 -->
    <div ref="tableSearch" class="table-page-search-wrapper newTableSearchName">
      <a-form layout="inline" id="chooseSupplier-form" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="8" :sm="24">
            <a-form-item label="供应商名称">
              <a-input id="chooseSupplier-nameLike" v-model.trim="queryParam.supplier.supplierName" allowClear placeholder="请输入供应商名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" style="margin-bottom: 10px;">
            <a-button type="primary" @click="searchForm(true)" :disabled="disabled" id="chooseSupplier-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="chooseSupplier-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
      <!-- 操作按钮 -->
      <div style="margin-bottom: 10px">
        <a-button type="primary" ghost :loading="loading" id="chooseSupplier-add" @click="handleBatchAudit">批量添加</a-button>
        <span style="margin-left: 5px">
          <template v-if="selectCount"> {{ `已选 ${selectCount} 项` }} </template>
        </span>
      </div>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.supplierSn"
      rowKeyName="supplierSn"
      :row-selection="{ columnWidth: 40}"
      @rowSelection="rowSelectionFun"
      :columns="columns"
      :data="loadData"
      :style="{ height:tableHeight-15+'px' }"
      :scroll="{ y:tableHeight+80 }"
      :defaultLoadData="false"
      bordered>
    </s-table>
  </a-spin>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import AreaList from '@/views/common/areaList.js'
import subarea from '@/views/common/subarea.js'
// 接口
import { querySupplierPage } from '@/api/bizuser'
export default {
  name: 'ChooseSupplier',
  mixins: [commonMixin],
  components: { STable, VSelect, subarea, AreaList },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      loading: false, // 批量添加loading
      //  查询条件
      queryParam: {
        supplier: {
          supplierName: ''// 供应商名称
        }
      },
      userSn: '', // 用户sn
      pageFlag: false, // 是否显示选择框
      chooseInfo: [], // 选中数据
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        this.queryParam.userSn = this.userSn
        // 获取列表数据  有分页
        return querySupplierPage(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          const _this = this
          if (res.status == 200) {
            data = res.data
            // 计算表格序号
            const no = 0
            const chooseData = []
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              data.list[i].supplierSn = data.list[i].supplier.supplierSn
              if (_this.chooseInfo && _this.chooseInfo.length > 0) {
                // 回显选中数据
                const flag = _this.chooseInfo.includes(data.list[i].supplier.supplierSn)
                if (flag) {
                  chooseData.push(data.list[i])
                }
              }
            }
            if (!_this.pageFlag) {
              _this.$refs.table.setTableSelected(_this.chooseInfo, chooseData) // 设置表格选中项
              _this.pageFlag = true
            }
          }
          _this.spinning = false
          return data
        })
      },
      rowSelectionInfo: null// 列表选择数据
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '供应商名称', dataIndex: 'supplier.supplierName', width: '96%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
        // { title: '联系人', dataIndex: 'supplier.contact', align: 'left', width: '26%', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '联系电话', dataIndex: 'supplier.contactMobile', width: '20%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      return arr
    },
    // 计算列表选中数据条数
    selectCount () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 地区
    areaChange (val) {
      this.queryParam.provinceSn = val[0] ? val[0] : ''
      this.queryParam.citySn = val[1] ? val[1] : ''
      this.queryParam.districtSn = val[2] ? val[2] : ''
    },
    // 查询
    searchForm (flag) {
      this.$refs.table.refresh(flag)
      this.spinning = false
    },
    // 区域分区
    subareaChange (val) {
      this.queryParam.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaAreaSn = val[1] ? val[1] : undefined
    },
    //  重置
    resetSearchForm () {
      this.queryParam = {
        supplier: {
          supplierName: ''
        }
      }
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(true)
      this.pageFlag = false
    },
    // 清除表格内容
    clearTable () {
      this.rowSelectionInfo = null
      this.$refs.table.clearTable()
    },
    // 批量添加
    handleBatchAudit () {
      const _this = this
      if (!_this.rowSelectionInfo || (_this.rowSelectionInfo && _this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        _this.$message.warning('请在列表勾选后再进行批量操作！')
        return
      }
      const supplierSnList = _this.rowSelectionInfo.selectedRowKeys
      this.spinning = false
      this.$emit('plAdd', supplierSnList)
    },
    // 添加
    handleAdd (row) {
      if (row.subareaArea) {
        const _this = this
        this.$confirm({
          title: '提示',
          content: '当前经销商已被其他区域分区绑定，确定要更新绑定关系吗?',
          centered: true,
          onOk () {
            _this.spinning = true
            _this.$emit('add', row)
          }
        })
      } else {
        this.spinning = true
        this.$emit('add', row)
      }
    },
    // 初始化
    pageInit (data) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.chooseInfo = data.list ? data.list : []
      this.userSn = data.sn
      this.resetSearchForm()
      this.pageFlag = false
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 300
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  }
}
</script>
