<?php require_once('../../setup.php'); ?>
<?php
session_start();
ob_start();
if (!isset($_SESSION['hash'])) {
  header("Location: login");
  exit();
} else {
  $hash = $_SESSION['hash'];
}
define('passportPath', '../passport/');
?>
<!DOCTYPE html>
<html lang="en">

<head>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">

  <title><?= $main_name; ?></title>

  <!-- Custom fonts for this template-->
  <link rel="shortcut icon" href="../../assets/images/logo.png" />
  <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

  <!-- Custom styles for this template -->
  <link href="css/sb-admin-2.min.css" rel="stylesheet">

  <!-- Custom styles for this page -->
  <link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">

</head>

<body id="page-top">

  <!-- Page Wrapper -->
  <div id="wrapper">

    <!-- Sidebar -->
    <?php include('admin_nav.php'); ?>
    <!-- End of Sidebar -->

    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">

      <!-- Main Content -->
      <div id="content">

        <!-- Topbar -->
        <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">&nbsp;</nav>
        <!-- End of Topbar -->

        <!-- Begin Page Content -->
        <div class="container-fluid">

          <!-- Page Heading -->
          <h1 class="h3 mb-1 text-primary"><i class="fa fa-camera"></i> Edit Account Profile Picture</h1>
          <p class="mb-4">You can edit user profile picture by using the form below.</p>

          <!-- Content Row -->
          <div class="row">

            <!-- Border Left Utilities -->
            <div class="col-lg-6">
              <div class="card mb-4 py-3 border-left-primary">
                <div class="card-body">
                  <?php
                  require_once("../conn.php");

                  if (isset($_GET['e'])) {
                    $track_id = $_GET['e'];
                    $sql3 = "SELECT * FROM accounts WHERE id=$track_id";
                    $result3 = $dbconfig->query($sql3);

                    if ($result3->num_rows > 0) {
                      // output data of each row
                      while ($row3 = $result3->fetch_assoc()) {
                        $dbid = $row3['id'];
                        $dbpassport = $row3['passport'];
                      }
                    }
                  } else {
                    header("Location: florence");
                  }


                  require_once("../conn.php");
                  if (isset($_POST['update_pic'])) {
                    $id = mysqli_real_escape_string($dbconfig, $_POST['id']);
                    $pass = 1;


                    //Passport upload query
                    $target_dir = "../passport/";
                    $passport = mysqli_real_escape_string($dbconfig, $_FILES['passport']['name']);
                    $target_file = $target_dir . basename($_FILES["passport"]["name"]);
                    $uploadOk = 1;
                    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
                    $check = @getimagesize($_FILES["passport"]["tmp_name"]);

                    // Check if image file is a actual image or fake image
                    if ($check == false) {
                      echo '<div class="alert alert-danger" role="alert">
                        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                        <i class="fa fa-bug"></i> &nbsp; Passport is not an image. <b>Upload an image!</b>
                      </div>';
                      $uploadOk = 0;
                    }

                    // Check if file already exists
                    if (file_exists($target_file)) {
                      echo '<div class="alert alert-danger" role="alert">
                        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                        <i class="fa fa-bug"></i> &nbsp; Passport name already exists. <b>Rename your passport!</b>
                      </div>';
                      $uploadOk = 0;
                    }

                    // Check file size
                    if ($_FILES["passport"]["size"] > 1000000) {
                      echo '<div class="alert alert-danger" role="alert">
                        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                        <i class="fa fa-bug"></i> &nbsp; Passport most be less than 1MB. <b>Resize your Passport!</b>
                      </div>';
                      $uploadOk = 0;
                    }

                    // Allow certain file formats
                    if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG") {
                      echo '<div class="alert alert-danger" role="alert">
                        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                        <i class="fa fa-bug"></i> &nbsp; Only JPG, JPEG & PNG Allowed. <b>Change passport format!</b>
                      </div>';
                      $uploadOk = 0;
                    }

                    // Check if $uploadOk is set to 0 by an error
                    if ($uploadOk == 0) {
                      echo '<div class="alert alert-danger" role="alert">
                        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                        <i class="fa fa-bug"></i> &nbsp; Your passport was not uploaded. <b>Please try again later!</b>
                      </div>';
                    } else {
                      if ($pass == 1) {
                        // if there's no passport
                        if($dbpassport == "no_passport"){
                          if (move_uploaded_file($_FILES["passport"]["tmp_name"], $target_file)) {
                            $dpu = "UPDATE accounts SET passport='$passport' WHERE id=$id";

                            if ($dbconfig->query($dpu) === TRUE) {
                              header("Refresh: 2; url=florence");
                              echo
                              '<div class="alert alert-success" role="alert">
                              <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                              <i class="fa fa-smile-o"></i> &nbsp; Your passport was uploaded <b>Successfully!</b>
                            </div>';
                            }
                          } else {
                            echo
                            '<div class="alert alert-danger" role="alert">
                            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                            <i class="fa fa-bug"></i> &nbsp; There was an error uploading your passport. <b>Please try again later!</b>
                          </div>';
                          }
                        } else {
                          // if there's passport it should delete the file and upload a new one
                          $file_to_delete = "../passport/$dbpassport";
                          if (@unlink($file_to_delete)) {
                            if (move_uploaded_file($_FILES["passport"]["tmp_name"], $target_file)) {
                              $dpu = "UPDATE accounts SET passport='$passport' WHERE id=$id";

                              if ($dbconfig->query($dpu) === TRUE) {
                                header("Refresh: 2; url=florence");
                                echo
                                '<div class="alert alert-success" role="alert">
                              <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                              <i class="fa fa-smile-o"></i> &nbsp; Your passport was uploaded <b>Successfully!</b>
                            </div>';
                              }
                            } else {
                              echo
                              '<div class="alert alert-danger" role="alert">
                            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true"><i class="notika-icon notika-close"></i></span><span class="sr-only">Close</span></button>
                            <i class="fa fa-bug"></i> &nbsp; There was an error uploading your passport. <b>Please try again later!</b>
                          </div>';
                            }
                          }
                        }
                      }
                    }
                  }
                  ?>
                  <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group">
                      <label for="email">Profile Picture:</label>
                      <input type="hidden" class="form-control" name="id" value="<?php echo $dbid; ?>" required>
                      <input type="file" class="form-control" name="passport" required>
                    </div>
                    <button type="submit" name="update_pic" class="btn btn-primary">Update Profile Picture</button>
                  </form>
                </div>
              </div>
            </div>

          </div>

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

      </div>
      <!-- End of Main Content -->

    </div>
    <!-- End of Content Wrapper -->

  </div>
  <!-- End of Page Wrapper -->

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Bootstrap core JavaScript-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="js/sb-admin-2.min.js"></script>

  <!-- Page level plugins -->
  <script src="vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>

  <!-- Page level custom scripts -->
  <script src="js/demo/datatables-demo.js"></script>

</body>

</html>
