<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />
		<!-- 列表 -->
		<base-table
			:page="queryParams.pageNo"
			:limit="queryParams.pageSize"
			:table-props="tableProps"
			:table-data="list"
			:max-height="tableH"
			@selection-change="selectChange">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="120"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:page.sync="queryParams.pageNo"
			:limit.sync="queryParams.pageSize"
			:total="total"
			@pagination="getList" />
		<!-- 新增 -->
		<base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="centervisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel">
			<energy-quantity-manual-add
				ref="energyQuantityManualAdd"
				:energyTypeList="energyTypeList"
				@successSubmit="successSubmit" />
		</base-dialog>
	</div>
</template>

<script>
import {
	energyQuantityManualPage,
	energyQuantityManualDelete,
	energyQuantityManualExport,
} from '@/api/base/energyQuantityManual';
import { getEnergyTypeListAll } from '@/api/base/energyType';
import { publicFormatter } from '@/utils/dict';
import { parseTimeTable } from '@/utils/ruoyi';
import EnergyQuantityManualAdd from './components/energyQuantityManualAdd';
import moment from 'moment';
import tableHeightMixin from '@/mixins/lb/tableHeightMixin';
const tableProps = [
	{
		prop: 'energyTypeLabel',
		label: '能源类型',
		minWidth: 110,
		showOverflowtooltip: true,
	},
	{
		prop: 'unit',
		label: '单位',
		filter: publicFormatter('unit_dict'),
		minWidth: 110,
	},
	{
		prop: 'tableName',
		label: '能源表名',
		filter: publicFormatter('table_name'),
		minWidth: 110,
		showOverflowtooltip: true,
	},
	{
		prop: 'recordTime',
		label: '抄表日期',
		filter: parseTimeTable('{y}-{m}-{d}'),
		minWidth: 110,
	},
	{
		prop: 'readingQuantity',
		label: '抄表值',
	},
	{
		prop: 'useQty',
		label: '差值',
	},
];
export default {
	name: 'EnergyQuantityManual',
	data() {
		return {
			formConfig: [
				{
					type: 'select',
					label: '能源类型',
					labelField: 'labelName',
					selectOptions: [],
					param: 'energyTypeId',
					filterable: true,
				},
				{
					type: 'datePicker',
					label: '抄表日期',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'timestamp',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'timeVal',
					defaultSelect: [],
				},
				{
					type: this.$auth.hasPermi('base:energy-quantity-manual:query')
						? 'button'
						: '',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: this.$auth.hasPermiOr([
						'base:energy-quantity-manual:export',
						'base:energy-quantity-manual:create',
					])
						? 'separate'
						: '',
				},
				{
					type: this.$auth.hasPermi('base:energy-quantity-manual:export')
						? 'button'
						: '',
					btnName: '导出',
					name: 'export',
					color: 'primary',
					plain: true,
				},
				{
					type: this.$auth.hasPermi('base:energy-quantity-manual:create')
						? 'button'
						: '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true,
				},
			],
			tableProps,
			// 总条数
			total: 0,
			// 班次基础信息列表
			list: [],
			tableBtn: [
				this.$auth.hasPermiAnd([
					'base:energy-quantity-manual:query',
					'base:energy-quantity-manual:create',
				])
					? {
							type: 'meterReading',
							btnName: '抄表',
					  }
					: undefined,
				this.$auth.hasPermiAnd([
					'base:energy-quantity-manual:update',
					'base:energy-quantity-manual:query',
				])
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
				this.$auth.hasPermi('base:energy-quantity-manual:delete')
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 20,
				energyTypeId: '',
				recordTime: [],
			},
			energyTypeList: [],
			exportList: [],
			addOrEditTitle: '',
			centervisible: false,
		};
	},
	components: { EnergyQuantityManualAdd },
	mixins: [tableHeightMixin],
	created() {
		let end = moment(moment().format('YYYY-MM-DD 23:59:59')).valueOf();
		let start = moment(
			moment().subtract(7, 'days').format('YYYY-MM-DD 00:00:00')
		).valueOf();
		this.formConfig[1].defaultSelect = [start, end];
		this.queryParams.recordTime[0] = start;
		this.queryParams.recordTime[1] = end;
		this.getList();
		this.getTypeList();
	},
	methods: {
		buttonClick(val) {
			this.queryParams.pageNo = 1;
			this.queryParams.energyTypeId = val.energyTypeId;
			this.queryParams.recordTime[0] = val.timeVal
				? moment(moment(val.timeVal[0]).format('YYYY-MM-DD 00:00:00')).valueOf()
				: null;
			this.queryParams.recordTime[1] = val.timeVal
				? moment(moment(val.timeVal[1]).format('YYYY-MM-DD 23:59:59')).valueOf()
				: null;
			switch (val.btnName) {
				case 'search':
					this.getList();
					break;
				case 'add':
					this.addOrEditTitle = '新增';
					this.centervisible = true;
					let params = {};
					params.type = 'add';
					this.$nextTick(() => {
						this.$refs.energyQuantityManualAdd.init(params);
					});
					break;
				default:
					this.$modal
						.confirm('是否确认导出')
						.then(() => {
							return energyQuantityManualExport({
								energyTypeId: this.queryParams.energyTypeId,
								recordTime: this.queryParams.recordTime,
							});
						})
						.then((response) => {
							this.$download.excel(response, '能源报表.xls');
						})
						.catch(() => {});
			}
		},
		/** 查询列表 */
		getList() {
			energyQuantityManualPage(this.queryParams).then((response) => {
				let arr = response.data.list || [];
				arr &&
					arr.map((item) => {
						item.amount = item.amount
							? !isNaN(parseFloat(item.amount)) && isFinite(item.amount)
								? item.amount.toFixed(2)
								: ''
							: '';
						this.getDictDatas('energy_type').map((subItem) => {
							if (item.energyType === subItem.value) {
								item.energyTypeLabel = subItem.label;
							}
						});
					});
				this.list = arr;
				this.total = response.data.total;
				this.exportList = [];
			});
		},
		getTypeList() {
			getEnergyTypeListAll().then((res) => {
				this.formConfig[0].selectOptions = res.data || [];
				this.energyTypeList = res.data || [];
			});
		},
		selectChange(val) {
			console.log(val);
			this.exportList = val;
		},
		handleClick(val) {
			console.log(val);
			switch (val.type) {
				case 'edit':
					this.addOrEditTitle = '编辑';
					this.centervisible = true;
					let paramA = {};
					paramA.type = 'edit';
					paramA.id = val.data.id;
					this.$nextTick(() => {
						this.$refs.energyQuantityManualAdd.init(paramA);
					});
					break;
				case 'meterReading':
					this.addOrEditTitle = '新增';
					this.centervisible = true;
					let paramB = {};
					paramB.type = 'meterReading';
					paramB.energyTypeId = val.data.energyTypeId;
					paramB.tableName = val.data.tableName;
					this.$nextTick(() => {
						this.$refs.energyQuantityManualAdd.init(paramB);
					});
					break;
				default:
					this.handleDelete(val.data);
			}
		},
		// 新增
		handleCancel() {
			this.$refs.energyQuantityManualAdd.formClear();
			this.centervisible = false;
			this.addOrEditTitle = '';
		},
		handleConfirm() {
			this.$refs.energyQuantityManualAdd.submitForm();
		},
		successSubmit() {
			this.handleCancel();
			this.getList();
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			console.log(row.id);
			this.$modal
				.confirm('是否确认删除能源类型为"' + row.energyTypeLabel + '"的数据项?')
				.then(function () {
					return energyQuantityManualDelete({ id: row.id });
				})
				.then(() => {
					this.queryParams.pageNo = 1;
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
	},
};
</script>
