<script setup name="search">
import { onMounted, reactive, ref } from 'vue'
import request from '@/api/axios.js'
import SubmitBtn from '@com/SubmitBtn.vue'

const queryForm = reactive({
	station: '',
	wtIds: [],
	st: Date.now() - 30 * 24 * 60 * 60 * 1000,
	et: Date.now(),
	// interval: 3
})
/**场站 */
const stationList = ref([])
const funGetStation = async () => {
	const res = await request.get("/base/gfstation")
	stationList.value = res.data
	if (stationList.value.length) {
		queryForm.station = 'HZJ_GDC'
		funGetWind(queryForm.station)
	}
}
const funStationChange = (stationId) => {
	if (stationId) {
		funGetWind(stationId)
	} else {
		windList.value = []
	}
}
/**风机 */
const checkAll = ref(true)
const windList = ref([])
const funGetWind = async (stationId) => {
	const res = await request.get("base/inverter", {params: { stationId }})
	windList.value = res.data
	queryForm.wtIds = res.data.map(o => o.id)
	checkAll.value = true
}
const funCheckAll = () => {
	checkAll.value = !checkAll.value
	if(checkAll.value){
		queryForm.wtIds = windList.value.map(o => o.id)
	}else{
		queryForm.wtIds = []
	}
}
/**导出 */
const emits = defineEmits(['submit'])
const funSubmit = async () => {
	const startDate = new Date(queryForm.st).setHours(0,0,0,0)
	const endDate = new Date(queryForm.et).setHours(0,0,0,0)
	const query = {
		station: queryForm.station,
		wtIds: queryForm.wtIds.join(),
		st: new Date(startDate).getTime(),
		et: new Date(endDate).getTime(),
		// interval: queryForm.interval
	}
	// switch (queryForm.interval) {
	// 	case 2:
	// 		query.interval = 60
	// 		break;
	// 	case 3:
	// 		query.interval = 600
	// 		break;
	// 	case 4:
	// 		query.interval = 900
	// 		break;
	// }
	emits('submit', query)
}
/**created */
funGetStation()
</script>
<template>
	<div class="pl-[20px] flex items-center h-[80px] relative">
		<div class="absolute top-[-7px] left-[20px] text-[#838383] text-[14px]">操作面板</div>
		<el-form class="" :inline="true" :model="queryForm">
			<el-form-item label="场站" class="!mb-0">
				<el-select v-model="queryForm.station" class="w-[150px]" @change="funStationChange">
					<el-option v-for="item in stationList" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="逆变器" class="!mb-0">
				<el-select multiple class="w-[150px]" v-model="queryForm.wtIds" @clear="checkAll = false" collapse-tags>
					<el-option label="全选" :class="{'selected': checkAll}" @click="funCheckAll"></el-option>
					<el-option v-for="item in windList" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="开始时间" class="!mb-0">
				<el-date-picker type="date" class="!w-[150px]" v-model="queryForm.st"></el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间" class="!mb-0">
				<el-date-picker type="date" class="!w-[150px]" v-model="queryForm.et"></el-date-picker>
			</el-form-item>
			<!-- <el-form-item label="等间隔" class="!mb-0">
				<el-radio-group v-model="queryForm.interval">
					<el-radio :label="1">一秒钟</el-radio>
					<el-radio :label="2">一分钟</el-radio>
					<el-radio :label="3">十分钟</el-radio>
					<el-radio :label="4">十五分钟</el-radio>
				</el-radio-group>
			</el-form-item> -->
			<el-form-item class="!mb-0">
				<submit-btn v-prevdbclick:5000="funSubmit" desc="执行"></submit-btn>
			</el-form-item>
		</el-form>
	</div>
</template>