<template>
  <div class="warehouseAllocationDetail-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="warehouseAllocationDetail-back" :style="{ padding: !outBizSn ? '16px 24px' : '0px 24px' }" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle" v-if="!outBizSn">
        <a id="warehouseAllocationDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        <a-button
          v-if="warehouse&&warehouse.stateDictValue!='已完结'&&$hasPermissions('B_warehouseAllocationEdit')"
          type="primary"
          size="small"
          style="background-color: #1890ff;margin-left: 20px;border: #1890ff;"
          id="warehouseAllocationEdit-btn"
          @click.stop="handleEdit">
          编辑
        </a-button>
      </template>
    </a-page-header>
    <!-- 基础信息 -->
    <a-card size="small" :bordered="false" class="warehouseAllocationDetail-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1" header="基础信息">
          <a-descriptions :column="3">
            <a-descriptions-item label="调出仓库">{{ (warehouse&&warehouse.outWarehouseName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="调入仓库">{{ (warehouse&&warehouse.putWarehouseName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="调拨单号">{{ (warehouse&&warehouse.allocationWarehouseNo) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="关联单号">{{ (warehouse&&warehouse.relevanceNo) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="单据状态">{{ (warehouse&&warehouse.stateDictValue) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="备注">{{ (warehouse&&warehouse.remark) || '--' }}</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <a-card size="small" :bordered="false" class="warehouseAllocationDetail-cont">
      <!-- 总计 -->
      <a-alert type="info" style="margin-bottom:10px">
        <div slot="message">
          总款数： <strong>{{ productTotal&&(productTotal.productTotalCategory || productTotal.productTotalCategory==0) ? productTotal.productTotalCategory : '--' }}</strong> ，
          总数量： <strong>{{ productTotal&&(productTotal.productTotalQty || productTotal.productTotalQty==0) ? productTotal.productTotalQty : '--' }}</strong> ，
          总成本： <strong>{{ productTotal&&(productTotal.productTotalCost || productTotal.productTotalCost==0) ? '￥'+productTotal.productTotalCost : '--' }}</strong>
        </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        bordered>
        <!-- 产品类别 -->
        <template slot="productType" slot-scope="text, record">
          <a-tooltip placement="top">
            <template slot="title">
              <span>{{ record.productTypeName }}</span>
            </template>
            {{ record.productTypeName3 }}
          </a-tooltip>
        </template>
        <!-- 成本小计 -->
        <template slot="totalCost" slot-scope="text, record">
          <span>{{ (record.allocationCost * record.allocationQty) || 0 }}</span>
        </template>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { allocWarehouseDetailSn, allocWarehouseDetailCount, allocWarehouseDetailList } from '@/api/allocWarehouse.js'
export default {
  components: { STable, VSelect },
  props: {
    outBizSn: { //  有值则为弹框，无值则为页面
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      exportLoading: false, // 导出loading
      warehouse: null, // 基础信息
      productTotal: null,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '类别', scopedSlots: { customRender: 'productType' }, width: '14%', align: 'center' },
        { title: '数量', dataIndex: 'allocationQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productUnit', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本小计(￥)', scopedSlots: { customRender: 'totalCost' }, width: '8%', align: 'center' },
        { title: '调出仓位', dataIndex: 'outWarehouseLocationName', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调入仓位', dataIndex: 'putWarehouseLocationName', width: '11%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return allocWarehouseDetailList(Object.assign(parameter, { allocationWarehouseSn: this.outBizSn || this.$route.params.sn })).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
          }
          this.getProduceTotal()
          this.disabled = false
          return data
        })
      }
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/allocationManagement/warehouseAllocation/list', query: { closeLastOldTab: true } })
    },
    //  新增/编辑
    handleEdit () {
      this.$router.push({ path: `/allocationManagement/warehouseAllocation/edit/${this.warehouse.id}/${this.warehouse.allocationWarehouseSn}` })
    },
    // 查询基础信息
    getHouse () {
      allocWarehouseDetailSn({ sn: this.outBizSn || this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.warehouse = res.data
          // this.queryParam.outWarehouseSn = res.data.outWarehouseSn
        } else {
          this.warehouse = null
        }
      })
    },
    // 已选产品总计查询
    getProduceTotal () {
      allocWarehouseDetailCount({ allocationWarehouseSn: this.outBizSn || 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.outBizSn) { // 页签刷新 或 为弹框时调用
      this.$refs.table.refresh(true)
      this.getHouse()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.$refs.table.refresh(true)
      this.getHouse()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .warehouseAllocationDetail-cont, .warehouseAllocationDetail-back{
    margin-bottom: 10px;
  }
</style>
