<?php
error_reporting(0);
$servername = "localhost"; // Your server name
$username = "root"; // Your database username
$password = ""; // Your database password
$dbname = "fsibplc_account_opening"; // Your database name


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);



// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['id'])) {
        $id = $_POST['id'];

        $date_time = date('d/m/y h:m:s a');
        //echo $date_time ;
        // echo "hello";
        // die();

        $select_sql = "SELECT  branch.branch_name,branch.branch_id, account_type.name as  'ac_name',account_type.code as 'ac_code', account_info.`name`,
`father_name`, `mother_name`, `dob`, `occupation`, `mobile`, `email`, `nationality`, `present_address`,
 `permanent_address`, `gender`, `nominee_name`, `relation`, `share`, `picture`,
 `user_front`, `user_back`, `nominee_front`, `nominee_back`, `status`, `ac_number`, `sign`,`inform` FROM `account_info` INNER JOIN branch ON account_info.branch = branch.branch_id INNER JOIN account_type on ac_type = account_type.code WHERE account_info.id=$id";


        $single_result = $conn->query($select_sql);





        if (isset($_POST['acc_update_btn'])) {
            $acc_no = $conn->real_escape_string(trim($_POST['acc_number']));
            // $cust_informed = $conn->real_escape_string(trim($_POST['cust_informed']));
            $cust_informed = isset($_POST['cust_informed']) ? "yes" : "";

            $data_fetch_sql = "select * from activity_log_tbl where  app_id=$id";
            $fetch_result = $conn->query($data_fetch_sql);
            $row_count = $fetch_result->num_rows;



            if (empty($cust_informed)) {

                $update_sql = "update account_info set status = 'Approved', ac_number='$acc_no' where id=$id";

                $updated_log_sql = "INSERT INTO activity_log_tbl(app_id,acc_open_id,acc_open_time) VALUES('$id','$user_id','$date_time')";
                $updated_log_sql_result = $conn->query($updated_log_sql);
            } else {

                $update_sql = "update account_info set status = 'Approved', ac_number='$acc_no', inform = '$cust_informed' where id=$id";
            }

            //echo $update_sql;
            $update_result = $conn->query($update_sql);
            $updated_row = $conn->affected_rows;



            if (!empty($cust_informed)) {
                if ($row_count > 0) {
                    $updated_informed_log = "UPDATE activity_log_tbl set informed_id='$user_id',informed_time= '$date_time' where app_id = '$id'";
                    $updated_informed_log_result = $conn->query($updated_informed_log);
                }
            }



            if ($updated_row > 0) {
                echo '<script>alert("Account Number Updated Successfully!!!");</script>';
                echo '<script>window.top.location="account_show.php";</script>';
            }
        }
    }
}

// Initialize variables for applicant details
// $applicant = null;

// Check if a form has been submitted to view applicant details
// if ($_SERVER["REQUEST_METHOD"] == "POST") {
//     if (isset($_POST['id'])) {
//         $id = $_POST['id'];

// Fetch the applicant details
//     $sql = "SELECT name, father_name, mother_name, dob, status, inform FROM account_info WHERE id = ?";
//     $stmt = $conn->prepare($sql);
//     $result = $stmt->execute();
//     $result = $stmt->get_result();

//     if ($result->num_rows > 0) {
//         $applicant = $result->fetch_assoc();
//     }
//     $stmt->close();
// }

// Check if the form to update details has been submitted
// if (isset($_POST['ac_number']) && isset($_POST['update_inform'])) {
//     $ac_number = $_POST['ac_number'];
//     $inform = isset($_POST['inform']) ? "yes" : "NULL"; // Checkbox value (1 for checked, 0 for unchecked)
//     if($ac_number){
//         $status = "Approved";
//     }
//     else{
//         $status = "NULL";
//     }

// Update the account_info table
// $update_sql = "UPDATE account_info SET ac_number = ?, account_info.status = ?, inform = ? WHERE id = ?";
// $update_stmt = $conn->prepare($update_sql);
// $update_stmt->bind_param("siii", $ac_number,$status, $inform, $id);
// $update_stmt->execute();
// $update_stmt->close();

// Optionally, you can fetch the updated applicant details again
// to show the latest information.
//     }
// }

// Fetch all applicants for the table
$sql = "SELECT id, name, father_name, mother_name, dob, status, inform FROM account_info";
$result = $conn->query($sql);
?>

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Applicant Information</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }

        th {
            background-color: #f2f2f2;
        }
    </style>
</head>

<body>
    <h1>Applicant Information</h1>
    <table>
        <thead>
            <tr>
                <th>SL</th>
                <th>Name</th>
                <th>Father Name</th>
                <th>Mother Name</th>
                <th>Date Of Birth</th>
                <th>Status</th>
                <th>Informed</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($result->num_rows > 0) {
                $index = 1;
                while ($row = $result->fetch_assoc()) {
                    echo "<tr>
                        <td>{$index}</td>
                        <td>{$row['name']}</td>
                        <td>{$row['father_name']}</td>
                        <td>{$row['mother_name']}</td>
                        <td>{$row['dob']}</td>
                        <td>{$row['status']}</td>
                        <td>{$row['inform']}</td>
                        <td>
                            <form method='POST'>
                                <input type='hidden' name='id' value='{$row['id']}'>
                                <input type='submit' value='View'>
                            </form>
                        </td>
                    </tr>";
                    $index++;
                }
            } else {
                echo "<tr><td colspan='8'>No applicants found.</td></tr>";
            }
            ?>
        </tbody>
    </table>



    <?php
    if ($single_result->num_rows > 0) {
        // $index = 1;
        while ($cus_details = $single_result->fetch_assoc()) {
            print_r($cus_details);
    ?>
            <h2>Customer Details</h2>
            <p><strong>Name:</strong> <?php echo $cus_details['name']; ?></p>
            <p><strong>Father Name:</strong> <?php echo $cus_details['father_name']; ?></p>
            <p><strong>Mother Name:</strong> <?php echo $cus_details['mother_name']; ?></p>
            <p><strong>Date Of Birth:</strong> <?php echo $cus_details['dob']; ?></p>
            <p><strong>Status:</strong> <?php echo $cus_details['status']; ?></p>
            <p><strong>Informed:</strong> <?php echo $cus_details['inform']; ?></p>

            <h3>Update Information</h3>
            <form method="POST">
                <input type="hidden" name="id" value="<?php echo $id; ?>">
                <label for="ac_number">Account Number:</label>
                <input type="text" id="numberInput" class="form-control" name="acc_number" minlength="15" maxlength="15"
                    onkeypress="return event.key >= '0' && event.key <= '9' && this.value.length < 15;"

                    value="<?php if ($cus_details['ac_number']) {
                                echo $cus_details['ac_number'];
                            } else {
                                echo '';
                            } ?>"
                    required>
                <br>
                <label for="inform">Informed:</label>
                <input type="checkbox" class="" id="cust_informed" name="cust_informed">

                <br>
                <input type="submit" class="btn btn-success"
                    name="acc_update_btn">
            </form>

    <?php
        }
    } else {
        echo "<tr><td colspan='8'>No customer details found.</td></tr>";
    }

    $conn->close(); // Close the database connection
    ?>
</body>

</html>