<template>
    <div class="default-main device-manage" :style="{ height: pageHeight.height }" v-loading="loading">
        <DeviceTree @node-click="nodeClick" @init="nodeClick"></DeviceTree>
        <div class="device-manage-right" v-if="deviceData">
            <el-descriptions title="方案信息" class="mb10" :column="4" border>
                <template #extra>
                    <el-button type="primary" size="small" icon="el-icon-Plus" @click="handleOpen(0)">
                        新增方案
                    </el-button>
                    <el-button type="primary" size="small" icon="el-icon-Plus" @click="handleOpen(1)">
                        新增测试项
                    </el-button>
                </template>
                <el-descriptions-item label="方案名称">
                    {{ deviceData[0]?.name }}
                </el-descriptions-item>
                <el-descriptions-item label="方案描述">
                    {{ echoName(deviceData[0]?.devType, devTypeOptions) }}
                </el-descriptions-item>
            </el-descriptions>
            <div class="history_title">
                <p>测试项信息</p>
            </div>
            <el-tabs v-model="activeName" type="border-card">
                <el-tab-pane v-for="(item, index) in deviceData" :label="item.name+(index+1)" :name="index">
                    <el-descriptions  class="mb10" :column="4" border>
                        <el-descriptions-item label="测试项名称">
                            {{ item.name+(index+1) }}
                        </el-descriptions-item>
                        <el-descriptions-item label="PT变比">
                            {{ echoName(item.devType, devTypeOptions) }}
                        </el-descriptions-item>

                        <el-descriptions-item label="CT变比">
                            {{ item.devAccessMethod }}
                        </el-descriptions-item>
                        <el-descriptions-item label="统计间隔">
                            {{ item.ndid }}
                        </el-descriptions-item>
                        <el-descriptions-item label="电压等级">
                            {{ echoName(item.devModel, devModelOptions) }}
                        </el-descriptions-item>
                        <el-descriptions-item label="接线方式">
                            {{ item.time }}
                        </el-descriptions-item>
                        <el-descriptions-item label="起始时间">
                            {{ item.time }}
                        </el-descriptions-item>
                        <el-descriptions-item label="结束时间">
                            {{ item.time }}
                        </el-descriptions-item>
                        <el-descriptions-item label="安装位置">
                            {{ echoName(item.devModel, devModelOptions) }}
                        </el-descriptions-item>
                        <el-descriptions-item label="操作">
                            <el-button type="primary" size="small" icon="el-icon-EditPen" @click="handleOpen(2)">
                                修改
                            </el-button>
                            <el-button type="primary" size="small" icon="el-icon-InfoFilled" @click="handleOpen(3)">
                                设备信息
                            </el-button>
                        </el-descriptions-item>
                    </el-descriptions>
                </el-tab-pane>
            </el-tabs>
            <div class="history_title">
                <p>历史趋势</p>
            </div>
            <div class="history_header">
                <el-form :model="form" class="history_select">
                    <el-form-item label="统计指标">
                        <el-select v-model="form.index" placeholder="请选择统计指标" clearable>
                            <el-option
                                v-for="item in rankOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="值类型">
                        <el-select v-model="form.type" placeholder="请选择值类型" clearable>
                            <el-option
                                v-for="item in rankOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div class="history_searchBtn">
                    <el-button type="primary" size="small" icon="el-icon-Search" @click="handleSearch">查询</el-button>
                    <el-button type="primary" size="small" icon="el-icon-Download" @click="handleExport">
                        报表导出
                    </el-button>
                </div>
            </div>
            <div class="history_chart">
                <MyEchart :options="echartsData" v-if="echartsData" />
            </div>
            <!-- <el-tabs v-model="dataSet" type="border-card" class="device-manage-box-card" @tab-click="handleClick">
                <el-tab-pane
                    lazy
                    :label="item.name"
                    :name="item.id"
                    v-for="(item, index) in deviceData.dataSetList"
                    :key="index"
                ></el-tab-pane>
                <div :style="{ height: tableHeight }" v-loading="tableLoading">
                    <vxe-table v-bind="defaultAttribute" :data="tableData" height="auto" style="width: 100%">
                        <vxe-column type="seq" title="序号" width="80"></vxe-column>
                        <vxe-column field="name" title="数据名称"></vxe-column>
                        <vxe-column field="phasic" title="相别"></vxe-column>
                        <vxe-column field="type" title="数据类型"></vxe-column>
                        <vxe-column field="unit" title="单位"></vxe-column>
                        <vxe-column field="startTimes" title="开始次数"></vxe-column>
                        <vxe-column field="endTimes" title="结束次数"></vxe-column>
                    </vxe-table>
                </div>
            </el-tabs> -->
        </div>
        <el-empty v-else description="请选择设备" class="device-manage-right" />
        <MangePopup ref="mangePopup" />
    </div>
</template>

<script lang="ts" setup>
import MangePopup from './popup.vue'
import DeviceTree from '@/components/tree/govern/deviceTree.vue'
import { mainHeight } from '@/utils/layout'
import { queryByCode, queryByid, queryCsDictTree } from '@/api/system-boot/dictTree'
import { getDeviceData } from '@/api/cs-device-boot/EquipmentDelivery'
// import { getTargetById } from '@/api/cs-device-boot/csDatalistay'
import { getGroup } from '@/api/cs-device-boot/csGroup'
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { defaultAttribute } from '@/components/table/defaultAttribute'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { getDeviceDataTrend } from '@/api/cs-harmonic-boot/datatrend'
defineOptions({
    name: 'govern/device/manage'
})
const pageHeight = mainHeight(20)
const loading = ref(true)
const tableLoading = ref(false)
const getGroupLoading = ref(false)
const deviceData = ref<any>([])
const dataSet = ref('')
const devTypeOptions = ref([])
const devModelOptions = ref([])
const tableData = ref([])
const tableHeight = mainHeight(235).height
const form = ref({})
form.value = {
    index: '',
    type: ''
}
const rankOptions = ref([
    {
        value: '1',
        label: '1级'
    },
    {
        value: '2',
        label: '2级'
    },
    {
        value: '3',
        label: '3级'
    }
])
const activeName: any = ref()
const nodeClick = (e: anyObj) => {
    if (!e) {
        loading.value = false
        return
    }
    if (e.level == 2) {
        loading.value = true
        deviceData.value=[]
        getDeviceData(e.id, 'rt').then((res: any) => {
            // deviceData.value = res.data
            loading.value = false
            for (let i = 0; i < 5; i++) {
                deviceData.value.push(res.data)
            }
            console.log(deviceData.value, '0000000')
            activeName.value=0
            if (res.data.dataSetList.length === 0) {
                dataSet.value = ''
                tableData.value = []
            } else {
                dataSet.value = res.data.dataSetList[0].id
                handleClick()
            }
        })
    }
}
const handleClick = () => {
    // tableLoading.value = true
    // tableData.value = []
    // setTimeout(() => {
    //     getTargetById(dataSet.value).then(res => {
    //         tableData.value = res.data
    //         tableLoading.value = false
    //     })
    // }, 100)
}
queryByCode('Device_Type').then(res => {
    queryCsDictTree(res.data.id).then(res => {
        devTypeOptions.value = res.data.map((item: any) => {
            return {
                value: item.id,
                label: item.name,
                ...item
            }
        })
    })
    queryByid(res.data.id).then(res => {
        devModelOptions.value = res.data.map((item: any) => {
            return {
                value: item.id,
                label: item.name,
                ...item
            }
        })
    })
})
const echoName = (value: any, list: any[]) => {
    return list.find(item => item.value == value)?.label
}

const mangePopup = ref()
const handleOpen = (val: any) => {
    mangePopup.value.open(val)
}

const echartsData = ref<any>(null)
//加载echarts图表
const init = () => {
    echartsData.value = null
    loading.value = true
    // prettier-ignore
    const data = [["2000-06-05", 116], ["2000-06-06", 129], ["2000-06-07", 135], ["2000-06-08", 86], ["2000-06-09", 73], ["2000-06-10", 85], ["2000-06-11", 73], ["2000-06-12", 68], ["2000-06-13", 92], ["2000-06-14", 130], ["2000-06-15", 245], ["2000-06-16", 139], ["2000-06-17", 115], ["2000-06-18", 111], ["2000-06-19", 309], ["2000-06-20", 206], ["2000-06-21", 137], ["2000-06-22", 128], ["2000-06-23", 85], ["2000-06-24", 94], ["2000-06-25", 71], ["2000-06-26", 106], ["2000-06-27", 84], ["2000-06-28", 93], ["2000-06-29", 85], ["2000-06-30", 73], ["2000-07-01", 83], ["2000-07-02", 125], ["2000-07-03", 107], ["2000-07-04", 82], ["2000-07-05", 44], ["2000-07-06", 72], ["2000-07-07", 106], ["2000-07-08", 107], ["2000-07-09", 66], ["2000-07-10", 91], ["2000-07-11", 92], ["2000-07-12", 113], ["2000-07-13", 107], ["2000-07-14", 131], ["2000-07-15", 111], ["2000-07-16", 64], ["2000-07-17", 69], ["2000-07-18", 88], ["2000-07-19", 77], ["2000-07-20", 83], ["2000-07-21", 111], ["2000-07-22", 57], ["2000-07-23", 55], ["2000-07-24", 60]];
    const dateList = data.map(function (item) {
        return item[0]
    })
    const valueList = data.map(function (item) {
        return item[1]
    })
    const iconThree =
        'path://M512 85.333333c235.637333 0 426.666667 191.029333 426.666667 426.666667S747.637333 938.666667 512 938.666667 85.333333 747.637333 85.333333 512 276.362667 85.333333 512 85.333333z m214.592 318.677334a32 32 0 0 0-45.248 0.064L544.736 541.066667l-81.792-89.109334a32 32 0 0 0-46.613333-0.576l-119.36 123.733334a32 32 0 1 0 46.058666 44.437333l95.754667-99.264 81.418667 88.704a32 32 0 0 0 46.24 0.96l160.213333-160.693333a32 32 0 0 0-0.064-45.248z'
    const iconDanger =
        'path://M1001.661867 796.544c48.896 84.906667 7.68 157.013333-87.552 157.013333H110.781867c-97.834667 0-139.050667-69.504-90.112-157.013333l401.664-666.88c48.896-87.552 128.725333-87.552 177.664 0l401.664 666.88zM479.165867 296.533333v341.333334a32 32 0 1 0 64 0v-341.333334a32 32 0 1 0-64 0z m0 469.333334v42.666666a32 32 0 1 0 64 0v-42.666666a32 32 0 1 0-64 0z'
    echartsData.value = {
        options: {
            title: [
                {
                    left: 'center',
                    text: '10KV 母线_监测点 相电压有效值'
                }
            ],
            // backgroundColor: '#1B232E',
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
                // textStyle: {
                //     color: 'rgba(255, 255, 255, 1)'
                // },
                // backgroundColor: 'rgba(0, 0, 0, 0.8)',
                // borderColor: 'rgba(219, 230, 255, 0.8)'
            },
            legend: {
                //legend使用iconfont图标
                data: [
                    {
                        name: 'A相',
                        icon: iconThree
                    },
                    {
                        name: 'B相',
                        icon: iconThree
                    },
                    {
                        name: 'C相',
                        icon: iconThree
                    },
                    {
                        name: '暂降',
                        icon: iconDanger
                    }
                ],
                x: 'right',
                textStyle: {
                    // color: 'white',
                    fontSize: 14
                },
                top: 10,
                right: 50,
                itemWidth: 20,
                itemHeight: 10,
                itemGap: 15
            },
            grid: {
                left: '1%',
                right: '5%',
                bottom: '2%',
                top: '10%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    name: '时间',
                    axisLabel: {
                        color: '#A9AEB2',
                        fontSize: 12
                    },
                    data: ['01:12', '02:12', '03:12', '04:12', '05:12', '06:12', '07:12', '08:12', '09:12', '10:12'],
                    axisLine: {
                        lineStyle: {
                            color: '#43485E'
                        }
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: 'KV',
                    axisLabel: {
                        color: '#A9AEB2',
                        fontSize: 12
                    },
                    axisTick: {
                        show: true
                    },
                    axisLine: {
                        show: true,
                        lineStyle: {
                            // color: 'white'
                        }
                    },
                    nameTextStyle: {
                        color: '#A9AEB2',
                        fontSize: '12'
                    },
                    splitLine: {
                        show: false,
                        lineStyle: {
                            color: ['#43485E'],
                            width: 1,
                            type: 'solid'
                        }
                    }
                }
            ],
            series: [
                {
                    name: 'A相',
                    type: 'line',
                    smooth: true,
                    emphasis: {
                        focus: 'series'
                    },
                    itemStyle: {
                        normal: {
                            color: '#DAA521',
                            lineStyle: {
                                color: '#DAA521'
                            }
                        }
                    },
                    data: [80, 90, 70, 60, 50, 40, 30, 50, 60, 70]
                },
                {
                    name: 'B相',
                    type: 'line',
                    smooth: true,
                    emphasis: {
                        focus: 'series'
                    },
                    itemStyle: {
                        normal: {
                            color: '#2E8B58',
                            lineStyle: {
                                color: '#2E8B58'
                            }
                        }
                    },
                    data: [70, 55, 36, 55, 42, 30, 25, 24, 23, 21]
                },
                {
                    name: 'C相',
                    type: 'line',
                    smooth: true,
                    emphasis: {
                        focus: 'series'
                    },
                    itemStyle: {
                        normal: {
                            color: '#A5292A',
                            lineStyle: {
                                color: '#A5292A'
                            }
                        }
                    },
                    data: [45, 53, 32, 29, 45, 36, 39, 34, 32, 31]
                },
                {
                    name: '暂降',
                    type: 'line',
                    smooth: true,
                    emphasis: {
                        focus: 'series'
                    },
                    itemStyle: {
                        normal: {
                            color: '#d81e06',
                            lineStyle: {
                                color: '#d81e06'
                            }
                        }
                    },
                    data: []
                }
            ]
        }
    }
}
//搜素
const handleSearch = () => {
    console.log(form.value, '搜索')
}
//导出
const handleExport = () => {
    console.log(form.value, '导出')
}

onMounted(() => {
    init()
})
</script>

<style lang="scss" scoped>
.device-manage {
    display: flex;
    height: calc(100vh - 100px);
    // overflow-y: auto;
    &-right {
        // overflow: auto;
        flex: 1;
        padding: 10px 10px 10px 0;
        .el-descriptions__header {
            height: 36px;
            margin-bottom: 7px;
            display: flex;
            align-items: center;
        }
    }
    .device-manage-right {
        overflow: hidden;
        flex: 1 !important;
        padding: 10px 10px 10px 10px;
        border: 2px solid #eeeeee;
        .el-descriptions__header {
            height: 36px;
            margin-bottom: 7px;
            display: flex;
            align-items: center;
        }
    }
}
.history_title {
    width: 100%;
    p {
        height: 32px;
        line-height: 32px;
        font-size: 16px;
        font-weight: bold;
    }
}
.history_header {
    display: flex;
    .history_select {
        width: 65%;
        display: flex;
        justify-content: flex-start;
        .el-select {
            margin-right: 10px;
        }
    }
    .history_searchBtn {
        flex: 1;
        display: flex;
        justify-content: flex-end;
    }
}
.history_chart {
    width: 100%;
    height: calc(100vh - 530px);
    margin-top: 10px;
}
::v-deep .el-select{
    width:200px !important;
}
</style>
