@{
    ViewData["MainNum"] = "3";
    ViewData["SubNum"] = "2";
    ViewData["Title"] = "電站交叉分析";
}

<div class="d-flex justify-content-center text-secondary" style="top: 10%; transform: translateY(50%); z-index: 100; position: relative;">
    <div class="spinner-border" role="status" style="width: 3rem; height: 3rem; display:none;" id="loading"></div>
</div>

<div class="container-fluid">
    <div class="row flex-nowrap wrapper">
        <div class="col-auto pr-0 collapse border-right sidebar vh-100  overflow-auto">
            <div class="list-group border-0 card text-center text-md-left" id="sidebar" style="width:300px">

                <div class="border bg-light rounded-top">
                    <div class="form-group p-2 m-0 rounded-top">
                        <input type="text" class="form-control form-control-lg shadow-inset-2 m-0" id="js_list_accordion_filter" placeholder="">
                    </div>
                    <div id="js_list_accordion" class="accordion accordion-hover accordion-clean js-list-filter">
                    </div>
                    <span class="filter-message js-filter-message"></span>
                </div>
            </div>
        </div>
        <div class="col-auto px-0">
            <a href="javascript:;" id="collapse" data-target=".sidebar" data-toggle="collapse" class="btn btn-default btn-xs btn-icon waves-effect waves-themed" style="border-radius: 0;" aria-expanded="true">
                <span class="collapsed-reveal"><i class="fal fa-angle-left fa-lg py-3"></i></span>
                <span class="collapsed-hidden"><i class="fal fa-angle-right fa-lg py-3"></i></span>
            </a>
        </div>

        <main class="col px-5 pl-md-2 main">
            <ol class="breadcrumb page-breadcrumb">
                <li class="breadcrumb-item"><a href="javascript:void(0);">交叉分析</a></li>
                <li class="breadcrumb-item active">@ViewData["Title"]</li>
                <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
            </ol>
            <div class="subheader">
                <h1 class="subheader-title">
                    <i class="subheader-icon fal fa-crosshairs"></i> @ViewData["Title"]
                </h1>
            </div>

            <div class="row">
                <div class="col-xl-12">
                    <div id="panel-5" class="panel">
                        <div class="panel-container show">
                            <div class="panel-content">
                                <div class="row mb-5 d-flex align-items-top px-3">
                                    <div class="pr-3">
                                        <div class="btn-group btn-group-md">
                                            <button type="button" class="btn btn-success waves-effect waves-themed btn-change-searchType" id="Group0" onclick="changeType(0,this)">日</button>
                                            <button type="button" class="btn btn-secondary waves-effect waves-themed btn-change-searchType" id="Group2" onclick="changeType(2,this)">月</button>
                                            <button type="button" class="btn btn-secondary waves-effect waves-themed btn-change-searchType" id="Group3" onclick="changeType(3,this)">年</button>
                                            <button type="button" class="btn btn-secondary waves-effect waves-themed btn-change-searchType" id="Group1" onclick="changeType(1,this)">自訂區間</button>
                                        </div>
                                    </div>
                                    <div class="pr-3" id="quickSearchOption">
                                        <button type="button" class="btn btn-secondary waves-effect waves-themed btn-change-quickSearch1" onclick="quickSearch(0)">今天</button>
                                        <button type="button" class="btn btn-secondary waves-effect waves-themed btn-change-quickSearch2" onclick="quickSearch(1)">昨天</button>
                                    </div>

                                    <div class="pr-3">
                                        <div class="btn-group" id="js-demo-nesting" role="group" aria-label="Button group with nested dropdown">
                                            <input type="date" class="form-control" id="DateGet" />
                                        </div>
                                        <div class="btn-group" id="DateGettextdiv" role="group" aria-label="Button group with nested dropdown">
                                            <input type="text" class="form-control" id="DateGettext" />
                                        </div>
                                    </div>
                                    <div class="pr-3">
                                        <button type="button" class="btn btn-secondary waves-effect waves-themed" id="btn-analysis-stationinfo" onclick="GetAnalysisStationInfo()">查詢</button>
                                        @*<button type="button" class="btn btn-secondary waves-effect waves-themed" onclick="DataBackFill()">回填</button>*@
                                    </div>
                                </div>

                                <div class="mb-5">
                                    <div class="card p-3 w-100">
                                        <div class="row mb-5 d-flex justify-content-end">
                                            <div class="pr-3">
                                                <div class="btn-group" id="js-demo-nesting" role="group" aria-label="Button group with nested dropdown">
                                                    <div class="btn-group" role="group">
                                                        <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" id="chooseToCompareBtn">選擇比較欄位</button>
                                                        <ul class="dropdown-menu" id="compare-dropdown-menu" style="width:10vw;overflow-x:hidden; max-height:50vh">
                                                        </ul>
                                                    </div>
                                                </div>
                                            </div>
                                            @*<div class="pr-3">
                                                <div class="btn-group" id="js-demo-nesting" role="group" aria-label="Button group with nested dropdown">
                                                    <div class="btn-group" role="group">
                                                        <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown">下載　</button>
                                                        <div class="dropdown-menu">
                                                            <a class="dropdown-item" href="javascript:void(0)">2021 四月</a>
                                                            <a class="dropdown-item" href="javascript:void(0)">2021 三月</a>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="pr-3">
                                                <button type="button" class="btn btn-secondary waves-effect waves-themed"><i class="fal fa-expand fs-md"></i></button>
                                            </div>*@

                                        </div>
                                        <figure class="highcharts-figure">
                                            <div id="container"></div>

                                        </figure>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

@section Scripts{
    <script>
        var searchType = 0;//搜尋條件(日,日區間,月,年)
        var datepicker;
        var timerange;//選取時間
        var selected_device = [];
        var PWS_compare_col = [
            { key: "KWH", title: "發電量", default: true },
            @*{ key: "Irradiance", title: "日照度", default: false },*@
            { key: "KWHKWP", title: "日均發電度數", default: false },
            { key: "PR", title: "PR %", default: false },
            @*{ key: "Temperature", title: "模組溫度", default: false },*@
            { key: "SolarHour", title: "日照小時", default: false },
        ]
        var PWR_compare_col = [
            { key: "OUTPUT_KWH", title: "輸出發電量(kWh)", default: true },
            { key: "Irradiance", title: "輸入發電量(kWh)", default: true },
            { key: "V_AB", title: "電壓 AB(V)", default: false },
            { key: "V_BC", title: "電壓 BC(V)", default: false },
            { key: "V_CA", title: "電壓 CA(V)", default: false },
            { key: "I_A", title: "電流 A(A)", default: false },
            { key: "I_B", title: "電流 B(A)", default: false },
            { key: "I_C", title: "電流 C(A)", default: false },
            { key: "P", title: "有效功率(kW)", default: false },
            { key: "F", title: "頻率(Hz)", default: false }
        ]
        var PYR_compare_col = [{ key: "Irradiance", title: "日照度", default: true }];
        var TPY_compare_col = [{ key: "IrrDay", title: "累積日照度", default: true }];
        var ETR_compare_col = [{ key: "EnvTemperature", title: "環境溫度計", default: true }];
        var MTR_compare_col = [{ key: "Temperature", title: "模組溫度", default: true }];
        var EMM_compare_col = [{ key: "Humidity", title: "濕度", default: true }];
        var VAN_compare_col = [{ key: "Vane", title: "風速", default: true }];
        var DST_compare_col = [{ key: "Dust", title: "落塵%", default: true }];
        var WIN_compare_col = [{ key: "Win", title: "風向", default: true }];
        var current_compare_col = [];
        var default_compare_col = [];
        var selected_YAxis = [];
        var analysisStationInfo;
        var chart;
        //Dictionary<string, string > sensor_type_detail_dictionary { get; set; };
        var sensor_type_detail_dictionary;

        function showLoading() {
            var i = document.getElementById("loading");
            i.style.display = "block";
        }

        function hideLoading() {
            var i = document.getElementById("loading");
            i.style.display = "none";
        }

        $(function () {
            $('#collapse').trigger("click");

            //#region 預設初始值
            $('#DateGet').val(new Date().toISOString().substring(0, 10));
            document.getElementById("DateGettextdiv").style.display = "none";//隱藏
            $('#DateGet').attr('style', 'width:205px');
            $('#DateGettext').attr('style', 'width:205px');
            timerange = $('#DateGet').val();
            //#endregion

            //#region 載入左邊選單列表
            GetPowerStationCollapse("");
            //#endregion
        })

        @*function myfunc(div) {
            var className = div.getAttribute("class");
            if (className == "fal fa-angle-left fa-lg py-3") {
                div.className = "fal fa-angle-right fa-lg py-3";
            }
            else {
                div.className = "fal fa-angle-left fa-lg py-3";
            }
        }*@

        //#region 更換搜尋條件(日,日區間,月,年)
        function changeType(type, e) {
            searchType = type;
            if ($(".btn-change-searchType").hasClass("btn-success")) {
                $(".btn-change-searchType").removeClass("btn-success").addClass("btn-secondary");
            }
            document.getElementById("DateGettextdiv").style.display = "none";//隱藏
            document.getElementById("DateGet").style.display = "";//隱藏
            $(e).removeClass("btn-secondary").addClass("btn-success");
            switch (type) {
                case 0: $('#DateGet').prop({ 'type': 'date' });
                    $(".btn-change-quickSearch1").html("今天");
                    $(".btn-change-quickSearch2").html("昨天");
                    var today = new Date().toISOString().substring(0, 10);
                    $('#DateGet').val(today);
                    break;
                case 1:

                    //#region Date-Picker
                    datepicker = $('#DateGettext').daterangepicker({
                        autoUpdateInput: false,
                        locale: { format: 'YYYY/MM/DD' },
                        opens: 'left'
                    });

                    $('#DateGettext').on('apply.daterangepicker', function (ev, picker) {
                        $(this).val(picker.startDate.format('YYYY/MM/DD') + ' - ' + picker.endDate.format('YYYY/MM/DD'));
                        $(this).trigger('change');
                    });

                    $('#DateGettext').on('cancel.daterangepicker', function (ev, picker) {
                        $(this).val('');
                        $(this).trigger('change');
                    });
                    //#endregion

                    $(".btn-change-quickSearch1").html("近7天");
                    $(".btn-change-quickSearch2").html("近30天");
                    //#region 預設近7天
                    var today = new Date();
                    var dateLimit = new Date(new Date().setDate(today.getDate() - 7));
                    var today_format = today.toISOString().slice(0, 10).replace(/-/g, "/");
                    var dateLimit_format = dateLimit.toISOString().slice(0, 10).replace(/-/g, "/");
                    datepicker.data('daterangepicker').setStartDate(dateLimit_format);
                    datepicker.data('daterangepicker').setEndDate(today_format);
                    document.getElementById("DateGettextdiv").style.display = "";//隱藏
                    document.getElementById("DateGet").style.display = "none";//隱藏
                    //#endregion
                    $('#DateGettext').val(dateLimit_format + ' - ' + today_format);

                    break;
                case 2: $('#DateGet').prop({ 'type': 'month' });
                    $(".btn-change-quickSearch1").html("本月");
                    $(".btn-change-quickSearch2").html("上個月");
                    var now_month = new Date().toISOString().substring(0, 7);
                    $('#DateGet').val(now_month);
                    break;
                case 3:
                    $(".btn-change-quickSearch1").html("今年");
                    $(".btn-change-quickSearch2").html("去年");
                    var now_year = new Date().toISOString().substring(0, 4);
                    $('#DateGet').prop({ 'type': 'number', 'min': 1900, 'max': now_year, 'step': 1 });
                    $('#DateGet').val(now_year);
                    break;
            }
            if (type == 1) {
                timerange = $('#DateGettext').val();
            }
            else {
                timerange = $('#DateGet').val();
            }
        }
        //#endregion

        //#region 快速填入條件(EX.今昨天)
        function quickSearch(day) {
            switch (searchType) {
                case 0:
                    if (day == 0) {
                        var today = new Date().toISOString().substring(0, 10);
                        $('#DateGet').val(today).trigger('change');
                    } else {
                        var dateLimit = new Date(new Date().setDate(new Date().getDate() - 1)).toISOString().substring(0, 10);
                        $('#DateGet').val(dateLimit).trigger('change');
                    }
                    break;
                case 1:
                    if (day == 0) {
                        //#region 預設近7天
                        var today = new Date();
                        var dateLimit = new Date(new Date().setDate(today.getDate() - 7));
                        var today_format = today.toISOString().slice(0, 10).replace(/-/g, "/");
                        var dateLimit_format = dateLimit.toISOString().slice(0, 10).replace(/-/g, "/");
                        datepicker.data('daterangepicker').setStartDate(dateLimit_format);
                        datepicker.data('daterangepicker').setEndDate(today_format);
                        //#endregion
                        $('#DateGettext').val(dateLimit_format + ' - ' + today_format).trigger('change');
                    } else {
                        //#region 預設近30天
                        var today = new Date();
                        var dateLimit = new Date(new Date().setDate(today.getDate() - 30));
                        var today_format = today.toISOString().slice(0, 10).replace(/-/g, "/");
                        var dateLimit_format = dateLimit.toISOString().slice(0, 10).replace(/-/g, "/");
                        datepicker.data('daterangepicker').setStartDate(dateLimit_format);
                        datepicker.data('daterangepicker').setEndDate(today_format);
                        //#endregion
                        $('#DateGettext').val(dateLimit_format + ' - ' + today_format).trigger('change');
                    }
                    break;
                case 2:
                    if (day == 0) {
                        var now_month = new Date().toISOString().substring(0, 7);
                        $('#DateGet').val(now_month).trigger('change');
                    } else {
                        var dateLimit = new Date(new Date().setMonth(new Date().getMonth() - 1)).toISOString().substring(0, 7);
                        $('#DateGet').val(dateLimit).trigger('change');
                    }
                    break;
                case 3:
                    if (day == 0) {
                        var now_year = new Date().toISOString().substring(0, 4);
                        $('#DateGet').val(now_year).trigger('change');
                    } else {
                        var dateLimit = new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().substring(0, 4);
                        $('#DateGet').val(dateLimit).trigger('change');
                    }
                    break;
            }
        }
        //#endregion

        //#region 更換input
        $('#DateGet').on('change', function () {
            timerange = $('#DateGet').val();
        });
        //#endregion

        //#region 更換inputtext
        $('#DateGettext').on('change', function () {
            timerange = $('#DateGettext').val();
        });
        //#endregion

        $("#js_list_accordion_filter").change(function (e) {
            GetPowerStationCollapse($(this).val());
        });

        $('#js_list_accordion').on("change", 'input[name="selectedDeviceId[]"]', function (event) {
            if (this.checked) {
                if (!selected_device.some(x => x.deviceId == this.value)) {
                    var type = $(this).attr("data-type");

                    var temp_obj = {
                        powerStationId: $(this).attr("data-power-station-id"),
                        deviceType: type,
                        deviceId: this.value,
                        sensorTypeId: $(this).attr("data-sensor-type-id"),
                        sensorTypeDetailId: $(this).attr("data-sensor-type-detail-id")
                    }

                    selected_device.push(temp_obj);
                    GetItemInfo(temp_obj.sensorTypeDetailId);
                    switch (type) {
                        case 'PWS':
                            PushSpecifyCurrentCompareCol(PWS_compare_col);
                            break;
                        case 'PWR': //電錶
                            PushSpecifyCurrentCompareCol(PWR_compare_col);
                            break;
                    //    case 'PYR': //日照計
                    //        PushSpecifyCurrentCompareCol(PYR_compare_col);
                    //        break;
                    //    case 'TPY': //累積日照
                    //        PushSpecifyCurrentCompareCol(TPY_compare_col);
                    //        break;
                    //    case 'ETR': //環境溫度計
                    //        PushSpecifyCurrentCompareCol(ETR_compare_col);
                    //        break;
                    //    case 'MTR': //模組溫度計
                    //        PushSpecifyCurrentCompareCol(MTR_compare_col);
                    //        break;
                    //    case 'VAN': //風速計
                    //        PushSpecifyCurrentCompareCol(VAN_compare_col);
                    //        break;
                    //    case 'DST': //落塵計  FOM
                    //        PushSpecifyCurrentCompareCol(DST_compare_col);
                    //        break;
                    //    case 'EMM': //環境濕度計
                    //        PushSpecifyCurrentCompareCol(EMM_compare_col);
                    //        break;
                    //    case 'WIN': //風向計
                    //        PushSpecifyCurrentCompareCol(WIN_compare_col);
                    //        break;
                    }
                }
            } else {
                if (selected_device.some(x => x.deviceId == this.value)) {

                    var cancel_device = this.value;

                    var temp_index;
                    selected_device.find(function (obj, index) {
                        if (obj.deviceId == cancel_device) {
                            temp_index = index;
                        }
                    });

                    selected_device.splice(temp_index, 1);

                    var type = $(this).attr("data-type");

                    switch (type) {
                        case 'PWS':
                            RemoveSpecifyCurrentCompareCol(PWS_compare_col);
                            break;
                        case 'PWR': //電錶
                            RemoveSpecifyCurrentCompareCol(PWR_compare_col);
                            break;
                        case 'PYR': //日照計
                            RemoveSpecifyCurrentCompareCol(PYR_compare_col);
                            break;
                        case 'TPY': //累積日照
                            RemoveSpecifyCurrentCompareCol(TPY_compare_col);
                            break;
                        case 'ETR': //環境溫度計
                            RemoveSpecifyCurrentCompareCol(ETR_compare_col);
                            break;
                        case 'MTR': //模組溫度計
                            RemoveSpecifyCurrentCompareCol(MTR_compare_col);
                            break;
                        case 'VAN': //風速計
                            RemoveSpecifyCurrentCompareCol(VAN_compare_col);
                            break;
                        case 'DST': //落塵計  FOM
                            RemoveSpecifyCurrentCompareCol(DST_compare_col);
                            break;
                        case 'EMM': //環境濕度計
                            RemoveSpecifyCurrentCompareCol(EMM_compare_col);
                            break;
                        case 'WIN': //風向計
                            RemoveSpecifyCurrentCompareCol(WIN_compare_col);
                            break;
                    }

                    current_compare_col = current_compare_col.filter(x => x.count > 0);
                }
            }

            ChangeCompareSelectOption();
        });

        $('#js_list_accordion').on("change", 'input[name="selectedDeviceLayer2[]"]', function (event) {
            if (this.checked) {
                $(this).parents(".selected_group").find('input[name="selectedDeviceId[]"]').prop("checked", true).trigger("change");
            } else {
                $(this).parents(".selected_group").find('input[name="selectedDeviceId[]"]').prop("checked", false).trigger("change");
            }
        });

        $('#compare-dropdown-menu').on('change', 'input[name="compare_col[]"]', function (e) {
            if (this.checked) {
                if ($.inArray($(this).val(), default_compare_col) == -1) {
                    default_compare_col.push($(this).val());
                }
            } else {
                if ($.inArray($(this).val(), default_compare_col) > -1) {
                    default_compare_col.splice($.inArray($(this).val(), default_compare_col), 1);
                }
            }

            ReloadHighCharts();
        });

        function GetPowerStationCollapse(filter) {
            showLoading();
            var url = "/AnalysisStationInfo/GetDeviceCollapse"

            var send_data = {
                Filter: filter
            }

            $.post(url, send_data, function (rel) {
                if (rel.code != "0000") {
                    hideLoading();
                    toast_error(rel.msg);
                    return;
                }

                var deviceCollapse = rel.data;

                $('#js_list_accordion').empty();

                if (deviceCollapse.length <= 0) {
                    $('#js_list_accordion').append("<div>查無結果</div>");
                }

                var str = "";

                Object.keys(deviceCollapse).map(function (key, index1) {
                    str += '<div class="card border-top-left-radius-0 border-top-right-radius-0" id="templateCard">' +
                        '<div class="card-header">' +
                        '<a href="javascript:void(0);" class="card-title collapsed" data-toggle="collapse" data-target="#js_list_accordion-' + index1 + '" aria-expanded="false" data-filter-tags="settings">' +
                        '<i class="fal fa-globe width-2 fs-xl"></i>' +
                        '<span class="city-name">' + key + '</span>' +
                        '<span class="ml-auto">' +
                        '<span class="collapsed-reveal"><i class="fal fa-chevron-up fs-xl"></i></span>' +
                        '<span class="collapsed-hidden"><i class="fal fa-chevron-down fs-xl"></i></span>' +
                        '</span>' +
                        '</a>' +
                        '</div>' +
                        '<div id="js_list_accordion-' + index1 + '" class="collapse" data-parent="#js_list_accordion" style="">';
                    Object.keys(deviceCollapse[key]).map(function (powerStationkey, index2) {
                        str += '<div id="cp-' + index1 + '-' + index2 + '" class="card py-3 selected_group">' +
                            '<div class="card-header pl-4 pr-3 d-flex justify-content-start">' +
                            '<div class="mr-3">' +
                            '<input type="checkbox" class="" name="selectedDeviceLayer2[]" >' +
                            '</div>' +
                            '<a href="javascript:;" class="" data-toggle="collapse" data-target="#cp-' + index1 + '-' + index2 + ' > .card-body" aria-expanded="true">' +
                            '<span class="collapsed-hidden"><h5 class="font-weight-bold mb-0" style="overflow: hidden;display: -webkit-box;text-overflow: ellipsis;-webkit-line-clamp: 3; -webkit-box-orient: vertical;white-space: normal;">' + powerStationkey + '<i class="fal fa-chevron-down fs-xl ml-2"></i></h5></span>' +
                            '<span class="collapsed-reveal"><h5 class="font-weight-bold mb-0" style="overflow: hidden;display: -webkit-box;text-overflow: ellipsis;-webkit-line-clamp: 3; -webkit-box-orient: vertical;white-space: normal;">' + powerStationkey + '<i class="fal fa-chevron-up fs-xl ml-2"></i></h5></span>' +
                            '</a>' +
                            '</div>' +
                            '<div class="card-body p-0 collapse show">' +
                            '<div class="p-0">' +
                            '<ul class="list-group list-group-flush">';
                        $.each(deviceCollapse[key][powerStationkey], function (index, device) {
                            var device_icon = "";
                            @*switch (device.deviceType) {
                                case "PWS": //電站總覽
                                    device_icon = '<i class="fal fa-charging-station"></i>';
                                    break;
                                case 'PWR': //電錶
                                    device_icon = '<i class="fal fa-tachometer-alt-slow"></i>';
                                    break;
                                case 'PYR': //日照計
                                    device_icon = '<i class="fal fa-sun"></i>';
                                    break;
                                case 'ETR': //環境溫度計
                                    device_icon = '<i class="fal fa-thermometer-half"></i>';
                                    break;
                                case 'MTR': //模組溫度計
                                    device_icon = '<i class="fal fa-thermometer-half"></i>';
                                    break;
                                case 'VAN': //風速計
                                    device_icon = '<i class="fal fa-wind"></i>';
                                    break;
                                case 'FOM': //落塵計
                                    device_icon = '<i class="fal fa-sun-dust"></i>';
                                    break;
                                case 'EMM': //環境濕度計
                                    device_icon = '<i class="fal fa-tint"></i>';
                                    break;
                            }*@

                            switch (device.sensorTypeDetailId) {

                            }





                            str += '<li class="list-group-item pr-0 d-flex justify-content-start">' +
                                '<div class="mr-3">' +
                                '<input type="checkbox" class="" name="selectedDeviceId[]" data-power-station-id="' + device.powerStationId + '" data-type="' + device.deviceType + '" value="' + device.deviceId + '" data-sensor-type-id="' + device.sensorTypeId + '" data-sensor-type-detail-id="' + device.sensorTypeDetailId + '">' +
                                '</div>' +
                                '<a href="javascript:void(0)">' + device_icon + device.deviceName + '</a>' +
                                '</li>';
                        });
                        str += '</ul>';
                        str += '</div>';
                        str += '</div>';
                        str += '</div>';
                    });
                    str += '</div>';
                    str += '</div>';
                });

                $('#js_list_accordion').append(str);
                $('#js_list_accordion').find('.card').first().addClass(" border-top-left-radius-0 border-top-right-radius-0");


                @*if (selected_device.length <= 0) {
                    $('input[name="selectedDeviceId[]"]').first().trigger("click");
                }*@

                $('input[name="selectedDeviceId[]"]').each(function () {
                    if ($.inArray(this.value, selected_device) > -1) {
                        $(this).prop('checked', true);
                    }
                });

                $("#js_list_accordion .collapse").collapse('show');
                hideLoading();

            }, 'json');
        }

        //在剛開始就在 sensor_type_detail 資料表中 取得ItemName
        //#region
        function GetItemInfo(itemId) {
            $("#chooseToCompareBtn").html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').attr("disabled", true);
            //sensor_type_detail_dictionary = {''}
            //my_dict = { 'first_name': 'eddie', 'last_name': 'kao', 'age': 30 }

            var url = "/AnalysisStationInfo/GetOneSensorDetail/";

            var send_data = {
                id: itemId
            }

            $.post(url, send_data, function (rel) {
                if (rel.code == "9999") {
                    $("#chooseToCompareBtn").html('選擇比較欄位').attr("disabled", false);
                    toast_error(rel.msg);
                    return;
                }
                else if (rel.code == "9998") {
                    $("#chooseToCompareBtn").html('選擇比較欄位').attr("disabled", false);
                    toast_error(rel.msg);
                    return;
                }
                var nowCompare_col = [{ key: rel.data.itemEName, title: rel.data.itemName, default: true }];
                PushSpecifyCurrentCompareCol(nowCompare_col);
                ChangeCompareSelectOption();
                $("#chooseToCompareBtn").html('選擇比較欄位').attr("disabled", false);
            }, 'json');
        }
        //#endregion

        //#region 加入已勾選的設備所要顯示得比較欄位
        function PushSpecifyCurrentCompareCol(compare_col) {
            $.each(compare_col, function (index, item) {
                if (!current_compare_col.some(x => x.key == item.key)) {
                    item.count = 1;
                    current_compare_col.push(item);
                } else {
                    current_compare_col.find(function (obj, index) {
                        if (obj.key == item.key) {
                            current_compare_col[index].count += 1;
                        }
                    });
                }

                if (item.default && $.inArray(item.key, default_compare_col) == -1) {
                    default_compare_col.push(item.key);
                }
            });

        }
        //#endregion

        //#region 將設備欄位移除要顯示得比較欄位
        function RemoveSpecifyCurrentCompareCol(compare_col) {
            $.each(compare_col, function (index, item) {
                current_compare_col.find(function (obj, index) {
                    if (obj.key == item.key) {
                        current_compare_col[index].count -= 1;
                    }
                });
            });

            $.each(current_compare_col, function (index, item) {
                if (item.count <= 0) {
                    if ($.inArray(item.key, default_compare_col) > -1)
                        default_compare_col.splice($.inArray(item.key, default_compare_col), 1);
                }
            });

        }
        //#endregion

        function ChangeCompareSelectOption() {
            $("#compare-dropdown-menu").empty();
            var str = "";
            $.each(current_compare_col, function (index, item) {
                str += '<li><a href="javascript:void(0)" class="dropdown-item" tabIndex="-1">' + item.title + '<input type="checkbox" class="float-right" name="compare_col[]" value="' + item.key + '" /></a></li>'
            });
            $("#compare-dropdown-menu").append(str);

            $('input[name="compare_col[]"]').each(function () {
                if ($.inArray(this.value, default_compare_col) > -1) {
                    $(this).prop('checked', true);
                }
            });
        }

        function GetAnalysisStationInfo() {
            var date;
            if (searchType != 1) {
                date = $('#DateGet').val();
            } else {
                date = $('#DateGettext').val();
            }

            var url = "/AnalysisStationInfo/GetAnalysisByDeviceIds";

            var send_data = {
                SearchType: searchType,
                SelectedDate: date,
                DeviceIdInfos: selected_device
            }

            if (selected_device.length <= 0) {
                toast_warning("請至少選擇一設備");
                return;
            }

            $("#btn-analysis-stationinfo").html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').attr("disabled", true);
            $('#container').hide();
            $.post(url, send_data, function (rel) {
                if (rel.code != "0000") {
                    toast_error(rel.msg);
                    return;
                }

                analysisStationInfo = rel.data;

                $('input[name="compare_col[]"]').each(function () {
                    if ($.inArray(this.value, default_compare_col) > -1) {
                        $(this).prop('checked', true);
                    }
                });

                $('input[name="compare_col[]"]').trigger('change');

                ReloadHighCharts()
                $("#btn-analysis-stationinfo").html('查詢').attr("disabled", false);
                $('#container').show();

            }, 'json');
        }

        function ReloadHighCharts() {

            if (analysisStationInfo == undefined) {
                return;
            }

            //彙整出 有被選擇的 要顯示的覽位
            selected_YAxis = default_compare_col.filter(obj1 => current_compare_col.some(obj2 => obj1 == obj2.key));

            myYAxis = []; mySeries = [];


            myXAxis = analysisStationInfo.xAxis


            Object.keys(analysisStationInfo.multipleYaxes).map(function (key, index) {

                if (selected_YAxis.indexOf(key) > -1) {
                    var yAxis = {
                        title: {
                            text: analysisStationInfo.multipleYaxes[key],
                        },
                        id: key,
                        opposite: myYAxis.length > 0 ? true : false,
                        showEmpty: false
                    }

                    myYAxis.push(yAxis);
                }
            });

            analysisStationInfo.series.map(function (item, index) {
                if (selected_YAxis.indexOf(item.yaxesKey) > -1) {
                    var ser = {
                        type: 'spline',
                        name: item.name,
                        data: item.values,
                        yAxis: item.yaxesKey,
                    }

                    mySeries.push(ser);
                }
            });

            if (chart) {
                chart.destroy();
            }

            if (myYAxis != undefined && myXAxis != undefined && mySeries != undefined) {
                chart = new Highcharts.Chart({
                    lang: {    //匯出相關中文名稱配置
                        printChart: '列印圖表',
                        downloadJPEG: '下載JPEG檔案',
                        downloadPDF: '下載PDF檔案',
                        downloadPNG: '下載PNG檔案',
                        downloadSVG: '下載SVG檔案',
                        downloadCSV: '下載CSV檔案',
                        downloadXLS: '下載XLS檔案',
                        viewData: '檢視資料表格',
                        viewFullscreen: '全屏檢視'
                    },
                    chart: {
                        renderTo: 'container',
                        height: 600,
                        animation: false
                    },
                    title: false,
                    tooltip: {
                        formatter: function () {
                            return '<b>' + this.series.name + '</b><br>' +
                                '<span>' + this.x + '</span><br>' +
                                '<b style = "color:rgb(103, 180, 172);" >' + this.point.y + '</b>';
                        }
                    },
                    xAxis: {
                        categories: myXAxis,
                        labels: {
                            step: 1,
                            formatter: function () {
                                if (searchType == 0) {
                                    var aa = this.value.substr(-2);
                                    if (aa == "00") {
                                        return '<span>' + this.value + '</span>';
                                    } else {
                                        return '<span style="display:none">' + this.value + '</span>';
                                    }
                                } else {
                                    return this.value
                                }
                            }
                        }
                    },
                    yAxis: myYAxis,

                    series: mySeries,
                });
            }
        }

        function DataBackFill() {
            var url = "/DataBackFill/BackFillHistory"

            $.ajax({
                url: url,
                type: 'post',
                beforSend: function (XMLHttpRequest) {

                },
                success: function (rel) {
                    alert("123")
                },
            });
        }
    </script>
}