<template>
  <div class="warehouseAllocationDetail-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="warehouseAllocationDetail-back" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <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>
      <!-- 操作区，位于 title 行的行尾 -->
      <!-- <template slot="extra">
        <a-button key="2" id="warehouseAllocationDetail-preview-btn">打印预览</a-button>
        <a-button key="1" type="primary" id="warehouseAllocationDetail-print-btn">快速打印</a-button>
        <a-button key="3" type="danger" @click="handleExport" :loading="exportLoading" id="warehouseAllocationDetail-export-btn">导出</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" showIcon style="margin-bottom:15px">
        <div slot="message">总款数： <strong>{{ productTotalCategory }}</strong> ，总数量： <strong>{{ productTotalQty }}</strong> ，总成本：￥<strong>{{ productTotalCost }}</strong></div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1335 }"
        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 moment from 'moment'
import { STable, VSelect } from '@/components'
import { allocWarehouseDetailSn, allocWarehouseDetailCount, allocWarehouseDetailList } from '@/api/allocWarehouse.js'
export default {
  components: { STable, VSelect },
  data () {
    return {
      exportLoading: false, // 导出loading
      queryParam: {
        allocationWarehouseSn: this.$route.params.sn
      },
      id: this.$route.params.id, // 单据id
      productTotalCost: 0, // 总成本
      productTotalQty: 0, // 总数量
      productTotalCategory: 0, // 总款项
      warehouse: null, // 基础信息
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'productCode', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: 200, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '类别', scopedSlots: { customRender: 'productType' }, width: 160, align: 'center' },
        { title: '数量', dataIndex: 'allocationQty', width: 100, align: 'center' },
        { title: '单位', dataIndex: 'productUnit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本小计（￥）', scopedSlots: { customRender: 'totalCost' }, width: 135, align: 'center' },
        { title: '调出仓位', dataIndex: 'outWarehouseLocationName', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调入仓位', dataIndex: 'putWarehouseLocationName', width: 140, align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return allocWarehouseDetailList(Object.assign(parameter, this.queryParam)).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' })
    },
    //  新增/编辑
    handleEdit () {
      this.$router.push({ path: `/allocationManagement/warehouseAllocation/edit/${this.warehouse.id}/${this.warehouse.allocationWarehouseSn}` })
    },
    // 查询基础信息
    getHouse () {
      allocWarehouseDetailSn({ sn: 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(this.queryParam).then(res => {
        if (res.status == 200) {
          this.productTotalCost = res.data.productTotalCost || 0
          this.productTotalQty = res.data.productTotalQty || 0
          this.productTotalCategory = res.data.productTotalCategory || 0
        } else {
          this.productTotalCost = 0
          this.productTotalQty = 0
          this.productTotalCategory = 0
        }
      })
    },
    //  导出
    handleExport () {
      const params = this.queryParam
      this.exportLoading = true
      // customerBundleExportDelay(params).then(res => {
      //   this.exportLoading = false
      //   this.download(res)
      // })
    },
    download (data) {
      if (!data) { return }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const a = moment().format('YYYYMMDDHHmmss')
      const fname = '仓库调拨' + a
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getHouse()
    })
  }
}
</script>

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