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

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

    $sql = "SELECT * FROM  web_app_enrollee_data WHERE id = '$id'";
    $result = mysqli_query($conn, $sql);
    $result_checker = mysqli_num_rows($result);
    if ($result_checker > 0) {
        while ($data = mysqli_fetch_assoc($result)) {
            // $enrollment_serial_no = $data['enrollment_serial_no'];
            // $company_name = $data['company_name'];
            // $surname = $data['surname'];
            // $first_name = $data['first_name'];
            $hmo_id = $data['hmo_id'];
            
            
            $enrollment_serial_no = $data["enrollment_serial_no"];
            $hmo_id = $data["hmo_id"];
            $principal_id = $data["principal_id"];
            $company_name = $data["company_name"];
            $first_name = $data["first_name"];
            $middle_name = $data["middle_name"];
            $surname = $data["surname"];
            $phone_number = $data["phone_number"];
            $email_address = $data["email_address"];
            $employee_id = $data["employee_id"];
            $gender = $data["gender"];
            $date_of_birth = $data["date_of_birth"];
            $type_of_plan = $data["type_of_plan"];
            $group_of_plan = $data["group_of_plan"];
            $address_state = $data["address_state"];
            $lga = $data["lga"];
            $address_city = $data["address_city"];
            $address_street = $data["address_street"];
            $hospitals = $data["hospitals"];
            $genotype = $data["genotype"];
            $blood_group = $data["blood_group"];
            $allergies = $data["allergies"];
            $has_pre_existing_condition = $data["has_pre_existing_condition"];
            $pre_existing_condition = $data["pre_existing_condition"];
            $has_surgical_history = $data["has_surgical_history"];
            $surgical_history = $data["surgical_history"];
            $status = $data["status"];
            $passport_link = $data["Passport_link"];
            $hmo_level = $data["hmo_level"];
        }
    }

    $sql = "SELECT company, link, company_code, groupplan FROM web_app_enrollment_form Where company = '$company_name'";
        $result = $conn->query($sql);
        
        
        // Check if there are any rows returned
        if ($result->num_rows > 0) {
            // Output data of each row
            while($row = $result->fetch_assoc()) {
                $company=$row["company"];
                $link=$row["link"];
                $company_code= $row["company_code"];
                $groupplan=$row["groupplan"];
                
            }
        } 
                    

} 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);
    }
    </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'>Passport Update</h2>
            
            <div style="margin: 0 auto; padding:20px 0;">
                <!--passport-->
               
               <div>
                    <img id="profilePic" src="<?php echo $passport_link; ?>" alt="Profile Picture" style="margin:auto; width: 200px;">
                    <br>
                    <form id="profileForm" action="./addnew/savepassport.php" method="post" enctype="multipart/form-data">
                        <input type="hidden" name="id" value="<?=$id?>">
                        <input type="hidden" name="enrollment_serial_no" value="<?=$enrollment_serial_no?>">
                        <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>
                
                <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]);
                        };
                    }
                </script>

            </div>

            
            <h2 class='text-center text-primary'>Update Other 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">
                        <!--<label for="group">Principal</label>-->
                        <input type="hidden" class="form-control" name="principal"
                            value="<?=$first_name. ' '. $surname?>" disabled>
                    <div class="form-group col-md-6  py-2">
                        <label for="group">Group</label>
                        <input type="text" class="form-control" name="group" value="<?= $company_name ?>">
                    </div>
                    
                    <!-- hidden  -->
                    <input type="hidden" name="id" value="<?=$id?>">
                    <input type="hidden" name="company_code" value="<?=$company_code?>">
                    <div class="form-group col-md-6">
                        <label for="firstName">First Name</label>
                        <input type="text" class="form-control" id="firstName" name="firstname" value="<?=$first_name?>"
                            required placeholder="First Name">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="lastName">Last Name</label>
                        <input type="text" class="form-control" id="lastName" name="lastname" value="<?= $surname?>"
                            required placeholder="Last Name">
                    </div>
                    
                    <div class="form-group col-md-6">
                        <label for="middlename">Middle Name</label>
                        <input type="text" class="form-control" id="lastName" name="middlename"
                            value="<?=$middle_name?>" placeholder="Last Name">
                    </div>
                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6  py-2">
                        <label for="hmo_level">HMO Level</label>
                        <select id="gender" class="form-control" name="hmo_level" required>
                            <option value='<?= $hmo_level?>' selected><?= $hmo_level?></option>
                            <option value='Principal'>Principal</option>
                            <option value='Spouse'>Spouse</option>
                            <option value='First Dependant'>First Dependant</option>
                            <option value='Second Dependant'>Second Dependant</option>
                            <option value='Third Dependant'>Third Dependant</option>
                            <option value='Fourth Dependant'>Fourth Dependant</option>
                            <option value='Fifth Dependant'>Fifth Dependant</option>
                            <option value='Sixth Dependant'>Sixth Dependant</option>
                            <option value='Seventh Dependant'>Seventh Dependant</option>
                            <option value='Eighth Dependant'>Eighth Dependant</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="phoneNumber">Phone Number</label>
                        <input type="tel" class="form-control" id="phoneNumber" name="phonenumber"
                            placeholder="Phone Number" value="<?=$phone_number?>">
                    </div>

                </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_address?>">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="Employee ID">Employee ID</label>
                        <input type="Employee ID" class="form-control" id="Employee ID" name="employeeid" placeholder="Employee ID"
                            value="<?=$employee_id?>">
                    </div>
                    <!-- <div class="form-group col-md-6">
                        <label for="dependant">Dependant Type</label>
                        <select id="dependant" class="form-control" name="dependant" required>
                            <option value='' selected></option>
                            <option value='B'>Spouse</option>
                            <option value='C'>First Dependant</option>
                            <option value='D'>Second Dependant</option>
                            <option value='E'>Third Dependant</option>
                            <option value='F'>Fourth Dependant</option>
                        </select>
                    </div> -->
                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="gender">Gender</label>
                        <select id="gender" class="form-control" name="gender" required>
                            <option value='<?= $gender?>' selected><?= $gender?></option>
                            <option>Male</option>
                            <option>Female</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <?php
                             $formatted_date = date("Y-m-d", strtotime(str_replace('/', '-', $date_of_birth)));
                        ?>
                        <label for="dob">Date Of Birth</label>
                        <input type="date" class="form-control" id="dob" name="dob" value="<?= $formatted_date?>"
                            required>
                    </div>


                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="state">State</label>
                        <select class="form-control select2" id="state" onchange="populateLocalGovernments()" name="state"
                            required>
                            <option value='<?= $address_state?>' selected><?= $address_state?></option>
                            <option value="Abia">Abia</option>
                            <option value="Adamawa">Adamawa</option>
                            <option value="Abuja">Abuja</option>
                            <option value="AkwaIbom">Akwa Ibom</option>
                            <option value="Anambra">Anambra</option>
                            <option value="Bauchi">Bauchi</option>
                            <option value="Bayelsa">Bayelsa</option>
                            <option value="Benue">Benue</option>
                            <option value="Borno">Borno</option>
                            <option value="CrossRiver">Cross River</option>
                            <option value="Delta">Delta</option>
                            <option value="Ebonyi">Ebonyi</option>
                            <option value="Edo">Edo</option>
                            <option value="Ekiti">Ekiti</option>
                            <option value="Enugu">Enugu</option>
                            <option value="Gombe">Gombe</option>
                            <option value="Imo">Imo</option>
                            <option value="Jigawa">Jigawa</option>
                            <option value="Kaduna">Kaduna</option>
                            <option value="Kano">Kano</option>
                            <option value="Katsina">Katsina</option>
                            <option value="Kebbi">Kebbi</option>
                            <option value="Kogi">Kogi</option>
                            <option value="Kwara">Kwara</option>
                            <option value="Lagos">Lagos</option>
                            <option value="Nassarawa">Nassarawa</option>
                            <option value="Niger">Niger</option>
                            <option value="Ogun">Ogun</option>
                            <option value="Ondo">Ondo</option>
                            <option value="Osun">Osun</option>
                            <option value="Oyo">Oyo</option>
                            <option value="Plateau">Plateau</option>
                            <option value="Rivers">Rivers</option>
                            <option value="Sokoto">Sokoto</option>
                            <option value="Taraba">Taraba</option>
                            <option value="Yobe">Yobe</option>
                            <option value="Zamfara">Zamfara</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="lga">Local Government</label>
                        <select class="form-control" name="lga" id="local-government">
                            <option value='<?= $lga?>' selected><?= $lga?></option>
                        </select>
                        <script src="./statelga.js"></script>
                    </div>
                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="City">City</label>
                        <input type="text" class="form-control" id="city" name="city" value="<?=$address_city?>"
                             placeholder="">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="address">Address</label>
                        <input type="text" class="form-control" id="address" name="address" value="<?=$address_street?>"
                            required placeholder="Address">
                    </div>

                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="planType">Plan Type</label>
                        <select class="form-control" class="form-control" id="planType" name="plantype" required>
                            <option value='<?= $type_of_plan?>' selected><?= $type_of_plan?></option>
                            <option value="Flex Plan"> Flex Plan </option>
            				<option value="Executive Plan"> Executive Plan </option>
            				<option value="Upper Plan"> Upper Plan </option>
            				<option value="Business Plan"> Business Plan </option>
            				<option value="Club Plan"> Club Plan </option>
            				<option value="Social Plan"> Social Paln </option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="roup Type">Group Type</label>
                        <select class="form-control" class="form-control" id="groupType" name="grouptype" required>
                            <option value='<?= $group_of_plan?>' selected><?= $group_of_plan?></option>
                            <option value="Individual">Individual</option>
                            <option value="Family">Family</option>
                        </select>
                    </div>
                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-12">
                        <label for="selectedProvider">Selected Provider</label>
                        <select class="form-control" class="form-control" id="provider" name="provider" required>
                            <option value='<?= $hospitals?>' selected><?= $hospitals?></option>
                            <?php
                                // Fetch data from the table
                                $sql = "SELECT provider_name FROM web_app_provider_list";
                                $result = $conn->query($sql);
                                
                                
                                // Check if there are any rows returned
                                if ($result->num_rows > 0) {
                                    // Output data of each row
                                    while($row = $result->fetch_assoc()) {
                                        $provider=$row["provider_name"];
                                        echo "<option value='" . $provider . "'>" . $provider . "</option>";
                                        
                                    }
                                } else {
                                    echo "<option value=''>No data available</option>";
                                }
                                                    
                            ?>
                        </select>
                    </div>
                </div>



                <div class="form-row row py-2">
                    <div class="form-group col-md-12">
                        <label for="status">Status</label>
                        <select class="form-control" class="form-control" id="status" name="status" required>
                            <option value='<?= $status?>' selected><?= $status?></option>
                            <option value="Approved">Approved</option>
                            <option value="Pending">Pending</option>
                            <option value="Deactivated">Deactivated</option>
                            <option value="Denied">Denied</option>
                        </select>
                    </div>
                    <!--<div class="form-group col-md-6">-->
                    <!--    <label for="passport">Passport</label>-->
                    <!--    <input type="file" class="form-control form-control-file" id="passport" name="passport">-->
                    <!--</div>-->
                </div>

                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="Genotype">Genotype</label>
                        <select class="form-control" class="form-control" id="Genotype" name="genotype">
                            <option value='<?= $genotype?>' selected><?= $genotype?></option>
                            <option value="AA">AA</option>
                            <option value="AS">AS</option>
                            <option value="SS">SS</option>
                            <option value="AC">AC</option>
                            <option value="SC">SC</option>
                            <option value="CC">CC</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="bloodgroup">Blood Group</label>
                        <select class="form-control" class="form-control" id="bloodgroup" name="bloodgroup">
                            <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-12">
                        <label for="allergies">Allergies</label>
                        <input type="text" class="form-control" id="allergies" name="allergies"
                            placeholder="Any Allergies" value='<?= $allergies?>'>
                    </div>
                </div>
                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="pre-existing">Has Pre-Existing Condition</label>
                        <select class="form-control" class="form-control" id="has-pre-existing" name="haspreexisting"
                            required>

                            <option value='<?= $has_pre_existing_condition?>' selected><?= $has_pre_existing_condition?>
                            </option>
                            <option value="Yes">Yes</option>
                            <option value="No">No</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="pre-existing">Pre-Existing Condition</label>
                        <input type="text" class="form-control" id="pre-existing" name="preexisting"
                            placeholder="Pre-Existing Condition" <?= $pre_existing_condition?>>
                    </div>
                </div>


                <div class="form-row row py-2">
                    <div class="form-group col-md-6">
                        <label for="surgicalhist">Has Surgical History</label>
                        <select class="form-control" class="form-control" id="hassurgicalhist" name="hassurgicalhist"
                            required>
                            <option value='<?= $has_surgical_history?>' selected><?= $has_surgical_history?></option>
                            <option value="Yes">Yes</option>
                            <option value="No">No</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="Surgical-History">Surgical History</label>
                        <input type="text" class="form-control" id="SurgicalHistory" name="surgicalhist"
                            placeholder="Surgical History" <?= $surgical_history?>>
                    </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>