<?php	
require_once '../functions/dataaccess.php';
session_start();

extract($_POST);

$table1 = TB_PRE . "admin";
$table2 = TB_PRE . "config";
$table3 = TB_PRE . "pages";
$table4 = TB_PRE . "structure";
$table5 = TB_PRE . "notification";
$table6 = TB_PRE . "groups";

$check1 = dbquery("SHOW TABLES LIKE '".$table1."'")->num_rows;
$check2 =  dbquery("SHOW TABLES LIKE '".$table2."'")->num_rows;
$check3 =  dbquery("SHOW TABLES LIKE '".$table3."'")->num_rows;
$check4 =  dbquery("SHOW TABLES LIKE '".$table4."'")->num_rows;
$check5 =  dbquery("SHOW TABLES LIKE '".$table5."'")->num_rows;
$check6 =  dbquery("SHOW TABLES LIKE '".$table6."'")->num_rows;

if($check1 < 1 && $check2 < 1 && $check3 < 1 && $check4 < 1 && $check5 < 1  && $check6 < 1){
    session_destroy();
    header("Location:../setup");
}

else if (!isset($_SESSION['phpsolid_id'])&&$_SESSION['phpsolid_id']==""){
   header("Location: ../login/");     
}
    
$id = 1;

$getconfig = dbquery("SELECT * FROM ".$table2." WHERE id = '".dbescape($id)."'")->rows;
$projectname = $getconfig[0]['projectname'];
$icon = $getconfig[0]['icon'];
$background = $getconfig[0]['background'];
$copyright = $getconfig[0]['copyright'];

$psid = $_SESSION['phpsolid_id'];
$getadmin = dbquery("SELECT * FROM ".$table1." WHERE id = '".dbescape($psid)."'")->rows;

$image = $getadmin[0]['image'];
$fullname = $getadmin[0]['fullname'];
$email = $getadmin[0]['email'];


$getnotification = dbquery("SELECT * FROM ".$table5." WHERE recipient = '".dbescape("All")."' OR recipient = '".dbescape($email)."' ORDER BY id DESC LIMIT 10")->rows;
$countnotification = dbquery("SELECT * FROM ".$table5." WHERE recipient = '".dbescape("All")."' OR recipient = '".dbescape($email)."' ORDER BY id DESC LIMIT 10")->num_rows;
    
?>


<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8" />
        <title><?php echo $projectname ?> - Manage Pages</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta content="<?php echo $projectname ?> - Admin Dashboard" name="description" />
        <meta content="<?php echo $projectname ?>" name="author" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <!-- App favicon -->
        <link rel="shortcut icon" href="<?php echo $icon ?>">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
      
        <!-- third party css -->
        <link href="../assets/libs/dataTables.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/buttons.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/responsive.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/select.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/select2.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/bootstrap-tagsinput.css" rel="stylesheet" />
        <link href="../assets/libs/switchery.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" id="bootstrap-stylesheet" />
        <link href="../assets/css/icons.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/css/app.min.css" rel="stylesheet" type="text/css" id="app-stylesheet" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/4.5.95/css/materialdesignicons.min.css" integrity="sha512-EhtFgx6fGa2B3UNje2rTcPBgryWKx7TVkcGuOsCkybAbAaWEGrWDjsMFPqJUwXf2u1qmz6BxocZvcXVmTfMG9g==" crossorigin="anonymous" />
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/themify-icons/0.1.2/css/themify-icons.css" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
        <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.min.css'><link rel="stylesheet" href="../assets/css/selstyle.css">
    </head>

    <body>

        <!-- Begin page -->
        <div id="wrapper" class="refresh">

            
            <?php include '../header/index.php'; ?>

            <!-- ============================================================== -->
            <!-- Start Page Content here -->
            <!-- ============================================================== -->

            <div class="content-page">
                <div class="content">

                    <!-- Start container-fluid -->
                    <div class="container-fluid">

                        <h4 class="header-title mb-3"></h4>
                        
                        <div class="mt-5">
                            <h4 class="header-title mb-3">Manage Pages List&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-primary waves-effect waves-light" data-toggle="modal" data-target="#myModal">Add New Page <i class="fas fa-plus"></i></button></h4>
                            <table id="datatable" class="table table-striped table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                <thead>
                                    <tr>
                                        <th>Icon</th>
                                        <th>Page Name</th>
                                        <th>Page Status</th>
                                        <th>Date</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>

                                <tbody>
                                    <?php 
                                        $getpages = dbquery("SELECT * FROM ".$table3." ORDER BY pagenumber ASC")->rows;
                                        foreach($getpages as $pages){
                                            $pid = $pages['id'];
                                            $pagename = $pages['pagename'];
                                            $pagenumber = $pages['pagenumber'];
                                            $pagestatus = $pages['pagestatus'];
                                            $pageicon = $pages['pageicon'];
                                            $date = $pages['date_created'];
                                            $ago = getdiff($date);
                                    ?>
                                    <tr>
                                        <td><i class="<?php echo $pageicon ?> fa-3x"></i></td>
                                        <td><?php echo $pagename ?></td>
                                        <td><?php echo $pagestatus ?></td>
                                        <td><?php echo $ago ?></td>
                                        <td>
                                            <!-- <button class="btn btn-warning btn-sm viewbtn" data-id="<?php echo $pid ?>" title="View Page Actions"><i class="fas fa-eye" aria-hidden="true"></i></button> -->
                                            
                                            <button class="btn btn-success btn-sm moveupbtn" data-id="<?php echo $pid ?>" title="Move Up"><i class="fas fa-arrow-up" aria-hidden="true"></i></button>
                                            <button class="btn btn-success btn-sm movedownbtn" data-id="<?php echo $pid ?>" title="Move Down"><i class="fas fa-arrow-down" aria-hidden="true"></i></button>
                                            
                                            <a href="../structure/<?php echo $pid ?>"><button type="button" class="btn btn-warning btn-sm structurebtn" data-id="<?php echo $pid ?>" title="Page Structure"><i class="fas fa-cubes"></i></button></a>
                                            
                                            <button type="button" class="btn btn-primary btn-sm editbtn" data-id="<?php echo $pid ?>" title="Edit"><i class="fas fa-edit"></i></button>
                                            <button class="btn btn-danger btn-sm delbtn" data-id="<?php echo $pid ?>" title="Delete "><i class="fas fa-trash" aria-hidden="true"></i></button>
                                        </td>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>

                        </div>


                    </div>
                    <!-- end container-fluid -->

                    

                    <!-- Footer Start -->
                    <footer class="footer">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-12">
                                    <?php echo date('Y'); ?> &copy; <?php echo $projectname ?> | PHPSOLID CMS DASHBOARD by <a href="https://medaltechie.com">MedalTechie</a>
                                </div>
                            </div>
                        </div>
                    </footer>
                    <!-- end Footer -->

                </div>
                <!-- end content -->

            </div>
            <!-- END content-page -->

        </div>
        <!-- END wrapper -->
        
        <br><br><br><br><br><br><br><br><br><br><br><br>
         <!-- sample modal content -->
        <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <form method="post" action="addprocess.php" enctype="multipart/form-data" class="form-validation">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="myModalLabel">Add New Page</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    </div>
                    <div class="modal-body">
                        <h5 class="font-14">Page Icon<span class="text-danger">*</span></h5>
                        <!-- partial:index.partial.html -->
                        <div id="icon"></div>
                        <select id="select" name="pageicon" class="form-control fa-select"></select>
                        
                        <h5 class="font-14">Page Name<span class="text-danger">*</span></h5>
                        <input type="text" name="pagename" parsley-trigger="change" required class="form-control" id="addpagename" value="" placeholder="Enter Page Name">
                        
                        <h5 class="font-14">Page Status<span class="text-danger">*</span></h5>
                        <select name="pagestatus" class="form-control addpagestat">
                            <option value="onepager">One Pager</option>
                            <option value="multipager">Multi Pager</option>
                            <option value="restrictedpager">Restricted Pager</option>
                        </select>
                        
                        <div style="display:none;" id="showrole">
                            <h5 class="font-14">Select A Role That Can Access The Page<span class="text-danger">*</span></h5>
                            <select id="addgrouprole" name="pagerole" class="form-control">
                                <?php $getrole = dbquery("SELECT * FROM ".$table6." ORDER BY id ASC")->rows;
                                        foreach($getrole as $role){
                                            $gid = $role['id'];
                                            $groupname = $role['groupname'];
                                ?>
                                <option value="<?php echo $gid ?>"><?php echo $groupname ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        
                        <!-- <h5 class="font-14">Select Page Actions<span class="text-danger">*</span></h5>
                        <select name="actions[]" class="form-control select2-multiple" data-toggle="select2" multiple="multiple" data-placeholder="Choose ...">
                            <option value="Add">Add</option>
                            <option value="Update">Update</option>
                            <option value="Delete">Delete</option>
                        </select> -->
                        
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary waves-effect waves-light add-btn">Save</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
            </form>
        </div>
        <!-- /.modal -->
        
        
        <!-- sample modal content -->
        <div id="viewModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <form method="post" enctype="multipart/form-data" class="form-validation">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="myModalLabel">View Page Actions</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    </div>
                    <div class="modal-body">
                        <input disabled id="viewaction" required parsley-trigger="change" type="text" class="form-control" value="" data-role="tagsinput">
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
            </form>
        </div>
        <!-- /.modal -->
        
        
        <!-- sample modal content -->
        <div id="editModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <form method="post" action="editprocess.php" enctype="multipart/form-data" class="form-validation">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="myModalLabel">Edit Page</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    </div>
                    <div class="modal-body">
                         <h5 class="font-14">Page Icon<span class="text-danger">*</span></h5>
                        <!-- partial:index.partial.html -->
                        <div id="iconic"></div>
                        <select id="selectic" name="pageicon" class="form-control fa-select"></select>
                        
                        <h5 class="font-14">Page Name<span class="text-danger">*</span></h5>
                        <input type="text" name="pagename" parsley-trigger="change" required class="form-control" id="editpagename" value="" placeholder="Enter Page Name">
                        
                        <h5 class="font-14">Page Status<span class="text-danger">*</span></h5>
                        <select id="editpagestatus" name="pagestatus" class="form-control editpagestat">
                            <option value="onepager">One Pager</option>
                            <option value="multipager">Multi Pager</option>
                            <option value="restrictedpager">Restricted Pager</option>
                        </select>
                        
                        <div style="display:none;" id="editshowrole">
                            <h5 class="font-14">Select A Role That Can Access The Page<span class="text-danger">*</span></h5>
                            <select id="editgrouprole" name="pagerole" class="form-control">
                                <?php $getrole = dbquery("SELECT * FROM ".$table6." ORDER BY id ASC")->rows;
                                        foreach($getrole as $role){
                                            $gid = $role['id'];
                                            $groupname = $role['groupname'];
                                ?>
                                <option value="<?php echo $gid ?>"><?php echo $groupname ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        
                        <!-- <h5 class="font-14">Select Access To Actions<span class="text-danger">*</span></h5>
                        <select id="pactions" name="actions[]" class="form-control select2-multiple" data-toggle="select2" multiple="multiple" data-placeholder="Choose ...">
                            <option value="Add">Add</option>
                            <option value="Update">Update</option>
                            <option value="Delete">Delete</option>
                        </select> -->
                        
                    </div>
                     <input type="hidden" name="pageid" parsley-trigger="change" required  class="form-control" id="editpageid">
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-success waves-effect waves-light edit-btn">Update</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
            </form>
        </div>
        <!-- /.modal -->
        
        
        <!-- sample modal content -->
        <div id="deleteModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <form method="post" action="deleteprocess.php" enctype="multipart/form-data" class="form-validation">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="myModalLabel">Delete Page</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    </div>
                    <div class="modal-body">
                        <center>
                        <h5 class="font-14"><span class="text-danger">Are you sure you want to delete this Page?.</span></h5>
                        <h5 class="font-14"><span class="text-danger">It may affect some data related to it.</span></h5>
                        </center>
                    </div>
                    <input type="hidden" name="pageid" parsley-trigger="change" required  class="form-control" id="delpageid">
                    <input type="hidden" name="pagename" parsley-trigger="change" required  class="form-control" id="delpagename">
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-danger waves-effect waves-light delete-btn">Delete</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
            </form>
        </div>
        <!-- /.modal -->
        

        <script>
            // $(document).on("click", ".viewbtn", function () {
            //      var pageId = $(this).data('id');
                 
            //      $.ajax({
            //       url: './processflow.php',
            //       type: 'POST',
            //       dataType: 'json',
            //       data: {pageId : pageId, type : "view"},
            //       success: function(datanow){
            //              var pageaction = datanow[0].pageaction;
                         
            //              $('#viewaction').tagsinput('removeAll');
                         
            //              var tagAction = $('#viewaction');
            //              tagAction.tagsinput();
            //              var actionarray = pageaction.split(',');
            //              for(var i=0; i<actionarray.length; i++){
            //                 tagAction.tagsinput('add', actionarray[i]);
            //              }
                         
            //              $('#viewModal').modal('show');
                         
            //         }
            //     });
            // });
            
            
            $(document).on("click", ".editbtn", function () {
                 var pageId = $(this).data('id');
                 
                 $.ajax({
                  url: './processflow.php',
                  type: 'POST',
                  dataType: 'json',
                  data: {pageId : pageId, type : "view"},
                  success: function(datanow){
                         var pagename = datanow[0].pagename;
                         var pagestatus = datanow[0].pagestatus;
                         var pageicon = datanow[0].pageicon;
                        //  var pageaction = datanow[0].pageaction;
                         var pagerole = datanow[0].pagerole;
                         
                         if(pagerole != "0"){
                             $('#editshowrole').show();
                             
                             var pageroleopts = document.getElementById("editgrouprole").options;
                             for(var i = 0; i < pageroleopts.length; i++) {
                                if(pageroleopts[i].value == pagerole) {
                                   var indexopt = i;
                                   document.getElementById("editgrouprole").selectedIndex = indexopt;
                                   break;
                                }
                             }
                         } else{
                             $('#editshowrole').hide();
                         }
                         
                         $('#editpagename').val(pagename);
                         
                         var pagestatusopts = document.getElementById("editpagestatus").options;
                         for(var i = 0; i < pagestatusopts.length; i++) {
                            if(pagestatusopts[i].value == pagestatus) {
                               var indexopt = i;
                               document.getElementById("editpagestatus").selectedIndex = indexopt;
                               break;
                            }
                         }
                         var pageicon = pageicon.replace("fas ", "");
                         
                         var pageicon2 = pageicon + "-o";
                         
                         var pageiconopts = document.getElementById("selectic").options;
                         for(var i = 0; i < pageiconopts.length; i++) {
                            if(pageiconopts[i].value == pageicon) {
                               var indexopt = i;
                               document.getElementById("selectic").selectedIndex = indexopt;
                               break;
                            }
                         }
                         
                         var pageiconopts = document.getElementById("selectic").options;
                         for(var i = 0; i < pageiconopts.length; i++) {
                            if(pageiconopts[i].value == pageicon2) {
                               var indexopt = i;
                               document.getElementById("selectic").selectedIndex = indexopt;
                               break;
                            }
                         }
                         
                        //  var add = "Add";
                        //  var update = "Update";
                        //  var delet = "Delete";
                         
                        //  $('#pactions option[value=' + add + ']').attr('selected', false).change();
                        //  $('#pactions option[value=' + update + ']').attr('selected', false).change();
                        //  $('#pactions option[value=' + delet + ']').attr('selected', false).change();
                         
                        //  var actionarray = pageaction.split(',');
                        //  for(var i=0; i<actionarray.length; i++){
                        //     $('#pactions option[value=' + actionarray[i] + ']').attr('selected','').change();
                        //  }
                         
                         $('#editpageid').val(pageId);
                         
                         $('#editModal').modal('show');
                         
                    }
                });
            });
            
            
            
            $(document).on("click", ".delbtn", function () {
                 var pageId = $(this).data('id');
                 
                 $.ajax({
                  url: './processflow.php',
                  type: 'POST',
                  dataType: 'json',
                  data: {pageId : pageId, type : "view"},
                  success: function(datanow){
                    var pagename = datanow[0].pagename;
                    $('#delpagename').val(pagename);
                    $('#delpageid').val(pageId);
                    $('#deleteModal').modal('show');
                         
                    }
                });
                 
            });
            
            $(document).on("click", ".moveupbtn", function () {
                var pageId = $(this).data('id');

                 $.ajax({
                  url: './processflow.php',
                  type: 'POST',
                  dataType: 'json',
                  data: {pageId : pageId, type : "view"},
                  success: function(datanow){
                     var pageNo = datanow[0].pagenumber;
                     var pagetogo = pageNo - 1;
                     $.ajax({
                      url: './processflow.php',
                      type: 'POST',
                      dataType: 'json',
                      data: {pageNo : pageNo, pagetogo : pagetogo, type : "moveup"},
                      success: function(datanow1){
                          window.location.reload();
                        }
                    });
                    
                    
                  }
                });
                 
            });
            
            
            $(document).on("click", ".movedownbtn", function () {
                 var pageId = $(this).data('id');
                 
                 $.ajax({
                  url: './processflow.php',
                  type: 'POST',
                  dataType: 'json',
                  data: {pageId : pageId, type : "view"},
                  success: function(datanow){
                     var pageNo = datanow[0].pagenumber;
                     var pagetogo = parseInt(pageNo) + 1;
                     
                     $.ajax({
                      url: './processflow.php',
                      type: 'POST',
                      dataType: 'json',
                      data: {pageNo : pageNo, pagetogo : pagetogo, type : "movedown"},
                      success: function(datanow1){
                          window.location.reload();
                        }
                    });
                    
                    
                  }
                });
            });
            
            
            
            $(document).on("change", ".addpagestat", function () {
                var statValue = $('.addpagestat :selected').text();
                
                if(statValue == "Restricted Pager"){
                    $('#showrole').show();
                } else{
                    $('#showrole').hide();
                }
            });
            
            
            $(document).on("change", ".editpagestat", function () {
                var statValue = $('.editpagestat :selected').text();
                
                if(statValue != "Restricted Pager"){
                    $('#editshowrole').hide();
                } else{
                    $('#editshowrole').show();
                }
            });
            
        
        </script>
        
        <script src='https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.jquery.min.js'></script>
        <script src='https://cdnjs.cloudflare.com/ajax/libs/js-yaml/3.6.0/js-yaml.min.js'></script>
        <script  src="selscript.js"></script>

        <!-- Vendor js -->
        <script src="../assets/js/vendor.min.js"></script>

        <!-- Required datatable js -->
        <script src="../assets/libs/jquery.dataTables.min.js"></script>
        <script src="../assets/libs/dataTables.bootstrap4.min.js"></script>

        <!-- Buttons examples -->
        <script src="../assets/libs/dataTables.buttons.min.js"></script>
        <script src="../assets/libs/buttons.bootstrap4.min.js"></script>
        <script src="../assets/libs/dataTables.keyTable.min.js"></script>
        <script src="../assets/libs/dataTables.select.min.js"></script>
        <script src="../assets/libs/select2.min.js"></script>
        <script src="../assets/libs/jszip.min.js"></script>
        <script src="../assets/libs/pdfmake.min.js"></script>
        <script src="../assets/libs/vfs_fonts.js"></script>
        <script src="../assets/libs/buttons.html5.min.js"></script>
        <script src="../assets/libs/buttons.print.min.js"></script>
        <script src="../assets/libs/bootstrap-tagsinput.min.js"></script>
        <script src="../assets/libs/switchery.min.js"></script>
        <script src="../assets/libs/moment.min.js"></script>

        <!-- Responsive examples -->
        <script src="../assets/libs/dataTables.responsive.min.js"></script>
        <script src="../assets/libs/responsive.bootstrap4.min.js"></script>

        <!-- Datatables init -->
        <script src="../assets/js/datatables.init.js"></script>
        <script src="../assets/libs/form-advanced.init.js"></script>

        <!-- App js -->
        <script src="../assets/js/app.min.js"></script>
        
        

    </body>
</html>