<template>
  <div class="salesDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="salesDetail-cont" style="padding:16px 24px;">
        <template slot="subTitle">
           <a href="javascript:;" @click="handleBack"><a-icon type="left"></a-icon> 返回列表</a>
        </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">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息" 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>
          </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="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"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: 500 }"
          :defaultLoadData="false"
          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-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/sales'
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,
    }
  },
  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
    }
  },
  methods: {
    handlePrint(){
      this.openModal = true
    },
    // 确定打印或预览,导出
    handleOk (objs) {
      const _this = this
      const params = JSON.parse(JSON.stringify(objs))
      console.log(params)
      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
        }
      })
    }
  },
  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-wrap{
    .salesDetail-cont{
      margin-bottom: 10px;
    }
    .footer-cont{
      margin-top: 5px;
      text-align: center;
    }
  }
</style>
