<?php
require_once("../includes/session.php");
require_once("../includes/config.php");
if (!checkAdmin($udata['type'])) exit;

if (isset($_POST['update_status'])) {
    $id = intval($_POST['request_id']);
    $action = $_POST['action'];
    $rec = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM dl_to_mobile_requests WHERE id=$id"));
    
    if ($action == 'success') {
        mysqli_query($ahk_conn, "UPDATE dl_to_mobile_requests SET status='Success', completed_date=NOW() WHERE id=$id");
        echo '<script>Swal.fire("Success!", "Done", "success"); setTimeout(function(){ location.reload(); }, 1500);</script>';
    } else {
        $userInfo = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT balance FROM users WHERE phone='{$rec['user_id']}'"));
        $new_balance = floatval($userInfo['balance']) + floatval($rec['amount']);
        mysqli_query($ahk_conn, "UPDATE users SET balance=$new_balance WHERE phone='{$rec['user_id']}'");
        mysqli_query($ahk_conn, "UPDATE dl_to_mobile_requests SET status='Rejected', rejection_reason='{$_POST['rejection_reason']}', completed_date=NOW() WHERE id=$id");
        echo '<script>Swal.fire("Refunded", "Done!", "success"); setTimeout(function(){ location.reload(); }, 1500);</script>';
    }
}

$status = $_GET['status'] ?? 'Pending';
$records = mysqli_query($ahk_conn, "SELECT * FROM dl_to_mobile_requests WHERE status='$status' ORDER BY id DESC LIMIT 100");

include('../template/ahkweb/header.php');
?>

<div class="page-wrapper">
    <div class="page-content">
        <div class="card">
            <div class="card-header bg-primary text-white">DL Mobile Link Admin</div>
            <div class="card-body p-2">
                <table class="table table-sm">
                    <thead><tr><th>#</th><th>Order</th><th>User</th><th>DL No</th><th>DOB</th><th>New Mobile</th><th>Action</th></tr></thead>
                    <tbody>
                        <?php $sr=1; while($rec = mysqli_fetch_assoc($records)): ?>
                        <tr>
                            <td><?=$sr++?></td>
                            <td><?=$rec['order_id']?></td>
                            <td><?=$rec['username']?></td>
                            <td><?=$rec['dl_number']?></td>
                            <td><?=$rec['dob']?></td>
                            <td><?=$rec['new_mobile_number']?></td>
                            <td>
                                <?php if($rec['status']=='Pending'): ?>
                                <form method="POST" class="d-inline">
                                    <input type="hidden" name="request_id" value="<?=$rec['id']?>">
                                    <input type="hidden" name="action" value="">
                                    <button type="submit" name="update_status" class="btn btn-sm btn-success" onclick="this.form.action.value='success'">✅</button>
                                    <button type="button" class="btn btn-sm btn-danger" onclick="$('#rej<?=$rec['id']?>').toggle()">❌</button>
                                    <div id="rej<?=$rec['id']?>" style="display:none;margin-top:5px;">
                                        <input type="text" name="rejection_reason" placeholder="Reason" class="form-control-sm">
                                        <button type="submit" name="update_status" class="btn btn-danger btn-sm mt-1" onclick="this.form.action.value='reject'">Reject</button>
                                    </div>
                                </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include('../admin/footers.php'); ?>