<?php
session_start();
require_once __DIR__ . "/includes/config.php";

if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin – User Control</title>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>
body{margin:0;font-family:Segoe UI;background:#0f172a;color:#fff}
header{background:#020617;padding:15px 20px;display:flex;align-items:center;gap:15px}
.back-btn{cursor:pointer;padding:8px 12px;background:#1e293b;border-radius:8px}
.container{padding:25px;max-width:1000px;margin:auto}
.box{background:#111827;padding:25px;border-radius:14px;margin-bottom:20px}
input{width:100%;padding:12px;border-radius:8px;border:none;margin-top:10px}
button{padding:12px 18px;border:none;border-radius:8px;cursor:pointer}
.btn-blue{background:#2563eb;color:white}
.btn-red{background:#dc2626;color:white}
.btn-green{background:#16a34a;color:white}
.btn-gray{background:#475569;color:white}
</style>
</head>

<body>

<header>
    <div class="back-btn" onclick="history.back()">⬅</div>
    <h2>User Management</h2>
</header>

<div class="container">

<div class="box">
    <h3>Search User</h3>
    <input type="text" id="mobile" placeholder="Enter Mobile">
    <br><br>
    <button class="btn-blue" onclick="searchUser()">Search</button>
</div>

<div class="box" id="result">
    <center style="opacity:.6">Search user to view details</center>
</div>

</div>

<script>
function searchUser(){
    let mobile = document.getElementById("mobile").value;

    fetch("deletebank_ajax.php", {
        method: "POST",
        headers: {"Content-Type":"application/x-www-form-urlencoded"},
        body: "action=search&mobile=" + mobile
    })
    .then(res => res.text())
    .then(html => {
        document.getElementById("result").innerHTML = html;
    });
}

function toggleBlock(id, status){
    let action = status === "blocked" ? "unblock_user" : "block_user";

    fetch("deletebank_ajax.php", {
        method: "POST",
        headers: {"Content-Type":"application/x-www-form-urlencoded"},
        body: "action=" + action + "&user_id=" + id
    })
    .then(res => res.text())
    .then(msg => {
        alert(msg);
        searchUser();
    });
}

function deleteBank(id){
    if(!confirm("Delete bank details?")) return;

    fetch("deletebank_ajax.php", {
        method: "POST",
        headers: {"Content-Type":"application/x-www-form-urlencoded"},
        body: "action=delete_bank&user_id=" + id
    })
    .then(res => res.text())
    .then(msg => {
        alert(msg);
        searchUser();
    });
}

function loginAsUser(id){
    fetch("deletebank_ajax.php", {
        method: "POST",
        headers: {"Content-Type":"application/x-www-form-urlencoded"},
        body: "action=login_as_user&user_id=" + id
    })
    .then(res => res.text())
    .then(url => {
        if(url.startsWith("http")){
            window.location.href = url;
        } else {
            alert(url);
        }
    });
}
</script>

</body>
</html>
