<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 v-model="queryParam.shelfSn"></shelfSList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="关联客户">
                <custList ref="custList" @change="custChange" :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">
          <p style="margin: 0;">
            <span style="color: red;">
              {{ record.shelfPlaceBindNum||record.shelfPlaceBindNum==0 ? record.shelfPlaceBindNum:'' }}</span> /
            {{ record.shelfPlaceNum }}
          </p>
        </template>
        <!-- 是否设置完成 -->
        <template slot="finishFlag" slot-scope="text, record">
          <a-switch
            checkedChildren="是"
            unCheckedChildren="否"
            v-if="record.finishFlag!=undefined"
            @change="changeStatus(record)"
            :checked="record.finishFlag == 1"
          />
          <span v-else :style="{ color: record.finishFlag == 1 ? '#00aa00' : '#999' }">
            {{ record.finishFlag == 1 ? '是' : (record.finishFlag!=undefined?'否':'--') }}
          </span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="link" class="button-primary" @click="handleSet(record)">设置</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'
import basicInfoModal from './basicInfoModal.vue'
import { shelfList, modifFinishFlag } 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,
        customerEntity: {
          customerName: 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: 'center' }
      ],
      // 加载数据方法 必须为 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, row) {
      if (row) {
        this.queryParam.customerSn = row && row.customerSn || undefined
        this.queryParam.customerEntity.customerName = undefined
      } else {
        this.queryParam.customerEntity.customerName = obj || undefined
        this.queryParam.customerSn = undefined
      }
    },
    // 启用 禁用
    changeStatus (record) {
      const params = {
        shelfSn: record.shelfSn,
        finishFlag: record.finishFlag == 1 ? '0' : '1'
      }
      this.spinning = true
      modifFinishFlag(params).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh()
          this.spinning = false
        } else {
          this.spinning = false
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.queryParam.shelfSn = undefined
      this.queryParam.customerSn = undefined
      this.queryParam.customerEntity.customerName = 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>
