<template>
  <a-modal
    centered
    class="chooseSupplier-modal"
    :footer="null"
    :maskClosable="false"
    title="选择供应商"
    v-model="isShow"
    @cancel="isShow=false"
    :width="900">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="products-con">
        <div>
          <!-- 搜索条件 -->
          <div class="table-page-search-wrapper">
            <a-form-model
              ref="ruleForm"
              class="form-model-con"
              layout="inline"
              :model="queryParam"
              :rules="rules"
              @keyup.enter.native="$refs.table.refresh(true)"
              :label-col="formItemLayout.labelCol"
              :wrapper-col="formItemLayout.wrapperCol">
              <a-row :gutter="15">
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="供应商名称">
                    <a-input id="chooseSupplier-supplierName" v-model.trim="queryParam.supplierName" allowClear placeholder="请输入供应商名称"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="状态">
                    <v-select code="ENABLE_FLAG" id="chooseSupplier-enabledFlag" v-model="queryParam.enabledFlag" allowClear placeholder="请选择状态"></v-select>
                  </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="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-model>
          </div>
          <p style="font-weight: bold;">当前已选：{{ rowSelectionInfo&&rowSelectionInfo.selectedRowKeys.length }}个</p>
          <!-- 列表 -->
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.supplierSn"
            rowKeyName="supplierSn"
            :row-selection="{ columnWidth: 40 }"
            @rowSelection="rowSelectionFun"
            :columns="columns"
            :data="loadData"
            :scroll="{ y: 450 }"
            :defaultLoadData="false"
            bordered>
          </s-table>
        </div>
        <!-- 按钮 -->
        <div class="btn-con">
          <a-button
            type="primary"
            id="chooseSupplier-save"
            size="large"
            class="button-primary"
            @click="handleSave"
            style="padding: 0 60px;">保存</a-button>
          <a-button
            id="chooseSupplier-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, VSelect } from '@/components'
import { supplierList } from '@/api/supplier'
export default {
  name: 'ChooseSupplierModal',
  components: { STable, VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    },
    dealerSn: {
      type: String || Number,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 20 }
      },
      queryParam: { //  查询条件
        supplierName: '',
        enabledFlag: undefined
      },
      rules: {},
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '供应商名称', dataIndex: 'supplierName', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'enableFlagDictValue', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return supplierList(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.queryParam.supplierName = ''
      this.queryParam.enabledFlag = undefined
      this.$refs.table.refresh(true)
    },
    // 保存
    handleSave () {
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      this.$emit('ok', this.rowSelectionInfo && this.rowSelectionInfo.selectedRows)
      this.isShow = false
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetSearchForm()
      } else {
        const _this = this
        const selectedRowKeys = []
        this.chooseData.map(item => {
          selectedRowKeys.push(item.rangeDataSn)
        })
        this.$nextTick(() => { // 页面渲染完成后的回调
          _this.$refs.table.refresh(true)
          _this.$refs.table.setTableSelected(selectedRowKeys, _this.chooseData) // 设置表格选中项
        })
      }
    }
  }
}
</script>

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