<?php
include_once './dbconn.php';

if(isset($_GET['id'])) {
    $id = $_GET['id'];

    $sql = "SELECT * FROM  submissions WHERE id = '$id'";
    $result = mysqli_query($conn, $sql);
    $result_checker = mysqli_num_rows($result);
    if ($result_checker > 0) {
        while ($data = mysqli_fetch_assoc($result)) {

            $submission_id = $data["submission_id"];
            $title = $data["title"];
            $first_name = $data["first_name"];
            $middle_name = $data["middle_name"];
            $last_name = $data["last_name"];
            $fullname = $data["fullname"];
            $cmo_induction_status = $data["cmo_induction_status"];
            $id_number = $data["id_number"];
            $phone = $data["phone"];
            $email = $data["email"];
            $approval_status = $data["approval_status"];
            $deanery_name = $data["deanery_name"];
            $parish_name = $data["parish_name"];
            $cmo_induction_status = $data["cmo_induction_status"];
            $blood_group = $data["blood_group"];
            $do_you_require_a_mattress_to_sleep_over = $data["do_you_require_a_mattress_to_sleep_over"];
            $do_you_require_a_hotel_accommodation = $data["do_you_require_a_hotel_accommodation"];
            $if_yes_click_a_range = $data["if_yes_click_a_range"];
            $how_long_have_you_been_canonically_married = $data["how_long_have_you_been_canonically_married"];
            $passport_link = $data["passport_link"];
            $signature_link = $data["signature_link"];
            $Marriage_certificate_link = $data["Marriage_certificate_link"];

        }
    }
                    

}else {
    // header("Location: ./index.php");
    die();
}

?>

<?php

$url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

  if(strpos($url, 'error') == true){


 echo '<script>
    setTimeout(function() {
        swal({
            title: "Error!",
            text: "Error Occured",
            type: "error"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
  </script>';
  

    }elseif (strpos($url, 'updated') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Data Updated successfully",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    } elseif (strpos($url, 'saved') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Passport updated successfully",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }




?>



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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">
</head>

<body>

    <style>
    .content {
        width: 80%;
        padding: 50px 5%;
        /* border: 1px solid gray; */
        border-radius: 10px;
        margin: 50px auto;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    }

    .form-upload_group img {
            width: 100%;
            height: auto;
            max-width: 200px;
            margin: 0 auto 10px;
        }
        .form-upload_group {
            display: flex;
            flex-direction: column;
            align-items: center;
            text-align: center;
        }
        .form-upload_group button {
            width: 80%;
        }
    </style>
    <div class="content">
        <a href="index.php" class="btn btn-secondary mb-3">Go Back</a>
        <!-- Modal content -->
        <div class="modal-content">
            <h2 class='text-center text-primary'>Uploads Update</h2>
            
            <div style="margin: 0 auto; padding:20px 0;">
                <!--passport-->
                <div class="container-fluid py-3">
                <div class="row"> 
               <div class="form-upload_group col-md-4  py-2">
                    <img id="profilePic" src="<?php echo $passport_link; ?>" alt="Profile Picture" style="margin:auto; width: 100px;">
                    <br>
                    <form id="profileForm" action="./addnew/savepassport.php" method="post" enctype="multipart/form-data">
                        
                        <input type="file" id="newImage" name="newImage" accept="image/*" style="display: none;">
                        <div class='py-2'>
                            <button type="button" class="btn btn-warning" onclick="changePicture()">Change Passport</button>
                            <button type="submit" class="btn btn-success" name="save">Save</button>
                        </div>
                    </form>
                </div>

                <div class="form-upload_group col-md-4  py-2">
                    <img id="signaturePic" src="<?php echo $signature_link; ?>" alt="Profile Picture" style="margin:auto; width: 100px;">
                    <br>
                    <form id="profileForm" action="./addnew/savesignature.php" method="post" enctype="multipart/form-data">
                        
                        <input type="file" id="newSignature" name="newSignature" accept="image/*" style="display: none;">
                        <div class='py-2'>
                            <button type="button" class="btn btn-warning" onclick="changeSignature()">Change Signature</button>
                            <button type="submit" class="btn btn-success" name="save">Save</button>
                        </div>
                    </form>
                </div>
                <div class="form-upload_group col-md-4  py-2">
                    <img id="certificatePic" src="<?php echo $Marriage_certificate_link; ?>" alt="Certificate Picture" style="margin:auto; width: 100px;">
                    <br>
                    <form id="profileForm" action="./addnew/savecertificate.php" method="post" enctype="multipart/form-data">
                        
                        <input type="file" id="newCertificate" name="newCertificate" accept="image/*" style="display: none;">
                        <div class='py-2'>
                            <button type="button" class="btn btn-warning" onclick="changeCertificte()">Change Certificate</button>
                            <button type="submit" class="btn btn-success" name="save">Save</button>
                        </div>
                    </form>
                </div>
                </div>
                </div>
                
                <script>
                    function changePicture() {
                        var fileInput = document.getElementById('newImage');
                        fileInput.click();
                        fileInput.onchange = function() {
                            var reader = new FileReader();
                            reader.onload = function(e) {
                                document.getElementById('profilePic').src = e.target.result;
                            }
                            reader.readAsDataURL(fileInput.files[0]);
                        };
                    }

                    function changeSignature() {
                        var fileInput = document.getElementById('newSignature');
                        fileInput.click();
                        fileInput.onchange = function() {
                            var reader = new FileReader();
                            reader.onload = function(e) {
                                document.getElementById('signaturePic').src = e.target.result;
                            }
                            reader.readAsDataURL(fileInput.files[0]);
                        };
                    }

                    function changeCertificte() {
                        var fileInput = document.getElementById('newCertificate');
                        fileInput.click();
                        fileInput.onchange = function() {
                            var reader = new FileReader();
                            reader.onload = function(e) {
                                document.getElementById('certificatePic').src = e.target.result;
                            }
                            reader.readAsDataURL(fileInput.files[0]);
                        };
                    }
                </script>

            </div>

            
            <h2 class='text-center text-primary'>Update Registration Details</h2>
            <!--details -->
            <form id="modalForm" action="./addnew/edit.php" method="post" enctype="multipart/form-data">


                <!--<img src="<?= $passport_link ?>" alt="Image" height= '200px'/><br />-->
                
                <div class="form-row row py-2">
                    <div class="form-group col-md-6  py-2">
                        <label for="title">Title</label>
                        <select id="title" class="form-control" name="title" required>
                            <option value='<?= $title?>' selected><?= $title?></option>
                            <option value='Chief'>Chief</option>
                            <option value='Dr'>Dr.</option>
                            <option value='Sir'>Sir</option>
                            <option value='Mr'>Mr</option>
                            <option value='Barr'>Barr</option>
                            <option value='Engr'>Engr</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="last_name">Last Name</label>
                        <input type="tel" class="form-control" id="last_name" name="last_name"
                            placeholder="Last Name" value="<?=$last_name?>">
                    </div>

                    <input type="hidden" class="form-control" id="id" name="id" value = "<?= $id?>" > </input>
                    <input type="hidden" class="form-control" id="submission_id" name="submission_id" value = "<?= $submission_id?>" > </input>

                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="first_name">First Name</label>
                        <input type="first_name" class="form-control" id="first_name" name="first_name" placeholder="First Name"
                            value="<?=$first_name?>">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="middle_name">Middle Name</label>
                        <input type="middle_name" class="form-control" id="middle_name" name="middle_name" placeholder="Middle Name"
                            value="<?=$middle_name?>">
                    </div>
                 
                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6  py-2">
                        <label for="cmo_induction_status">Induction Status</label>
                        <select id="cmo_induction_status" class="form-control" name="cmo_induction_status" required>
                            <option value='<?= $cmo_induction_status?>' selected><?= $cmo_induction_status?></option>
                            <option value='Inducted Member'>Inducted Member</option>
                            <option value='New Inductee'>New Inductee</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="id_number">ID Number</label>
                        <input type="tel" class="form-control" id="id_number" name="id_number"
                            placeholder="ID Number" value="<?=$id_number?>">
                    </div>

                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="deanery_name">Deanary</label>
                        <select id="deanery_name" class="form-control" onchange="populateparish2()" name="deanery_name" required>
                            <option value='<?= $deanery_name?>' selected><?= $deanery_name?></option>
                            <option value="Agege Deanery">Agege Deanery</option>
                            <option value="Apapa Deanery">Apapa Deanery</option>
                            <option value="Ajah Deanery">Ajah Deanery</option>
                            <option value="Badagry Deanery">Badagry Deanery</option>
                            <option value="Epe Deanery">Epe Deanery</option>
                            <option value="Festac Deanery">Festac Deanery</option>
                            <option value="Iba Deanery">Iba Deanery</option>
                            <option value="Ijanikin Deanery">Ijanikin Deanery</option>
                            <option value="Ijede Deanery">Ijede Deanery</option>
                            <option value="Ikeja Deanery">Ikeja Deanery</option>
                            <option value="Ilemba Deanery">Ilemba Deanery</option>
                            <option value="Ikorodu Deanery">Ikorodu Deanery</option>
                            <option value="Ipaja Deanery">Ipaja Deanery</option>
                            <option value="Isolo Deanery">Isolo Deanery</option>
                            <option value="Lagos Island Deanery">Lagos Island Deanery</option>
                            <option value="Lekki Deanery">Lekki Deanery</option>
                            <option value="Maryland Deanery">Maryland Deanery</option>
                            <option value="Satellite Deanery">Satellite Deanery</option>
                            <option value="Surulere Deanery">Surulere Deanery</option>
                            <option value="Yaba Deanery">Yaba Deanery</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="parish_name">Parish</label>
                        <select id="parish_name" class="form-control"  name="parish_name" required>
                            <option value='<?= $parish_name?>' selected><?= $parish_name?></option>
                            <script src="../deanaryParish.js"></script>
                        </select>
                    </div>

                    <script>
                        function populateparish2() {
                        const deanarySelect = document.getElementById("deanery_name");
                        const parishSelect = document.getElementById("parish_name");
                        const selectedDeanary = deanarySelect.value;

                        // Clear the current options in the local government select
                        parishSelect.innerHTML =
                            '<option value="">Select Parish</option>';

                        // Check if a state is selected
                        if (selectedDeanary) {
                            // Get the local governments for the selected state
                            const deaneryparish = parish[selectedDeanary];

                            // Populate the local government select with options
                            deaneryparish.forEach((parishname) => {
                            const option = document.createElement("option");
                            option.value = parishname;
                            option.textContent = parishname;
                            parishSelect.appendChild(option);
                            });
                        }
                        }


                    </script>


                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="email">Email</label>
                        <input type="email" class="form-control" id="email" name="email" placeholder="Email"
                            value="<?=$email?>">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="phone">Phone</label>
                        <input type="phone" class="form-control" id="phone" name="phone" placeholder="Phone"
                            value="<?=$phone?>">
                    </div>
                 
                </div>

                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="approval_status">Approval Status</label>
                        <select class="form-control" class="form-control" id="approval_status" name="approval_status">
                            <option value='<?= $approval_status?>' selected><?= $approval_status?></option>
                            <option value="Approved">Approved</option>
                            <option value="Denied">Denied</option>
                            <option value="In Progress">In Progress</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="blood_group">Blood Group</label>
                        <select class="form-control" class="form-control" id="blood_group" name="blood_group">
                            <option value='<?= $blood_group?>' selected><?= $blood_group?></option>
                            <option value="A+">A+</option>
                            <option value="A-">A-</option>
                            <option value="B+">B+</option>
                            <option value="B-">B-</option>
                            <option value="AB+">AB+</option>
                            <option value="AB-">AB-</option>
                            <option value="O+">O+</option>
                            <option value="O-">O-</option>
                        </select>
                    </div>
                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="do_you_require_a_hotel_accommodation">Require Accommodation?</label>
                        <select class="form-control" class="form-control" id="do_you_require_a_hotel_accommodation-existing" name="do_you_require_a_hotel_accommodation"
                            required>

                            <option value='<?= $do_you_require_a_hotel_accommodation?>' selected><?= $do_you_require_a_hotel_accommodation?>
                            </option>
                            <option value="Yes">Yes</option>
                            <option value="No">No</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="do_you_require_a_mattress_to_sleep_over">Require Mattress?</label>
                        <select class="form-control" class="form-control" id="do_you_require_a_mattress_to_sleep_over" name="do_you_require_a_mattress_to_sleep_over"
                            required>

                            <option value='<?= $do_you_require_a_mattress_to_sleep_over?>' selected><?= $do_you_require_a_mattress_to_sleep_over?>
                            </option>
                            <option value="Yes">Yes</option>
                            <option value="No">No</option>
                        </select>
                    </div>
                </div>

                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="if_yes_click_a_range">Acommodation Range</label>
                        <select class="form-control" class="form-control" id="if_yes_click_a_range-existing" name="if_yes_click_a_range"
                            required>

                            <option value='<?= $if_yes_click_a_range?>' selected><?= $if_yes_click_a_range?>
                            </option>
                            <option value="less than N15k">less than N15k</option>
                            <option value="N20K">N20K</option>
                            <option value="N30k">N30K</option>
                            <option value="N50K">N50K</option>
                            <option value="N60K">N60K</option>
                            <option value="Not Required">Not Required</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="how_long_have_you_been_canonically_married">Marriage Duration</label>
                        <select class="form-control" class="form-control" id="how_long_have_you_been_canonically_married" name="how_long_have_you_been_canonically_married"
                            required>

                            <option value='<?= $how_long_have_you_been_canonically_married?>' selected><?= $how_long_have_you_been_canonically_married?></option>
                            <option value="below 30 years"> below 30 years</option>
                            <option value="30 - 40 years">30 - 40 years</option>
                            <option value="45-50 years">45-50 years</option>
                            <option value="above 50 years">above 50 years</option>
                            <option value="I am single">I am single</option>
                        </select>
                    </div>
                </div>
                

                <button type="submit" class="btn btn-success px-4 mt-4" name="update">Update</button>
            </form>
        </div>
    </div>
    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <!-- jQuery Library -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- Font Awesome JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/js/all.min.js"></script>
</body>

</html>