<template>
    <div class="fatherDiv">
        <div class="tabulation">
            <div class="tabulationCente">
                <div class="left">
                    <p>
                        当前选择 ：
                        <span style="color: rgba(100, 195, 164, 1)">{{ label2 }}</span>
                    </p>

                    <el-tree
                        :data="devicesArr"
                        :props="defaultProps"
                        @node-click="handleNodeClick"
                    />
                </div>
                <div class="freckle">
                    <div class="searchDiv">
                        <el-form :inline="true" :model="formInline" class="demo-form-inline">
                            <el-form-item style="width: 300px" label="监测时间:">
                                <el-date-picker
                                    v-model="value1"
                                    type="daterange"
                                    range-separator="➡️"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                />
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="onSubmit">查询</el-button>
                                <el-button plain @click="resetForm">重置</el-button>
                            </el-form-item>
                        </el-form>
                        <div class="modeDiv" @click="SwitchMode">
                            <div :class="SwitchFlag ? 'Selected' : ''">表格模式</div>
                            <div :class="SwitchFlag ? '' : 'Selecteds'">图表模式</div>
                        </div>
                    </div>
                    <div class="chartModeFather">
                        <div class="tableBox" v-show="SwitchFlag">
                            <el-table
                                :data="tableNewData"
                                :header-cell-style="{ 'text-align': 'center' }"
                                :cell-style="{ 'text-align': 'center' }"
                                header-row-class-name="tabth"
                                height="80%"
                                highlight-current-row
                            >
                                <el-table-column prop="dataTime" label="日期" width="180">
                                    <template #default="scope">
                                        <div>{{ handleDate(scope.row.dataTime) }}</div>
                                        <div>{{ handleTime(scope.row.dataTime) }}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="airTemperature" label="空气温度" />
                                <el-table-column prop="airHumidity" label="空气湿度" />
                                <el-table-column prop="windSpeed" label="风速" />
                                <el-table-column prop="windDirection" label="风向" />
                                <el-table-column prop="rainfall" label="雨量" />
                                <el-table-column prop="atmosphericPressure" label="大气压力" />
                                <el-table-column prop="evaporation" label="蒸发" />
                                <el-table-column prop="lightIntensity" label="光照强度" />
                                <el-table-column prop="sunlightHours" label="日照时数（h）" />
                                <el-table-column
                                    prop="photosyntheticEffectiveRadiation"
                                    label="光合有效"
                                />
                            </el-table>
                            <div class="example-pagination-block">
                                共{{ tabulation.length }}条
                                <el-pagination
                                    background
                                    layout="prev, pager, next"
                                    :total="tabulation.length"
                                    :current-page="tabulationcurrentPage"
                                    :page-size="tabulationpageSize"
                                    @current-change="tabulationCurrentChange"
                                />
                            </div>
                        </div>
                        <div v-show="!SwitchFlag" class="chartMode"></div>
                    </div>
                </div>
                <div class="centent">
                    <div class="arrow" @click="foldClick">
                        <p v-if="!flags">
                            <img style="width: 300%" src="@/assets/images/shouhui.png" />
                        </p>
                        <p v-if="flags">
                            <img style="width: 300%" src="@/assets/images/zhankai.png" />
                        </p>
                    </div>
                </div>
                <div v-show="!flags" class="cesiumContainersDiv">
                    <div id="cesiumContainers" class="detailed"></div>
                </div>
            </div>
            <el-dialog
                v-model="dialogVisible"
                append-to-body
                class="video"
                destroy-on-close
                title="实时监控"
                width="30%"
                :before-close="handleClose"
            >
                <div class="myVideo"><VideoJs :videoSrc="data.videoSrc" autoPlay /></div>
            </el-dialog>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, inject, reactive, watch } from 'vue';
import * as echarts from 'echarts';
import { useEcharts } from '@/hooks/useEcharts';
import { getarea, getTownship, getvillage, getareas, dow } from '@/api/crops/classify.js';
import axios from 'axios';
// import { getTreeData, getCameraName } from '../../../api/plough/VideoSurveillance';
import {
    getmeteorological,
    getmeteorology,
    getMeteorological,
} from '@/api/meteorological/monitor/weatherStation.js';
import VideoJs from '@/components/video/video.vue';
import 'leaflet/dist/leaflet.css';
import L from 'leaflet';
import iconShadow from '/img/marker/mark.png';
import moment from 'Moment';

let map = L.Map;
let tabulation = ref([]);
let popdata = ref({});
let devicesArr = ref([]); //树结构数据
let label1 = ref('');
let label2 = ref('全部');
let tableNewData = ref([]);
const dialogVisible = ref(false);
let value1 = ref('');
let formInline = ref({
    yearMonth: '',
    flag: '',
    divisions: '',
});
let flags = ref(false);
let tabulationcurrentPage = ref(1); //列表当前页
let tabulationpageSize = ref(9); //每页条数
let center = null; // 指定中心点
let SwitchFlag = ref(true);
let leftWraFlag = ref(true);
const data = reactive({
    videoSrc:
        'http://cmgw-vpc.lechange.com:8888/LCO/9C023C5FACFE48F/0/1/20230530T033445/a44e6e04b0608347f0209ab2b25aa258.m3u8',
});
onMounted(() => {
    getmeteorologicals();
    initLeafletMap();
    getmeteorologys();
});

const initLeafletMap = () => {
    //加载地图
    map = L.map('cesiumContainers', {
        center: [36.4, 119.166326], // 地图中心
        zoom: 7, //缩放比列
        zoomControl: false, //禁用 + - 按钮
        doubleClickZoom: false, // 禁用双击放大
        attributionControl: false, // 移除右下角leaflet标识
    });
    //   渲染底图
    L.tileLayer(
        'http://t0.tianditu.com/DataServer?T=vec_w&tk=5956e6519f2bb0ae8e57bc834298c9f1&x={x}&y={y}&l={z}'
    ).addTo(map);
    L.tileLayer(
        'http://t0.tianditu.com/DataServer?T=cia_w&tk=5956e6519f2bb0ae8e57bc834298c9f1&x={x}&y={y}&l={z}'
    ).addTo(map);
    map._onResize();
};

/*-----------接口---------------*/
const getmeteorologicals = () => {
    getmeteorological().then(res => {
        console.log(res.data);
        res.data.forEach(item => {
            devicesArr.value.push(item);
        });
    });
};
const getmeteorologys = city => {
    getmeteorology({
        name: city,
        whetherToDownload: false,
        startTime: value1.value[0] ? moment(value1.value[0]).format('YYYY-MM-DD HH:mm:ss') : null,
        endTime: value1.value[1] ? moment(value1.value[1]).format('YYYY-MM-DD HH:mm:ss') : null,
    }).then(res => {
        tabulation.value = res.data;
        tableNewData.value = res.data.slice(
            (tabulationcurrentPage.value - 1) * tabulationpageSize.value,
            (tabulationcurrentPage.value - 1) * tabulationpageSize.value + tabulationpageSize.value
        );
    });
    getMeteorological().then(res => {
        res.data.forEach(item => {
            center = L.latLng(item.msLatitude, item.msLongitude);
            map.setView(center, 17);
            L.marker([item.msLatitude, item.msLongitude], { icon: greenIcon })
                .addTo(map)
                .bindPopup(
                    `<b>${item.msName}</b><br><br>经度:${item.msLongitude}<br><br>纬度:${item.msLatitude}`
                )
                .openPopup();
        });
    });
};
/*------------事件----------------*/
//树结构
const handleNodeClick = (data, node, data1, data2) => {
    label2.value = data.label;
    formInline.value.flag = '';
    let device = '';
    // let oriFatherId = node.parent.parent.data;
    // label1.value = oriFatherId.label;
    // getCameraNames(label2.value);
    console.log(label2.value);
    getMeteorological().then(res => {
        console.log(res.data);
        res.data.forEach(item => {
            if (item.msName == label2.value) {
                center = L.latLng(item.msLatitude, item.msLongitude);
                map.setView(center, 17);
                L.marker([item.msLatitude, item.msLongitude], { icon: greenIcon })
                    .addTo(map)
                    .bindPopup(
                        `<b>${item.msName}</b><br><br>经度:${item.msLongitude}<br><br>纬度:${item.msLatitude}`
                    )
                    .openPopup();
            }
        });
    });
};

//查询
const onSubmit = e => {
    getmeteorologys();
};

//创建ICON构造函数，相当于继承于ICON，可以重新设置属性
var LeafIcon = L.Icon.extend({
    options: {
        // shadowUrl: './icon/leaf-shadow.png', //阴影地址
        iconSize: [50, 50], //图标宽高
        shadowSize: [50, 64], //阴影宽高
        iconAnchor: [22, 94], //图标锚点
        shadowAnchor: [4, 62], //阴影锚点
        popupAnchor: [0, -86], //弹出框弹出位置，相对于图标锚点
    },
});
//创建多个icon
var greenIcon = new LeafIcon({
    iconUrl: iconShadow,
});
//列表模式模式切换
const SwitchMode = () => {
    SwitchFlag.value = !SwitchFlag.value;
    const statInfo = chartModeDiv.value; // 获取图表元素
    statInfo.style.width = document.querySelector('.chartModeFather').offsetWidth + 'px'; //初始化echarts图表宽度

    statInfo.style.height = document.querySelector('.chartModeFather').offsetHeight - 100 + 'px';
    console.log(document.querySelector('.chartModeFather').offsetWidth);
    const myChart = echarts.init(statInfo);
    // 设置宽度自适应
    window.addEventListener('resize', () => {
        statInfo.style.width = document.querySelector('.chartModeFather').offsetWidth + 'px';
        statInfo.style.height =
            document.querySelector('.chartModeFather').offsetHeight - 100 + 'px';
        myChart.resize();
    });
    chartModes();
};

//折叠右侧地图
const foldClick = () => {
    flags.value = !flags.value;
};
//分页
const tabulationCurrentChange = val => {
    tabulationcurrentPage.value = val;
    tableNewData.value = tabulation.value.slice(
        (tabulationcurrentPage.value - 1) * tabulationpageSize.value,
        (tabulationcurrentPage.value - 1) * tabulationpageSize.value + tabulationpageSize.value
    );
};
//时间换行
const handleDate = timestamp => {
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return Y + M + D;
};
const handleTime = timestamp => {
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return h + m + s;
};
//重置
const resetForm = () => {
    value1.value = '';
    formInline.value.flag = '';
    getmeteorologys();
};
</script>

<style lang="scss" scoped>
$height: calc(100vh - 100px);

.fatherDiv {
    width: 100%;
    height: 108%;
}
.tabulation,
.centerBox {
    width: 100%;
    height: 98%;
    .imgrotate {
        transform: rotate(180deg);
    }
    .title {
        width: 100%;
        height: 45px;
        background: url('@/assets/images/title.png');
        background-repeat: no-repeat;
        background-size: 110% 100%;
        color: #fff;
        font-size: 18px;
        font-weight: 700;
        line-height: 45px;
        padding-left: 46px;
        display: flex;
        cursor: pointer;
        align-items: center;
        justify-content: space-between;

        span {
            width: 80%;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
            background: linear-gradient(
                180deg,
                rgba(255, 255, 255, 1) 33.33%,
                rgba(41, 255, 219, 1) 100%
            );
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        p {
            width: 110px;
            height: 25px;
            border-radius: 20px;
            font-size: 14px;
            background: linear-gradient(
                180deg,
                rgba(16, 111, 111, 1) 0%,
                rgba(47, 214, 214, 1) 100%
            );
            border: 1.5px solid rgba(23, 194, 180, 1);
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
}
.demo-tabs {
    // width: 100%;
    // height: 98%;
    :deep(.el-tabs__nav.is-top) {
        transform: translateX(60px) !important;
    }
}
.tabulationCente {
    width: 100%;
    height: 89%;
    display: flex;
    padding: 10px;
    .left {
        width: 15%;
        height: 100%;
        box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.08);
        margin-right: 20px;
        font-size: 14px;
        color: rgba(0, 0, 0, 1);
        padding: 10px 20px;
    }
    .cesiumContainersDiv {
        width: 30%;
        height: 100%;
        background: #ccc;
        box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.08);
        position: relative;
        overflow: hidden;
        .VideoPlayDiv {
            width: 100%;
        }
        .detailed {
            width: 100%;
            height: 100%;
        }
        #pops {
            opacity: 1;
            border-radius: 5px;
            background: rgba(2, 31, 26, 0.6);
            display: none;
            flex-direction: column;
            justify-content: flex-start;
            align-items: flex-end;
            padding: 6px 8px 6px 8px;
            color: rgba(255, 255, 255, 1);
            font-size: 14px;
            .content {
                div {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    p {
                        width: 100px;
                        border-radius: 2px;
                        background: rgba(217, 231, 255, 0.2);
                        display: flex;
                        justify-content: center;
                        align-items: center;
                    }
                }
            }
            .cancel {
                position: absolute;
                right: 10px;
                top: 5px;
                color: rgba(255, 255, 255, 1);
                cursor: pointer;
            }
        }
    }

    .centent {
        width: 20px;
        height: 100%;
        position: relative;
        z-index: 99;
        .arrow {
            width: 22px;
            height: 22px;
            // background: #ccc;
            position: absolute;
            top: 50%;
            transform: translate(-80%, -50%);
            z-index: 99;
            border-radius: 50%;
            cursor: pointer;
        }
    }
    .freckle {
        width: 50%;
        flex: 1;
        // background: #ccc;
        box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.08);
        :deep(.el-input__wrapper) {
            opacity: 1;
            border-radius: 4px;
            background: #fff;
            border: none;
            box-shadow: 0px 3px 9px 0px rgba(0, 0, 0, 0.25);
            width: 20%;
            .el-input__inner {
                color: rgba(170, 170, 170, 1);
            }
        }
        .searchDiv {
            margin: 20px;
            display: flex;
            .modeDiv {
                width: 234px;
                height: 26px;
                opacity: 1;
                border-radius: 8px;
                display: flex;
                justify-content: flex-start;
                align-items: center;
                text-align: center;
                font-size: 14px;
                // font-weight: 400;
                border: 1px solid #ccc;
                cursor: pointer;
                div {
                    flex: 1;
                    height: 100%;
                    border-radius: 8px;
                    line-height: 26px;
                }
                .Selected {
                    background: rgba(100, 195, 164, 1);
                    border-radius: 8px 0px 0px 8px;
                    color: rgba(255, 255, 255, 1);
                }
                .Selecteds {
                    background: rgba(100, 195, 164, 1);
                    border-radius: 0px 8px 8px 0px;
                    color: rgba(255, 255, 255, 1);
                }
            }
        }
        .tableBox {
            :deep(
                    .el-table .el-table__header-wrapper th,
                    .el-table .el-table__fixed-header-wrapper th
                ) {
                color: rgba(0, 0, 0, 1);
            }
            height: 93%;
            :deep(.el-table__inner-wrapper) {
                height: 96% !important;
            }
            .example-pagination-block {
                font-size: 14px;
                display: flex;
                align-items: center;
                justify-content: flex-end;
            }
            :deep(.tabth) {
                background-color: rgba(247, 247, 247, 1);
                .el-table-fixed-column--left {
                    background: rgba(247, 247, 247, 1) !important;
                }
            }
            .insectDiv {
                width: 100%;
                height: 90%;
                overflow: hidden;
                .gutters {
                    width: 100%;
                    height: 100%;
                    overflow-y: scroll;
                    .ins {
                        position: relative;
                        .yj {
                            color: rgba(255, 255, 255, 1);
                            position: absolute;
                            font-size: 14px;
                            border-radius: 0px 30px, 0px, 30px;
                            top: 0;
                            right: 0;
                            text-align: center;
                            line-height: 24px;
                            padding: 1px 5px;
                        }
                    }
                }
            }
        }
    }
    :deep().el-dialog__body {
        width: 300px;
        height: 300px;
    }
    .chartMode {
        :deep(
                .el-table .el-table__header-wrapper th,
                .el-table .el-table__fixed-header-wrapper th
            ) {
            color: rgba(0, 0, 0, 1);
        }
        height: 93%;
        :deep(.el-table__inner-wrapper) {
            height: 96% !important;
        }
        .example-pagination-block {
            font-size: 14px;
            display: flex;
            align-items: center;
            justify-content: flex-end;
        }
        :deep(.tabth) {
            background-color: rgba(247, 247, 247, 1);
        }
        .insectDiv {
            width: 100%;
            height: 90%;
            overflow: hidden;
            .gutters {
                width: 100%;
                height: 100%;
                overflow-y: scroll;
                .ins {
                    position: relative;
                    .yj {
                        color: rgba(255, 255, 255, 1);
                        position: absolute;
                        font-size: 14px;
                        border-radius: 0px 30px, 0px, 30px;
                        top: 0;
                        right: 0;
                        text-align: center;
                        line-height: 24px;
                        padding: 1px 5px;
                    }
                }
            }
        }
        .tableBoxs {
            width: 100%;
            height: 100%;
            overflow: hidden;
            .gutters {
                width: 100%;
                height: 100%;
                overflow-y: scroll;
                .ins {
                    position: relative;
                    .yj {
                        color: rgba(255, 255, 255, 1);
                        position: absolute;
                        font-size: 14px;
                        border-radius: 0px 30px, 0px, 30px;
                        top: 0;
                        right: 0;
                        text-align: center;
                        line-height: 24px;
                        padding: 1px 5px;
                    }
                }
            }
        }
        .chartModeDiv {
            width: 100%;
            height: 50%;
        }
        .depth {
            width: 50%;
            height: 26px;
            opacity: 1;
            border-radius: 4px;
            font-size: 12px;
            color: rgba(102, 102, 102, 1);
            margin-top: 20px;
            cursor: pointer;
            background: linear-gradient(
                180deg,
                rgba(16, 111, 111, 0.1) 0%,
                rgba(47, 214, 214, 0.1) 100%
            );

            border: 1px solid rgba(228, 231, 237, 1);
            display: flex;
            align-items: center;
            p {
                flex: 1;
                text-align: center;
                height: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            .SelectedDiv {
                opacity: 1;
                border-radius: 4px;
                background: rgba(100, 195, 164, 1);
            }
            margin: 0 auto;
        }
    }
}
.myVideo {
    width: 100%;
    height: 100%;
}
</style>
<style lang="scss">
.video {
    .el-dialog__body {
        width: 100%;
        height: 400px;
    }
}
</style>
