<template>
  <a-card size="small" :bordered="false" class="companyReceivablePayableList-wrap">
    <!-- 搜索条件 -->
    <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="companyReceivablePayableList-settleClientName" v-model.trim="queryParam.settleClientName" 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="companyReceivablePayableList-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="companyReceivablePayableList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="companyReceivablePayableList-reset">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- alert -->
    <a-alert type="info" showIcon style="margin-bottom:10px">
      <div slot="message">
        共 <strong>{{ total }}</strong> 条记录，
        应收金额合计 <strong>{{ (productTotal&&(productTotal.receipt || productTotal.receipt==0)) ? '￥'+productTotal.receipt : '--' }}</strong> ，
        应付金额合计 <strong>￥{{ (productTotal&&productTotal.pay) ? Math.abs(productTotal.pay) : 0 }}</strong>
      </div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ y: tableHeight }"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button
          v-if="$hasPermissions('M_collectionPayment')"
          size="small"
          type="link"
          class="button-info"
          @click="handleCollectionPayment(record)"
          id="companyReceivablePayableList-collectionPayment-btn">收付款</a-button>
        <a-button
          v-if="$hasPermissions('M_companyReceivablePayable_detail')"
          size="small"
          type="link"
          class="button-success"
          @click="handleDetail(record)"
          id="companyReceivablePayableList-detail-btn">详情</a-button>
        <span v-if="!$hasPermissions('M_collectionPayment') && !$hasPermissions('M_companyReceivablePayable_detail')">--</span>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { settleUnitClientList, settleInfoQuerySum } from '@/api/settle'
export default {
  components: { STable, VSelect },
  data () {
    return {
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      // 查询参数
      queryParam: {
        settleClientName: '',
        settleClientType: undefined
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '单位名称', dataIndex: 'settleClientName', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位类型', dataIndex: 'settleClientTypeDictValue', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '应收金额', dataIndex: 'unSettleAmountReceipt', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '应付金额', dataIndex: 'unSettleAmountPay', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 380
        }
        return settleUnitClientList(Object.assign(parameter, this.queryParam)).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
            // 应付金额  值为负,应显示为正
            data.list[i].unSettleAmountPay = Math.abs(data.list[i].unSettleAmountPay) || 0
          }
          this.disabled = false
          this.total = data.count
          this.getTotal(Object.assign(parameter, this.queryParam))
          return data
        })
      },
      total: 0, //  总条数
      productTotal: null
    }
  },
  methods: {
    //  详情
    handleDetail (row) {
      this.$router.push({ path: `/financialManagement/companyReceivablePayable/detail/${row.settleClientSn}/${row.settleClientName}` })
    },
    // 合计
    getTotal (param) {
      settleInfoQuerySum(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  收付款
    handleCollectionPayment (row) {
      this.$router.push({ path: `/financialManagement/companyReceivablePayable/collectionPayment/${row.settleClientSn}/${row.settleClientName}` })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.settleClientName = ''
      this.queryParam.settleClientType = undefined
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less">
  .companyReceivablePayableList-wrap{
    .sTable{
      margin-top: 15px;
    }
  }
</style>
