<template>
  <div class="urgentItemsOffsetDetail-page-wrapper">
    <!-- 操作 -->
    <a-page-header :ghost="false" :backIcon="false" class="urgentItemsOffsetDetail-operation-wrapper" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="urgentItemsOffsetDetail-btn-back" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
      </template>
      <!-- 操作区，位于 title 行的行尾 -->
      <template slot="extra">
        <a-button key="3" type="danger" v-if="!isWriteDown" @click="handleSubmit" id="urgentItemsOffsetDetail-btn-submit">冲减</a-button>
        <a-button key="2" @click="handlePreview" id="urgentItemsOffsetDetail-btn-preview">打印预览</a-button>
        <a-button key="1" type="primary" @click="handlePrint" id="urgentItemsOffsetDetail-btn-print">快速打印</a-button>
      </template>
    </a-page-header>
    <!-- 急件单信息 -->
    <a-card size="small" :bordered="false" class="urgentItemsOffsetDetail-info-wrapper">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1" header="基础信息">
          <a-descriptions :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.buyerName || '--' }}</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.urgentBillNo || '--' }}</a-descriptions-item>
            <a-descriptions-item label="状态">{{ detailData.statusDictValue || '--' }}</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <!-- 表格 -->
    <a-card size="small" :bordered="false" class="urgentItemsOffsetDetail-table-wrapper">
      <!-- 合计 -->
      <a-alert type="info" showIcon style="margin-bottom:15px" v-if="detailData">
        <div class="ftext" slot="message">总款数：<strong>{{ detailData.totalCategory || 0 }}</strong>；总数量：<strong>{{ detailData.totalQty || 0 }}</strong>。</div>
      </a-alert>
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="isWriteDown ? columns : unColumns"
        :data="loadData"
        :scroll="{ x: isWriteDown ? 1570 : '100%' }"
        bordered>
        <!-- 库存数量 -->
        <template slot="inventoryQuantity" slot-scope="text, record">
          <span :class="[record.stockEntity.currentStockQty < record.qty ? 'red' : '']" :style="{fontWeight: record.stockEntity.currentStockQty < record.qty?'bold':''}">{{ record.stockEntity.currentStockQty }}</span>
        </template>
      </s-table>
    </a-card>
  </div>

</template>

<script>
import { STable, VSelect } from '@/components'
import { urgentDetail, urgentWriteDown } from '@/api/urgent'
import { urgentDetailList } from '@/api/urgentDetail'
export default {
  components: { STable, VSelect },
  data () {
    return {
      unColumns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: 200, align: 'center' },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', align: 'center', ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: 200, align: 'center' },
        { title: '库存数量', scopedSlots: { customRender: 'inventoryQuantity' }, width: 100, align: 'center' },
        { title: '未冲减数量', dataIndex: 'qty', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: 100, align: 'center', customRender: function (text) { return text || '--' } }
      ],
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: 200, align: 'center' },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', align: 'center', ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'stockInDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'price', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '已冲减数量', dataIndex: 'qty', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本小计', dataIndex: 'totalCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return urgentDetailList(Object.assign(parameter, { urgentBillSn: 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
            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 || '--'
          }
          this.disabled = false
          return data
        })
      },
      detailData: null //  详情数据
    }
  },
  computed: {
    isWriteDown () {
      return this.detailData && this.detailData.status == 'FINISH'
    }
  },
  methods: {
    //  返回
    handleBack () {
      this.$router.push({ path: '/salesManagement/urgentItemsOffset/list' })
    },
    //  详情
    getDetail () {
      urgentDetail({ id: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        } else {
          this.detailData = null
        }
      })
    },
    //  冲减
    handleSubmit () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定要进行冲减吗？',
        centered: true,
        onOk () {
          urgentWriteDown({ id: _this.$route.params.id }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.getDetail()
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    //  快速打印
    handlePrint () {

    },
    //  打印预览
    handlePreview () {

    }
  },
  mounted () {
    this.getDetail()
  }
}
</script>

<style lang="less">
  .urgentItemsOffsetDetail-page-wrapper{
    .urgentItemsOffsetDetail-info-wrapper{
      margin: 15px 0;
      .item-cont {
        margin-bottom: 15px;
        display: flex;
        .item-label {
          width: 115px;
          font-size: 14px;
          color: rgba(0, 0, 0, 0.85);
          flex-shrink: 0;
        }
        .item-main {
          flex-grow: 1;
          word-break: break-all;
        }
      }
    }
    .urgentItemsOffsetDetail-table-wrapper{
      .red{
        color: #ed1c24;
      }
    }
  }
</style>
