<template>
  <div class="urgentItemsOffsetDetail-page-wrapper" :style="{'paddingBottom': (!isWriteDown&&$hasPermissions('B_urgentWriteDown')) ? '64px' : '0px'}">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作 -->
      <a-page-header :ghost="false" :backIcon="false" class="urgentItemsOffsetDetail-operation-wrapper" :style="{ padding: !outBizSn ? '16px 24px' : '0px 24px' }" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle" v-if="!outBizSn">
          <a id="urgentItemsOffsetDetail-btn-back" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        </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">
              <a-descriptions-item label="客户名称">{{ (detailData&&detailData.buyerName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="客户地址">
                <div v-if="detailData&&(detailData.shippingAddressProvinceName || detailData.shippingAddressCityName || detailData.shippingAddressCountyName || detailData.shippingAddress)">
                  {{ (detailData&&detailData.shippingAddressProvinceName) || '' }}
                  {{ (detailData&&detailData.shippingAddressCityName) || '' }}
                  {{ (detailData&&detailData.shippingAddressCountyName) || '' }}
                  {{ (detailData&&detailData.shippingAddress) || '' }}
                </div>
                <span v-else>--</span>
              </a-descriptions-item>
              <a-descriptions-item label="联系电话">{{ detailData&&detailData.consigneeTel || '--' }}</a-descriptions-item>
              <a-descriptions-item label="收款方式">{{ (detailData&&detailData.settleStyleEntity&&detailData.settleStyleEntity.name) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="急件单号">{{ (detailData&&detailData.urgentBillNo) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="状态">{{ (detailData&&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" style="margin-bottom:15px" v-if="detailData">
          <div class="ftext" slot="message">
            总款数：<strong>{{ (detailData&&(detailData.totalCategory || detailData.totalCategory == 0)) ? detailData.totalCategory : '--' }}</strong>；
            总数量：<strong>{{ (detailData&&(detailData.totalQty || detailData.totalQty == 0)) ? detailData.totalQty : '--' }}</strong>。
          </div>
        </a-alert>
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="isWriteDown ? columns : unColumns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
          <!-- 库存数量 -->
          <template slot="inventoryQuantity" slot-scope="text, record">
            <span v-if="record.stockEntity&&(record.stockEntity.currentStockQty||record.stockEntity.currentStockQty==0)" :class="[record.stockEntity.currentStockQty < record.qty ? 'red' : '']" :style="{fontWeight: record.stockEntity.currentStockQty < record.qty?'bold':''}">{{ record.stockEntity.currentStockQty }}</span>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-card>
    </a-spin>
    <div class="affix-cont" v-if="!isWriteDown&&$hasPermissions('B_urgentWriteDown')">
      <a-button
        size="large"
        style="padding: 0 60px;"
        type="primary"
        :disabled="spinning"
        class="button-primary"
        @click="handleSubmit"
        id="urgentItemsOffsetDetail-btn-submit">冲减</a-button>
    </div>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { urgentWriteDown, urgentDetailSn, urgentDetailList } from '@/api/urgent'
export default {
  name: 'UrgentOffsetDetail',
  components: { STable, VSelect },
  mixins: [commonMixin],
  props: {
    outBizSn: { //  有值则为弹框，无值则为页面
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      unColumns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: '20%', customRender: function (text) { return text || '--' }, align: 'center' },
        { title: '库存数量', scopedSlots: { customRender: 'inventoryQuantity' }, width: '12%', align: 'center' },
        { title: '未冲减数量', dataIndex: 'qty', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: '11%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', width: '17%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'stockInDate', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '7%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '7%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '成本价', dataIndex: 'cost', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '已冲减数量', dataIndex: 'qty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本小计', dataIndex: 'totalCost', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return urgentDetailList(Object.assign(parameter, { urgentBillSn: this.outBizSn || this.$route.params.sn })).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
              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 () {
      urgentDetailSn({ sn: this.outBizSn || this.$route.params.sn }).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 () {
          _this.spinning = true
          urgentWriteDown({ urgentBillSn: _this.outBizSn || _this.$route.params.sn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.getDetail()
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab || this.outBizSn) { // 页签刷新 或 为弹框时调用
      this.$refs.table.refresh(true)
      this.getDetail()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.$refs.table.refresh(true)
      this.getDetail()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .urgentItemsOffsetDetail-page-wrapper{
    position: relative;
    height: 100%;
    box-sizing: border-box;
    >.ant-spin-nested-loading{
      overflow-y: scroll;
      height: 100%;
    }
    .urgentItemsOffsetDetail-info-wrapper{
      margin: 10px 0;
      .item-cont {
        margin-bottom: 10px;
        display: flex;
        .item-label {
          width: 115px;
          font-size: 14px;
          color: rgba(0, 0, 0);
          flex-shrink: 0;
        }
        .item-main {
          flex-grow: 1;
          word-break: break-all;
        }
      }
    }
    .urgentItemsOffsetDetail-table-wrapper{
      .red{
        color: #ed1c24;
      }
    }
  }
</style>
