<template>
  <div class="jg-page-wrap">
    <a-card size="small" :bordered="false" class="table-page-search-wrapper">
      <!-- 搜索条件 -->
      <div ref="tableSearch">
        <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="crpnList-settleClientName" v-model.trim="queryParam.settleClientNameCurrent" allowClear placeholder="请输入客户名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="单位类型">
                <v-select
                  v-model="queryParam.settleClientType"
                  ref="settleClientType"
                  id="crpnList-settleClientType"
                  code="SETTLE_CLIENT_TYPE"
                  placeholder="请选择单位类型"
                  allowClear></v-select>
              </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="crpnList-refresh">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="crpnList-reset">重置</a-button>
                <a-button
                  type="primary"
                  v-if="$hasPermissions('B_collectionPaymentExoprt')"
                  class="button-warning"
                  @click="handleExport"
                  :disabled="disabled"
                  :loading="exportLoading"
                  id="companyCollectionPaymentList-export">导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="crpnList-wrap">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 统计 -->
        <div class="table-operator">
          <div class="alert-message">
            共 <strong>{{ total }}</strong> 条记录，
            应收金额合计 <strong>{{ (productTotal&&(productTotal.receipt || productTotal.receipt==0)) ? toThousands(productTotal.receipt) : '--' }}</strong> ，
            应付金额合计 <strong>{{ (productTotal&&productTotal.pay) ? toThousands(productTotal.pay) : 0 }}</strong>
          </div>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+84.5+'px' }"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 客户名称 -->
          <template slot="customerName" slot-scope="text, record">
            <span v-if="record.settleClientType&&record.settleClientType=='CUSTOMER'">{{ record.settleClientNameCurrent||'--' }}</span>
            <span v-else>{{ record.settleClientName||'--' }}</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              v-if="$hasPermissions('M_collectionPayment')"
              size="small"
              type="link"
              class="button-primary"
              @click="handleCollectionPayment(record)"
              :id="'crpnList-collectionPayment-pay-'+record.id">收付款</a-button>
            <a-button
              size="small"
              type="link"
              class="button-success"
              @click="handleDetail(record)"
              :id="'crpnList-collectionPayment-detail-'+record.id"
              v-if="$hasPermissions('M_companyReceivablePayable_detail')">详情</a-button>
            <span v-if="!$hasPermissions('M_collectionPayment') && !$hasPermissions('M_companyReceivablePayable_detail')">--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { downloadExcel } from '@/libs/JGPrint.js'
import { STable, VSelect } from '@/components'
import { settleUnitClientList, settleInfoQuerySum, exportClientUnSettle } from '@/api/settle'
export default {
  name: 'CollectionPaymentList',
  components: { STable, VSelect },
  mixins: [commonMixin],
  data () {
    const _this = this
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0, // 表格高度
      exportLoading: false,
      // 查询参数
      queryParam: {
        settleClientNameCurrent: '', // 客户名称
        settleClientType: undefined // 单位类型
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '客户名称', width: '20%', align: 'center', scopedSlots: { customRender: 'customerName' } },
        { title: '单位类型', dataIndex: 'settleClientTypeDictValue', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '应收金额', dataIndex: 'unSettleAmountReceipt', width: '20%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '应付金额', dataIndex: 'unSettleAmountPay', width: '20%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return settleUnitClientList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getTotal(Object.assign(parameter, this.queryParam))
            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].unSettleAmountPay = Math.abs(data.list[i].unSettleAmountPay) || 0
            }
            this.disabled = false
            this.total = data.count || 0
          }
          this.spinning = false
          return data
        })
      },
      total: 0, //  总条数
      productTotal: null // 金额统计
    }
  },
  methods: {
    //  导出
    handleExport () {
      const _this = this
      const params = this.queryParam
      this.exportLoading = true
      _this.spinning = true
      exportClientUnSettle(params).then(res => {
        this.exportLoading = false
        _this.spinning = false
        downloadExcel(res, '单位收付款客户')
      })
    },
    //  详情
    handleDetail (row) {
      const clientName = row.settleClientType == 'CUSTOMER' ? row.settleClientNameCurrent : row.settleClientName
      this.$router.push({ name: 'companyReceivablePayableNewDetail', params: { sn: row.settleClientSn, name: clientName, type: row.settleClientType } })
    },
    // 金额合计
    getTotal (param) {
      settleInfoQuerySum(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  去收付款
    handleCollectionPayment (row) {
      const clientName = row.settleClientType == 'CUSTOMER' ? row.settleClientNameCurrent : row.settleClientName
      this.$router.push({ name: 'companyReceivablePayableNewCollectionPayment', params: { sn: row.settleClientSn, name: clientName, type: row.settleClientType } })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.settleClientNameCurrent = ''
      this.queryParam.settleClientType = undefined
      this.$refs.table.refresh(true)
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 265
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .crpnList-wrap{
    .sTable{
      margin-top: 10px;
    }
  }
</style>
