<%@ page language="C#" autoeventwireup="true" inherits="Pages_MobileView_SubjectWisePercAnalysis, App_Web_jmzpgt0x" masterpagefile="~/Pages/Common/MasterPages/MobileViewMasterPage.master" enableEventValidation="false" viewStateEncryptionMode="Never" %>

<asp:Content ID="Content1" ContentPlaceHolderID="CP" runat="Server">
    <script>
        var Id;
        var OrgId;
        var BatchId;
        var statusId;
        var ExamSubGroupId;
        var Header = [];
        var ReHeader;
        

        $(document).ready(function () {
            var myurl = window.location.href;
            var keyval = myurl.split('?')[1].split('&');
            OrgId = parseInt(keyval[0].split('=')[1].toString());
            Id = OrgId = parseInt(keyval[0].split('=')[1].toString());
            BatchId = parseInt(keyval[1].split('=')[1].toString());
            ExamSubGroupId = parseInt(keyval[2].split('=')[1].toString());

            $.ajax({
                type: "POST",
                url: "SubjectWisePercAnalysis.aspx/GetOrgId",
                data: JSON.stringify({ "Id": Id }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    var obj = (result.d);
                    Header[0] = "<h5 class='page-title'><a href='#' onclick='dashboardpage()'>" + obj[0].Title + "</a></h5>" +
                    " <h5 class='page-title' ><a href='#' onclick='PageName()'>Subject Wise Perc Analysis</a></h5>";
                    $("#HeaderLabel").text("Assessment Dashboard");
                    document.getElementById("HeaderTitle").innerHTML = Header;
                    ReHeader = document.getElementById("HeaderTitle").innerHTML;
                }
            });
            ClassDataBind(OrgId, BatchId, ExamSubGroupId);
        });

        Id = OrgId;

        function PageName() {
            window.location.href = "../MobileView/AssessmentDetailDashboard.aspx?OrgId=" + OrgId;
        }

        function HeaderDisplay(id)
        {
            if (id == 1) {
                document.getElementById("ClassHead").style.display = "block";
                document.getElementById("DivisionHead").style.display = "none";
                $("#class").hide();
                var h1 = [Header[0], Header[1]];
                Header = h1;
            }
        }

        function dashboardpage() {
            window.location.href = "../MobileView/AssessmentDetailDashboard.aspx?OrgId=" + OrgId;
        }

        function ClassDataBind(OrgId, BatchId, ExamSubGroupId)
        {
            $.ajax({
                type: "POST",
                url: "SubjectWisePercAnalysis.aspx/ClassWiseDataGet",
                data: JSON.stringify({ "OrgId": OrgId, "BatchId": parseInt(BatchId), "ExamSubGroupId": parseInt(ExamSubGroupId) }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    //alert(JSON.stringify(result));
                    var total = 0;
                    var totalCol = 0;
                    var grandtotal = 0;
                    var total1 = " ";
                    var finaltotal = 0;
                    var newObj = {};
                    var newObj1 = {};
                    var keyCount = 0;
                    var alreadyAdded = [];
                    var obj = (result.d);
                    var tblClass = "";
                    if (obj != "")
                    {
                        document.getElementById("HeaderTitle").innerHTML = Header;
                        ReHeader = document.getElementById("HeaderTitle").innerHTML;
                        var newObj = {};
                        var keyCount = 0;
                        tblClass += "<tr>";

                        for (var item in obj[0]) {
                            var columnsIn = obj[0][item];
                           
                            if (columnsIn.key === "GroupId") { continue; }
                            else if (columnsIn.key === "SubjectId") { continue; }
                            else if (columnsIn.key === "GroupTitle") { continue; }
                            else
                            {
                                if (columnsIn.key === "GroupName") {
                                    tblClass += "<th>Class Name</th>";
                                    newObj[item] = { "key": "GroupName", "value": "Total" };
                                }
                                else if (columnsIn.key === "SubjectName") {
                                    tblClass += "<th>Subject Name</th>";
                                    newObj[item] = { "key": "SubjectName", "value": "" };
                                }
                                else {
                                    //alert(JSON.stringify(columnsIn));
                                    tblClass += "<th>" + columnsIn.key + "</th>";
                                    newObj[item] = { "key": columnsIn.key, "value": 0 };
                                    //newObj[columnsIn.key] = 0;
                                }
                            }
                        }
                        tblClass += "<th>Total</th></tr>";
                       // keyCount = 1;
                        //alert(JSON.stringify(newObj));
                        var checkClass = [];
                        $.each(obj, function (i, item2) {
                            keycount = 0;
                            
                          if (checkClass.indexOf(item2[1].value) == -1)
                          {
                                checkClass.push(item2[1].value);
                           
                                tblClass += "<tr><th colspan='15'>Class : " + item2[1].value + "</th></tr>";


                            $.each(obj, function (i, item1) {
                                
                                if (item2[1].value === item1[1].value) {
                                    tblClass += "<tr>";
                                    for (var key in item1) {
                                     
                                        if (item1[key].key !== "SubjectId")
                                        {
                                            if (item1[key].key !== "GroupTitle")
                                            {
                                                if (item1[key].value === null || item1[key].value === undefined) {
                                                    item1[key].value = 0;
                                                }

                                                if (item1[key].key === "GroupId")
                                                {
                                                    var GroupId = item1[key].value;
                                                }
                                                else if (item1[key].key === "GroupName")
                                                {
                                                    tblClass += "<td><a onclick='DivisionDataBind(" + OrgId + "," + BatchId + "," + GroupId + ",\"" + String(item1[key].value) + " \"," + ExamSubGroupId + ")'>" + item1[key].value + "</a></td>";
                                                    newObj[key].value = "Total";
                                                }
                                                else
                                                {
                                                    tblClass += "<td>" + item1[key].value + "</td>";

                                                    if (item1[key].value !== "null" && item1[key].value !== undefined && item1[key].value !== null && item1[key].key !== "GroupName")
                                                    {
                                                        if (item1[key].key === "SubjectName")
                                                        {
                                                            newObj[key].value = " ";
                                                        }
                                                        else
                                                        {
                                                            total += parseInt(item1[key].value);
                                                            newObj[key].value += parseInt(item1[key].value);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    tblClass += "<td>" + total + "</td></tr>";
                                    finaltotal += total;
                                    total = 0;
                                }
                               
                            });
                            tblClass += "<tr>";
                            for (var item in newObj) {
                                tblClass += "<th>" + newObj[item].value + "</th>";
                                if (newObj[item].key == "GroupName")
                                {
                                    newObj[item].value = "Total";
                                    continue;
                                }
                                newObj[item].value = 0;
                            }
                            tblClass += "<th>" + finaltotal + "</th></tr>";
                            finaltotal = 0;
                        }
                    });
                    }
                    else
                    {
                        tblClass += "<tr><td colspan='8' class='record'>No records found..!!</td></tr>";
                    }
                    document.getElementById("tblClass").innerHTML = tblClass;
                    document.getElementById("HeaderTitle").innerHTML = '';
                    $.each(Header, function (i, item) {
                        document.getElementById("HeaderTitle").innerHTML += Header[i];
                    });

                    document.getElementById("ClassHead").style.display = "block";
                    console.log(Header);
                }
            });
        }

        function DivisionDataBind(OrgId, BatchId, GroupId, GroupName, ExamSubGroupId) {
            $.ajax({
                type: "POST",
                url: "SubjectWisePercAnalysis.aspx/DivWiseDataGet",
                data: JSON.stringify({ "OrgId": parseInt(OrgId), "BatchId": parseInt(BatchId), "ClassId": parseInt(GroupId), "ExamSubGroupId": parseInt(ExamSubGroupId) }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    var obj = (result.d);
                    var tblDivision = "";
                    if (obj != "") {
                        Header[1] = " <h5 class='page-title' style='display:block'  id='class'><a href='#' onclick='HeaderDisplay(1)'>" + GroupName + "</a></h5>";
                        document.getElementById("HeaderTitle").innerHTML = Header;
                        ReHeader = document.getElementById("HeaderTitle").innerHTML;

                        var total = 0;
                        var total1 = " ";
                        var finaltotal = 0;
                        var newObj = {};
                        var newObj1 = {};
                        var keyCount = 0;
                        var alreadyAdded = [];
                        tblDivision += "<tr>";

                        for (var item in obj[0])
                        {
                            var columnsIn = obj[0][item];
                            if (columnsIn.key === "GroupId") { continue; }
                            else if (columnsIn.key === "SubjectId") { continue; }
                            else if (columnsIn.key === "GroupTitle") { continue; }
                            else
                            {
                                if (columnsIn.key === "GroupName") {
                                    tblDivision += "<th>Division Name</th>";
                                    newObj[item] = { "key": "GroupName", "value": "Total" };
                                }
                                else if (columnsIn.key === "SubjectName") {
                                    tblDivision += "<th>Subject Name</th>";
                                    newObj[item] = { "key": "SubjectName", "value": "" };
                                }
                                else {
                                    tblDivision += "<th>" + columnsIn.key + "</th>";
                                    newObj[item] = { "key": columnsIn.key, "value": 0 };
                                }
                            }
                        }

                        tblDivision += "<th>Total</th></tr>";
                        //keyCount = 1;
                        var checkDiv = [];
                        $.each(obj, function (i, item2)
                        {
                            keycount = 0;
                            if (checkDiv.indexOf(item2[1].value) == -1)
                            {
                                checkDiv.push(item2[1].value);
                                tblDivision += "<tr><th colspan='15'>Class/Division : " + item2[1].value + "</th></tr>";

                                $.each(obj, function (i, item1)
                                {
                                    if (item2[1].value === item1[1].value)
                                    {
                                        tblDivision += "<tr>";
                                        for (var key in item1)
                                        {
                                            if (item1[key].key !== "GroupId")
                                            {
                                                if (item1[key].key !== "SubjectId")
                                                {
                                                    if (item1[key].key !== "GroupTitle")
                                                    {
                                                        if (alreadyAdded.indexOf(item1[1].value) >= 0)
                                                        { }
                                                        else
                                                        {
                                                            if (item1[key].value === null || item1[key].value === undefined)
                                                            {
                                                                item1[key].value = 0;
                                                            }

                                                            tblDivision += "<td>" + item1[key].value + "</td>";

                                                            if (item1[key].key === "GroupName")
                                                            {
                                                                newObj[key].value = "Total";
                                                            }

                                                            if (item1[key].value !== "null" && item1[key].value !== undefined && item1[key].value !== null && item1[key].key !== "GroupName")
                                                            {
                                                               
                                                                if (item1[key].key === "SubjectName")
                                                                {
                                                                    newObj[key].value = " ";
                                                                }
                                                                else
                                                                {
                                                                    total += parseInt(item1[key].value);
                                                                    newObj[key].value += parseInt(item1[key].value);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        tblDivision += "<td>" + total + "</td></tr>";
                                        finaltotal += total;
                                        total = 0;
                                    }
                                });

                                tblDivision += "<tr>";

                                for (var item in newObj)
                                {
                                    tblDivision += "<th>" + newObj[item].value + "</th>";
                                    if (item == "GroupName")
                                    {
                                        newObj[item].value = "Total";
                                        continue;
                                    }
                                    newObj[item].value = 0;
                                }

                                tblDivision += "<th>" + finaltotal + "</th></tr>";
                                finaltotal = 0;
                            }
                            });
                           
                    }
                    else
                    {
                        tblDivision += "<tr><td colspan='8' class='record'>No records found..!!</td></tr>";
                    }
                    document.getElementById("tblDivision").innerHTML = tblDivision;
                    document.getElementById("HeaderTitle").innerHTML = '';
                    $.each(Header, function (i, item) {
                        document.getElementById("HeaderTitle").innerHTML += Header[i];

                    });
                    document.getElementById("ClassHead").style.display = "none";
                    document.getElementById("DivisionHead").style.display = "block";
                    statusId = 2;
                }
            });
        }
      
        function BackOrgFunction() {
           if (statusId == 2) {
               document.getElementById("ClassHead").style.display = "block";
               document.getElementById("DivisionHead").style.display = "none";
               $("#class").hide();
               var h1 = [Header[0], Header[1]];
               Header = h1;
               statusId--;
           }
            else {
                window.location.href = "../MobileView/AssessmentDetailDashboard.aspx?OrgId=" + OrgId; 
            }

        }
        function DashBoard() { window.location.href = "../MobileView/DashBoard.aspx"; }
    </script>
    <asp:UpdatePanel ID="DoctorScheduleUpdatePanel" runat="server">
        <ContentTemplate>
           <div class="page-header row mb20">
                <div class="col-xl-12 col-sm-12 col-xs-12 border-bottom-inline padd-0">
                     <div class="col-md-12 ptb12 d-inline-flex">
                        <span class='btn btn-home btn-icon' onclick='DashBoard()' title='home'><span><i class='material-icons'>home</i></span></span>
                        <label class="header-topic justify-content-center" id="HeaderLabel"></label>
                        <input type="button" value="&#xf3e5;" id="bntBackOrg" onclick="BackOrgFunction()" skinid="0" class="float-right btn btn-back btn-icon fas" />
                    </div>
                    <div class="main-topic inner" id="HeaderTitle">
                    </div>
                </div>
            </div>


            <div class="bg-white row center-block">
                <div class="container-fluid">
                    <div class="gutters-tiny push js-appear-enabled animated fadeIn overflow-hidden" data-toggle="appear">
                        <div class="himssuHeader1WireBox col-md-12 pTop5" id="ClassHead" style="display: none">
                            <h3 class="table-topic">Class Wise Percentage Analysis</h3>
                            <div class='himsBoxContent'>
                                <div class='table-responsive'>
                                    <table id='tblClass' class='table table-striped table-bordered'>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="himssuHeader1WireBox col-md-12 pTop5" id="DivisionHead" style="display: none">
                            <h3 class="table-topic">Division Wise Percentage Analysis</h3>
                            <div class='himsBoxContent'>
                                <div class='table-responsive'>
                                    <table id='tblDivision' class='table table-striped table-bordered'>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

