<template>
  <a-modal
    centered
    class="chooseSupplier-modal"
    :footer="null"
    :maskClosable="false"
    title="选择供应商"
    v-model="isShow"
    @cancle="isShow=false"
    :width="900">
    <div class="products-con">
      <div>
        <!-- 搜索条件 -->
        <div class="table-page-search-wrapper">
          <a-form-model
            layout="inline"
            ref="ruleForm"
            :model="queryParam"
            :rules="rules"
            @keyup.enter.native="getSupplierList(1)"
            :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="handleSearch" :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></a-form>
        </div>
        <p style="font-weight: bold;">当前已选：{{ selectedRowKeys.length }}个</p>
        <!-- 列表 -->
        <a-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.supplierSn"
          :row-selection="{
            selectedRowKeys: selectedRowKeys,
            onChange: onChange,
            onSelect: onSelectChange,
            onSelectAll: onSelectAllChange
          }"
          :data-source="loadData"
          :columns="columns"
          :pagination="false"
          :loading="loading"
          :scroll="{ y: 450 }"
          bordered>
        </a-table>
        <!-- 分页 -->
        <tablePagination
          ref="tablePagination"
          :total="paginationProps.total"
          :current="paginationProps.current"
          :pageSize="paginationProps.pageSize"
          @changePageSize="changePageSize"
          @changePage="changePage" />
      </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-modal>
</template>

<script>
import { STable, VSelect } from '@/components'
import tablePagination from '@/views/common/tablePagination.vue'
import { supplierList } from '@/api/supplier'
export default {
  name: 'ChooseSupplierModal',
  components: { STable, VSelect, tablePagination },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    },
    dealerSn: {
      type: String || Number,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 20 }
      },
      queryParam: { //  查询条件
        supplierName: '',
        enabledFlag: undefined
      },
      rules: {},
      disabled: false, //  查询、重置按钮是否可操作
      loading: 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 || '--' } }
      ],
      loadData: [],
      paginationProps: {
        total: 0, //  分页总条数
        current: 1,
        pageSize: 20
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    // 查询
    handleSearch () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.getSupplierList(1)
        } else {
          return false
        }
      })
    },
    // 重置数据
    resetData () {
      this.queryParam.supplierName = ''
      this.queryParam.enabledFlag = undefined
      this.paginationProps.total = 0
      this.paginationProps.total = 0
      this.paginationProps.current = 1
      this.paginationProps.pageSize = 20
    },
    //  重置
    resetSearchForm () {
      this.resetData()
      this.loadData = []
      this.disabled = false
    },
    // 保存
    handleSave () {
      if (this.selectedRowKeys.length < 1) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      this.$emit('ok', this.selectedRows)
      this.isShow = false
    },
    onChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
    },
    onSelectChange (record, selected, selectedRows, nativeEvent) {
      const _this = this
      if (selected) { //  选择
        this.selectedRows.push(record)
      } else { //  取消
        this.selectedRows.map((item, index) => {
          if (item.supplierSn == record.supplierSn) {
            _this.selectedRows.splice(index, 1)
          }
        })
      }
    },
    // 本页全选/取消全选
    onSelectAllChange (selected, selectedRows, changeRows) {
      const _this = this
      if (selected) { //  选择
        this.selectedRows = [...this.selectedRows, ...changeRows]
      } else { //  取消
        this.selectedRows.map((item, index) => {
          this.selectedRows.map((subItem, ind) => {
            if (item.supplierSn == subItem.supplierSn) {
              _this.selectedRows.splice(index, 1)
            }
          })
        })
      }
    },
    // 供应商列表
    getSupplierList (pageNo) {
      this.paginationProps.current = pageNo || this.paginationProps.current
      this.disabled = true
      const params = {
        pageNo: this.paginationProps.current,
        pageSize: this.paginationProps.pageSize
      }
      this.loading = true
      supplierList(Object.assign(params, this.queryParam)).then(res => {
        this.loading = false
        if (res.status == 200) {
          const data = res.data
          this.paginationProps.total = Number(res.data.count) || 0
          this.paginationProps.current = data.pageNo
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.loadData = data.list
          this.disabled = false
        } else {
          this.paginationProps.total = 0
          this.paginationProps.current = 1
          this.paginationProps.pageSize = 20
          this.loadData = []
        }
      })
    },
    //  已选产品 分页  一页多少条change
    changePageSize (current, pageSize) {
      this.paginationProps.current = current
      this.paginationProps.pageSize = pageSize
      this.getSupplierList()
    },
    //  已选产品 分页 页码change
    changePage (current) {
      this.paginationProps.current = current
      this.getSupplierList()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.loadData = []
        this.disabled = false
        this.resetData()
      } else {
        this.getSupplierList(1)
        this.selectedRows = this.chooseData
        this.selectedRowKeys = []
        this.chooseData.map(item => {
          this.selectedRowKeys.push(item.rangeDataSn)
        })
      }
    }
  }
}
</script>

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