<template>
  <div class="goodsAllocationSet-page">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="goodsAllocationSet-baseInfo" >
        <template slot="subTitle">
          <a href="javascript:;" @click="handleBack"><a-icon type="left"></a-icon> 返回列表</a>
          <span style="margin: 0 15px;color: #666;"> {{ this.$route.params.shelfName||'--' }} </span>
        </template>
      </a-page-header>
      <!-- 额度统计 授信结算类型 显示-->
      <a-card size="small" :bordered="false" style="margin-bottom: 10px;" v-if="this.$route.params.settleType=='CREDIT'">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" >
            <template slot="header">
              额度统计
              <a-tooltip placement="right">
                <template slot="title">
                  <p>待结金额：指客户已经扫码从数字货架取走配件，但还没有结算</p>
                  <p>冻结金额：指客户已经下单，但还没有从数字货架取走配件</p>
                </template>
                <a-icon type="question-circle" style="display: inline-block;margin: 12px 0 0 12px;"/>
              </a-tooltip>
            </template>
            <a-descriptions size="small" :column="4">
              <a-descriptions-item label="授信额度">
                <span>{{ total && toThousands(total.creditLimit,2) }}</span>
              </a-descriptions-item>
              <a-descriptions-item label="待结金额">
                <span>{{ total && toThousands(total.waitSettleAmount,2) }}</span>
              </a-descriptions-item>
              <a-descriptions-item label="冻结金额">
                <span>{{ total && toThousands(total.freezeAmount,2) }}</span>
              </a-descriptions-item>
              <a-descriptions-item label="可用额度">
                <span>{{ total && toThousands(total.usableAmount,2) }}</span>
              </a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <a-card :bordered="false" size="small" class="goodsAllocationSet-baseInfo">
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :xl="6" :lg="6" :md="12" :sm="24">
                <a-form-item label="下单时间">
                  <a-range-picker v-model="orderDate" allowClear :disabledDate="disabledDate" format="YYYY-MM-DD" @change="orderDateChange"/>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="6" :md="12" :sm="24">
                <a-form-item label="取货时间">
                  <a-range-picker v-model="time" allowClear :disabledDate="disabledDate" format="YYYY-MM-DD" @change="dateChange"/>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="6" :md="12" :sm="24">
                <a-form-item label="取货单编号">
                  <a-input placeholder="请输入取货单编号" v-model="queryParam.orderBillNo" allowClear/>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="6" :md="6" :sm="6" >
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="outboundOrderList-refresh" >查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="outboundOrderList-reset">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <a-alert type="info" style="margin:10px 0">
          <div slot="message" style="display: flex;align-items: center;padding: 2px 0;">
            共<strong>{{ count }}</strong>条记录；
            待结算金额合计￥<strong>{{ toThousands(priceTotal,2) }}</strong>
          </div>
        </a-alert>
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          rowKey="id"
          :showPagination="false"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
        </s-table>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { toThousands } from '@/libs/tools.js'
import moment from 'moment'
import { queryOrderBillDetailPage, queryOrderBillDetailCount, merchantCashOut } from '@/api/merchant'
export default {
  mixins: [commonMixin],
  components: { STable, VSelect, commonMixin },
  data () {
    return {
      toThousands,
      spinning: false,
      disabled: false,
      isEdit: false, // 货位号是否为编辑
      openModal: false, // 货位模板弹窗初始状态
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      time: [],
      orderDate: [],
      count: 0,
      priceTotal: 0,
      total: null,
      queryParam: {
        orderBeginDate: '',
        orderEndDat: '',
        outBeginDate: '',
        outEndDate: '',
        orderBillNo: ''
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '下单时间', dataIndex: 'orderDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '取货时间', dataIndex: 'outDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '订单编号', dataIndex: 'orderBillNo', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '货位号', dataIndex: 'shelfPlaceCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', width: '10%', dataIndex: 'productCode', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', width: '15%', dataIndex: 'productName', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '结算价', width: '10%', dataIndex: 'settlePrice', align: 'right', customRender: function (text) { return toThousands(text, 2) || '--' } },
        { title: '下单数量', width: '8%', dataIndex: 'totalQty', align: 'center', customRender: function (text) { return toThousands(text) || '--' } },
        { title: '合计金额', width: '10%', dataIndex: 'settleAmount', align: 'right', customRender: function (text) { return toThousands(text, 2) || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        const paramsPage = Object.assign(parameter, this.queryParam)
        paramsPage.settleState = 'WAIT'
        paramsPage.billState = 'FINISH'
        paramsPage.shelfSn = this.$route.params.shelfSn
        return queryOrderBillDetailPage(paramsPage).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getCount(paramsPage)
            const no = (data.pageNo - 1) * data.pageSize
            for (let i = 0; i < data.list.length; i++) {
              const _item = data.list[i]
              _item.no = no + i + 1
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },

  methods: {
    handleBack () {
      this.$router.go(-1)
    },
    orderDateChange (date) {
      console.log(date, date)
      this.orderDate = date
      this.queryParam.orderBeginDate = moment(date[0]).format('YYYY-MM-DD')
      this.queryParam.orderEndDat = moment(date[1]).format('YYYY-MM-DD')
    },
    dateChange (date) {
      this.time = date
      this.queryParam.outBeginDate = moment(date[0]).format('YYYY-MM-DD')
      this.queryParam.outEndDate = moment(date[1]).format('YYYY-MM-DD')
    },
    disabledDate (current) {
      return current && current > moment().endOf('day')
    },
    getPageInfo () {
      this.spinning = true
      const info = JSON.parse(sessionStorage.getItem('settlementDetail'))
      merchantCashOut({shelfSn:this.$route.params.shelfSn}).then(res => {
        console.log(res, info, '===========')
        if (res.status == 200) {
          this.total = res.data
          this.total.creditLimit = info.creditLimit
          this.total.waitSettleAmount = info.waitSettleAmount
        } else {
          this.total = null
        }
        this.spinning = false
      })
    },
    getCount (val) {
      queryOrderBillDetailCount(val).then(res => {
        if (res.status == 200) {
          this.count = res.data.detailNum
          this.priceTotal = res.data.settleAmount
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.orderDate = []
      this.queryParam = {
        orderBeginDate: '',
        orderEndDat: '',
        outBeginDate: '',
        outEndDate: '',
        orderBillNo: ''
      }
      this.$refs.table.refresh(true)
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getPageInfo()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getPageInfo()
      this.resetSearchForm()
    }
  }
}
</script>

<style lang="less">
  .goodsAllocationSet-page{
    position: relative;
    height: 100%;
    padding-bottom: 51px;
    box-sizing: border-box;
    .goodsAllocationSet-baseInfo{
      margin-bottom: 10px;
    }
    .bzj-collapse{
      margin-top: 10px;
      .ant-collapse-content-box{
        padding: 10px !important;
      }
    }

  }
</style>
