<script setup name="prepare">
import searchCop from './components/search.vue'
import excelCop from '@/components/excel.vue'
import treeCop from '@/components/tree.vue'
import tableCop from './components/table.vue'
import { ElMessage } from 'element-plus';
import { onMounted, ref, onActivated } from 'vue'
import request from '@/api/axios.js'
import {baseURL, socketURL} from '@/api/axios.js'
/**配置参数 */
const treeHeight = ref(window.innerHeight - 260 + 'px') //tree高度
const excelHeight = ref(window.innerHeight - 260 + 'px') //excel高度
const tableHeight = ref(window.innerHeight - 260 + 'px')
/**excel 开始 */
const excelList = ref([])
const funExcelChange = async (obj) => { //点击excel项时
	tableShowId.value = obj.id
	tableName.value = obj.name
	tableLoading.value = true
	const res = await request.get('/power/prepare/show', { params: { id: obj.id } })
	if(res.code === 200){
		tableColumn.value = res.data.title.map(o => {
			return {
				prop: o.key,
				label: o.des,
				width: o.des==='时间'? 100: 80,
			}
		})
		tableData.value = res.data.data
		tableLoading.value = false
	}else{
		tableLoading.value = false
	}
}
/**tree 开始 */
const treeData = ref([])
const actTreeNode = ref(null) //当前激活的treeNode
const funRepeatMap = (arr) => {
	return arr.map(o => {
		if (o.children) {
			const findIndex = o.children.findIndex(p => !!p.type)
			if (findIndex !== -1) {
				o.childs = o.children
				o.children = []
				if(!actTreeNode.value){ //判断当且仅有process获取tree时 赋值
					actTreeNode.value = o
				}
			}
		}
		return {
			...o,
			children: o.children?.length ? funRepeatMap(o.children) : []
		}
	})
}
const funGetTree = async () => {
	actTreeNode.value = null
	const res = await request.get("/power/prepare/tree")
	treeData.value = funRepeatMap(res.data)
	excelList.value = []
	if(actTreeNode.value){
		funCurrentChange({current: actTreeNode.value, currentNode: null})
		const child = actTreeNode.value.childs[0]
		const obj = {
			id: child.id,
			interval: child.interval,
			path: child.path,
			prepareid: child.prepareid,
			station: child.station,
			time: child.time,
			type: child.type,
			windturbine: child.windturbine,
			name: child.path.substring(child.path.indexOf(child.station + '_') + (child.station + '_').length)
		}
		funExcelChange(obj)
	}
}
const funCurrentChange = ({ current, currentNode }) => {
	if (current.childs) {
		excelList.value = current.childs.map(o => {
			return {
				id: o.id,
				interval: o.interval,
				path: o.path,
				prepareid: o.prepareid,
				station: o.station,
				time: o.time,
				type: o.type,
				windturbine: o.windturbine,
				name: o.path.substring(o.path.indexOf(o.station + '_') + (o.station + '_').length)
			}
		})
	} else {
		excelList.value = []
	}
}
/**table 开始 */
const tableShowId = ref('')
const tableName = ref('')
const tableColumn = ref([])
const tableLoading = ref(false)
const tableData = ref([])
const funExport = async () => {
	const a = document.createElement('a')
	a.href = baseURL + '/power/prepare/download?id=' + tableShowId.value
	a.download = ''
	a.click()
}
/**submit */
const progress = ref(0)
const funWebSocket = () => {
	const webSocket = new WebSocket(`${socketURL}/ws/powerfitting/admin`)
	webSocket.onerror = () => setTimeout(() => { funWebSocket() }, 2000)

	webSocket.onmessage = (event) => {
		const message = JSON.parse(event.data)
		if (message.code === 200) {
			progress.value = Number(message.data) * 100
			if (progress.value === 100) {
				ElMessage.success('数据加载完成')
				funGetTree()
				progress.value = 0
			}
		}
	}
}
const funSubmit = async (params) => {
	const res = await request.get('/power/prepare/data', { params: params })
	if (res.code === 200) {
		ElMessage.success(res.msg)

	}
}
/**created */
// funGetTree()
funWebSocket()
/**mounted */
onMounted(() => {
	tableHeight.value = window.innerHeight - 260 + 'px'
	excelHeight.value = window.innerHeight - 260 + 'px'
	treeHeight.value = window.innerHeight - 260 + 'px'
	window.addEventListener('resize', () => {
		tableHeight.value = window.innerHeight - 260 + 'px'
		excelHeight.value = window.innerHeight - 260 + 'px'
		treeHeight.value = window.innerHeight - 260 + 'px'
	})
})
/**activated */
onActivated(() => {
	funGetTree()
})
</script>
<template>
	<div class="bg-white py-[10px] px-[10px] relative">
		<search-cop class="mb-[20px] shadow rounded-[6px] shadow-blue-500" @submit="funSubmit">
		</search-cop>
		<div class="relative shadow rounded-[6px] shadow-blue-500 px-[10px] pt-[20px] pb-[10px]">
			<div class="text-[14px] absolute top-[-7px] text-[#838383] left-[20px]">数据展示</div>
			<el-row :gutter="10">
				<el-col :span="5">
					<tree-cop :data="treeData" :height="treeHeight" @currentChange="funCurrentChange" @refresh="funGetTree">
					</tree-cop>
				</el-col>
				<el-col :span="3">
					<excel-cop :data="excelList" :height="excelHeight" @excelChange="funExcelChange"></excel-cop>
				</el-col>
				<el-col :span="16">
					<div>
						<table-cop class="" :data="tableData" :column="tableColumn" :loading="tableLoading"
							:height="tableHeight" :tableId="tableShowId" :tableName="tableName" @export="funExport"></table-cop>
					</div>
				</el-col>
			</el-row>
		</div>

		<el-progress :percentage="progress" v-if="progress" class="!absolute top-0 right-0 left-0" :indeterminate="false"
			color="rgb(19,206,102)" :stroke-width="4" :show-text="false" />
	</div>
</template>