<template>
  <a-card size="small" :bordered="false" class="shelfSetList-wrap">
    <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="货架名称">
                <shelfSList ref="shelfList" @change="shelfChange" :itemSn="queryParam.shelfSn"></shelfSList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="关联客户">
                <custList ref="custList" @change="custChange" :itemSn="queryParam.customerSn" :isEnabled="true"></custList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="是否设置完成">
                <v-select
                  v-model="queryParam.finishFlag"
                  ref="finishFlag"
                  id="shelfSetList-finishFlag"
                  code="FLAG"
                  placeholder="请选择"
                  allowClear></v-select>
              </a-form-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="shelfSetList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="shelfSetList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 提示 -->
      <a-alert type="info" style="margin-bottom: 10px">
        <div slot="message">注意：只有当数字货架的所有信息都设置完成，才能在其他功能中看到该数字货架</div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 货位数量 -->
        <template slot="shelfPlaceNum" slot-scope="text, record">
          <div v-if="record.finishFlag=='1'">{{ record.shelfPlaceNum }}</div>
          <div v-else>
            <p style="margin: 0;">
              <span style="color: red;">{{ record.shelfPlaceBindNum||record.shelfPlaceBindNum==0 ? record.shelfPlaceBindNum:'' }}</span> /
              {{ record.shelfPlaceNum }}
            </p>
          </div>
        </template>
        <!-- 是否设置完成 -->
        <template slot="finishFlag" slot-scope="text, record">
          <span v-if="record.finishFlagDictValue" :style="{color: record.finishFlagDictValue=='否' ? 'red':''}">{{ record.finishFlagDictValue }}</span>
          <span v-else>--</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="link" class="button-primary" @click="handleSet(record)">{{ record.finishFlag=='1'?'修改':'设置' }}</a-button>
        </template>
      </s-table>
    </a-spin>
    <!-- 基础设置 -->
    <basic-info-modal :openModal="openModal" :nowData="nowData" @ok="handleOk" @close="openModal=false" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import custList from '@/views/common/custList.vue'
import shelfSList from '@/views/common/shelfList.vue'
import basicInfoModal from './basicInfoModal.vue'
import { shelfList } from '@/api/shelf'
export default {
  components: { STable, VSelect, custList, shelfSList, basicInfoModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: {
        shelfSn: undefined,
        customerSn: undefined,
        finishFlag: undefined
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '货架名称', dataIndex: 'shelfName', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '关联客户', dataIndex: 'customerEntity.customerName', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '货位数量', scopedSlots: { customRender: 'shelfPlaceNum' }, width: '15%', align: 'center' },
        { title: '是否设置完成', scopedSlots: { customRender: 'finishFlag' }, width: '20%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'left' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfList(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
        })
      },
      openModal: false,
      nowData: null
    }
  },
  methods: {
    // 设置/修改
    handleSet (row) {
      if (row.customerSn) {
        this.$router.push({ path: `/numsGoodsShelves/shelfSet/set/${row.shelfSn}` })
      } else {
        this.nowData = {
          shelfSn: row.shelfSn,
          shelfName: row.shelfName
        }
        this.openModal = true
      }
    },
    handleOk (data) {
      this.$router.push({ path: `/numsGoodsShelves/shelfSet/set/${data.shelfSn}` })
    },
    // 客户 change
    custChange (obj) {
      this.queryParam.customerSn = obj.key || undefined
    },
    // 货架  change
    shelfChange (obj) {
      this.queryParam.shelfSn = obj.key || undefined
    },
    // 重置
    resetSearchForm () {
      this.queryParam.shelfSn = undefined
      this.$refs.shelfList.resetForm()
      this.queryParam.customerSn = undefined
      this.$refs.custList.resetForm()
      this.queryParam.finishFlag = undefined
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
