<template>
	<div class="container">
		<div class="search-area">
			<Form ref="formInline" inline :label-width="120" :model="formInline" :rules="ruleInline">
				<FormItem prop="user" label="站点名称：">
					<Input type="text" clearable  v-model="formInline.charge_station_name" placeholder="" />
				</FormItem>
				<FormItem>
					<Button type="primary" @click="handleSubmit('formInline')">搜索</Button>
				</FormItem>
			</Form>
		</div>
		<div class="action-btn">
			<Button type="primary" @click="add">添加</Button>
			<!-- <Button type="error">删除</Button> -->
		</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(row.charge_station_id)">编辑</Button>
					<Button type="error" size="small" @click="remove(row.charge_station_id)">删除</Button>
				</template>
			</Table>
			<Page :total="total" show-total show-sizer class="page" @on-change="onChangePage"
				@on-page-size-change="onChangePageSize" />
		</div>

		<!-- 添加、编辑 -->
		<Modal v-model="show_modal" title="添加站点" :mask-closable="false">
			<Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
				<FormItem label="站点名称" prop="charge_station_name">
					<Input v-model="formValidate.charge_station_name" placeholder="站点名称" />
				</FormItem>
				<FormItem label="站点编号" prop="charge_station_number">
					<Input v-model="formValidate.charge_station_number" placeholder="站点编号" />
				</FormItem>
				<FormItem label="省" prop="city">
					<Input v-model="formValidate.city" placeholder="省" />
				</FormItem>
				<FormItem label="市" prop="area">
					<Input v-model="formValidate.area" placeholder="市" />
				</FormItem>
				<FormItem label="县" prop="street">
					<Input v-model="formValidate.street" placeholder="县" />
				</FormItem>
				<FormItem label="详细地址" prop="address">
					<Input v-model="formValidate.address" placeholder="详细地址" />
				</FormItem>
				<FormItem label="联系电话" prop="charge_station_phone">
					<Input v-model="formValidate.charge_station_phone" placeholder="联系电话" />
				</FormItem>
				<FormItem label="精度" prop="longitude">
					<Input v-model="formValidate.longitude" placeholder="精度" />
				</FormItem>
				<FormItem label="纬度" prop="latitude">
					<Input v-model="formValidate.latitude" placeholder="纬度" />
				</FormItem>
			</Form>
			<div slot="footer">
				<Button type="text" @click="cancel">取消</Button>
				<Button type="primary" @click="ok">确定</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
	import {
		getStationList, addPosition, getStationRead, getStationUpdate, getStationDelete,
	} from '@/api'

	export default {
		name: 'charging_station',
		data() {
			return {
				show_modal: false,
				page: 1,
				pageSize: 10,
				total: 100,
				tableHeight: 500,
				formInline: {
					charge_station_name: '',
				},
				ruleInline: {},
				columns: [{
						title: '站点名称',
						key: 'charge_station_name',
					},
					{
						title: '站点编号',
						key: 'charge_station_number',
					},
					{
						title: '站点地址',
						key: 'address',
					},
					{
						title: '精度',
						key: 'longitude',
					},
					{
						title: '纬度',
						key: 'latitude',
					},
					{
						title: '操作',
						slot: 'action',
						width: 150,
						align: 'center',
					},
				],
				data: [],
				formValidate: {
					charge_station_name: '',
					charge_station_number: '',
					city: '',
					area: '',
					street: '',
					address: '',
					charge_station_phone: '',
					longitude: [],
					latitude: '',
				},
				ruleValidate: {
					charge_station_name: [{
						required: true,
						message: '请输入站点名称',
						trigger: 'blur',
					}],
					charge_station_number: [{
							required: true,
							message: '请输入站点编号',
							trigger: 'blur',
					}],
					city: [{
						required: true,
						message: '请输入省',
						trigger: 'change',
					}],
					area: [{
						required: true,
						message: '请输入市',
						trigger: 'change',
					}],
					street: [{
						required: true,
						message: '请输入县',
						trigger: 'change',
					}],
					address: [{
						required: true,
						message: '请输入详细地址',
						trigger: 'change',
					}],
					charge_station_phone: [{
						required: true,
						message: '请输入联系电话',
						trigger: 'change',
					}],
					longitude: [{
						required: true,
						message: '请输入精度',
						trigger: 'blur',
					}],
					latitude: [{
						required: true,
						message: '请输入纬度',
						trigger: 'blur',
					}],
				},
			}
		},
		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 actionBtnHeight = document.querySelector('.action-btn').offsetHeight
				const pageHeight = 32 // 分页组件大约高度
				const margins = 40 // 上下边距总和
				this.tableHeight = window.innerHeight - actionBtnHeight - searchHeight - pageHeight - margins - 130
			},
			async getList() {
				await getStationList({
					page: this.page,
					pageSize: this.pageSize,
					...this.formInline,
				}).then((res) => {
					this.total = res.data.total
					this.data = res.data.data
				})
			},
			handleSubmit() {
				this.getList()
			},
			add() {
				this.show_modal = true
				this.$refs.formValidate.resetFields()
				this.formValidate.charge_station_id = ''
			},
			cancel() {
				this.$refs.formValidate.resetFields()
				this.show_modal = false
			},
			// 详情
			async show(id) {
				await getStationRead({ id }).then(res => {
					this.formValidate = { ...res.data }
					this.show_modal = true
				})
			},
			async remove(id) {
				const confirm = await new Promise(resolve => {
					this.$Modal.confirm({
						title: '确认删除',
						content: '您确定要删除此项吗？',
						onOk: () => resolve(true),
						onCancel: () => resolve(false),
					})
				})
				if (!confirm) {
					this.$Message.info('已取消删除')
					return
				}
				await getStationDelete({ id }).then(res => {
					this.$Message.success(res.msg)
					this.getList()
				})
			},
			async ok() {
				const valid = await new Promise(resolve => this.$refs.formValidate.validate(resolve))
				if (!valid) {
					this.$Message.error('请正确填写表单')
					return
				}
				if (this.formValidate.charge_station_id) {
					await getStationUpdate(this.formValidate).then(res => {
						this.$Message.success(res.msg)
						this.getList()
						this.cancel()
					})
				} else {
					await addPosition(this.formValidate).then((res) => {
						this.$Message.success(res.msg)
						this.cancel()
						this.getList()
					})
				}
			},
			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()
				}
			},
		},
	}
</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>