<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />

		<!-- 列表 -->
		<base-table
			v-loading="dataListLoading"
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
      :max-height="tableH"
			:table-data="tableData"></base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />
	</div>
</template>

<script>
import basicPage from '../mixins/basic-page';
import codeFilter from '../mixins/code-filter';
import {
	getCostEneryAutoPage,
	exportCostEneryExcel,
} from '@/api/cost/costEneryAutoReport';
import { getEnergyTypeListAll } from '@/api/base/energyType';
import { publicFormatter } from '@/utils/dict';
import moment from 'moment';

const tableProps = [
	{
		prop: 'energyTypeName',
		label: '能源类型',
	},
	{
		prop: 'objName',
		label: '监控对象',
		filter: (val) => (val != null ? val : '--'),
	},
	{
		prop: 'tableName',
		label: '水/气表名',
		filter: (val) => (val != null ? val : '--'),
		filter: publicFormatter('table_name'),
	},
	{
		prop: 'totalUsed',
		label: '累计使用量',
	},
	{
		prop: 'totalCost',
		label: '总价',
		align: 'right',
	},
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getCostEneryAutoPage,
        exportURL: exportCostEneryExcel
			},
			tableData: [],
			tableProps,
      tableH: this.tableHeight(260),
			drawerVisible: false,
			formConfig: [
				{
					type: 'select',
					label: '能源类型',
					selectOptions: [],
					param: 'energyTypeId',
					filterable: true,
				},
				{
					type: 'datePicker',
					label: '时间范围',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'timestamp',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'searchTime',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: this.$auth.hasPermi('cost:energyCost:export')
						? 'button'
						: '',
					btnName: '导出',
					name: 'export',
					color: 'primary',
					plain: true,
				},
			],
		};
	},
	components: {},
	created() {
    window.addEventListener('resize', () => {
      this.tableH = this.tableHeight(260)
    })
		getEnergyTypeListAll().then((response) => {
			this.formConfig[0].selectOptions = response.data;
		});
	},
	methods: {
    // 获取数据列表
    getDataList() {
      if(this.listQuery.searchTime){
      this.dataListLoading = true;
      this.urlOptions.getDataListURL(this.listQuery).then(response => {
        if(response.hasOwnProperty('data')){
          this.tableData = response.data.list;
          this.listQuery.total = response.data.total;
        }
        this.dataListLoading = false;
      });
    }else{
      this.$message.warning('请选择时间范围')
    }
    },
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.energyTypeId = val.energyTypeId;
          this.listQuery.searchTime = val.searchTime ? val.searchTime[0] : null;
					this.listQuery.startTime = val.searchTime
						? moment(val.searchTime[0]).startOf('day').format('x')
						: null;
					this.listQuery.endTime = val.searchTime
						? moment(val.searchTime[1]).endOf('day').format('x')
						: null;
					this.getDataList();
					break;
				case 'export':
          const data = {
            energyTypeId:val.energyTypeId,
            startTime : val.searchTime
						? moment(val.searchTime[0]).startOf('day').format('x')
						: null,
            endTime : val.searchTime
						? moment(val.searchTime[1]).endOf('day').format('x')
						: null,
          }
					this.handleExport(data);
					break;
				default:
					console.log(val);
			}
		},
	},
};
</script>
<style>
.app-container .el-table .el-table__cell {
	padding: 0;
	height: 35px;
}
</style>
