<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="收货地址">
              {{ shippingAddress }}
            </a-descriptions-item>
            <a-descriptions-item label="收款方式">{{ detailData.settleStyleSnDictValue || '--' }}</a-descriptions-item>
            <a-descriptions-item label="收货人">{{ detailData.salesBillExtEntity.consigneeName || '--' }}</a-descriptions-item>
            <a-descriptions-item label="收货电话">{{ detailData.salesBillExtEntity.consigneeTel || '--' }}</a-descriptions-item>
            <a-descriptions-item label="备注">{{ detailData.remarks || '--' }}</a-descriptions-item>
            <a-descriptions-item label="审核时间">{{ detailData.auditDate || '--' }}</a-descriptions-item>
            <a-descriptions-item label="业务状态">{{ detailData.billStatusDictValue || '--' }}</a-descriptions-item>
            <a-descriptions-item label="财务状态">{{ detailData.financialStatusDictValue || '--' }}</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: 10px;" v-if="detailData">
        <div slot="message">
          总销售数量：<strong>{{ detailData.totalQty || 0 }}</strong>；已取消数量：<strong>{{ detailData.totalCancelQty || 0 }}</strong>；已下推数量：<strong>{{ detailData.totalPushedQty || 0 }}</strong>；待下推数量：<strong>{{ detailData.totalUnpushedQty || 0 }}</strong>；已发货数量：<strong>{{ detailData.totalDispatchQty || 0 }}</strong>；待发货数量：<strong>{{ detailData.totalUndispatchQty || 0 }}</strong>；<br/>
          总售价：<strong>{{ detailData.totalAmount || 0 }}</strong>；总成本：<strong>{{ detailData.totalCost || 0 }}</strong>；总毛利：<strong>{{ detailData.grossProfit || 0 }}</strong>；已取消金额：<strong>{{ detailData.totalCancelAmount || 0 }}</strong>；已下推金额：<strong>{{ detailData.totalPushedAmount || 0 }}</strong>；待下推金额：<strong>{{ detailData.totalUnpushedAmount || 0 }}</strong>；已发货金额：<strong>{{ detailData.totalDispatchAmount || 0 }}</strong>；待发货金额：<strong>{{ detailData.totalUndispatchAmount || 0 }}</strong>；
        </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1440 }"
        bordered>
        <!-- 产品编码 -->
        <template slot="productCode" slot-scope="text, record">
          <a-badge count="促" v-if="record.promotionFlag == 1">
            <div style="padding-right: 15px;">{{ text }}</div>
          </a-badge>
          <span v-else>{{ text }}</span>
        </template>
      </s-table>
    </a-card>
    <a-card size="small" :bordered="false" class="footer-cont">
      <a-button
        type="primary"
        class="button-info"
        size="large"
        style="width: 150px;"
        id="salesDetail-edit-btn"
        v-if="detailData&&detailData.salesBillSource != 'PURCHASE' && (detailData.billStatus == 'WAIT_SUBMIT' || detailData.billStatus == 'WAIT_AUDIT' || detailData.billStatus == 'AUDIT_REJECT')&&$hasPermissions('B_salesEdit')"
        @click="handleEdit(record)"
      >
        编辑
      </a-button>
      <a-button
        size="large"
        style="width: 150px;"
        type="primary"
        class="button-info"
        id="salesDetail-edit-btn"
        v-if="detailData&&detailData.salesBillSource == 'PURCHASE' && (detailData.billStatus == 'WAIT_AUDIT'||detailData.billStatus == 'HQ_CHANGE')&&$hasPermissions('B_salesEdit')"
        @click="handleEdit(record)"
      >
        改单
      </a-button>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { salesDetailBySn } 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: 220, scopedSlots: { customRender: 'productCode' }, align: 'center' },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'showCost', width: 100, align: 'center', customRender: function (text) { return '￥' + (text || 0) } },
        { title: '销售价', dataIndex: 'price', width: 100, align: 'center', customRender: function (text) { return '￥' + (text || 0) } },
        { title: '单位', dataIndex: 'productOrigUnit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售数量', dataIndex: 'qty', width: 100, align: 'center', customRender: function (text) { return text || 0 } },
        { title: '已取消数', dataIndex: 'cancelQty', width: 100, align: 'center', customRender: function (text) { return text || 0 } },
        { title: '已下推数', dataIndex: 'pushedQty', width: 100, align: 'center', customRender: function (text) { return text || 0 } },
        { title: '待下推数', dataIndex: 'unpushedQty', width: 100, align: 'center', customRender: function (text) { return text || 0 } }
      ],
      // 加载数据方法 必须为 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 == ' ' ? '--' : productOrigCode
            data.list[i].productOrigUnit = productOrigUnit || '--'
          }
          this.disabled = false
          return data
        })
      },
      detailData: null //  详情数据
    }
  },
  computed: {
    shippingAddress () {
      const shippingAddrProvinceName = this.detailData && this.detailData.salesBillExtEntity && this.detailData.salesBillExtEntity.shippingAddrProvinceName ? this.detailData.salesBillExtEntity.shippingAddrProvinceName : ''
      const shippingAddrCityName = this.detailData && this.detailData.salesBillExtEntity && this.detailData.salesBillExtEntity.shippingAddrCityName ? this.detailData.salesBillExtEntity.shippingAddrCityName : ''
      const shippingAddrCountyName = this.detailData && this.detailData.salesBillExtEntity && this.detailData.salesBillExtEntity.shippingAddrCountyName ? this.detailData.salesBillExtEntity.shippingAddrCountyName : ''
      const shippingAddr = this.detailData && this.detailData.salesBillExtEntity && this.detailData.salesBillExtEntity.shippingAddr ? this.detailData.salesBillExtEntity.shippingAddr : ''
      if (!shippingAddrProvinceName && !shippingAddrCityName && !shippingAddrCountyName && !shippingAddr) {
        return '--'
      } else {
        return shippingAddrProvinceName + shippingAddrCityName + shippingAddrCountyName + shippingAddr
      }
    }
  },
  methods: {
    //  返回
    handleBack () {
      this.$router.push({ name: 'salesQueryList' })
    },
    // 编辑
    handleEdit () {
      const row = this.detailData
      this.$router.push({ name: 'salesEdit', params: { sn: row.salesBillSn } })
    },
    //  详情
    getDetail () {
      salesDetailBySn({ salesBillSn: this.$route.params.sn }).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: 10px;
    }
    .footer-cont{
      margin-top: 5px;
      text-align: center;
    }
  }
</style>
