<template>
  <div>
    <a-modal
      centered
      wrapClassName="settlementModifyModal-modal"
      :footer="null"
      :maskClosable="false"
      title="选择参与货架"
      v-model="isshow"
      @cancle="handleBack"
      width="80%">
      <a-spin :spinning="spinning" tip="Loading...">
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-item label="货架名称">
                  <a-input id="goodsShelvesAdministrationList-shelfName" allowClear v-model.trim="queryParam.shelfName" placeholder="请输入货架名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="配件经销商">
                  <dealerList ref="dealerList" @change="dealerChange" id="goodsShelvesAdministrationList-dealerChange"></dealerList>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="汽车修理厂">
                  <storeList ref="storeList" @change="storeChange" id="goodsShelvesAdministrationList-storeChange"></storeList>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24" style="margin-bottom: 10px;">
                <a-button type="primary" @click="$refs.table.refresh(true)" id="goodsShelvesAdministrationList-search">查询</a-button>
                <a-button type="" @click="reset" id="goodsShelvesAdministrationList-reset" style="margin-left: 10px;">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <s-table
          class="sTable"
          ref="table"
          size="small"
          rowKey="shelfSn"
          :columns="columns"
          :row-selection="{ columnWidth: 40, getCheckboxProps: record => ({ props: { disabled: !!record.rewardRuleStoreList && !checkedList.find(item=>item.shelfSn==record.shelfSn) } })}"
          @rowSelection="rowSelectionFun"
          :data="loadData"
          :defaultLoadData="false"
          :scroll="{ y: 350 }"
          bordered>
          <template slot="ruleName" slot-scope="text, record">
            <div v-if="record.rewardRuleStoreList">
              <div v-for="item in record.rewardRuleStoreList" :key="item.id">
                {{ item.rewardRule.ruleName }}({{ item.rewardRule.ruleStatusDictValue }})
              </div>
            </div>
          </template>
        </s-table>
        <div style="padding:10px;text-align:center;">
          <a-button type="default" @click="handleBack" style="margin-right:20px;">取消</a-button>
          <a-button type="primary" @click="handleOk">确定</a-button>
        </div>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { rewardRuleShelfQueryPage } from '@/api/redPacket.js'
import dealerList from '@/views/common/dealerList.vue'
import storeList from '@/views/common/storeList.vue'
export default {
  name: 'ShelfModal',
  components: {
    STable, VSelect, storeList, dealerList
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    checkedList: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      spinning: false,
      isshow: this.visible,
      // 查询参数
      queryParam: {
        dealerSn: undefined, // 配件经销商sn
        shelfName: '', // 货架名称
        storeSn: undefined, // 汽车修理厂
        state: 'ENABLE'
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '货架名称', dataIndex: 'shelfName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '配件经销商', dataIndex: 'dealerName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '汽车修理厂', dataIndex: 'storeName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '货架状态', dataIndex: 'stateDictValue', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: <div>已参加活动&nbsp;<a-tooltip placement='top' title='如果货架存在未开始或进行中的红包活动，则不能再参与其它红包活动。'><a-icon type="question-circle" /></a-tooltip></div>, width: '20%', align: 'center', scopedSlots: { customRender: 'ruleName' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return rewardRuleShelfQueryPage(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (let i = 0; i < data.list.length; i++) {
              const _item = data.list[i]
              _item.no = no + i + 1
            }
          }
          this.setCheckList()
          this.spinning = false
          return data
        })
      },
      rowSelectionInfo: null
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 经销商 change
    dealerChange (obj) {
      this.queryParam.dealerSn = obj.key || undefined
    },
    // 汽车修理厂 change
    storeChange (obj) {
      this.queryParam.storeSn = obj.key || undefined
    },
    // 设置已选中项
    setCheckList () {
      const selected = []
      this.checkedList.map(item => {
        selected.push(item.shelfSn)
      })
      this.$refs.table.setTableSelected(selected, [])
    },
    handleBack () {
      this.$emit('close')
    },
    handleOk () {
      const _this = this
      console.log(_this.rowSelectionInfo)
      if (!_this.rowSelectionInfo || (_this.rowSelectionInfo && _this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        _this.$message.warning('请选择货架！')
        return
      }
      const ret = []
      _this.rowSelectionInfo.selectedRowKeys.map(item => {
        ret.push({
          'shelfSn': item
        })
      })
      this.$emit('ok', ret)
    },
    // 重置
    reset (flag) {
      this.queryParam = {
        dealerSn: undefined, // 配件经销商sn
        shelfName: '', // 货架名称
        storeSn: undefined, // 汽车修理厂
        state: 'ENABLE'
      }
      this.$refs.dealerList.resetForm()
      this.$refs.storeList.resetForm()
      this.$refs.table.refresh(true)
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.$nextTick(() => {
          this.reset(true)
        })
      }
    }
  }
}
</script>
<style lang="less">
.settlementModifyModal-modal{
  .ant-input-number-handler-wrap{
    display: none;
  }
}
</style>
