<?php 
session_start();

// 1. Access Control
if(!isset($_SESSION['customer_login'])) {
    header('location:index.php');
    exit;
}

// 2. Database Connection & Data Retrieval
include '_inc/dbconn.php';

$cust_id = $_SESSION['cust_id'];

// Handle Profile Update Request
if(isset($_POST['update_profile'])) {
    $new_name = mysqli_real_escape_string($con, $_POST['fullname']);
    $new_email = mysqli_real_escape_string($con, $_POST['email']);
    
    // Update query targeting the 'users' table
    $update_sql = "UPDATE users SET fullname='$new_name', email='$new_email' WHERE id='$cust_id'";
    
    if(mysqli_query($con, $update_sql)) {
        echo "<script>alert('Profile updated successfully');</script>";
    } else {
        echo "<script>alert('Error updating profile');</script>";
    }
}

// Fetch fresh data from 'users' table
$sql = "SELECT * FROM users WHERE id='$cust_id'";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$rws = mysqli_fetch_assoc($result);

$name         = $rws['fullname'] ?? '';
$account_no   = $rws['account_number'] ?? '';
$email        = $rws['email'] ?? '';
$acc_status   = ($rws['verified'] == 1) ? 'Active' : 'Pending';
$initials     = !empty($name) ? strtoupper(substr($name, 0, 1)) : 'U';

include 'header.php'; 
include 'customer_navbar.php'; 
?>

<div class="main-content-wrapper" style="margin-left: 260px; transition: all 0.3s ease;">
    <header class="top-navbar px-4 py-3 d-flex justify-content-between align-items-center bg-white shadow-sm">
        <div class="d-flex align-items-center">
            <button class="btn btn-light d-lg-none mr-3" id="sidebarToggle"><i class="mdi mdi-menu"></i></button>
            <h5 class="mb-0 font-weight-bold text-dark">Edit Profile</h5>
        </div>
    </header>

    <div class="content-body p-4" style="background: #F8FAFC; min-height: 100vh;">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-4">
                    <div class="card border-0 shadow-sm text-center p-4 mb-4" style="border-radius: 20px;">
                        <div class="mx-auto bg-soft-primary d-flex align-items-center justify-content-center mb-3 shadow-sm" style="width: 100px; height: 100px; border-radius: 50%; background: #E6F0FF;">
                            <h1 class="text-primary font-weight-bold mb-0"><?php echo $initials; ?></h1>
                        </div>
                        <h4 class="font-weight-bold text-dark mb-1"><?php echo $name; ?></h4>
                        <div class="mb-4">
                            <span class="badge <?php echo ($acc_status == 'Active') ? 'badge-success' : 'badge-warning'; ?> px-3 py-2" style="border-radius: 8px;">
                                <?php echo strtoupper($acc_status); ?> ACCOUNT
                            </span>
                        </div>
                        <div class="text-left border-top pt-4">
                            <label class="text-muted small font-weight-bold text-uppercase mb-0">Account Number</label>
                            <p class="text-dark font-weight-bold"><?php echo $account_no; ?></p>
                        </div>
                    </div>
                </div>

                <div class="col-lg-8">
                    <div class="card border-0 shadow-sm mb-4" style="border-radius: 20px;">
                        <div class="card-header bg-white border-0 pt-4 px-4">
                            <h5 class="font-weight-bold text-dark mb-0">Account Information</h5>
                        </div>
                        <div class="card-body px-4 pb-4">
                            <form action="" method="POST">
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="small font-weight-bold text-muted">Full Name</label>
                                        <input type="text" name="fullname" class="form-control border-light" value="<?php echo $name; ?>" style="border-radius: 10px;" required>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label class="small font-weight-bold text-muted">Email Address</label>
                                        <input type="email" name="email" class="form-control border-light" value="<?php echo $email; ?>" style="border-radius: 10px;" required>
                                    </div>
                                    <div class="col-md-12 mb-3">
                                        <label class="small font-weight-bold text-muted">Account Number (View Only)</label>
                                        <input type="text" class="form-control bg-light border-0" value="<?php echo $account_no; ?>" style="border-radius: 10px;" readonly>
                                    </div>
                                </div>
                                <button type="submit" name="update_profile" class="btn btn-primary px-5 font-weight-bold" style="border-radius: 10px; height: 45px;">
                                    Save Changes
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .badge-success { background-color: #DEF7EC; color: #03543F; }
    .badge-warning { background-color: #FEF3C7; color: #92400E; }
    .form-control:focus { box-shadow: none; border-color: #0052FF; }
    @media (max-width: 992px) { .main-content-wrapper { margin-left: 0 !important; } }
</style>

<?php include 'footer.php'; ?>
