<template>
  <div class="allocateBillDetail-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="allocateBillDetail-back" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="allocateBillDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        <a-button
          v-if="isEdit"
          type="primary"
          size="small"
          style="background-color: #1890ff;margin-left: 20px;border: #1890ff;"
          id="allocateBillDetail-edit-btn"
          @click.stop="handleEdit">编辑</a-button>
      </template>
      <!-- 操作区，位于 title 行的行尾 -->
      <template slot="extra">
        <a-button key="2" id="allocateBillDetail-preview-btn">打印预览</a-button>
        <a-button key="1" type="primary" id="allocateBillDetail-print-btn">快速打印</a-button>
      </template>
    </a-page-header>
    <a-card size="small" :bordered="false" class="allocateBillDetail-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1" header="基础信息">
          <a-descriptions :column="3">
            <a-descriptions-item label="调往对象">{{ (basicInfoData&&basicInfoData.targetName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="调拨单号">{{ (basicInfoData&&basicInfoData.allocateNo) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="调拨类型">{{ (basicInfoData&&basicInfoData.allocateTypeName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="业务状态">{{ (basicInfoData&&basicInfoData.stateDictValue) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="备注">{{ (basicInfoData&&basicInfoData.remark) || '--' }}</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <a-card size="small" :bordered="false" class="allocateBillDetail-cont">
      <!-- 总计 -->
      <a-alert type="info" showIcon style="margin-bottom:15px">
        <div slot="message">总数量：<strong>{{ (productTotal&&productTotal.totalQty) || 0 }}</strong> ，总成本（￥）：<strong>{{ (productTotal&&productTotal.totalCost) || 0 }}</strong> ，总售价（￥）：<strong>{{ (productTotal&&productTotal.totalPrice) || 0 }}</strong></div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1335 }"
        bordered>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { getOperationalPrecision } from '@/libs/tools.js'
import { allocateBillDetailList, allocateBillDetail, allocateBillDetailCount } from '@/api/allocateBill'
export default {
  components: { STable, VSelect },
  data () {
    return {
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'productEntity.code', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productEntity.origCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productEntity.unit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'cost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '售价', dataIndex: 'price', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '调出数量', dataIndex: 'qty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '成本小计(￥)', dataIndex: 'costSubtotal', width: 115, align: 'center' },
        { title: '售价小计(￥)', dataIndex: 'priceSubtotal', width: 100, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return allocateBillDetailList(Object.assign(parameter, { allocateSn: 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
            // 成本小计  由于数据库内小数位数为4位，页面则需显示2位。因此会做小数运算精度处理
            data.list[i].costSubtotal = getOperationalPrecision(data.list[i].cost, data.list[i].qty)
            data.list[i].priceSubtotal = getOperationalPrecision(data.list[i].price, data.list[i].qty)
          }
          return data
        })
      },
      basicInfoData: null, //  基本信息
      productTotal: null //  合计
    }
  },
  computed: {
    isEdit () {
      return (this.basicInfoData && this.basicInfoData.state == 'WAIT_SUBMIT') || (this.basicInfoData && this.basicInfoData.state == 'WAIT_AUDIT')
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/allocationManagement/transferOut/list' })
    },
    // 基本信息
    getDetail () {
      allocateBillDetail({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 合计
    getDetailCount () {
      allocateBillDetailCount({ allocateSn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  编辑
    handleEdit () {
      this.$router.push({ path: `/allocationManagement/transferOut/edit/${this.basicInfoData.id}/${this.basicInfoData.allocateSn}/${this.basicInfoData.targetName}` })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getDetail()
      vm.getDetailCount()
    })
  }
}
</script>

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