<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.providerSn"></shelfSList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="调回单号">
                <a-input allowClear placeholder="请输入调回单号" v-model.tirm="queryParam.putBizNo"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="状态">
                <v-select
                  v-model="queryParam.state"
                  ref="state"
                  id="shelfSetList-state"
                  code="STOCK_PUT_STATE"
                  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">
          共 <strong>{{ count||0 }}</strong> 条记录，
          其中待入库 <strong>{{ putCount||0 }}</strong> 条
        </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)" v-if="record.state=='WAIT'">确认入库</a-button>
          <span v-else style="display: inline-block;padding-left: 7px;">--</span>
        </template>
      </s-table>
    </a-spin>

  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import shelfSList from '@/views/common/shelfList'
import { recallStockList, stockPutCount } from '@/api/shelfRecall.js'
export default {
  components: { STable, VSelect, shelfSList },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      count: 0, // 数据总条数
      putCount: 0, // 待入库条数
      queryParam: {
        putBizNo: '',
        providerSn: undefined,
        state: undefined
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '调回单号', dataIndex: 'putBizNo', width: '15%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '货架名称', dataIndex: 'providerName', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '入库总数量', dataIndex: 'productTotalQty', width: '10%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '入库总成本', dataIndex: 'productTotalCost', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '状态', dataIndex: 'stateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'auditTime', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'left' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return recallStockList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.count = res.data.count
            this.getStockPutCount(this.queryParam)
            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: {
    // 查询待入库条数
    getStockPutCount () {
      stockPutCount(this.queryParam).then(res => {
        if (res.status == 200) {
          this.putCount = res.data
        } else {
          this.putCount = 0
        }
      })
    },
    // 设置/修改
    handleSet (row) {
      this.$router.push({ path: `/numsGoodsShelves/recallStockManagement/editStock/${row.stockPutSn}` })
    },
    // 重置
    resetSearchForm () {
      this.queryParam.providerSn = undefined
      this.queryParam.putBizNo = ''
      this.queryParam.state = 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>
