<?php
ob_start();
include('../include/db_conx.php');
isset($_SESSION['admin_login']);
if (!isset($_SESSION['admin_login'])) {
    header("location: auth-login.php");
    exit();
} else {
    include('../include/admin_head.php');

    include('../include/admin_action/info.php');

    $success = $_GET['success'];
    if ($success == 'true') {
        $msg = 'Visa Registered Successfully';
        $link = 'add_visa.php';
        swal_success($msg, $link);
    }
?>



    <body>

        <!-- <body data-layout="horizontal"> -->

        <!-- Begin page -->
        <div id="layout-wrapper">

            <?php include('../include/admin_header.php'); ?>

            <!-- ========== Left Sidebar Start ========== -->

            <!-- Left Sidebar End -->
            <?php include('../include/admin_sidebar.php'); ?>



            <!-- ============================================================== -->
            <!-- Start right Content here -->
            <!-- ============================================================== -->
            <div class="main-content">

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

                        <!-- start page title -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                    <h4 class="mb-sm-0 font-size-18">Add new Account below </h4>

                                    <div class="page-title-right">
                                        <ol class="breadcrumb m-0">
                                            <li class="breadcrumb-item"><a href="javascript: void(0);">Visa</a></li>
                                            <li class="breadcrumb-item active">Account Creation</li>
                                        </ol>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <!-- end page title -->




                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title"> Kindly fill all input to complete Creation</h4>
                            </div>
                            <div class="card-body p-4">

                                <div class="row">
                                    <form method="POST" autocomplete="off" enctype="multipart/form-data">
                                        <div class="col-lg-12 d-block m-auto">


                                            <div class = 'row mb-3'>
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Top Text</label>
                                                    <input class="form-control" type="text"  name="online_non" placeholder=" Online Nonimmigrant Visa Application (DS-160) ">
                                                </div>
                                          
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Name Provided</label>
                                                    <input class="form-control" type="text"  name="name_provided" placeholder="TEST, TEST">
                                                </div>
                                          
                                            </div>

                                            <!-- end each row -->
                                            <div class = 'row mb-3'>
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Date Of Birth</label>
                                                    <input class="form-control" type="text"  name="dob" >
                                                </div>
                                          
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Place of Birth</label>
                                                    <input class="form-control" type="text"  name="pob" >
                                                </div>
                                          
                                            </div>

                                            <!-- end each row -->


                                            <!-- end each row -->
                                            <div class = 'row mb-3'>
                                            <div class="col-md-6">
                                                    <label class="form-label">Select Gender </label>
                                                    <select class="form-select" name="gender">
                                                        <option value="Male">Male</option>
                                                        <option value="Female"> Female</option>
                                                        <option value="Others"> Others</option>
                                                    </select>
                                                </div>
                                          
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label"> Nation </label>
                                                    <input class="form-control" type="text"  name="nation" >
                                                </div>
                                          
                                            </div>

                                            <!-- end each row -->
                                            <div class = 'row mb-3'>
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Passport No</label>
                                                    <input class="form-control" type="text"  name="passport_no" >
                                                </div>
                                          
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">  Purpose </label>
                                                    <input class="form-control" type="text"  name="purpose" >
                                                </div>
                                          
                                            </div>

                                            <!-- end each row -->
                                            <div class = 'row mb-3'>
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Completed On</label>
                                                    <input class="form-control" type="text"  name="completed_on" >
                                                </div>
                                          
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">  App ID </label>
                                                    <input class="form-control" type="text"  name="app_id" >
                                                </div>
                                          
                                            </div>

                                            <!-- end each row -->
                                            <div class = 'row mb-3'>
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Location Selected</label>
                                                    <input class="form-control" type="text"  name="location_selected" >
                                                </div>
                                          
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">  Remark</label>
                                                    <input class="form-control" type="text"  name="remark" >
                                                </div>
                                          
                                            </div>

                                          



                                            <div class = 'row mb-3'>
                                        
                                          
                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">  Progress* (%)</label>
                                                    <input class="form-control" type="number"  name="percentage" >
                                                </div>

                                                <div class="col-md-6">
                                                    <label for="example-search-input" class="form-label">Passport photograph</label>
                                                    <input type="file" class="form-control " name="file" required>
                                                </div>
                                          
                                            </div>
          

                                            <!-- end each row -->

                                            <div class="md-6">
                                                <!-- just a break -->
                                            </div>

                                            <button type="submit" class="btn btn-primary" name="submit">Click Here to create Visa <i class="fas fa-chart-line"></i></button>


                                        </div>
                                    </form>

                                    <?php
                                    if (isset($_POST['submit'])) {
                                        $online_non = $db_conx->real_escape_string($_POST['online_non']);
                                        $name_provided = $db_conx->real_escape_string($_POST['name_provided']);
                                        $dob = $db_conx->real_escape_string($_POST['dob']);
                                        $pob = $db_conx->real_escape_string($_POST['pob']);
                                        $gender = $db_conx->real_escape_string($_POST['gender']);
                                        $nation = $db_conx->real_escape_string($_POST['nation']);
                                        $passport_no = $db_conx->real_escape_string($_POST['passport_no']);
                                        $purpose = $db_conx->real_escape_string($_POST['purpose']);
                                        $completed_on = $db_conx->real_escape_string($_POST['completed_on']);
                                        $app_id = $db_conx->real_escape_string($_POST['app_id']);
                                        $location_selected = $db_conx->real_escape_string($_POST['location_selected']);
                                        $remark = $db_conx->real_escape_string($_POST['remark']);
                                        $percentage = $db_conx->real_escape_string($_POST['percentage']);
                                       

                                        // now i will make sure that the code is not duplicated  
                                        $sql_code = "SELECT * FROM clients WHERE app_id = '$app_id'";
                                        $query_code = $db_conx->query($sql_code);
                                        $code_check = mysqli_num_rows($query_code);
                                        if ($code_check > 0) {
                                            swal_error('Tracking Code Duplicate');
                                        }else{

                                        

                                            //now we will code the type of file we want to allow to be uploaded 

                                                    

                                            $files = $_FILES['file'];

                                          $fileName = $files['name'];
                                          $fileTmpName = $files['tmp_name'];
                                          $fileSize = $files['size'];
                                          $fileError = $files['error'];
                                          $fileType = $files['type'];

                                          //now we will code the type of file we want to allow to be uploaded 

                                          $fileExt = explode('.', $fileName);
                                          $fileActualExt = strtolower(end ($fileExt));//the end get the last piece of data in an array 

                                          //creating an array of the information we want to allow 
                                          $allowed = array('jpg', 'jpeg', 'png', 'pdf');

                                          if(in_array($fileActualExt,$allowed)){
                                              if ($fileError === 0) {
                                                  if($fileSize < 50000000){
                                                  // main upload file code 
                                                  // we have to rename it so that it will not have same name for different images
                                                  $fileNameNew =  $app_id.".".$fileActualExt;
                                                  $fileDestination = 'photo/'.$fileNameNew;	
                                                  //main upload code
                                                  move_uploaded_file($fileTmpName, $fileDestination);
                                                  }
                                              }
                                          }
                                                  
                                          $passport = $app_id.'.'.$fileActualExt;


                                              // creating a visa record
                                            $sql_test = "INSERT INTO clients(online_non,name_provided,dob,pob,gender,nation,passport_no,purpose,completed_on,app_id,location_selected,remark,percentage,passport,date_added)
                                            VALUES('$online_non','$name_provided','$dob','$pob','$gender','$nation','$passport_no','$purpose','$completed_on','$app_id','$location_selected','$remark','$percentage','$passport','$today')";
                                            $result = $db_conx->query($sql_test);
                                            if ($result) {
                                                header('location:add_visa.php?success=true');
                                            } else {
                                                echo 'An error has occurred please contact admin';
                                            }

                                        }




                                      
                                    }

                                    ?>


                                </div>
                                <!-- end row -->
                            </div>
                        </div>



                    </div> <!-- container-fluid -->
                </div>
                <!-- End Page-content -->


                <?php include('../include/admin_footer.php'); ?>
            </div>
            <!-- end main content-->

        </div>
        <!-- END layout-wrapper -->




        <!-- Right bar overlay-->
        <div class="rightbar-overlay"></div>

        <script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
        <script src="assets/libs/metismenu/metisMenu.min.js"></script>
        <script src="assets/libs/simplebar/simplebar.min.js"></script>
        <script src="assets/libs/node-waves/waves.min.js"></script>
        <script src="assets/libs/feather-icons/feather.min.js"></script>
        <!-- pace js -->
        <script src="assets/libs/pace-js/pace.min.js"></script>

        <!-- flatpickr js -->
        <script src="assets/libs/flatpickr/flatpickr.min.js"></script>

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

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

        <!-- init js -->
        <script src="assets/js/pages/invoices-list.init.js"></script>

        <script src="assets/js/app.js"></script>
    </body>

    </html>

<?php  }
ob_end_flush();
?>