<template>
	<div class="container">
		<div class="search-area">
			<Form ref="formInline" inline :label-width="120" :model="formInline" :rules="ruleInline">
				<FormItem prop="openid" label="openid">
					<Input type="text" v-model="formInline.openid" placeholder="" />
				</FormItem>
				<FormItem prop="phone" label="手机号">
					<Input type="text" v-model="formInline.phone" placeholder="" />
				</FormItem>
				<FormItem prop="type" label="类型">
					<Select v-model="formInline.type" style="width:200px">
						<Option value="1">充电订单</Option>
						<Option value="2">充值</Option>
						<Option value="3">提现</Option>
					</Select>
				</FormItem>
				<FormItem prop="" label="起止时间">
					<DatePicker type="daterange" placement="bottom-end" placeholder="请选择时间" style="width: 200px"
						@on-change="chage" />
				</FormItem>
				<FormItem>
					<Button type="primary" @click="handleSubmit('formInline')"><Icon type="ios-search" />搜索</Button>
					<Button style="margin-left: 8px" @click="resetSearch"><Icon type="md-refresh" />重置</Button>
				</FormItem>
			</Form>
		</div>
		<div class="table-container">
			<Table border :columns="columns" stripe :height="tableHeight" :data="data">
				<template #action="{ row, index }">
					<!-- <Button type="primary" size="small" style="margin-right: 5px" @click="show(index)">详情</Button> -->
				</template>
			</Table>
			<Page :total="total" show-total show-sizer class="page" @on-change="onChangePage"
				@on-page-size-change="onChangePageSize" />
		</div>

	</div>
</template>

<script>
	import {
		getCaiWuData,
	} from '@/api'

	export default {
		name: 'charging_station',
		data() {
			return {
				show_modal: false,
				page: 1,
				pageSize: 10,
				total: 100,
				tableHeight: 500,
				formInline: {
					openid: '',
					type: '',
					start_time: '',
					end_time: '',
					phone: '',
				},
				ruleInline: {},
				columns: [{
						title: 'Id',
						key: 'id',
					},
					{
						title: '用户',
						key: 'openid',
					},
					{
						title: '手机号',
						key: 'phone',
					},
					{
						title: '类型',
						key: 'type_text',
					},
					{
						title: '金额',
						key: 'money',
					},
					{
						title: '说明',
						key: 'mark',
					},
					{
						title: '时间',
						key: 'createtime',
					},
					{
						title: '操作',
						slot: 'action',
						width: 150,
						align: 'center',
					},
				],
				data: [],
			}
		},
		mounted() {
			this.getList()
			this.calculateTableHeight()
			window.addEventListener('resize', this.calculateTableHeight)
		},
		beforeDestroy() {
			window.removeEventListener('resize', this.calculateTableHeight)
		},
		methods: {
			calculateTableHeight() {
				// 计算表格高度 = 窗口高度 - 搜索区域高度 - 分页高度 - 其他间距
				const searchHeight = document.querySelector('.search-area').offsetHeight
				const pageHeight = 32 // 分页组件大约高度
				const margins = 40 // 上下边距总和
				this.tableHeight = window.innerHeight - searchHeight - pageHeight - margins - 130
			},
			async getList() {
				await getCaiWuData({
					page: this.page,
					pageSize: this.pageSize,
					...this.formInline,
				}).then((res) => {
					this.total = res.total
					this.data = res.data
					this.page = res.current_page
				})
			},
			handleSubmit() {
				this.getList()
			},
			resetSearch(name) {
				this.formInline = {
					openid: '',
					type: '',
					start_time: '',
					end_time: '',
					phone: '',
				}
				this.getList()
			},
			show(index) {
				this.$Modal.info({
					title: 'User Info',
					content: `Name：${this.data[index].name}<br>Age：${this.data[index].age}<br>Address：${this.data[index].address}`,
				})
			},
			onChangePage(e) {
				if (this.page != e) {
					this.page = e
					this.getList()
				}
			},
			onChangePageSize(e) {
				if (this.pageSize != e) {
					this.page = 1
					this.pageSize = e
					this.getList()
				}
			},
			chage(e) {
				this.formInline.start_time = ''
				this.formInline.end_time = ''
				this.formInline.start_time = e[0]
				this.formInline.end_time = e[1]
			},
		},
	}
</script>
<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		padding: 20px;
		box-sizing: border-box;
	}

	.action-btn {
		margin: 20px 0;
		background-color: white;
		padding: 20px;
		border: 1px solid #F3F7FD;
		border-radius: 15px;
		display: flex;
		align-items: center;

		button {
			margin-right: 20px;
		}
	}

	.table-container {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.page {
		margin-top: 10px;
		text-align: right;
	}

	.search-area {
		background-color: white;
		padding-top: 20px;
		border: 1px solid #F3F7FD;
		border-radius: 15px;
	}
</style>