<template>
  <a-card size="small" :bordered="false" class="companyCollectionPaymentList-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="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 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="unitSettleNo" slot-scope="text, record">
          <span :class="$hasPermissions('M_companyCollectionPayment_detail')?'active':'common'" @click="handleDetail(record)">{{ record.unitSettleNo }}</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
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 {
  name: 'CompanyCollectionPaymentList',
  components: { STable, VSelect, rangeDate },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      advanced: true, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        settleClientName: '',
        settleClientType: undefined,
        settleType: undefined,
        unitSettleNo: '',
        settleStyleSn: undefined
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '单位名称', dataIndex: 'settleClientName', width: '17%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位类型', dataIndex: 'settleClientTypeDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '结算类型', dataIndex: 'settleTypeDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '结算单号', scopedSlots: { customRender: 'unitSettleNo' }, width: '17%', align: 'center' },
        { title: '结算金额', dataIndex: 'realSettleAmount', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '结算时间', dataIndex: 'createDate', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作人', dataIndex: 'operateName', width: '14%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '结算方式', dataIndex: 'settleStyleName', width: '12%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return settleUnitList(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
            }
            this.disabled = false
            this.total = data.count || 0
          }
          this.spinning = false
          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 = []
        }
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.getSettleStyle()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 230
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$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()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .companyCollectionPaymentList-wrap{
    .active{
      color: #ed1c24;
      cursor: pointer;
    }
    .common{
      color: rgba(0, 0, 0);
    }
  }
</style>
