<?php
    session_start();
		require('incl/const.php');
    require('class/database.php');
    require('class/map.php');
				
		if(!isset($_SESSION['user']) || $_SESSION['user']->accesslevel != 'Admin') {
				header('Location: ../login.php');
				exit;
		}
		
		$database = new Database(DB_HOST, DB_NAME, DB_USER, DB_PASS, DB_PORT, DB_SCMA);
		$dbconn = $database->getConn();

		$maps_obj = new map_Class($dbconn, $_SESSION['user']->id);
		$rows = $maps_obj->getRows();
		
		//TODO: get directory names with new maps
?>
<!DOCTYPE html>
<html dir="ltr" lang="en">

<head>
  <?php include("incl/meta.php"); ?>
	<link href="dist/css/table.css" rel="stylesheet">

	<link rel="stylesheet" href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap4.min.css">
	<script src="https://cdn.datatables.net/1.13.8/js/jquery.dataTables.min.js"></script>
	<script src="https://cdn.datatables.net/1.13.8/js/dataTables.bootstrap4.min.js"></script>
	
		<script type="text/javascript">

			$(document).ready(function() {
						$('[data-toggle="tooltip"]').tooltip();

							// Delete row on delete button click
							$(document).on("click", ".delete", function() {
							    var obj = $(this);
							    var data = {'delete': true, 'id': obj.parents("tr").attr('data-id')}
									if(confirm('Map will be deleted ?')){
							    	$.ajax({
                                    type: "POST",
                                    url: 'action/map.php',
                                    data: data,
                                    dataType:"json",
                                    success: function(response){
                                        if(response.success) { // means, new record is added
                                            obj.parents("tr").remove();
                                        }

                                        $(".add-new").removeAttr("disabled");
                                        alert(response.message);
                                    }
                                });
									}

							});
							
							$(document).on("click", ".clear", function() {
							    var obj = $(this);
							    var data = {'clear': true, 'id': obj.parents("tr").attr('data-id')}
									if(confirm('Map cache will be deleted ?')){
							    	$.ajax({
                        type: "POST",
                        url: 'action/map.php',
                        data: data,
                        dataType:"json",
                        success: function(response){
													if(response.success){
														obj.hide();
													}
													alert(response.message);
                        }
                    });
									}
							});

						});
		</script>


<style>

.card {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    word-wrap: break-word;
    background-color: #fff;
    border: none!important;
    border-radius: inherit;
    text-decoration: none!important;
}
.bg-warning {
    background-color: #50667f!important;

}

</style>

</head>

<body>
   
    <div id="main-wrapper" data-layout="vertical" data-navbarbg="skin5" data-sidebartype="full"
        data-sidebar-position="absolute" data-header-position="absolute" data-boxed-layout="full">

        <?php define('MENU_SEL', 'maps.php');
					include("incl/topbar.php");
					include("incl/sidebar.php");
				?>
       
        <div class="page-wrapper">
           
            <div class="page-breadcrumb" style="padding-left:30px; padding-right: 30px; padding-top:0px; padding-bottom: 0px">
                <div class="row align-items-center">
                    <div class="col-6">
                        <nav aria-label="breadcrumb">

                          </nav>
                        <h1 class="mb-0 fw-bold">Maps</h1>
                    </div>
                    <div class="col-6">
                        <div class="text-end upgrade-btn">
													<a href="edit_map.php" class="btn btn-primary text-white add-new" role="button" aria-pressed="true">
														<i class="fa fa-plus"></i> Add New
													</a>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="container-fluid">

				<table class="table table-bordered" id="sortTable">
					<thead>
						<tr>
							<th data-name="id" data-editable='false'>ID</th>
							<th data-name="name">Name</th>
							<th data-name="description">Description</th>
							<th data-editable='false' data-action='true'>Actions</th>
						</tr>
					</thead>

					<tbody> <?php while($row = pg_fetch_object($rows)): ?> <tr data-id="<?=$row->id?>" align="left">
							<td><?=$row->id?></td>
							<td><?=$row->name?></td>
							<td><?=$row->description?></td>
							<td>
								<a href="edit_map.php?id=<?=$row->id?>" class="edit" 	title="Edit"	 data-toggle="tooltip">
									<i class="material-icons">&#xE254;</i>
								</a>
								<?php if(is_dir(CACHE_DIR.'/'.$row->id)) { ?>
								<a class="clear" title="Clear cache" data-toggle="tooltip">
									<i class="material-icons">&#xf0ff;</i>
								</a>
								<?php } ?>
								<a class="delete" title="Delete" data-toggle="tooltip">
									<i class="material-icons">&#xE872;</i>
								</a>
							</td>
						</tr> <?php endwhile; ?>
					</tbody>
				</table>

                </div>
            </div>
            <footer class="footer text-center">
            </footer>
        </div>

    </div>
    <script src="dist/js/sidebarmenu.js"></script>
    <script src="dist/js/custom.js"></script>
		<script>new DataTable('#sortTable', { paging: false });</script>
</body>

</html>