<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');

$session_phone = $_SESSION['phone'];



/* ================= HANDLE POST ================= */
if($_SERVER['REQUEST_METHOD']==='POST'){

    if(isset($_POST['curpass'])){
        $r = change_password(
            $tng_conn,
            $session_phone,
            $_POST['curpass'],
            $_POST['npass'],
            $_POST['cpass']
        );

        echo "<script>
            Swal.fire('{$r['title']}','{$r['text']}','".($r['status']?'success':'error')."');
        </script>";
    }

    if(isset($_POST['name'])){
        $r = update_profile($tng_conn,$session_phone,$_POST);

        echo "<script>
    Swal.fire({
        icon: '".($r['status'] ? 'success' : 'error')."',
        title: '{$r['title']}',
        text: '{$r['text']}',
        timer: 1500,
        showConfirmButton: false
    });
    ".($r['status'] ? "setTimeout(()=>{ window.location.href='profile.php'; },1500);" : "")."
</script>";

    }
}
?>

<!-- =================== UI =================== -->

<div class="page-wrapper">
<div class="page-content">
<div class="row">

<!-- PROFILE CARD -->
<div class="col-lg-4">
<div class="card shadow border-0 text-center">
<div class="card-body">
<img src="<?= XyZ_val('logo'); ?>" class="rounded-circle p-1 bg-primary" width="110">
<h4 class="mt-3"><?= $udata['name']; ?></h4>
<p class="text-muted"><?= XyZ_val('brand'); ?></p>
<hr>
<ul class="list-group list-group-flush text-start">
<li class="list-group-item"><b>Phone :</b> <?= $udata['phone']; ?></li>
<li class="list-group-item"><b>Email :</b> <?= $udata['email']; ?></li>
<li class="list-group-item"><b>Balance :</b> ₹<?= $udata['balance']; ?></li>
</ul>
</div>
</div>
</div>

<!-- PROFILE EDIT -->
<div class="col-lg-8">
<div class="card shadow border-0">
<div class="card-body">
<h5 class="mb-3 text-primary fw-bold">Edit Profile</h5>

<form method="POST">

<div class="mb-3">
<label>Full Name</label>
<input type="text" name="name" value="<?= $udata['name']; ?>" class="form-control" required>
</div>

<div class="mb-3">
<label>Email</label>
<input type="email" name="email" value="<?= $udata['email']; ?>" class="form-control" required>
</div>

<div class="mb-3">
<label>Phone</label>
<input type="text" class="form-control" value="<?= $udata['phone']; ?>" readonly>
</div>

<div class="mb-3">
<label>Address</label>
<input type="text" name="address" value="<?= $udata['address']; ?>" class="form-control">
</div>

<div class="mb-3">
<label>OTP System</label>
<select name="otp_system" class="form-control">
<option value="yes" <?= $udata['otp_system']=='yes'?'selected':''; ?>>Yes</option>
<option value="no" <?= $udata['otp_system']=='no'?'selected':''; ?>>No</option>
</select>
</div>

<button class="btn btn-primary px-4">Save Profile</button>
</form>

<hr>

<h5 class="mt-4 text-danger fw-bold">Change Password</h5>

<form method="POST">

<input type="password" name="curpass" class="form-control mb-2" placeholder="Current Password" required>
<input type="password" name="npass" class="form-control mb-2" placeholder="New Password" required>
<input type="password" name="cpass" class="form-control mb-2" placeholder="Confirm Password" required>

<button class="btn btn-danger px-4">Change Password</button>

</form>

</div>
</div>
</div>

</div>
</div>
</div>

<?php include('../template/tngweb/footer.php'); ?>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="../template/tngweb/assets/js/app.js"></script>

<style>
.card{border-radius:16px}
.btn{border-radius:50px}
</style>
