<template>
	<div class="table-container">
		<vab-query-form>
			<div class="title">
				<div class="left">
					<div class="left-item">
						<div>公司：</div>
						<el-select v-model="companyId" placeholder="请选择" clearable @change="getRegionAndStation">
							<el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
					</div>
					<div class="left-item">
						<div>区域：</div>
						<el-select v-model="regionId" placeholder="请选择" clearable>
							<el-option v-for="item in regionList" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
					</div>
					<div class="left-item">
						<div>场站：</div>
						<el-select v-model="stationId" placeholder="请选择" clearable>
							<el-option v-for="item in stationList" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
					</div>
					<el-button icon="el-icon-search" type="primary" native-type="submit" @click="fetchData">
						查询
					</el-button>
				</div>
				<div class="right">
					<!-- <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
						添加
					</el-button>
					<el-button icon="el-icon-delete" type="danger" @click="handleDelete">
						删除
					</el-button> -->
					<!-- <UploadExcel style="margin-left: 10px;" @getResult="getMyExcelData"></UploadExcel> -->
					<ExportExcel :exportList="exportExcel" :useType="'export'" partsName="装机量"></ExportExcel>
					<!-- <ExportExcel :exportList="templateExcel" :useType="'template'" partsName="设备类型模板"></ExportExcel> -->
				</div>
			</div>
			<el-table header-cell-class-name="table_header_style" border ref="tableSort" v-loading="listLoading" :data="list"
				:element-loading-text="elementLoadingText" :height="height" @selection-change="setSelectRows">
				<!-- <el-table-column show-overflow-tooltip type="selection" width="40"></el-table-column> -->
				<el-table-column show-overflow-tooltip label="id" prop="id" align="center" sortable></el-table-column>
				<el-table-column show-overflow-tooltip label="编号" prop="code" align="center" sortable></el-table-column>
				<el-table-column show-overflow-tooltip label="名称" prop="name" align="center" sortable></el-table-column>
				<el-table-column show-overflow-tooltip label="装机容量" prop="capacity" align="center" sortable>
				</el-table-column>
				<el-table-column show-overflow-tooltip label="装机容量单位" prop="capacityunit" align="center" sortable>
				</el-table-column>
				<el-table-column show-overflow-tooltip label="装机台数" prop="quantity" align="center" sortable>
				</el-table-column>
				<el-table-column show-overflow-tooltip label="机型" prop="model" align="center" sortable>
				</el-table-column>
				<el-table-column show-overflow-tooltip label="厂家" prop="systemmanufact" align="center" sortable>
				</el-table-column>
				<el-table-column show-overflow-tooltip label="地址" prop="address" align="center" sortable>
				</el-table-column>
				<!-- <el-table-column show-overflow-tooltip label="操作" width="100px" fixed="right">
					<template #default="{ row }">
						<el-button type="text" @click="handleEdit(row)">编辑</el-button>
						<el-button type="text" @click="handleDelete(row)">删除</el-button>
					</template>
				</el-table-column> -->
			</el-table>
		</vab-query-form>
	</div>
</template>
<script>
import UploadExcel from '@/components/UploadExcel/index'
import ExportExcel from '@/components/ExportExcel/index'
import api from '@/api/realtimeDatabaseConfiguration'
import stationApi from '@/api/station'
export default {
	components: {
		UploadExcel,
		ExportExcel,
	},
	data() {
		return {
			fuzzyQuery_name: '',
			listLoading: true,
			list: [],
			options: [],
			exportExcel: [],
			templateExcel: [],
			elementLoadingText: '正在加载...',
			selectRows: '',
			layout: 'total, sizes, prev, pager, next, jumper',
			total: 0,
			background: true,
			exportList: [],
			queryForm: {
				pageNo: 1,
				pageSize: 20,
			},
			companyId: '',
			regionId: '',
			stationId: '',
			companyList: [],
			regionList: [],
			stationList: [],
		};
	},
	computed: {
		height() {
			return this.$baseTableHeight() + 50
		},
	},
	created() {
		this.getCompany()
	},
	methods: {
		getMyExcelData(val) {
			val.forEach(item => {
				api.addModelpower(item).then(res => {
					if (res.code == 200) {
						this.$message({
							type: 'success',
							message: '添加成功!'
						});
						this.fetchData()
					}
				})
			})
		},
		getCompany() {
			stationApi.companys({
				id: '',
				name: '',
				pageNum: 1,
				pageSize: 10000,
			}).then(res => {
				if (res.data) {
					this.companyId = ''
					this.companyList = res.data.records
					if (this.companyList.length) {
						this.companyId = this.companyList[0].id
						this.getRegionAndStation(this.companyId)
					} else {
						this.getRegionAndStation('')
					}
					this.fetchData()
				}
			})
		},
		getRegionAndStation(companyId) {
			stationApi.regionList({
				companyid: companyId ? companyId : '',
				name: '',
				pageNum: 1,
				pageSize: 10000,
			}).then(res => {
				if (res.data) {
					this.regionId = ''
					this.regionList = res.data.records
				}
			})
			stationApi.windpowerstationList({
				id: '',
				name: '',
				companyid: companyId ? companyId : '',
				pageNum: 1,
				pageSize: 10000,
			}).then(res => {
				if (res.data) {
					this.stationId = ''
					this.stationList = res.data.records
				}
			})
		},
		fetchData() {
			stationApi.getAdmList({
				companyid: this.companyId,
				regionid: this.regionId,
				id: this.stationId,
				// name: this.fuzzyQuery_name,
				// pageNum: this.queryForm.pageNo,
				// pageSize: this.queryForm.pageSize,
			}).then(res => {
				if (res.data) {
					this.listLoading = false
					// this.total = res.data.total
					this.list = res.data
					this.templateExcel = [
						{
							id: '',
							code: '',
							name: '',
							capacity: '',
							capacityunit: '',
							quantity: '',
							model: '',
							systemmanufact: '',
							address: ''
						}
					]
					// 导出
					let exportExcel = []
					res.data.forEach(item => {
						exportExcel.push({
							id: item.id,
							code: item.code,
							name: item.name,
							capacity: item.capacity,
							capacityunit: item.capacityunit,
							quantity: item.quantity,
							model: item.model,
							systemmanufact: item.systemmanufact,
							address: item.address
						})
					})
					this.exportExcel = exportExcel
				}
			})
		},
		handleSizeChange(val) {
			this.queryForm.pageSize = val
			this.fetchData()
		},
		handleCurrentChange(val) {
			this.queryForm.pageNo = val
			this.fetchData()
		},
		handleAdd() {
			this.$refs['edit'].showEdit()
		},
		handleEdit(row) {
			this.$refs['edit'].showEdit(row)
		},
		handleDelete(row) {
			if (row.id || this.selectRows.length > 0) {
				this.$baseConfirm('你确定要删除吗', null, async () => {
					let ids = ''
					if (this.selectRows.length > 0) {
						ids = this.selectRows.map((item) => item.id).join()
					}
					api.deleteModelpower({
						id: row.id || ids
					}).then(res => {
						if (res.data) {
							this.$baseMessage('删除成功', 'success')
							this.fetchData()
						}
					})
				})
			} else {
				this.$baseMessage('未选中任何行', 'error')
				return false
			}
		},
		setSelectRows(val) {
			this.selectRows = val
		},
	},
}
</script>
<style lang="less" scoped>
.title {
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	margin-bottom: 20px;
}

.left {
	display: flex;
	flex-direction: row;
	align-items: center;

	.left-item {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-right: 10px;
	}
}
</style>