<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        :label="`${agencyName()}名称`"
        label-width="140px"
        prop="keyWords1"
      >
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>
      <el-form-item
        label="起始时间"
        prop="startDate"
      >
        <el-date-picker
          v-model="form.startDate"
          type="date"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="至"
        label-width="30px"
        prop="endDate"
      >
        <el-date-picker
          v-model="form.endDate"
          type="date"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <div class="btn-wrap">
        <el-button @click="reset()">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getAgencyStatistic"
        >
          查询
        </el-button>
        <el-button
          type="primary"
          @click="exportStatist">
          导出
        </el-button>
      </div>
    </el-form>

    <div
      class="table-box">
      <div class="block-title">
        {{ agencyName() }}预付款统计
      </div>

      <el-table
        border
        class="spanTable"
        v-loading="loading"
        v-if="tableMaxHeight"
        :max-height="tableMaxHeight"
        :data="tableData"
        :row-class-name="totalRowClassName"
        :row-click="(row, column, event)=>{console.log(row)}"
      >
        <!-- :row-class-name="totalRowClassName" -->
        <el-table-column
          prop="agency_name"
          :label="`${agencyName()}名称`"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="early_amount"
          label="初期预收款金额"
        >
          <template
            slot="header">
            初期预收款金额
            <Tip msg="今日之前有预存款金额"></Tip>
          </template>
        </el-table-column>
        <el-table-column
          prop="new_amount"
          label="新增预收款金额"
        >
          <template
            slot="header">
            新增预收款金额
            <Tip msg="今日新增预收款金额"></Tip>
          </template>
        </el-table-column>
        <el-table-column
          prop="use_amount"
          label="冲预收款金额"
        >
          <template
            slot="header">
            冲预收款金额
            <Tip msg="今日用预收款金额"></Tip>
          </template>
        </el-table-column>
        <el-table-column
          prop="left_amount"
          label="期末收款金额"
        >
          <template
            slot="header">
            期末收款金额
            <Tip msg="一共剩余预收款金额"></Tip>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getAgencyAmountFlow, exportAgencyAmount } from '@/api/travelAgency'
import VueScrollTo from 'vue-scrollto'
import moment from 'moment'

export default {
  name: 'agencyPreOrderStatistic',
  data () {
    return {
      firstTimeIn: true,
      form: {
        startDate: new Date(moment().format('YYYY-MM-DD')),
        endDate: new Date(moment().format('YYYY-MM-DD')),
        keyWords1: ''
      },
      tableData: [],
      currentItem: {},
      dialogVisible: false,
      loading: false,
      tableMaxHeight: 0
    }
  },
  created () {
    this.getAgencyStatistic()
  },
  mounted () {
    this.setTableHeight()
    window.addEventListener('resize', this.setTableHeight)
  },
  methods: {
    totalRowClassName ({ row, rowIndex }) {
      if (row.agency_name === '总计') {
        return 'table-total-row'
      }

      if (row.travelAgencyName === '总计' && row.price === '小计' && row.ticketTypeName === '小计') {
        return 'table-total-row table-total-row1'
      }

      if (row.price === '小计' && row.ticketTypeName === '小计') {
        return 'table-total-row table-total-row2'
      }

      if (row.price === '小计') {
        return 'table-total-row table-total-row3'
      }
    },
    getAgencyStatistic () {
      this.loading = true
      this.colunmSpan0 = null
      getAgencyAmountFlow(this.form).then(res => {
        this.tableData = res
        // 第一次进入页面不滚动
        if (this.firstTimeIn) {
          this.firstTimeIn = false
          return
        }
        this.$nextTick(() => {
          VueScrollTo.scrollTo('#table-box', 1000, { container: '.app-main .el-scrollbar__wrap' })
        })
      }).finally(() => {
        this.loading = false
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    exportStatist () {
      exportAgencyAmount(this.form).then(res => {
        this.$message.success('导出成功。')
        this.goToDownload()
      })
    },
    setTableHeight () {
      let wrap = document.querySelector('.app-main').getBoundingClientRect()
      // let table = this.$refs['table-wrap'].getBoundingClientRect()
      this.tableMaxHeight = wrap.height - 50
    }
  }
}
</script>
<style lang="scss" scoped>
// .boxWrap {
//   position: absolute;top: 0;left: 0;width: 100%;height: 100%; overflow: hidden;
// }

::v-deep .el-table {
  .table-total-row.border3 td{
    border-bottom-width: 4px;
  }
  .table-total-row.border2 td{
    border-bottom-width: 3px;
  }
  .table-total-row.border1 td{
    border-bottom-width: 2px;
  }
}

::v-deep .el-table--enable-row-hover .el-table__body tr:hover > td{
    background: rgba(248, 237, 117, 0.561) !important;
}

::v-deep .el-table--border {
  td,th{
    border-color: rgba(48, 77, 121, 0.5);
  }
}
.table-box {
  border: 0;
   ::v-deep .el-tabs__nav-wrap {
    padding: 0 24px;
  }
}

</style>

<style lang="scss">
.spanTable {
  tr td:first-child .cell,tr th:first-child .cell {
    padding-left: 10px;
  }
}
</style>
