<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 CurrentScatterChart from './components/current-scatter-chart.vue'
import request from '@/api/axios.js'
import {baseURL, socketURL} from '@/api/axios.js'
import dayjs from 'dayjs';
/**配置参数 */
const tableHeight = ref(window.innerHeight - 214 + 'px')
const treeHeight = ref(window.innerHeight - 160 + 'px') //tree高度
const excelHeight = ref(window.innerHeight - 160 + 'px') //excel高度
/**excel 开始 */
const excelList = ref([])
const excelCheckIds = ref([])
const excelCheckData = ref([]) //选中后的所有对象数组
const funExcelChange = async (obj) => { //点击excel项时
	return false
}
const funExcelCheckChange = ({ checkArr, data }) => {   //bug 
	if(!checkArr.length){
		excelCheckIds.value = []
		excelCheckData.value = []
	}else{
		excelCheckIds.value = checkArr
		excelCheckData.value = data.filter(o => checkArr.includes(o.id))
	}
	funSubmit() // check 后进行操作
}
/**tree 开始 */
const treeData = ref([])
const actTreeNode = ref(null)
const funRepeatMap = (arr) => {
	return arr.map(o => {
		if (o.children) {
			const findIndex = o.children.findIndex(p => !!p.path)
			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
	excelCheckIds.value = []
	excelCheckData.value = []
	const res = await request.get("/new/photovol/allfilelist")
	treeData.value = funRepeatMap(res.data)
	excelList.value = []
	if(actTreeNode.value){
		funCurrentChange({current: actTreeNode.value, currentNode: null})
	}
}
const funCurrentChange = ({ current, currentNode }) => {
	excelCheckIds.value = []
	excelCheckData.value = []
	if (current.childs) {
		excelList.value = current.childs.map(o => {
			return {
				id: o.path,
				interval: o.interval,
				path: o.path,
				station: o.station,
				time: o.time,
				type: o.type,
				windturbineId: o.windturbineId,
				name: o.path
			}
		})
	} 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()
}
/**tab  */
const activeTab = ref('1')
/**chart Data */
const xAxisData = ref([])
const chartRef = ref() //chart 的ref
const seriesData = ref([])
const dataSet = ref('')
const funChartSelect = async (batch) => {
	return false
}
/**submit */
const funSubmit = async () => {
	if (!excelCheckIds.value.length) {
		ElMessage.error('请勾选要执行的项')
		return false
	}
	const params = {
		filelist: excelCheckIds.value.join()
	}
	tableShowId.value = '1'
	tableName.value = params.filelist
	tableLoading.value = true
	const res = await request.get('/new/photovol/analysis/powertimefile', { params })
	if(res.code !== 200){ 
		tableLoading.value = false
		return false
	}
	tableColumn.value = res.data.title.map(o => {
			return {
				prop: o.key,
				label: o.des,
				width: o.des==='时间'? 100: 80,
			}
		})
		const wtIds = excelCheckData.value.map(o => o.windturbineId)
		const xAxis = []
		const series = []
		if(!res.data.data || !wtIds.length){ tableLoading.value = false; return false}
		if (wtIds && wtIds.length>0) {
			for(const index in wtIds){
				const llgl = []
				const sjgl = []
				// console.log(wtIds[index],res.data.data[wtIds[index]])
				for(const o of res.data.data[wtIds[index]]){
					if(index === '0'){
						xAxis.push(o.datetime)
					}
					tableData.value.push(o)
					llgl.push(o.ideaP)
					sjgl.push(o.actualP)
				}
				series.push(
					{
						name: wtIds[index] + "\n实际功率",
						type: "line",
						symbol: "line", //设定为实心点
						symbolSize: 0, //设定实心点的大小
						smooth: false, //这个是把线变成曲线
						data: sjgl,
						xAxisIndex: 0,
					}
				)
				series.push(
					{
						name: wtIds[index] + "\n理论功率",
						type: "line",
						symbol: "line", //设定为实心点
						symbolSize: 0, //设定实心点的大小
						smooth: false, //这个是把线变成曲线
						data: llgl,
						xAxisIndex: 0,
					}
				)
			}
		}
		xAxisData.value = xAxis
		seriesData.value = series
		tableLoading.value = false
		activeTab.value = '2'
}
/**created */
// funGetTree()
/**mounted */
onMounted(() => {
	tableHeight.value = window.innerHeight - 214 + 'px'
	excelHeight.value = window.innerHeight - 160 + 'px'
	treeHeight.value = window.innerHeight - 160 + 'px'
	window.addEventListener('resize', () => {
		tableHeight.value = window.innerHeight - 214 + 'px'
		excelHeight.value = window.innerHeight - 160 + 'px'
		treeHeight.value = window.innerHeight - 160 + 'px'
	})
})
/**activated */
onActivated(() => {
	funGetTree()
	// funSubmit()
})
</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="4">
					<tree-cop :data="treeData" type="light" :height="treeHeight" @currentChange="funCurrentChange" @refresh="funGetTree">
					</tree-cop>
				</el-col>
				<el-col :span="4">
					<excel-cop :data="excelList" :checkIds="excelCheckIds" :height="excelHeight" showCheckbox @excelChange="funExcelChange" @checkChange="funExcelCheckChange"></excel-cop>
				</el-col>
				<el-col :span="16">
					<div class="px-[10px] shadow rounded-[6px] shadow-blue-500 ">
						<el-tabs v-model="activeTab">
							<el-tab-pane label="表格数据" name="1">
							</el-tab-pane>
							<el-tab-pane label="图表展示" name="2">
							</el-tab-pane>
							<table-cop v-show="activeTab === '1'" :data="tableData" :loading="tableLoading" :column="tableColumn"
								:height="tableHeight" :tableId="tableShowId" :tableName="tableName"></table-cop>
							<div v-show="activeTab === '2'"
								:style="{ height: typeof tableHeight === 'string' ? tableHeight : tableHeight + 'px' }"
								class="p-[10px]">
								<CurrentScatterChart ref="chartRef" width="100%" :height="`calc( ${tableHeight} - 20px )`" :chartTitle="''"
									:xAxisData="xAxisData" :seriesData="seriesData"
									:showLegend="true" :brushSelected="false" :dataSet="dataSet" @getSelected="funChartSelect" />
							</div>
						</el-tabs>
					</div>
				</el-col>
			</el-row>
		</div>
	</div>
</template>