<template>
  <div class="inventoryCheckDetail-wrap" :style="{paddingBottom:detail&&(detail.state!='FINISH' && detail.state!='DELETE')?'51px':'0px'}">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" @back="handleBack" class="inventoryCheckDetail-cont" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="inventoryCheckDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span class="billno">单号：{{ detail&&detail.checkWarehouseNo }}</span>
          <span class="state-c" v-if="detail&&detail.state=='DELETE'">已作废</span>
          <a-button size="small" type="link" class="button-default" @click="showDetail=!showDetail">
            <a-icon :type="showDetail ? 'eye-invisible' : 'eye'"/> {{ showDetail?'隐藏':'查看' }}信息
          </a-button>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="inventoryCheckDetail-cont" v-show="showDetail">
        <a-descriptions :column="4">
          <a-descriptions-item label="创建人">{{ (detail&&detail.creatorName) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="创建时间">{{ (detail&&detail.createDate) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="盘点类型">{{ (detail&&detail.checkTypeDictValue) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="仓库">{{ (detail&&detail.warehouseName) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="盘点状态">{{ (detail&&detail.checkStateDictValue) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="盘点人">{{ (detail&&detail.checkPersonName) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="盘点时间">{{ (detail&&detail.checkTime) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="监盘状态">{{ (detail&&detail.checkSuperviseStateDictValue) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="监盘人">{{ (detail&&detail.checkSupervisePersonName) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="监盘时间">{{ (detail&&detail.checkSuperviseTime) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="财务确认时间">{{ (detail&&detail.financeAuditTime) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="作废时间" v-if="detail&&detail.state=='DELETE'">{{ (detail&&detail.deleteTime) || '--' }}</a-descriptions-item>
        </a-descriptions>
      </a-card>
      <!-- 产品详情 -->
      <a-card size="small" :bordered="false" class="inventoryCheckDetail-cont">
        <!-- 总计 -->
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            库存数量：<strong>{{ productTotal && (productTotal.stockQty || productTotal.stockQty==0) ? productTotal.stockQty : '--' }}</strong>，
            盘点数量：<strong>{{ productTotal && (productTotal.checkQty || productTotal.checkQty==0) ? productTotal.checkQty : '--' }}</strong>，
            监盘数量：<strong>{{ productTotal && (productTotal.checkSuperviseQty || productTotal.checkSuperviseQty==0) ? productTotal.checkSuperviseQty : '--' }}</strong>
          </div>
        </a-alert>
        <!-- 搜索条件 -->
        <div class="table-page-search-wrapper">
          <a-row :gutter="15">
            <a-col :span="18">
              <a-form-model :model="queryParam" layout="inline" @keyup.enter.native="getList(1)" >
                <a-row :gutter="15">
                  <a-col :md="8" :sm="24">
                    <a-form-model-item label="产品编码">
                      <a-input v-model.trim="queryParam.productCode" allowClear placeholder="输入产品编码" />
                    </a-form-model-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-button type="primary" @click="getList(1)" :disabled="disabled" id="inventoryCheckDetail-refresh">查询</a-button>
                    <a-button style="margin-left: 5px" @click="resetSearchForm" id="inventoryCheckDetail-reset">重置</a-button>
                  </a-col>
                </a-row>
              </a-form-model>
            </a-col>
            <a-col :span="6">
              <div style="float:right;overflow: hidden;margin-top:6px;">
                <a-checkbox v-model="foldState" @change="foldStateChange"><span style="display: inline-block;margin-top: 1px;">折叠列表</span></a-checkbox>
              </div>
            </a-col>
          </a-row>
        </div>
        <!-- 列表 -->
        <a-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id + record.type"
          :columns="columns"
          :pagination="false"
          :data-source="list"
          :loading="loading"
          @expand="handleExpand"
          :expandedRowKeys="expandedRowKeys"
          :expandRowByClick="true"
          bordered>
        </a-table>
        <!-- 分页 -->
        <tablePagination
          ref="tablePagination"
          :total="paginationProps.total"
          :current="paginationProps.current"
          :pageSize="paginationProps.pageSize"
          @changePageSize="changePageSize"
          @changePage="changePage" />
      </a-card>
    </a-spin>
    <div class="affix-cont" v-if="detail&&(detail.state!='FINISH' && detail.state!='DELETE') && $hasPermissions('B_inventoryCheckVoid')">
      <a-button
        type="primary"
        id="inventoryCheckDetail-submit-btn"
        size="large"
        class="button-primary"
        :disabled="spinning"
        @click="handleSubmit"
        style="padding: 0 60px;">作废盘点单</a-button>
    </div>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import tablePagination from '@/views/common/tablePagination.vue'
import { checkWarehouseDetail, checkWarehouseDetailList, checkWarehouseDetailCount, checkWarehouseDelete } from '@/api/checkWarehouse'
export default {
  name: 'InventoryCheckDetail',
  mixins: [commonMixin],
  components: { STable, VSelect, tablePagination },
  data () {
    return {
      spinning: false,
      disabled: false,
      loading: false,
      showDetail: false,
      queryParam: {
        productCode: ''
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '产品名称', dataIndex: 'productName', width: '16%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'productUnit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '12%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '11%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '入库时间', dataIndex: 'lastStockTime', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '批次', dataIndex: 'stockBatchNo', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存数量', dataIndex: 'stockQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盘点数量', dataIndex: 'checkQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '监盘数量', dataIndex: 'checkSuperviseQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      list: [],
      paginationProps: {
        total: 0, //  分页总条数
        current: 1,
        pageSize: 20
      },
      expandedRowKeys: [],
      detail: null, //  详情数据
      foldState: false, //  是否折叠列表
      productTotal: null //  合计
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/inventoryManagement/inventoryCheck/list/0', query: { closeLastOldTab: true } })
    },
    // 重置
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.getList(1)
    },
    // 作废盘点单
    handleSubmit () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '作废后数据无法恢复，确认作废吗？',
        centered: true,
        closable: true,
        onOk () {
          _this.spinning = true
          checkWarehouseDelete({ checkWarehouseSn: _this.$route.params.sn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.spinning = false
              _this.$router.push({ path: '/inventoryManagement/inventoryCheck/list/1', query: { closeLastOldTab: true } })
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 获取列表数据
    getList (pageNo) {
      this.disabled = true
      this.paginationProps.current = pageNo || this.paginationProps.current
      const params = {
        pageNo: this.paginationProps.current,
        pageSize: this.paginationProps.pageSize,
        checkWarehouseSn: this.$route.params.sn,
        productCode: this.queryParam.productCode,
        // checkWarehouseSn: this.detail.warehouseSn
      }
      this.loading = true
      checkWarehouseDetailList(params).then(res => {
        this.loading = false
        if (res.status == 200) {
          this.getCount(params)
          const data = res.data
          this.paginationProps.total = Number(res.data.count) || 0
          this.paginationProps.current = data.pageNo
          const no = (data.pageNo - 1) * data.pageSize
          this.expandedRowKeys = []
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
            if (!this.foldState) {
              this.expandedRowKeys.push(data.list[i].id + data.list[i].type)
            }
          }
          this.list = data.list
          this.disabled = false
        } else {
          this.paginationProps.total = 0
          this.paginationProps.current = 1
          this.paginationProps.pageSize = 20
          this.list = []
        }
      })
    },
    getCount (params) {
      checkWarehouseDetailCount(params).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  分页  一页多少条change
    changePageSize (current, pageSize) {
      this.paginationProps.current = current
      this.paginationProps.pageSize = pageSize
      this.getList()
    },
    //  分页 页码change
    changePage (current) {
      this.paginationProps.current = current
      this.getList()
    },
    //  详情
    getDetail () {
      checkWarehouseDetail({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.detail = res.data
          this.resetSearchForm()
        } else {
          this.detail = null
        }
      })
    },
    // 展开收起
    handleExpand (expanded, record) {
      if (record.type == 'superior') { // 只处理父级
        if (expanded) {
          this.expandedRowKeys.push(record.id + record.type)
        } else {
          if (this.expandedRowKeys.length > 0) {
            const expandedRowKeys = this.expandedRowKeys.filter(RowKey => RowKey !== (record.id + record.type))
            this.expandedRowKeys = expandedRowKeys
          }
        }
      }
    },
    // 折叠列表
    foldStateChange (e) {
      if (e.target.checked) {
        this.expandedRowKeys = []
      } else {
        this.expandedRowKeys = []
        this.list.map(item => {
          this.expandedRowKeys.push(item.id + item.type)
        })
      }
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getDetail()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getDetail()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .inventoryCheckDetail-wrap{
    position: relative;
    height: 100%;
    padding-bottom: 51px;
    box-sizing: border-box;
    >.ant-spin-nested-loading{
      overflow-y: scroll;
      height: 100%;
    }
    .inventoryCheckDetail-cont{
      margin-bottom: 6px;
      .billno{
        font-size: 16px;
        font-weight: bold;
        margin: 0 15px;
      }
      .state-c{
        color: #ed1c24;
        font-size: 13px;
        font-weight: bold;
        margin: 0 15px 0 0;
      }
    }
  }
</style>
