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

if ($udata['id'] != '1' || $udata['type'] != 'admin') {
    echo '<script>
alert("Access Denied! Only Admin can view this page.");
window.location.href = "../includes/logout.php";
</script>';
    exit;
}


if(isset($_GET['id']) && $_GET['id'] != NULL){
    $id = base64_decode(getSafe($_GET['id']));
    $data = fetchRow("SELECT * FROM users WHERE id='$id'");
}



if(isset($_POST['name']) && isset($_POST['phone'])){
    $id = getSafe($_POST['id']);
    $result = updateUserData($id, $_POST, $fields, $fee_fields, $status_only_fields);
    ?>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        Swal.fire({
            icon: '<?php echo $result['success'] ? "success" : "error"; ?>',
            title: '<?php echo $result['success'] ? "Success!" : "Failed!"; ?>',
            text: '<?php echo $result['message']; ?>',
            confirmButtonText: 'OK'
        }).then((res) => {
            if(res.isConfirmed && <?php echo $result['success'] ? "true" : "false"; ?>){
                window.location.href = 'users.php'; 
            }
        });
    </script>
    <?php
}
?>


<div class="page-wrapper">
<div class="page-content">
<div class="row">
<div class="col-xl-12 mx-auto">
        <div class="mb-4">
        <a href="javascript:history.back()" class="back-btn">
            <i class="bi bi-arrow-left-circle"></i> Go Back
        </a>
    </div>
<h6 class="mb-0 text-uppercase">Edit user data</h6>
<hr/>
<div class="card border-top border-0 border-4 border-primary">
<div class="card-body p-5">
<form action="" method="POST" class="row g-3">
<input type="hidden" name="id" value="<?php echo $data['id']; ?>">


<div class="col-md-3">
    <label class="form-label">Name</label>
    <input name="name" type="text" value="<?php echo $data['name']; ?>" class="form-control">
</div>
<div class="col-md-3">
    <label class="form-label">Phone</label>
    <input name="phone" type="text" value="<?php echo $data['phone']; ?>" class="form-control">
</div>
<div class="col-md-3">
    <label class="form-label">Email</label>
    <input name="email" type="email" value="<?php echo $data['email']; ?>" class="form-control">
</div>
<div class="col-md-3">
    <label class="form-label">Status</label>
    <select class="form-control" name="status">
        <option value="">Select Status</option>
        <option value="1" <?php if($data['status']=='1') echo 'selected'; ?>>Active</option>
        <option value="0" <?php if($data['status']=='0') echo 'selected'; ?>>Inactive</option>
    </select>
</div>
<div class="col-md-3">
    <label class="form-label">Usertype</label>
    <select class="form-control" name="type">
        <option value="">Select User Type</option>
        <option value="retailer" <?php if($data['type']=='retailer') echo 'selected'; ?>>Retailer</option>
        <option value="distributor" <?php if($data['type']=='distributor') echo 'selected'; ?>>Distributor</option>
    </select>
</div>

<hr>
<h5>🔧 Service Control (On/Off + Price)</h5>
<?php foreach($fields as $i => $statusField): 
    $feeField = $fee_fields[$i];
?>
<div class="col-md-3">
    <label class="form-label" style="
    color: #DC3545;      /* Bootstrap Red */
    font-weight: 600;     /* Bold but readable */
    font-size: 14px;      /* Clean readable size */
    margin-bottom: 5px;   /* Thodi spacing niche */
    display: block;       /* Har label alag line me */
    letter-spacing: 0.5px;/* Stylish touch */
">
    <?php echo ucwords(str_replace("_"," ", $feeField)); ?>
</label>

    <select name="<?php echo $statusField; ?>" class="form-control">
        <option value="on" <?php if($data[$statusField]=='on') echo 'selected'; ?>>On</option>
        <option value="off" <?php if($data[$statusField]=='off') echo 'selected'; ?>>Off</option>
    </select>
    <input type="number" step="0.01" name="<?php echo $feeField; ?>" 
           value="<?php echo $data[$feeField]; ?>" class="form-control mt-1" placeholder="Price">
</div>
<?php endforeach; ?>

<hr>
<h5>🔧 Status Only Services</h5>
<?php foreach($status_only_fields as $statusField): ?>
<div class="col-md-3">
   <label class="form-label" style="
    color: #1E90FF;      /* Dodger Blue, thoda bright aur clean */
    font-weight: 600;     /* Bold but not too heavy */
    font-size: 14px;      /* Clean readable size */
    margin-bottom: 5px;   /* Thodi spacing niche */
    display: block;       /* Har label alag line me */
    letter-spacing: 0.5px;/* Thoda stylish touch */
">
    <?php echo ucwords(str_replace("_"," ", $statusField)); ?>
</label>

    <select name="<?php echo $statusField; ?>" class="form-control">
        <option value="on" <?php if($data[$statusField]=='on') echo 'selected'; ?>>On</option>
        <option value="off" <?php if($data[$statusField]=='off') echo 'selected'; ?>>Off</option>
    </select>
</div>
<?php endforeach; ?>

<div class="col-12 mt-3">
    <button type="submit" class="btn btn-primary px-5">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>

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


<script src="../template/tngweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/tngweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/tngweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/tngweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/tngweb/assets/js/app.js"></script>
</body>
</html>
