<template>
  <a-card size="small" :bordered="false" class="companyReceivablePayableList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <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 :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" 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 fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        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-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { settleUnitClientList, settleInfoQuerySum } from '@/api/settle'
export default {
  name: 'CollectionPaymentList',
  components: { STable, VSelect },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      // 查询参数
      queryParam: {
        settleClientName: '',
        settleClientType: undefined
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '单位名称', dataIndex: 'settleClientName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位类型', dataIndex: 'settleClientTypeDictValue', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '应收金额', dataIndex: 'unSettleAmountReceipt', width: '20%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '应付金额', dataIndex: 'unSettleAmountPay', width: '20%', align: 'center', customRender: function (text) { return ((text || text == 0) ? 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: {
    //  详情
    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)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 230
    }
  },
  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">
  .companyReceivablePayableList-wrap{
    .sTable{
      margin-top: 10px;
    }
  }
</style>
