@{
    ViewData["MainNum"] = "8";
    ViewData["SubNum"] = "1";
    ViewData["Title"] = "電站管理";
}
@using SolarPower.Models.Role
@model RoleLayerEnum

<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>

<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="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: 100%">

                <div class="border bg-light rounded-top">
                    <div class="accordion accordion-hover accordion-clean js-list-filter" id="js_list_accordion">
                        <div class="card border-top-left-radius-0 border-top-right-radius-0">
                            <div class="card-header">
                                <a href="javascript:void(0);" class="card-title collapsed" data-toggle="collapse" data-target="#js_list_accordion-a" aria-expanded="true" data-filter-tags="settings">
                                    <i class="fal fa-globe width-2 fs-xl"></i>
                                    新北市
                                </a>
                            </div>
                            <div id="js_list_accordion-a" class="collapse" data-parent="#js_list_accordion">
                            </div>
                        </div>
                        <div class="card">
                            <div class="card-header">
                                <a href="javascript:void(0);" class="card-title collapsed" data-toggle="collapse" data-target="#js_list_accordion-b" aria-expanded="false" data-filter-tags="merge">
                                    <i class="fal fa-globe width-2 fs-xl"></i>
                                    台南市
                                </a>
                            </div>
                            <div id="js_list_accordion-b" class="collapse" data-parent="#js_list_accordion">
                            </div>
                        </div>
                        <div class="card">
                            <div class="card-header">
                                <a href="javascript:void(0);" class="card-title collapsed" data-toggle="collapse" data-target="#js_list_accordion-c" aria-expanded="false" data-filter-tags="backup">
                                    <i class="fal fa-globe width-2 fs-xl"></i>
                                    屏東縣
                                    <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-c" class="collapse" data-parent="#js_list_accordion">
                                <div class="card-body">
                                    <ul class="list-group">
                                        <li class="list-group-item">
                                            <span data-filter-tags="reports file">Reports</span>
                                        </li>
                                        <li class="list-group-item">
                                            <span data-filter-tags="analytics graphs">Analytics</span>
                                        </li>
                                        <li class="list-group-item">
                                            <span data-filter-tags="export download">Export</span>
                                        </li>
                                        <li class="list-group-item">
                                            <span data-filter-tags="storage">Storage</span>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </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">
            <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="subheader">
                                    <h1 class="subheader-title"> 電站分佈區域 </h1>
                                    <div>
                                        <a asp-controller="PowerStation" asp-action="Edit" asp-route-stationId="new" class="btn btn-success waves-effect waves-themed mb-3">
                                            <span class="fal fa-plus mr-1"></span>
                                            新增電站
                                        </a>
                                    </div>
                                </div>
                                <ul id="areaTab" class="nav nav-tabs mb-5" role="tablist" hidden="hidden">
                                </ul>
                                <div id="area" class="tab-content p-3">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div id="templateAreaTab" hidden="hidden">
                <ul>
                    <li class="nav-item">
                        <a class="nav-link fs-lg px-4" data-toggle="tab" href="#tab-newtaipei" role="tab">
                            <span id="areaName" class="hidden-sm-down ml-1">新北市</span> <span id="solarCount" class="badge bg-primary-500 ml-2">4</span>
                        </a>
                    </li>
                </ul>
            </div>
            <div id="templateArea" hidden="hidden">
                <div class="tab-pane" id="tab-name" role="tabpanel" aria-labelledby="tab-name">
                    @*<div class="row mb-5 d-flex justify-content-end">

                            <div class="col-6 text-right">
                                <a href="javascript:TableDisplay();" class="btn btn-secondary btn-icon waves-effect waves-themed mr-1" style="width: 47px;"><span class="fal fa-list fa-2x mt-2"></span></a>
                                <a href="javascript:CardDisplay();" class="btn btn-secondary btn-icon waves-effect waves-themed mr-1" style="width: 47px;"><span class="fal fa-grip-horizontal fa-2x mt-2"></span></a>
                            </div>
                        </div>*@

                    <div class="row solarCard" id="solarCard">
                    </div>
                    <div class="TableArea">
                        <table id="solarTable" class="table table-bordered table-hover m-0 text-center">
                            <thead class="thead-themed">
                                <tr>
                                    <th>電站名稱</th>
                                    <th>裝置容量(kWp)</th>
                                    <th>逆變器數量</th>
                                    <th>台電掛錶日</th>
                                    <th>狀態</th>
                                    <th>功能</th>
                                </tr>
                            </thead>
                            <tbody>
                                @*<tr>
                                        <th scope="row">PEP-NTP001-PYR-01</th>
                                        <td>日照計01</td>
                                        <td>日照計</td>
                                        <td>ADTEK</td>
                                        <td>CS1</td>
                                        <td></td>
                                        <td>CS1</td>
                                        <td></td>
                                        <td></td>
                                        <td>
                                            <button type="button" class="btn btn-primary btn-pills waves-effect waves-themed">修改</button>
                                        </td>
                                    </tr>*@
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>

            <div id="templateCard" hidden="hidden">
                <div class="col-xl-2">
                    <div class="card border m-auto m-lg-0">
                        <a id="editSolarUrl" href="0station.html">
                            <img id="Solarimg" src="img/card-backgrounds/cover-3-lg.png" class="card-img-top" alt="...">
                        </a>
                        <div class="card-body d-flex justify-content-between pb-0 px-2">
                            <h4 id="solarName" class="font-weight-bold" style="line-height: 27px;">
                                新竹巨城站
                            </h4>

                        </div>
                        <ul class="list-group list-group-flush">
                            <li class="list-group-item px-2">
                                <div class=" d-flex justify-content-between">
                                    <p>裝置容量(kWp)</p>
                                    <p><span id="generating_capacity" class="color-primary-400">185</span> kWh</p>

                                </div>
                                <div class=" d-flex justify-content-between">
                                    <p>逆變器數量</p>
                                    <p><span id="inverter_amount" class="color-primary-400">2,5840</span></p>

                                </div>
                                <div class=" d-flex justify-content-between">
                                    <p>台電掛表日</p>
                                    <p><span id="electricity_meter_at" class="color-primary-400">90</span></p>

                                </div>
                            </li>
                        </ul>

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








<!-- Your main content goes below here: -->


@section Scripts{
    <script>
        var tablocation = "";
        var ids = new Array(0);
        var localurl = this.location.href;
        var index = 0;

        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");
            Cityes();
            
            $('.nav-tabs a[href="#tab-newtaipei"]').tab('show');

            TableDisplay();

           
        });

        function Cityes() {
            showLoading();
            var url = "/PowerStation/GetSolarCitySummary";
            $.post(url, function (rel) {
                if (rel.code != "0000") {
                    hideLoading();
                    toast_error(rel.msg);
                    return;
                }
                else {
                    $('#areaTab').empty();
                    $('#js_list_accordion').empty();
                    var sidebar = "";
                    $.each(rel.data, function (index, val) {
                        //電站區域
                        
                        if (index == 0) {
                            tablocation = val.cityId;
                            
                        } else {
                            
                        }
                        ids.push(val.cityId);


                        sidebar += "<div class='card'>";
                        sidebar += "<div class='card-header'>";
                        sidebar += "<a id='tab-" + index + "-collapse' href='#tab-" + val.cityId + "' class='card-title collapsed' data-toggle='collapse' data-target='#js_list_accordion-" + val.cityId + "' aria-expanded='false' data-filter-tags='backup' onclick=changecity(" + val.cityId + "," + index + ")> ";
                        sidebar += "<i class='fal fa-globe width-2 fs-xl'></i>";
                        sidebar += val.cityName;
                        sidebar += "<span class='ml-auto'>";
                        sidebar += "<span class='badge bg-primary-500 ml-2'>" + val.amount + "</span>";
                        sidebar += "</span>";
                        sidebar += "</a>";
                        sidebar += "</div>";
                        sidebar += "<div id='js_list_accordion-" + val.cityId + "' class='collapse' data-parent='#js_list_accordion'>";
                        sidebar += "</div>";
                        sidebar += "</div>";



                        $('#sidebar2').find('.card').attr('id', 'sidebar' + val.cityId).clone().appendTo($('#sidebar'));




                        $('#templateAreaTab').find('li').attr('id', 'AreaTab' + val.cityId).clone().appendTo($('#areaTab'));
                        $('#AreaTab' + val.cityId).find('a').attr('href', '#tab-' + val.cityId);
                        $('#AreaTab' + val.cityId).find('a').attr('class', 'nav-link fs-lg px-4');
                        $('#AreaTab' + val.cityId).find('#areaName').html(val.cityName)
                        $('#AreaTab' + val.cityId).find('#solarCount').html(val.amount)
                        //alert(val.cityId+val.city + val.amount);
                        //電站區域內容
                        $('#templateArea').find('.tab-pane').attr('id', 'tab-' + val.cityId).clone().appendTo($('#area'));
                        $('#tab-' + val.cityId).attr('aria-labelledby', val.cityId)
                        $('#tab-' + val.cityId).find('#solarCard').attr('id', 'solarCard' + val.cityId);
                        $('#tab-' + val.cityId).find('#solarTable').attr('id', 'solarTable' + val.cityId);
                    });
                    $('#js_list_accordion').append(sidebar);
                    addPowerStationCard(ids);

                    $("#areaTab").find(".nav-item > a").first().click();
                    $('#tab-' + index + '-collapse').trigger("click");
                    hideLoading();
                    return;
                }
            }, 'json');
        }

        function changecity(a,i)
        {
            $('#AreaTab' + a).find('a').trigger("click");
            index = i;
        }



        function addPowerStationCard(ids) {
            showLoading();
            //alert(ids);
            var url = "/PowerStation/GetSolarByCity";
            var send_data = {
                cityid: ids
            }
            $.post(url, send_data, function (rel) {
                if (rel.code != "0000") {
                    hideLoading();
                    toast_error(rel.msg);
                    return;
                }
                else {
                    $.each(rel.data, function (index, val) {
                        $('#solarTable' + val.cityId).find('tbody').empty();
                    });
                    $.each(rel.data, function (index, val) {
                        //電站卡片

                        if (val.electricityMeterAt == undefined || val.electricityMeterAt == null) {
                            val.electricityMeterAt = "";
                        }
                        $('#templateCard').find('.col-xl-2').attr('id', 'card' + val.id).clone().appendTo($('#solarCard' + val.cityId));
                        $('#card' + val.id).find('#editSolarUrl').attr('href', localurl + '/edit?stationId=' + val.id);
                        $('#card' + val.id).find('#editSolarUrl').find('#Solarimg').attr('src', val.mainDisplay);
                        $('#card' + val.id).find('#editSolarUrl').find('#Solarimg').attr('style', "width: 100%;height: 138px;");
                        $('#card' + val.id).find('#solarName').text(val.name);
                        $('#card' + val.id).find('#generating_capacity').text(val.generatingCapacity);
                        $('#card' + val.id).find('#inverter_amount').text(val.inverterAmount);
                        $('#card' + val.id).find('#electricity_meter_at').text(val.electricityMeterAt);

                        var del_str = "";
                        @if (ViewBag.myUser.Role.Layer == (int)RoleLayerEnum.PlatformAdmin){
                            <text>
                        del_str = '<button type = "button" class= "btn btn-danger btn-pills waves-effect waves-themed del-btnto" > 刪除</button >';
                            </text>
                        }
                        var status_str = "";
                        
                        if (val.status == 0) {
                        status_str = '<button type = "button" class= "btn btn-info btn-pills waves-effect waves-themed status-btnto" > 啟用</button >';
                        }
                        else {
                            status_str = '<button type = "button" class= "btn btn-danger btn-pills waves-effect waves-themed unstatus-btnto" > 停用</button >';
                        }

                        $('#solarTable' + val.cityId).find('tbody').append('<tr data-id="' + val.id + '" datacity-id="' + val.cityId + '"">' +
                            '<td><a href="' + localurl + '/edit?stationId=' + val.id +'">' + val.name + '</td>' +
                            '<td>' + toThousands(val.generatingCapacity.toFixed(3)) + '</td>' +
                            '<td>' + toThousands(val.inverterAmount) + '</td>' +

                            '<td>' + val.electricityMeterAt + '</td>' +
                            '<td>' + ((val.status == 0) ? "停用" : "啟用") + '</td>' +
                            '<td>' + status_str + ' <button type="button" class="btn btn-primary btn-pills waves-effect waves-themed" onclick="location.href=\'' + localurl + '/edit?stationId=' + val.id + '\'">選擇</button>   ' +
                            del_str +
                            '</td > ' +

                            '</tr>');
                    });
                    $('.nav-tabs a[href="#' + tablocation + '"]').tab('show');
                    hideLoading();
                    return;
                }

            }, 'json');
        }
        $(document).on("click", "button.status-btnto", function () {
            showLoading();
            var selected_id = $(this).parents('tr').attr('data-id');

            var url = "/PowerStation/CheckStationStatus";
            var send_data = {
                Id: selected_id
            }

            $.post(url, send_data, function (rel) {
                if (rel.code == "9999") {
                    hideLoading();
                    toast_error(rel.msg);
                    return;
                }
                else
                {
                    if (rel.code == "0000") {
                        toast_ok(rel.msg);
                        Cityes();
                    }
                    else if (rel.code == "0099") {
                        var text = "";
                        $.each(rel.data, function (index, val) {
                            text += index+1 + "." + val + "<br\>";
                        });
                        text += "電站尚有以上問題至無法啟用電站<br\>";
                        Swal.fire(
                            {
                                title: "啟用失敗",
                                icon: 'warning',
                                html: text,
                            });
                    }
                    
                }
                hideLoading();
            }, 'json');
        });

        $(document).on("click", "button.unstatus-btnto", function () {
            showLoading();
            var selected_id = $(this).parents('tr').attr('data-id');

            var url = "/PowerStation/StationUnStatus";
            var send_data = {
                Id: selected_id
            }

            $.post(url, send_data, function (rel) {
                if (rel.code == "9999") {
                    hideLoading();
                    toast_error(rel.msg);
                    return;
                }
                else {
                    if (rel.code == "0000") {
                        toast_ok(rel.msg);
                    }
                    Cityes();
                }
                hideLoading();
            }, 'json');
        });


        function CardDisplay() {
            $('.TableArea').hide();
            $('.solarCard').show();

        }

        $(document).on("click", "button.del-btnto", function () {
            var selected_id = $(this).parents('tr').attr('data-id');
            var selected_cityid = $(this).parents('tr').attr('datacity-id');
            showLoading();
            var url = "/PowerStation/GetShareDevicePowerstationName";
            var send_data = {
                Id: selected_id
            }
            $.post(url, send_data, function (rel) {
                if (rel.code != "0000") {
                    hideLoading();
                    toast_error(rel.msg);
                    return;
                }
                else {
                    var stationName = "";
                    if (rel.data.length != 0) {
                        stationName = "此電站與 <br\><br\>"
                        $.each(rel.data, function (index, val) {
                            stationName += index + 1 + "." + val + "<br\>";
                        });
                        stationName += "<br\>以上電站共用設備<br\>刪除則將解除所有設備共用關係!<br\>"

                    }
                    var text = stationName + "請問確定是否刪除該電站? <br\>"
                    Swal.fire(
                        {
                            title: "刪除",
                            icon: 'warning',
                            html: text,
                            showCancelButton: true,
                            confirmButtonText: "刪除",
                            cancelButtonText: "取消"
                        }).then(function (result) {
                            if (result.value) {
                                //刪除電站
                                var url = "/PowerStation/DeletePowerStation/";
                                var send_data = {
                                    stationId: selected_id
                                }
                                $.post(url, send_data, function (rel) {
                                    if (rel.code == "9999") {
                                        toast_error(rel.msg);
                                        return;
                                    }
                                    toast_ok(rel.msg);
                                    var cityamount = $('#ul-List-' + selected_cityid).text();
                                    $('#ul-List-' + selected_cityid).text(cityamount - 1);
                                    $('#li-List-' + selected_id).hide();

                                    Cityes();

                                }, 'json');
                            }
                        });

                    hideLoading();
                    return;
                }

            }, 'json');





        });


        function TableDisplay() {
            $('.TableArea').show();
            $('.solarCard').hide();
        }
    </script>
}
