
<!-- latest jquery-->
<script src="assets2/js/jquery-3.2.1.min.js"></script>

<!-- Bootstrap js-->
<script src="assets2/js/popper.min.js"></script>
<script src="assets2/js/bootstrap.js"></script>



<!-- Chart JS-->
<script src="assets2/js/Chart.min.js"></script>

<!-- Morris Chart JS-->
<script src="assets2/js/raphael.js"></script>
<script src="assets2/js/morris.js"></script>

<!-- owlcarousel js-->
<script src="assets2/js/owl.carousel.js"></script>

<!-- Sidebar jquery-->
<script src="assets2/js/sidebar-menu.js"></script>

<!--Sparkline  Chart JS-->
<script src="assets2/js/sparkline.js"></script>

<!--Height Equal Js-->
<script src="assets2/js/height-equal.js"></script>

<!-- prism js -->
<script src="assets2/js/prism.min.js"></script>

<!-- clipboard js -->
<script src="assets2/js/clipboard.min.js"></script>

<!-- custom card js  -->
<script src="assets2/js/custom-card.js"></script>

<!-- Theme js-->
<script src="assets2/js/script.js"></script>
<!-- <script src="./assets2/js/theme-customizer/customizer.js"></script> -->
<!-- <script src="./assets2/js/chat-sidebar/chat.js"></script> -->
<!-- <script src="./assets2/js/dashboard-default.js" ></script> -->


<!-- Counter js-->
<script src="assets2/js/jquery.waypoints.min.js"></script>
<script src="assets2/js/jquery.counterup.min.js"></script>
<script src="assets2/js/counter-custom.js"></script>

<script src="assets2/js/bootstrap-notify.min.js"></script>
<script src="assets2/js/index.js"></script>

<script src="assets2/js/bootbox.min.js"></script>

<script>

    var previousLink;
    var globalWebServicePath = "./";

    var tabDiv = $('#pills-icontabContent');
    var pillsDiv=$('#pills-icontab');
    
    function closeTab(sender,mySBID)
    {
        bootbox.confirm({
            title: "Confirmation",
            message: "Close this tab?",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
            
                if (!result) return;
                $(sender).parents('li.nav-item').remove();
                
                $("div#pills-"+mySBID).remove();
                
                
                if ($('#pills-icontab>li.nav-item').length>0)
                {
                    $('#pills-icontab>li.nav-item:last>a.nav-link').click();
                }
                // <ul class="nav nav-pills custom-scrollbar slim" id="pills-icontab" role="tablist" style="margin: 0;padding: 0;/* max-height: 40px; *//* list-style-type: none; */line-height: 26px;/* max-height: 40px; */overflow: hidden;display: inline-block;white-space: nowrap;overflow-x:auto;position:relative;">
                    
                //     <li class="nav-item">
                //         <a class="nav-link active" id="pills-0-tab" data-toggle="pill" href="#pills-0" role="tab" aria-controls="pills-0" aria-selected="false">
                //             <i class="icon-desktop icon-sidebar" style="margin-left:0px;margin-right:5px;font-size:10px"></i>Dashboard<i class="icon-close" onclick="closeTab(this,'0')"  style="margin-left:5px;margin-right:0px;font-size:10px"></i>
                //         </a>
                //     </li>

        
                    
                // </ul>


                    

            }
        });
    }
    
    function navigateTo(sender,pageLink,sideBarName,sideBarID,flag)
    {
        if (flag=="1")
        {
            window.location = pageLink;
            return;
        }
        else if (flag=="2")
        {
            window.open( pageLink, "_blank"); 
            return;
        }
        
        
        if (sender)
        {
            $('.sidebar-menu>li.active').removeClass('active');
            $('.sidebar-submenu a.active').removeClass('active');

            $(sender).addClass('active');
            $(sender).parents('ul').first().parent("li").addClass('active');
            var checkElement = $(sender).parents('ul').first();

            pageLink = pageLink + (pageLink.indexOf("?") == -1 ? "?" : "&") + "refresh=" + Math.random();
            pageLink = pageLink + "&sidebarname=" + sideBarName + "&sidebarid=" + sideBarID;

            console.log(pageLink)
            $('#frame_body1w').attr('src',pageLink)
           // previousLink = sender;

        }

        var mySBID;
        
        mySBID = sideBarID;
        mySBID = $('#pills-icontabContent').children('div.tab-pane').length;

        /*<i class="icon-desktop icon-sidebar" style="margin-left:0px;margin-right:5px;font-size:10px"></i>

<i class="icon-close" onclick="closeTab(this,\'' + mySBID +'\')"  style="margin-left:5px;margin-right:0px;font-size:10px"></i>
        */
        var tabHTML='\n'+
        '<li class="nav-item">\n'+
        '    <a class="nav-link" id="pills-' + mySBID + '-tab" data-toggle="pill" href="#pills-'+ mySBID +'" role="tab" aria-controls="pills-'+ mySBID +'" aria-selected="false">\n'+
        '        '+ sideBarName +'\n'+
        '    </a>\n'+
        '</li>\n';

        pillsDiv.html(tabHTML);

        var tabPAGE = '\n'+
        //'<div class="tab-content" id="pills-icontabContent">\n'+
        '    <div class="tab-pane fade show" id="pills-'+mySBID+'" role="tabpanel" aria-labelledby="pills-' + mySBID +'-tab">\n'+
        '        <iframe src="' + pageLink + '" class="container-fluid" style="position:absolute; overflow:hidden; height:100%;border:none;padding-right:0px!important;padding-left:0px!important;" ></iframe>\n'+
        '    </div>\n'+
        '\n';
        
        tabDiv.append(tabPAGE);
        
        
        $("#pills-"+mySBID+ "-tab").click();
        

        //$('#menuList').scrollTo("#pills-"+mySBID+ "-tab");
        //$("#pills-"+mySBID+ "-tab")[0].scrollIntoView(true);


        // checkElement.slideUp(300, function() {
        //      checkElement.removeClass('menu-open');
        // });
        

        //$('.switch-state').click();--Removed by mr. Anas

        //alert(pageLink)
        //SetCookies();
    }
    function loadSideBar()
    {
        var SideBar = $('#main-page-sidebar');

        $.ajax({
            url: globalWebServicePath + '/WebService_Inventroid.asmx/GetSideBar', 
            dataType: 'JSON',
            type: 'GET',
            async: true,

            beforeSend: function()
            {
                // var el = $(SideBar).closest("div.page-sidebar");
                // App.blockUI({target: el,animate: true,overlayColor: 'none'});
            },
            complete: function()
            {
                // var el = $(SideBar).closest("div.page-sidebar");
                // App.unblockUI(el);
            },

            success: function (ourData, textStatus, XMLHttpRequest) {
                // <li>
                //     <a href="#" class="sidebar-header">
                //         <i class="icon-shopping-cart-full icon-sidebar"></i><span class="sub-header-title">Accounts</span>
                //         <i class="fa fa-angle-right pull-right"></i>
                //     </a>
                //     <ul class="sidebar-submenu">
                //         <li><a href="#" onclick="javascript: navigateTo(this, './pages/accounts_group.html')"><i class="fa fa-angle-right"></i>Accounts Group</a></li>
                //         <li><a href="#" onclick="javascript: navigateTo(this, './pages/ledger.html')"><i class="fa fa-angle-right"></i>Create Ledger</a></li>
                //         <li><a href="#" onclick="javascript: navigateTo(this, './pages/vouchers.html')"><i class="fa fa-angle-right"></i>Vouchers</a></li>
                //         <li><a href="#" onclick="javascript: navigateTo(this, './reportViewer.php?isDebug=1&refresh=1&reportFileName=reports/ChartOfAccounts.trdx')"><i class="fa fa-angle-right"></i>Chart Of Accounts Report</a></li>

                //         <li><a href="#" onclick="javascript: navigateTo(this, './reportViewer.php?isDebug=1&refresh=1&reportFileName=reports/TrialBalanceReport.trdx')"><i class="fa fa-angle-right"></i>Trial Balance Report</a></li>
                //         <li><a href="#" onclick="javascript: navigateTo(this, './reportViewer.php?isDebug=1&refresh=1&reportFileName=reports/LedgerReport.trdx')"><i class="fa fa-angle-right"></i>Ledger Wise Statement Report</a></li>
                //         <li><a href="#" onclick="javascript: navigateTo(this, './reportViewer.php?isDebug=1&refresh=1&reportFileName=reports/VoucherReport.trdx')"><i class="fa fa-angle-right"></i>Voucher Report</a></li>


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

                var data = ourData;
                for (var i=0; i<data.sidebar.length; i++)
                {
                    var start_tag= "";
                    if (i==0)
                        start_tag = "start active open";
                    
                    var separator = "";
                    if (data.sidebar[i].separator)
                    {
                        separator='<div class="sidebar-title">'+ (data.sidebar[i].separator+'') +'</div>\n';
                    }
                    
                    var coding = '<li>\n'+
                    separator +
                    '        <a href="#" class="sidebar-header">\n'+
                    '           <i class="' + data.sidebar[i].sidebarpicture + ' icon-sidebar"></i><span class="sub-header-title">'+ data.sidebar[i].sidebarname +'</span>\n'+
                    '           <i class="fa fa-angle-right pull-right"></i>\n'+
                    '        </a>';
                    coding+='        <ul class="sidebar-submenu">\n';
                    
                    for (var j = 0; j<data.sidebar[i].children.length; j++)
                    {
                        
                        coding+='<li><a href="#" onclick="javascript: navigateTo(this, \'' + data.sidebar[i].children[j].urllink +'\', \'' + data.sidebar[i].children[j].sidebarname +'\', \'' + data.sidebar[i].children[j].sidebarid +'\', \'' + data.sidebar[i].children[j].flag +'\' )"><i class="fa fa-angle-right"></i>' + data.sidebar[i].children[j].sidebarname  + '</a></li>\n';
                        // coding+='                <li class="nav-item ">\n'+// start active open
                        // '                   <a href="'+ data.sidebar[i].children[j].urllink +'" class="ajaxify nav-link ">\n'+
                        // '\n'+
                        // // '                       <img id="sidebar-children-icon" src="' + data.sidebar[i].children[j].sidebarpicture + '">\n'+
                        // '                     <i class="fa ' + data.sidebar[i].children[j].sidebarpicture + '"></i>\n'+
                        // '\n'+
                        // '                       <span class="title" id="sidebar-children">' + data.sidebar[i].children[j].sidebarname  + '</span>\n'+
                        // '                       <span class="selected"></span>\n'+
                        // '                   </a>\n'+
                        // '               </li>\n';
                        
                    }
                    coding+='</ul>\n';
                    coding+='</li>\n';

                    //console.log(coding)
                    $(SideBar).append(coding);
                    coding="";
                    // console.log(coding);
                }//For Loop

                //$('.page-sidebar li > a.ajaxify:first').click();
            },
            // error: function (XMLHttpRequest, textStatus, errorThrown) {console.log(XMLHttpRequest);console.log(textStatus);console.log(errorThrown);alert('error');}
        });
    }
    function changePassword(){
       // alert('cccc')
        navigateTo(this,"./pages/reset-password.html","Change password",-1);
    }
        function changePasscode(){
        navigateTo(this,"./pages/reset-passcode.html","Change Passcode",-1);
    }
    function editProfile(){
        navigateTo(this,"./pages/edit-profile.html","Edit Profile",-1);
    }
    function formlogout() {/*
        $.ajax({

            url: globalWebServicePath + '/WebService_Inventroid.asmx/AbandonSession',

            dataType: 'JSON',
            type: 'GET',
            async: true,
            success: function (data, textStatus, XMLHttpRequest) {
            
                window.location.replace("./index.php");
                return true;

            },
            error: function () {
                // alert('x')
            }

        });
    */
window.location='logout.php';
}

    //loadSideBar();

    window.user_UserName = "";
    window.user_EmployeeName = "";
    
    window.user_SetUser = function (user_name,employee_name) {
        window.user_UserName = user_name;
        window.user_EmployeeName = employee_name;

        $('#user_name').html(window.user_UserName)
    };

    window.modal_PictureData = "";

    window.modal_BrowsePicture = function(ExistingPictures,allowMultiple,callBack,title)
    {
        
        window.modal_PictureData = ExistingPictures;

        var html = '<iframe src="./pages/browse-picture.html?title='+title+'&1=1' + (allowMultiple ? "&AllowMultiple=1" : "") + '" class="container-fluid" style="min-height: 195px;overflow:hidden; height:100%;border:none;padding-right:0px!important;padding-left:0px!important;" id="frame_body"></iframe>'

        bootbox.confirm({title: 'Browse Picture',message: html  ,size: 'large',
                    buttons: {confirm: {label: 'Save',className: 'btn-success'},cancel: {label: 'Cancel',className: 'btn-danger'}},
                    callback: function (result) {if (result){if (callBack) callBack(window.modal_PictureData)}}})

        // $.ajax({
        //     url: '',
        //     success: function (data) {
        //             bootbox.confirm({title: 'Browse Picture',message: data,size: 'large',
        //             buttons: {confirm: {label: 'Save',className: 'btn-success'},cancel: {label: 'Cancel',className: 'btn-danger'}},
        //             callback: function (result) {if (result){if (callBack) callBack(window.modal_PictureData)}}})
        //     },
        //     error: function (ex) {alert("Something went wrong, Please browse & try again!");}
        // });
    }
</script>
