<template>
  <div class="salesDetail-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="salesDetail-cont">
      <template slot="subTitle">
        <a href="javascript:;" @click="handleBack"><a-icon type="left"></a-icon>返回列表</a>
      </template>
      <template slot="extra">
        <span>打印字段：</span>
        <a-select key="1" style="width:150px" id="salesDetail-pritKey" placeholder="请选择打印字段" allowClear>
          <a-select-option v-for="item in pritKey" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
        </a-select>
        <span><i style="color: red;">*</i>打印模板：</span>
        <a-select key="2" style="width:150px" id="salesDetail-pritKey" placeholder="请选择打印模板" allowClear>
          <a-select-option v-for="item in pritModal" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
        </a-select>
        <a-button key="3" id="salesDetail-preview-btn">打印预览</a-button>
        <a-button key="4" type="primary" id="salesDetail-print-btn">快速打印</a-button>
        <a-button key="5" id="salesDetail-export-btn">导出</a-button>
      </template>
    </a-page-header>
    <!-- 基础信息 -->
    <a-card size="small" :bordered="false" class="salesDetail-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1" header="基础信息">
          <a-descriptions size="small" :column="3" v-if="detailData">
            <a-descriptions-item label="客户名称">{{ detailData.buyerName || '--' }}</a-descriptions-item>
            <a-descriptions-item label="客户地址">{{ detailData.shippingAddressProvinceName || '--' }}{{ detailData.shippingAddressCityName || '--' }}{{ detailData.shippingAddressCountyName || '--' }}{{ detailData.shippingAddress || '--' }}</a-descriptions-item>
            <a-descriptions-item label="支付方式">{{ detailData.payType || '--' }}</a-descriptions-item>
            <a-descriptions-item label="收款方式">{{ detailData.settleStyleSn || '--' }}</a-descriptions-item>
            <a-descriptions-item label="联系电话">{{ detailData.consigneeTel || '--' }}</a-descriptions-item>
            <a-descriptions-item label="备注">{{ detailData.buyerName || '--' }}</a-descriptions-item>
            <a-descriptions-item label="销售单号">{{ detailData.salesBillNo || '--' }}</a-descriptions-item>
            <a-descriptions-item label="制单人">{{ detailData.operatorName || '--' }}</a-descriptions-item>
            <a-descriptions-item label="业务员">{{ detailData.salesManName || '--' }}</a-descriptions-item>
            <a-descriptions-item label="业务状态">{{ detailData.auditStatus || '--' }}</a-descriptions-item>
            <a-descriptions-item label="财务状态">{{ detailData.financialStatus || '--' }}</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <a-card size="small" :bordered="false" class="pages-wrap">
      <!-- alert -->
      <a-alert type="info" style="margin-bottom: 15px;" v-if="detailData">
        <div slot="message">
          总款数：<strong>{{ detailData.totalCategory || 0 }}</strong>；总数量：<strong>{{ detailData.totalQty || 0 }}</strong>；总赠品数量：<strong>{{ detailData.totalGiftNum || 0 }}</strong>；急件总款数：<strong>{{ detailData.urgentTotalCategory || 0 }}</strong>；急件总数量：<strong>{{ detailData.urgentTotalQty || 0 }}</strong>；<br/>
          总售价：<strong>{{ detailData.totalAmount || 0 }}</strong>；总成本：<strong>{{ detailData.totalCost || 0 }}</strong>；总毛利：<strong>{{ detailData.grossProfit || 0 }}</strong>；折后总售价：<strong>{{ detailData.discountedAmount || 0 }}</strong>；折扣：<strong>{{ detailData.discountRate || 0 }}%</strong>；折扣金额：<strong>{{ detailData.discountAmount || 0 }}</strong>；
        </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1660 }"
        bordered>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { getOperationalPrecision } from '@/libs/tools.js'
import { STable, VSelect } from '@/components'
import { salesDetail } from '@/api/sales'
import { salesDetailList } from '@/api/salesDetail'
export default {
  name: 'SalesDetail',
  components: { STable, VSelect },
  data () {
    return {
      disabled: false,
      pritKey: [
        { id: 0, name: '打印促销' },
        { id: 1, name: '打印支付方式' },
        { id: 2, name: '打印收款方式' }
      ],
      pritModal: [
        { id: 0, name: '普通模板一' },
        { id: 1, name: '普通模板二' },
        { id: 2, name: '仓库模板一' }
      ],
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'productCode', width: 140, align: 'center' },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'costPrice', 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: 'discountedPrice', 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: 'productOrigUnit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '售价小计', dataIndex: 'salePriceSubtotal', width: 100, align: 'center' },
        { title: '折后小计', dataIndex: 'discountedAmount', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '折扣金额', dataIndex: 'discountAmount', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓库', dataIndex: 'warehouseName', width: 100, align: 'center' },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: 100, align: 'center' },
        { title: '销售类型', dataIndex: 'salesBillType', width: 100, align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return salesDetailList(Object.assign(parameter, { salesBillSn: 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
            const productCode = (data.list[i].productEntity && data.list[i].productEntity.code) || (data.list[i].dealerProductEntity && data.list[i].dealerProductEntity.code)
            const productName = (data.list[i].productEntity && data.list[i].productEntity.name) || (data.list[i].dealerProductEntity && data.list[i].dealerProductEntity.name)
            const productOrigCode = (data.list[i].productEntity && data.list[i].productEntity.origCode) || (data.list[i].dealerProductEntity && data.list[i].dealerProductEntity.origCode)
            const productOrigUnit = (data.list[i].productEntity && data.list[i].productEntity.unit) || (data.list[i].dealerProductEntity && data.list[i].dealerProductEntity.unit)
            data.list[i].productCode = productCode || '--'
            data.list[i].productName = productName || '--'
            data.list[i].productOrigCode = productOrigCode || '--'
            data.list[i].productOrigUnit = productOrigUnit || '--'
            data.list[i].warehouseName = data.list[i].warehouseEntity && data.list[i].warehouseEntity.name || '--'
            data.list[i].warehouseLocationName = data.list[i].warehouseLocationEntity && data.list[i].warehouseLocationEntity.name || '--'
            // 售价小计  由于数据库内小数位数为4位，页面则需显示2位。因此会做小数运算精度处理
            data.list[i].salePriceSubtotal = getOperationalPrecision(data.list[i].price, data.list[i].qty)
          }
          this.disabled = false
          return data
        })
      },
      detailData: null //  详情数据
    }
  },
  methods: {
    //  返回
    handleBack () {
      this.$router.push({ name: 'salesQueryList' })
    },
    //  详情
    getDetail () {
      salesDetail({ id: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        } else {
          this.detailData = null
        }
      })
    }
  },
  mounted () {
    this.getDetail()
  }
}
</script>

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