<template>
  <div class="salesDetail-warehouse-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="salesDetail-cont">
        <template slot="subTitle">
          <a id="salesEdit-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span style="margin: 0 15px;color: #666;font-weight: bold;">单号：{{ detailData&&detailData.salesBillNo }}</span>
          <span v-if="detailData&&detailData.salesBillNoSource">（原：{{ detailData&&detailData.salesBillNoSource || '--' }}）</span>
          <span style="margin: 0 10px;color: #666;">客户名称：{{ detailData&&detailData.buyerName }}</span>
          <a-button type="link" size="small" class="button-default" @click="showDetail=!showDetail">
            <a-icon :type="showDetail ? 'eye-invisible' : 'eye'"/> {{ showDetail?'隐藏':'查看' }}信息
          </a-button>
        </template>
        <template slot="extra">
          <a-button
            key="3"
            type="primary"
            class="button-info"
            v-if="$hasPermissions('B_salesOrderWarehouseExport')"
            @click="handlePrint">导出Excel</a-button>
        </template>
      </a-page-header>

      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="salesDetail-cont" v-show="showDetail">
        <div v-if="detailData">
          <a-descriptions size="small" :column="3">
            <a-descriptions-item label="销售单号">{{ detailData.salesBillNo || '--' }}</a-descriptions-item>
            <a-descriptions-item label="创建时间">{{ detailData.createDate || '--' }}</a-descriptions-item>
            <a-descriptions-item label="客户名称">{{ detailData.buyerName || '--' }}</a-descriptions-item>
            <a-descriptions-item label="收货地址" :span="2" v-if="detailData&&detailData.salesBillExtEntity">
              {{ detailData.salesBillExtEntity&&detailData.salesBillExtEntity.shippingAddrProvinceName || '' }}
              {{ detailData.salesBillExtEntity&&detailData.salesBillExtEntity.shippingAddrCityName || '' }}
              {{ detailData.salesBillExtEntity&&detailData.salesBillExtEntity.shippingAddrCountyName || '' }}
              {{ detailData.salesBillExtEntity&&detailData.salesBillExtEntity.shippingAddr || '' }}
            </a-descriptions-item>
            <a-descriptions-item label="收货人" v-if="detailData.salesBillExtEntity">{{ detailData.salesBillExtEntity&&detailData.salesBillExtEntity.consigneeName || '--' }}</a-descriptions-item>
            <a-descriptions-item label="收货电话" v-if="detailData.salesBillExtEntity">{{ detailData.salesBillExtEntity&&detailData.salesBillExtEntity.consigneeTel || '--' }}</a-descriptions-item>
            <a-descriptions-item label="业务状态">{{ detailData.billStatusDictValue || '--' }}</a-descriptions-item>
          </a-descriptions>
        </div>
      </a-card>
      <a-card size="small" :bordered="false" class="pages-wrap">
        <!-- 统计 -->
        <a-alert type="info" style="margin-bottom: 10px;" v-if="countData">
          <div slot="message">
            <span>
              总款数：<strong>{{ countData.totalCategory || countData.totalCategory==0? countData.totalCategory : '--' }}</strong>；
            </span>
            总数量：<strong>{{ countData.totalQty || countData.totalQty==0? countData.totalQty : '--' }}</strong>；
          </div>
        </a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          :style="{ height: tableHeight+65+'px' }"
          ref="table"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :columns="columns"
          :pageSize="30"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 产品编码 -->
          <template slot="productCode" slot-scope="text, record">
            <span>{{ text }}</span>
            <a-badge count="促" v-if="record.promotionFlag&&record.promotionFlag.indexOf('GIFT')>=0" :number-style="{ backgroundColor: '#52c41a', zoom:'80%' }"></a-badge>
            <a-badge count="特" v-if="record.promotionFlag&&record.promotionFlag.indexOf('DISCOUNT')>=0" :number-style="{ backgroundColor: '#faad14', zoom:'80%' }"></a-badge>
          </template>
        </s-table>
      </a-card>
    </a-spin>
    <!-- 导出excel -->
    <print-modal :openModal="openModal" :itemData="detailData" @ok="handleOk" @close="closePrint" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import moment from 'moment'
import { exportExcel } from '@/libs/JGPrint.js'
import { queryPageForWarehouseDetail, queryCountForWarehouseDetail, salesDetailBySn, salesDetailExcel } from '@/api/salesNew'
import printModal from './printModal.vue'
export default {
  name: 'SalesOrderWarehouseDetail',
  mixins: [commonMixin],
  components: { STable, VSelect, printModal },
  data () {
    return {
      disabled: false,
      spinning: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return queryPageForWarehouseDetail(Object.assign(parameter, { salesBillSn: this.$route.params.sn, warehouseSn: this.$route.params.wsn })).then(res => {
          let data
          if (res.status == 200) {
            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.disabled = false
          }
          return data
        })
      },
      detailData: null, //  详情数据
      countData: null, // 数量数据
      fromRouter: null, // 打开此页面的来源页面
      openModal: false, // 导出Excel
      showDetail: false, // 是否显示详情
      tableHeight: 300 // 表格高度
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '25%', scopedSlots: { customRender: 'productCode' }, align: 'center' },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '33%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productEntity.origCode', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productEntity.unit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售数量', dataIndex: 'qty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('B_dispatchDetail_salesPrice')) { //  售价权限
        arr.splice(4, 0, { title: '出库仓库', dataIndex: 'warehouseName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } })
      }
      return arr
    }
  },
  watch: {
    // 监听详情显示隐藏，并计算表格高度
    showDetail (newValue, oldValue) {
      this.getTableHeight()
    }
  },
  methods: {
    // 表格高度
    getTableHeight () {
      this.tableHeight = window.innerHeight - (this.showDetail ? 376 : 265)
    },
    // 导出Excel
    handlePrint () {
      this.openModal = true
    },
    // 确定打印或预览,导出
    handleOk (objs) {
      const _this = this
      const params = JSON.parse(JSON.stringify(objs))
      delete params.type
      _this.spinning = true
      exportExcel(salesDetailExcel, params, '销售清单' + moment().format('YYYYMMDDHHmmss'), () => {
        _this.spinning = false
      })
    },
    closePrint () {
      this.openModal = false
    },
    //  返回
    handleBack () {
      this.$router.push({ name: 'salesOrderWarehouseList' })
    },
    //  获取详情
    getDetail () {
      salesDetailBySn({ salesBillSn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.$refs.table.refresh(true)
          this.detailData = res.data || null
        } else {
          this.detailData = null
        }
      })
    },
    // 统计数据
    getCount () {
      queryCountForWarehouseDetail({ salesBillSn: this.$route.params.sn, warehouseSn: this.$route.params.wsn }).then(res => {
        if (res.status == 200) {
          this.countData = res.data
        } else {
          this.countData = null
        }
        this.getTableHeight()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab || this.outBizSubSn || this.bizSn) { // 页签刷新 或 为弹框时调用
      this.getDetail()
      this.getCount()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getDetail()
      this.getCount()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.fromRouter = from
    })
  }
}
</script>

<style lang="less" scoped>
  .salesDetail-warehouse-wrap{
    .salesDetail-cont{
      margin-bottom: 6px;
    }
    .footer-cont{
      margin-top: 5px;
      text-align: center;
    }
  }
</style>
