<template>
  <div class="inventoryCheckOverallList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="inventoryCheckOverallList-back" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="inventoryCheckOverallList-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span class="billno">单号：{{ basicInfoData&&basicInfoData.checkWarehouseNo }}</span>
          <a-tag color="blue">{{ basicInfoData&&basicInfoData.stateDictValue }}</a-tag>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="inventoryCheckOverallList-cont">
        <a-collapse>
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions :column="3">
              <a-descriptions-item label="盘点类型">{{ (basicInfoData&&basicInfoData.checkTypeDictValue) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="创建人">{{ (basicInfoData&&basicInfoData.creatorName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="创建时间">{{ (basicInfoData&&basicInfoData.createDate) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="审核时间">{{ (basicInfoData&&basicInfoData.checkSuperviseTime) || '--' }}</a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <a-card size="small" :bordered="false" class="inventoryCheckOverallList-cont">
        <!-- 总计 -->
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            总款数： <strong>{{ productTotal&&(productTotal.totalCategory || productTotal.totalCategory==0) ? productTotal.totalCategory : '--' }}</strong> ，
            总数量： <strong>{{ productTotal&&(productTotal.stockQty || productTotal.stockQty==0) ? productTotal.stockQty : '--' }}</strong> ，
            总成本： <strong>{{ productTotal&&(productTotal.checkCost || productTotal.checkCost==0) ? '￥'+productTotal.checkCost : '--' }}</strong>
          </div>
        </a-alert>
        <!-- 筛选条件 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="8" :sm="24">
                <a-form-item label="产品编码" prop="productCode">
                  <a-input id="inventoryCheckOverallList-productCode" v-model.trim="queryParam.productCode" placeholder="请输入" allowClear />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="产品名称" prop="productName">
                  <a-input id="inventoryCheckOverallList-productName" v-model.trim="queryParam.productName" placeholder="请输入" allowClear />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24" style="margin-bottom: 10px;">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="inventoryCheckOverallList-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="inventoryCheckOverallList-reset">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
        </s-table>
      </a-card>
    </a-spin>
    <div class="affix-cont">
      <a-button
        type="primary"
        id="inventoryCheckOverallList-submit"
        size="large"
        :disabled="spinning"
        class="button-primary"
        @click="handleSubmit"
        style="padding: 0 60px;">提交</a-button>
    </div>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { checkWarehouseDetailList, checkWarehouseDetailCount, checkWarehouseSubmit, checkWarehouseDetail } from '@/api/checkWarehouse'
export default {
  name: 'CheckWarehouseOverall',
  components: { STable, VSelect },
  mixins: [commonMixin],
  props: {},
  data () {
    return {
      spinning: false,
      disabled: false,
      queryParam: {
        productCode: '',
        productName: ''
      },
      basicInfoData: null, // 基础信息
      productTotal: null,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '36%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productUnit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存数量', dataIndex: 'stockQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '最后入库时间', dataIndex: 'lastStockTime', width: '16%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return checkWarehouseDetailList(Object.assign(parameter, this.queryParam, { checkWarehouseSn: this.$route.params.sn })).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.getProduceTotal()
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.queryParam.productName = ''
      this.$refs.table.refresh(true)
    },
    //  提交
    handleSubmit () {
      const _this = this
      _this.spinning = true
      checkWarehouseSubmit({ id: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          setTimeout(() => {
            _this.handleBack()
            _this.spinning = false
          }, 1000)
        } else {
          _this.spinning = false
        }
      })
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/inventoryManagement/inventoryChecking/list' })
    },
    // 查询基础信息
    getDetail () {
      checkWarehouseDetail({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data || null
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 已选产品总计查询
    getProduceTotal () {
      checkWarehouseDetailCount(Object.assign(this.queryParam, { checkWarehouseSn: this.$route.params.sn })).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = 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()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .inventoryCheckOverallList-wrap{
    position: relative;
    height: 100%;
    padding-bottom: 51px;
    box-sizing: border-box;
    .inventoryCheckOverallList-cont, .inventoryCheckOverallList-back{
      margin-bottom: 10px;
    }
    .billno{
      font-size: 16px;
      font-weight: bold;
      margin: 0 15px;
    }
    >.ant-spin-nested-loading{
      overflow-y: scroll;
      height: 100%;
    }
  }
</style>
