<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
    }
   
// Process delete operation after confirmation
if(isset($_POST["id"]) && !empty($_POST["id"])){
    // Include config file
    require_once "../assets/php/config.php";
    
    // Prepare a delete statement
    $sql = "INSERT INTO tickets_arch SELECT * FROM tickets WHERE id = ?";
    $sql2 = "DELETE FROM tickets WHERE id = ?";
    
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_id);
        
        // Set parameters
        $param_id = trim($_POST["id"]);
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records moved successfully. Deleting from active

    if($stmt2 = mysqli_prepare($link, $sql2)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt2, "i", $param_id);
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt2)){
            header("location: tickets.php");
            exit();

        } else{
            echo "Dell Oops! Something went wrong. Please try again later.";
        }
    }
    mysqli_stmt_close($stmt2);
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
    
    // Close connection
    mysqli_close($link);
} else{
    // Check existence of id parameter
    if(empty(trim($_GET["id"]))){
        // URL doesn't contain id parameter. Redirect to error page
        header("location: error.php?error=tdne");
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en-us">

<head>
    <title>SYSDesk - Achive ticket</title>
    <meta charset="UTF-8">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico" />
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700italic,700,900,900italic" rel="stylesheet">
    <!-- STYLESHEETS -->
    <style type="text/css">
            [fuse-cloak],
            .fuse-cloak {
                display: none !important;
            }
        </style>
    <!-- Icons.css -->
    <link type="text/css" rel="stylesheet" href="../assets/icons/fuse-icon-font/style.css">
    <!-- Animate.css -->
    <link type="text/css" rel="stylesheet" href="../assets/node_modules/animate.css/animate.min.css">
    <!-- Perfect Scrollbar -->
    <link type="text/css" rel="stylesheet" href="../assets/node_modules/perfect-scrollbar/css/perfect-scrollbar.css" />
    <!-- Fuse Html -->
    <link type="text/css" rel="stylesheet" href="../assets/fuse-html/fuse-html.min.css" />
    <!-- Main CSS -->
    <link type="text/css" rel="stylesheet" href="../assets/css/main.css">
	<!-- Custom CSS -->
	<link type="text/css" rel="stylesheet" href="../assets/css/custom.css">
    <!-- / STYLESHEETS -->
    <!-- JAVASCRIPT -->
    <!-- jQuery -->
    <script type="text/javascript" src="../assets/node_modules/jquery/dist/jquery.min.js"></script>
    <!-- Mobile Detect -->
    <script type="text/javascript" src="../assets/node_modules/mobile-detect/mobile-detect.min.js"></script>
    <!-- Perfect Scrollbar -->
    <script type="text/javascript" src="../assets/node_modules/perfect-scrollbar/dist/perfect-scrollbar.min.js"></script>
    <!-- Popper.js -->
    <script type="text/javascript" src="../assets/node_modules/popper.js/dist/umd/popper.min.js"></script>
    <!-- Bootstrap -->
    <script type="text/javascript" src="../assets/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- Data tables -->
    <script type="text/javascript" src="../assets/node_modules/datatables.net/js/jquery.dataTables.js"></script>
    <script type="text/javascript" src="../assets/node_modules/datatables-responsive/js/dataTables.responsive.js"></script>
    <!-- Fuse Html -->
    <script type="text/javascript" src="../assets/fuse-html/fuse-html.min.js"></script>
    <!-- Main JS -->
    <script type="text/javascript" src="../assets/js/main.js"></script>
    <!-- / JAVASCRIPT -->
</head>
<body class="layout layout-vertical layout-left-navigation layout-below-toolbar layout-below-footer">
    <main>
        <div id="wrapper">
            <aside id="aside" class="aside aside-left" data-fuse-bar="aside" data-fuse-bar-media-step="md" data-fuse-bar-position="left">
                <div class="aside-content bg-primary-700 text-auto">
                    <div class="aside-toolbar">
                        <div class="logo">
                            <span class="logo-icon icon-desktop-mac"></span>
                            <p class="logo-text texttoolbar">SYSDesk</p>
                        </div>
                        <button id="toggle-fold-aside-button" type="button" class="btn btn-icon d-none d-lg-block" data-fuse-aside-toggle-fold>
                            <i class="icon icon-backburger"></i>
                        </button>
                    </div>
                    <ul class="nav flex-column custom-scrollbar" id="sidenav" data-children=".nav-item">
                        <li class="nav-item">
                            <a class="nav-link ripple " href="systems.php" data-url="index.html">
                                <i class="icon s-4 icon-desktop-tower"></i>
                                <span>Systems</span>
                            </a>
                        </li>
						<li class="nav-item">
                            <a class="nav-link ripple" href="tickets.php">
                                <i class="icon s-4 icon-message-bulleted"></i>
                                <span>Tickets</span>
                            </a>
                        </li>

                        <li class="nav-item" role="tab" id="heading-ecommerce">

                            <a class="nav-link ripple with-arrow collapsed" data-toggle="collapse" data-target="#collapse-ecommerce" href="#" aria-expanded="false" aria-controls="collapse-ecommerce">

                                <i class="icon s-4 icon-settings"></i>

                                <span>Settings</span>
                            </a>
                            <ul id="collapse-ecommerce" class='collapse ' role="tabpanel" aria-labelledby="heading-ecommerce" data-children=".nav-item">

                                <li class="nav-item">
                                    <a class="nav-link ripple " href="users.php" data-url="index.html">

                                        <span>Users</span>
                                    </a>
                                </li>
                                 <li class="nav-item">
                                    <a class="nav-link ripple " href="reset-password.php">

                                        <span>Reset Password</span>
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link ripple " href="other-settings.php" data-url="index.html">

                                        <span>Other settings</span>
                                    </a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </aside>
            <div class="content-wrapper">
                <nav id="toolbar" class="bg-white">
                    <div class="row no-gutters align-items-center flex-nowrap">
                        <div class="col">
                            <div class="row no-gutters align-items-center flex-nowrap">
                                <button type="button" class="toggle-aside-button btn btn-icon d-block d-lg-none fuse-ripple-ready" data-fuse-bar-toggle="aside">
                                    <i class="icon icon-menu"></i>
                                </button>
                                <div class="toolbar-separator d-block d-lg-none"></div>
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="row no-gutters align-items-center justify-content-end">
							<a href="logout.php" class="logintoolbar ripple icon row align-items-center no-gutters px-2 px-sm-4" role="button" id="logintoolbar" aria-haspopup="true" aria-expanded="false">Logout</a>
                            </div>
                        </div>
                    </div>
                </nav>
					   <div class="content custom-scrollbar">
						<div class="page-layout simple full-width">
						 <div class="page-content-wrapper custom-scrollbar">
						  <!-- CONTENT -->
      <div class="clearfix" class="formbackend" style="background:white; padding:20px;">
       <a href="tickets.php" class="btn btn-secondary btn-fab pull-left"><i class="icon-arrow-left"></i></a>
       <h2 class="pull-right">Achive ticket</h2>
      </div>
     <div class="del form-wrapper md-elevation-8 p-4">  
	                    <form class="del" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                            <input type="hidden" name="id" value="<?php echo trim($_GET["id"]); ?>"/>
                            <center><h4>Are you sure you want to achive this ticket?</h4></center><br>
                            <center>
                                <input type="submit" value="Yes" class="btn btn-success">
                                <a class="btn btn-default" href="javascript:history.back()">No</a>
                            </center>
                    </form>
</div>
						  <!-- / CONTENT -->
						  
						  
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
</body>

</html>