<template>
  <div class="companyCollectionPaymentDetail-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="companyCollectionPaymentDetail-cont" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="companyCollectionPaymentDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        <p style=" display: inline-block;margin: 0 0 0 60px;color: #000;font-size: 16px;font-weight: 600;">客户：{{ $route.params.name || '--' }}</p>
      </template>
      <!-- 操作区，位于 title 行的行尾 -->
      <template slot="extra">
        <a-button key="2" id="companyCollectionPaymentDetail-preview-btn">打印预览</a-button>
        <a-button key="1" type="primary" id="companyCollectionPaymentDetail-print-btn">快速打印</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="审核时间">
                <a-range-picker
                  style="width:100%"
                  id="companyCollectionPaymentDetail-auditTime"
                  :disabledDate="disabledDate"
                  v-model="auditTime"
                  :format="dateFormat"
                  :placeholder="['开始时间', '结束时间']" />
              </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>
                <a-button
                  type="danger"
                  style="margin-left: 8px"
                  @click="handleExport"
                  :disabled="disabled"
                  :loading="exportLoading"
                  id="companyReceivablePayableDetail-export-btn">导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- alert -->
      <a-alert type="info" showIcon style="margin-bottom:15px">
        <div slot="message">共 <strong>{{ total }}</strong> 条明细，金额合计 <strong>￥{{ (productTotal&&productTotal.totalAmount) || 0 }}</strong> ，余额合计 <strong>￥{{ (productTotal&&productTotal.unsettleAmount) || 0 }}</strong> </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        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>
  </div>
</template>

<script>
import moment from 'moment'
import { STable, VSelect } from '@/components'
import { settleInfoList, settleInfoQueryReceiptOrPaySum } from '@/api/settle'
export default {
  components: { STable, VSelect },
  data () {
    return {
      disabled: false, //  查询、重置按钮是否可操作
      // 查询参数
      queryParam: {
        bizNo: ''
      },
      auditTime: [], //  审核时间
      dateFormat: 'YYYY-MM-DD',
      exportLoading: false, // 导出loading
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '单据号', dataIndex: 'bizNo', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '类型', dataIndex: 'bizTypeDictValue', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'auditTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '金额', dataIndex: 'totalAmount', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '余额', dataIndex: 'unsettleAmount', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // 审核时间
        if (this.auditTime && this.auditTime.length > 0) {
          this.queryParam.auditBeginDate = moment(this.auditTime[0]).format(this.dateFormat) + '00:00:00'
          this.queryParam.auditEndDate = moment(this.auditTime[1]).format(this.dateFormat) + '23:59:59'
        } else {
          this.queryParam.auditBeginDate = undefined
          this.queryParam.auditEndDate = undefined
        }
        const params = Object.assign(parameter, this.queryParam, { settleClientSn: this.$route.params.sn })
        return settleInfoList(params).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
          }
          this.disabled = false
          this.total = data.list.length
          this.getTotal(params)
          return data
        })
      },
      basicInfoData: null, //  基本信息
      total: 0, //  总条数
      productTotal: null //  合计
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/financialManagement/companyReceivablePayable/list' })
    },
    // 合计
    getTotal (param) {
      settleInfoQueryReceiptOrPaySum(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.queryParam.bizNo = ''
      this.auditTime = []
    },
    //  导出
    handleExport () {
      const params = this.queryParam
      this.exportLoading = true
      // customerBundleExportDelay(params).then(res => {
      //   this.exportLoading = false
      //   this.download(res)
      // })
    },
    download (data) {
      if (!data) { return }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const a = moment().format('YYYYMMDDHHmmss')
      const fname = '仓库调拨' + a
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    }
  }
}
</script>

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