<template>
  <a-card size="small" :bordered="false" class="companyCollectionPaymentList-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="companyCollectionPaymentList-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="companyCollectionPaymentList-settleClientType"
                code="SETTLE_CLIENT_TYPE"
                placeholder="请选择单位类型"
                allowClear></v-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="结算类型">
              <v-select
                v-model="queryParam.settleType"
                ref="settleType"
                id="companyCollectionPaymentList-settleType"
                code="SETTLE_TYPE"
                placeholder="请选择结算类型"
                allowClear></v-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="结算单号">
                <a-input id="companyCollectionPaymentList-unitSettleNo" v-model.trim="queryParam.unitSettleNo" allowClear placeholder="请输入结算单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="结算时间">
                <rangeDate ref="rangeDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="结算方式">
                <a-select id="companyCollectionPaymentList-settleStyleSn" v-model="queryParam.settleStyleSn" placeholder="请选择结算方式" allowClear>
                  <a-select-option v-for="item in settleStyleList" :value="item.settleStyleSn" :key="item.settleStyleSn">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="companyCollectionPaymentList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="companyCollectionPaymentList-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </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.receiptCount || productTotal.receiptCount==0)) ? productTotal.receiptCount : '--' }}</strong> 条，
        收款金额合计 <strong>{{ (productTotal&&(productTotal.receiptAmount || productTotal.receiptAmount==0)) ? '￥'+productTotal.receiptAmount : '--' }}</strong> ，
        付款单 <strong>{{ (productTotal&&(productTotal.payCount || productTotal.payCount==0)) ? productTotal.payCount : '--' }}</strong> 条，
        付款金额合计 <strong>{{ (productTotal&&(productTotal.payAmount || productTotal.payAmount==0)) ? '￥'+productTotal.payAmount : '--' }}</strong>
      </div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1340, y: tableHeight }"
      bordered>
      <!-- 结算单号 -->
      <template slot="unitSettleNo" slot-scope="text, record">
        <span :class="$hasPermissions('M_companyCollectionPayment_detail')?'active':'common'" @click="handleDetail(record)">{{ record.unitSettleNo }}</span>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { settleStyleQueryAll } from '@/api/settleStyle'
import { settleUnitList, settleUnitQuerySum } from '@/api/settle'
export default {
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      advanced: true, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        settleClientName: '',
        settleClientType: undefined,
        settleType: undefined,
        unitSettleNo: '',
        settleStyleSn: undefined
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '单位名称', dataIndex: 'settleClientName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位类型', dataIndex: 'settleClientTypeDictValue', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '结算类型', dataIndex: 'settleTypeDictValue', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '结算单号', scopedSlots: { customRender: 'unitSettleNo' }, width: 220, align: 'center' },
        { title: '结算金额', dataIndex: 'realSettleAmount', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '结算时间', dataIndex: 'createDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作人', dataIndex: 'operateName', width: 140, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '结算方式', dataIndex: 'settleStyleName', width: 140, align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 420
        }
        return settleUnitList(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
          }
          this.disabled = false
          this.total = data.count
          this.getTotal(Object.assign(parameter, this.queryParam))
          return data
        })
      },
      settleStyleList: [],
      total: 0, //  总条数
      productTotal: null
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    //  详情
    handleDetail (row) {
      if (this.$hasPermissions('M_companyCollectionPayment_detail')) {
        this.$router.push({ path: `/financialManagement/companyCollectionPayment/detail/${row.id}/${row.unitSettleSn}` })
      }
    },
    // 合计
    getTotal (param) {
      settleUnitQuerySum(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  重置
    resetSearchForm () {
      if (this.advanced) {
        this.$refs.rangeDate.resetDate()
      }
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.settleClientName = ''
      this.queryParam.settleClientType = undefined
      this.queryParam.settleType = undefined
      this.queryParam.unitSettleNo = ''
      this.queryParam.settleStyleSn = undefined
      this.$refs.table.refresh(true)
    },
    //  结算方式
    getSettleStyle () {
      settleStyleQueryAll({}).then(res => {
        if (res.status == 200) {
          this.settleStyleList = res.data
        } else {
          this.settleStyleList = []
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getSettleStyle()
    })
  }
}
</script>
<style lang="less">
  .companyCollectionPaymentList-wrap{
    .sTable{
      margin-top: 15px;
    }
	.active{
		color: #ed1c24;
		cursor: pointer;
	}
	.common{
		color: rgba(0, 0, 0, 0.65);
	}
  }
</style>
