<template>
  <div class="inventoryQueryWarehouseDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-card size="small" :bordered="false" class="inventoryQueryWarehouseDetail-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" @change="dateChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="关联单号">
                  <a-input id="inventoryQueryWarehouseDetail-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入关联单号"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="下推单号">
                  <a-input id="inventoryQueryWarehouseDetail-bizSubNo" v-model.trim="queryParam.bizSubNo" allowClear placeholder="请输入下推单号"/>
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-item label="变动类型">
                    <v-select
                      v-model="queryParam.flowType"
                      ref="flowType"
                      id="inventoryQueryWarehouseDetail-flowType"
                      code="STOCK_FLOW_TYPE"
                      placeholder="请选择变动类型"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="单据类型">
                    <v-select
                      v-model="queryParam.bizType"
                      ref="bizType"
                      id="inventoryQueryWarehouseDetail-bizType"
                      code="STOCK_FLOW_BIZ_TYPE"
                      placeholder="请选择单据类型"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="单位名称">
                    <a-input id="inventoryQueryWarehouseDetail-unitName" v-model.trim="queryParam.unitName" allowClear placeholder="请输入单位名称"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="出入库状态">
                    <v-select
                      v-model="queryParam.state"
                      ref="state"
                      id="inventoryQueryWarehouseDetail-state"
                      code="STOCK_FLOW_STATE"
                      placeholder="请选择出入库状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
              </template>
              <a-col :md="6" :sm="24" style="text-align:center;">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="inventoryQueryWarehouseDetail-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="inventoryQueryWarehouseDetail-reset">重置</a-button>
                <a @click="advanced=!advanced" style="margin-left: 5px" id="inventoryQueryList-advanced">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 合计 -->
        <a-alert type="info" style="margin:10px 0">
          <div class="ftext" slot="message">
            当前库存总数量：<strong>{{ (productTotal&&(productTotal.totalQty || productTotal.totalQty==0)) ? productTotal.totalQty : '--' }}个</strong>；
            入库总数量：<strong>{{ (productTotal&&(productTotal.totalPutQty || productTotal.totalPutQty==0)) ? productTotal.totalPutQty : '--' }}个</strong>；
            出库总数量：<strong>{{ (productTotal&&(productTotal.totalOutQty || productTotal.totalOutQty==0)) ? productTotal.totalOutQty : '--' }}个</strong>；
            <span v-if="$hasPermissions('B_inventoryQuery_rkDetail_costPrice')">当前库存总成本：<strong>{{ (productTotal&&(productTotal.totalCost || productTotal.totalCost==0)) ? toThousands(productTotal.totalCost) : '--' }}</strong>。</span>
          </div>
        </a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
        >
          <template slot="productInfo" slot-scope="text, record">
            <div class="tr-row">
              <div class="tr-title">{{ record.productName }}</div>
              <div>产品编码：<span class="code">{{ record.productCode }}</span></div>
              <div>原厂编码：<span>{{ record.productOrigCode ||'--' }}</span></div>
              <div>批次号：<span>{{ record.stockBatchNo ||'--' }}</span></div>
              <div>单据类型：<span>{{ record.bizTypeDictValue ||'--' }}</span></div>
              <div>关联单号：<span>{{ record.bizNo ||'--' }}</span></div>
              <div>下推单号：<span>{{ record.bizSubNo ||'--' }}</span></div>
              <div>单据审核时间：<span>{{ record.auditTime ||'--' }}</span></div>
              <div>单位名称：<span>{{ record.unitName ||'--' }}</span></div>
              <div>仓库仓位：<span>{{ record.warehouseName }}</span>><span>{{ record.warehouseLocationName }}</span></div>
              <div style="display: flex;">
                <div>数量：
                  <span :class="record.flowType=='PUT' ? 'green':'red'" style="margin: 0;">
                    <b v-if="record.qty!=0">{{ record.flowType=='PUT' ? '+':'-' }}</b>
                    {{ record.qty }}
                  </span>
                </div>
                <div style="margin-left: 15px;" v-if="$hasPermissions('B_inventoryQuery_rkDetail_costPrice')">
                  总成本：<span>{{ toThousands(record.totalCost) }}</span>
                </div>
                <div style="margin-left: 15px;" v-if="$hasPermissions('B_inventoryQuery_rkDetail_salesPrice')">
                  总售价：
                  <span v-if="record.bizType!='SPARE_PARTS_RETURN'"> {{ toThousands(record.totalPrice) }}</span>
                  <span v-else>--</span>
                </div>
              </div>
              <div>出入库状态：<span>{{ record.stateDictValue }}</span></div>
            </div>
          </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 { stockFlowCount, stockFlowList } from '@/api/stock'
export default {
  name: 'InventoryQueryWarehouseDetail',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      spinning: false,
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        beginDate: '', // 开始时间
        endDate: '', // 结束时间
        bizType: undefined, // 单据类型
        flowType: undefined, // 变动类型
        unitName: '', // 单位
        bizNo: '', // 关联单号
        bizSubNo: '', // 下推单号
        warehouseSn: undefined, // 仓库sn
        state: undefined // 单据状态
      },
      disabled: false, //  查询、重置按钮是否可操作
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam, { productSn: this.$route.params.sn, warehouseSn: this.$route.params.warehouseSn })
        return stockFlowList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getTotal(params)
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              if (data.list[i].flowType == 'PUT') {
                data.list[i].qty = data.list[i].putQty
              } else if (data.list[i].flowType == 'OUT') {
                data.list[i].qty = data.list[i].outQty
              }
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      exportLoading: false, // 导出按钮loading
      productTotal: null //  合计
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '40px', align: 'center' },
        { title: '产品信息', scopedSlots: { customRender: 'productInfo' }, width: '90%', align: 'center' }
      ]
      return arr
    }
  },
  methods: {
    //  创建时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 合计
    getTotal (param) {
      stockFlowCount(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.bizType = undefined
      this.queryParam.flowType = undefined
      this.queryParam.unitName = ''
      this.queryParam.bizSubNo = ''
      this.queryParam.bizNo = ''
      this.queryParam.warehouseSn = undefined
      this.queryParam.state = undefined
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 268
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.resetSearchForm()
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.resetSearchForm()
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .inventoryQueryWarehouseDetail-wrap{
    .inventoryQueryWarehouseDetail-back{
      margin-bottom: 6px;
    }
    .inventoryQueryWarehouseDetail-cont{
      margin-bottom: 6px;
    }
    .tr-row{
      text-align: left;
      .tr-title{
        font-weight: bold;
      }
      > div{
        padding: 2px 0;
        color: #666;
        span{
          color: #222;
        }
        .green{
          color: #19be6b;
        }
        .red{
          color: #ed1c24;
        }
        .code{
          color: #00aaff;
        }
        .nums{
          color: #ff0000;
        }
      }
    }
  }
</style>
