<template>
  <div class="shelfMonitoringWarehousing-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" @back="handleBack" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="shelfMonitoringWarehousing-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span class="store-info" v-if="basicInfoData">{{ basicInfoData.shelfName || '' }} —— 货位号{{ basicInfoData.shelfPlaceCode || '' }}</span>
        </template>
      </a-page-header>
      <!-- 内容 -->
      <a-card size="small" :bordered="false" class="shelfMonitoringWarehousing-cont">
        <!-- 搜索条件 -->
        <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="变动时间">
                  <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="关联单据类型">
                  <v-select
                    v-model="queryParam.bizType"
                    ref="bizType"
                    id="shelfMonitoringWarehousing-bizType"
                    code="STOCK_BIZ_TYPE"
                    placeholder="请选择关联单据类型"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="关联单据号">
                  <a-input id="shelfMonitoringWarehousing-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入关联单据号"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="shelfMonitoringWarehousing-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm()" :disabled="disabled" id="shelfMonitoringWarehousing-reset">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div style="margin-top: 25px;font-weight: bold;display: flex;align-items: center;">
          <p style="margin: 0 70px 8px 0;">产品编码：{{ (basicInfoData&&basicInfoData.productCode) || '--' }}</p>
          <p style="margin: 0 0 8px;">产品名称：{{ (basicInfoData&&basicInfoData.productName) || '--' }}</p>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
          <template slot="qty" slot-scope="text, record">
            {{ (record.bizType == 'PUT_REPLENISH_BILL' || record.bizType == 'MONTH') ? '+' : (record.bizType == 'OUT_RECALL_BILL' || record.bizType == 'OUT_ORDER_BILL') ? '-':'' }}
            {{ (record.qty || record.qty==0) ? record.qty : '--' }}
          </template>
        </s-table>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { shelfPutOutDetailList, shelfProductDetail } from '@/api/shelf'
export default {
  components: { STable, VSelect, rangeDate },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      time: [],
      queryParam: {
        beginDate: undefined,
        endDate: undefined,
        bizType: undefined,
        bizNo: ''
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '变动时间', dataIndex: 'changeDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '17%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '24%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '关联单据类型', dataIndex: 'bizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '关联单据号', dataIndex: 'bizNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '数量', scopedSlots: { customRender: 'qty' }, width: '8%', align: 'center' }
        // { title: '状态', dataIndex: 'stateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfPutOutDetailList(Object.assign(parameter, this.queryParam, { shelfPlaceSn: this.$route.params.shelfPlaceSn, productSn: this.$route.params.productSn })).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
        })
      },
      basicInfoData: null
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = undefined
      this.queryParam.endDate = undefined
      this.queryParam.bizType = undefined
      this.queryParam.bizNo = ''
      this.$refs.table.refresh(true)
    },
    // 基本信息
    getDetail () {
      shelfProductDetail({ shelfPlaceSn: this.$route.params.shelfPlaceSn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/numsGoodsShelves/shelfMonitoring/list' })
    },
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] ? date[0] + ' 00:00:00' : null
      this.queryParam.endDate = date[1] ? date[1] + ' 23:59:59' : null
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getDetail()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getDetail()
      this.resetSearchForm()
    }
  }
}
</script>

<style lang="less">
  .shelfMonitoringWarehousing-wrap{
    .store-info{
      margin: 0 15px;
      color: rgb(102, 102, 102);
    }
    .shelfMonitoringWarehousing-cont{
      margin-top: 10px;
    }
    .ant-descriptions-item-label.ant-descriptions-item-colon{
      width: 120px;
    }
  }
</style>
