<template>
  <div class="companyCollectionPaymentDetail-wrap jg-page-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="companyCollectionPaymentDetail-cont" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <span style="margin-right: 30px;color: #000;font-size: 14px;font-weight: 600;">客户：{{ $route.params.name!='undefined'?$route.params.name: '--' }}</span>
          <a-button id="companyCollectionPaymentDetail-back-btn" type="link" @click="handleBack" class="button-default"> <a-icon type="rollback" /> 返回列表</a-button>
        </template>
      </a-page-header>
      <a-card>
        <!-- 搜索条件 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-item label="单据号">
                  <a-input id="companyReceivablePayableDetail-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入单据号"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="审核时间">
                  <rangeDate id="companyReceivablePayableDetail-auditTime" ref="rangeDate" @change="dateChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="companyReceivablePayableDetail-refresh">查询</a-button>
                  <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="companyReceivablePayableDetail-reset">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- alert -->
        <div class="table-operator">
          <div class="alert-message">
            共 <strong>{{ total }}</strong> 条明细，
            金额合计 <strong>{{ (productTotal&&(productTotal.totalAmount || productTotal.totalAmount==0)) ? toThousands(productTotal.totalAmount) : '--' }}</strong> ，
            余额合计 <strong>{{ (productTotal&&(productTotal.unsettleAmount || productTotal.unsettleAmount==0)) ? toThousands(productTotal.unsettleAmount) : '--' }}</strong>
          </div>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
          <!-- 采购数量 -->
          <template slot="purchaseNum" slot-scope="text, record">
            <span>{{ record.purchaseNum }}</span>
          </template>
          <!-- 缺货数量 -->
          <template slot="outOfStockNum" slot-scope="text, record">
            <span>{{ record.outOfStockNum }}</span>
          </template>
        </s-table>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { settleInfoList, settleInfoQueryReceiptOrPaySum } from '@/api/settle'
export default {
  name: 'CompanyReceivablePayableDetail',
  components: { STable, VSelect, rangeDate },
  mixins: [commonMixin],
  data () {
    const _this = this
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      // 查询参数
      queryParam: {
        auditBeginDate: '', // 开始审核时间
        auditEndDate: '', // 结束审核时间
        bizNo: '' // 单据号
      },
      exportLoading: false, // 导出loading
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '单据号', dataIndex: 'bizNo', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '类型', dataIndex: 'bizTypeDictValue', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'auditTime', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '总款数', dataIndex: 'bizTotalCategory', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总数量', dataIndex: 'bizTotalQty', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '金额', dataIndex: 'totalAmount', width: '14%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '余额', dataIndex: 'unsettleAmount', width: '14%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam, { settleClientSn: this.$route.params.sn, settleClientType: this.$route.params.type })
        return settleInfoList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getTotal(params)
            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
            this.total = data.count || 0
          }
          this.spinning = false
          return data
        })
      },
      basicInfoData: null, //  基本信息
      total: 0, //  总条数
      productTotal: null //  合计
    }
  },
  methods: {
    //  审核时间  change
    dateChange (date) {
      this.queryParam.auditBeginDate = date[0]
      this.queryParam.auditEndDate = date[1]
    },
    //  返回列表
    handleBack () {
      this.$router.push({ name: 'companyReceivablePayableNewList' })
    },
    // 合计
    getTotal (param) {
      settleInfoQueryReceiptOrPaySum(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.auditBeginDate = ''
      this.queryParam.auditEndDate = ''
      this.queryParam.bizNo = ''
      this.$refs.table.refresh(true)
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .companyCollectionPaymentDetail-cont{
    margin-bottom: 6px;
  }
</style>
