<template>
  <div class="inventoryQueryWarehouseDetail-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="inventoryQueryWarehouseDetail-back">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="inventoryQueryWarehouseDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
      </template>
    </a-page-header>
    <a-card size="small" :bordered="false" class="inventoryQueryWarehouseDetail-cont">
      <!-- 搜索条件 -->
      <div 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="变动类型">
                <v-select
                  v-model="queryParam.flowType"
                  ref="flowType"
                  id="warehousingAuditList-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-select id="inventoryQueryWarehouseDetail-warehouseSn" placeholder="请选择仓库" v-model="queryParam.warehouseSn" >
                  <a-select-option v-for="item in warehouseList" :key="item.warehouseSn" :value="item.warehouseSn">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <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-unitName" v-model.trim="queryParam.unitName" allowClear placeholder="请输入单位名称"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="inventoryQueryWarehouseDetail-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="inventoryQueryWarehouseDetail-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 合计 -->
      <a-alert type="info" showIcon style="margin-bottom:10px">
        <div class="ftext" slot="message">
          当前库存总数量（个）：<strong>{{ (productTotal&&(productTotal.totalQty || productTotal.totalQty==0)) ? productTotal.totalQty : '--' }}</strong>；
          当前库存总成本(￥)：<strong>{{ (productTotal&&(productTotal.totalCost || productTotal.totalCost==0)) ? productTotal.totalCost : '--' }}</strong>。
        </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1760 }"
        bordered>
        <!-- 数量 -->
        <template slot="qty" slot-scope="text, record">
          <p :class="record.flowType=='PUT' ? 'green':'red'" style="margin: 0;">
            <span v-if="record.qty!=0">{{ record.flowType=='PUT' ? '+':'-' }}</span>
            {{ record.qty }}
          </p>
        </template>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { stockFlowList, stockFlowCount } from '@/api/stockFlow'
import { warehouseAllList } from '@/api/warehouse'
export default {
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        bizType: undefined,
        flowType: undefined,
        unitName: '',
        warehouseSn: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'product.code', width: 140, align: 'center', fixed: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'product.origCode', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '批次号', dataIndex: 'stockBatchNo', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单据类型', dataIndex: 'bizTypeDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单据审核时间', dataIndex: 'auditTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位名称', dataIndex: 'unitName', width: 200, align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: 140, align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: 140, align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '数量', scopedSlots: { customRender: 'qty' }, width: 100, align: 'center' },
        { title: '总成本', dataIndex: 'totalCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总售价', dataIndex: 'totalPrice', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, this.queryParam, { productSn: this.$route.params.sn })
        return stockFlowList(params).then(res => {
          const 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
            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.getTotal(params)
          return data
        })
      },
      warehouseList: [], //  仓库  下拉数据
      productTotal: null //  合计
    }
  },
  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 () {
      if (this.advanced) {
        this.$refs.rangeDate.resetDate()
      }
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.bizType = undefined
      this.queryParam.flowType = undefined
      this.queryParam.unitName = ''
      this.queryParam.warehouseSn = undefined
      this.$refs.table.refresh(true)
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/inventoryManagement/inventoryQuery/list', query: { closeLastOldTab: true } })
    },
    // 仓库下拉数据
    getWarehouseList (type) {
      warehouseAllList({}).then(res => {
        if (res.status == 200) {
          this.warehouseList = res.data
        } else {
          this.warehouseList = []
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getWarehouseList()
    })
  }
}
</script>

<style lang="less">
  .inventoryQueryWarehouseDetail-wrap{
    .inventoryQueryWarehouseDetail-back{
      margin-bottom: 15px;
    }
    .inventoryQueryWarehouseDetail-cont{
      margin-bottom: 15px;
    }
    .green{
      color: #19be6b;
    }
    .red{
      color: #ed1c24;
    }
  }
</style>
