<?php require_once('../database/header.php'); ?>

<style>
    /* General box container (three boxes per row) */
    .box-container {
        display: flex;
        justify-content: flex-start;
        gap: 20px;
        flex-wrap: wrap;
        margin-bottom: 20px;
    }

    /* General box styling with hover zoom effect */
    .box {
        border: 1px solid #ddd;
        padding: 15px;
        width: calc(33.33% - 20px); /* Adjust width to fit three boxes per row with gap */
        position: relative;
        background-color: #fff;
        text-decoration: none; /* Remove link underline */
        transition: transform 0.3s ease, box-shadow 0.3s ease; /* Smooth transition for zoom effect */
        overflow: hidden; /* Ensure badge doesn't overflow */
        border-radius: 8px;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    /* Box hover effect (zoom in and shadow) */
    .box:hover {
        transform: scale(1.03); /* Slight zoom effect */
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15); /* Enhanced shadow on hover */
    }

    /* Box header with badge on top right */
    .box-header {
        position: absolute;
        top: 10px;
        right: 10px;
    }

    .badge-btn-live {
        padding: 5px 10px;
        background-color: #4CAF50; /* Green badge background color */
        color: white;
        font-size: 12px;
        font-weight: bold;
        border-radius: 20px;
    }
    
    .logo img {
       width: 50px;
       height: 50px;
    }
    
    /* Box content */
    .box-content {
        display: flex;
        align-items: center;
        margin-top: 20px;
    }

    /* Logo styling */
    .logo {
        margin-right: 15px;
    }

    /* Text and name styling */
    .text {
        display: flex;
        flex-direction: column;
    }

    .name {
        font-size: 16px;
        font-weight: bold;
        color: #333;
        margin-bottom: 3px;
    }

    .subtitle {
        font-size: 13px;
        color: #666; /* Lighter color for the subtitle */
    }

    /* Responsive Layout for smaller screens */
    @media (max-width: 992px) {
        .box {
            width: calc(50% - 20px); /* 2 boxes per row on medium screens */
        }
    }

    @media (max-width: 576px) {
        .box {
            width: 100%; /* 1 box per row on extra small screens */
        }
    }
</style>

<div class="main-panel">
    <div class="content-wrapper">
        <!-- DataTales Example -->
        <div class="card shadow mb-4">
            <div class="card-header py-3 bg-primary">
                <h6 class="m-0 font-weight-bold text-white">管理商户</h6>
            </div>
            <div class="card-body">
                
                <?php
                if ($userdata['usertype'] == 'mainadmin') {
                    // Handle Status Toggle
                    if(isset($_POST['toggle_status'])){
                        $uid = $_POST['uid'];
                        $current_status = $_POST['current_status'];
                        $new_status = ($current_status == 'approved') ? 'banned' : 'approved';
                        
                        $upd = $conn->prepare("UPDATE loginusers SET status=? WHERE mobile_no=? OR id=?");
                        $upd->execute([$new_status, $uid, $uid]);
                        $upd->execute([$new_status, $uid, $uid]);
                        echo "<script>window.location='manage-merchant.php';</script>";
                    }

                    // Handle Delete
                    if(isset($_POST['delete_user'])){
                        $uid = $_POST['delete_uid'];
                        $del = $conn->prepare("DELETE FROM loginusers WHERE mobile_no=? OR id=?");
                        if($del->execute([$uid, $uid])) {
                            echo "<script>alert('用户删除成功'); window.location='manage-merchant.php';</script>";
                        }
                    }

                    // Admin View: List all users
                    
                    $search_term = $_GET['search'] ?? ($_GET['user'] ?? ''); // Check both params
                    
                    // Fetch Users Query
                    $user_sql = "SELECT * FROM loginusers WHERE usertype != 'mainadmin'";
                    $params = [];
                    
                    if ($search_term) {
                        $user_sql .= " AND (username LIKE ? OR mobile_no LIKE ?)";
                        $params[] = "%$search_term%";
                        $params[] = "%$search_term%";
                    }
                    
                    $user_sql .= " ORDER BY id DESC";
                    
                    $stmt = $conn->prepare($user_sql);
                    $stmt->execute($params);
                    $user_list = $stmt->fetchAll(PDO::FETCH_ASSOC);
                    ?>

                    <!-- Search Bar -->
                    <form action="" method="GET" class="mb-4">
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text bg-primary text-white">搜索用户</span>
                            </div>
                            <input type="text" class="form-control" name="search" placeholder="输入手机号码" value="<?php echo htmlspecialchars($search_term); ?>">
                            <div class="input-group-append">
                                <button class="btn btn-primary" type="submit">搜索</button>
                                <?php if($search_term){ ?>
                                <a href="manage-merchant.php" class="btn btn-secondary">重置</a>
                                <?php } ?>
                            </div>
                        </div>
                    </form>

                    <div class="box-container">
                        <?php if(count($user_list) > 0): ?>
                            <?php foreach($user_list as $row): 
                                $is_active = ($row['status'] == 'approved');
                                $badge_class = $is_active ? 'badge-btn-live' : 'badge-btn-coming'; // Green vs Red (Assuming 'coming' is red defined in style)
                                $badge_text = $is_active ? '活跃' : '不活跃';
                            ?>
                            <!-- User Box -->
                            <div class="box">
                                <a href="rspay-settings.php?user=<?php echo $row['mobile_no']; ?>" style="text-decoration:none; color:inherit; display:block;">
                                    <div class="box-header">
                                        <div class="<?php echo $badge_class; ?>"><?php echo $badge_text; ?></div>
                                    </div>
                                    <div class="box-content">
                                        <div class="logo">
                                            <img src="../assets/logo/pay2inr.png" alt="User" class="rounded-logo"> 
                                        </div>
                                        <div class="text">
                                            <div class="name"><?php echo htmlspecialchars($row['mobile_no']); ?></div>
                                            <div class="subtitle">pay2inr 商户</div>
                                        </div>
                                    </div>
                                </a>
                                
                                <!-- Action Buttons -->
                                <div class="mt-3 text-center border-top pt-2">
                                    <form method="POST" style="display:inline;">
                                        <input type="hidden" name="uid" value="<?php echo $row['mobile_no']; ?>">
                                        <input type="hidden" name="current_status" value="<?php echo $row['status']; ?>">
                                        <button type="submit" name="toggle_status" class="btn btn-sm <?php echo $is_active ? 'btn-danger' : 'btn-success'; ?>" style="font-size: 10px;">
                                            <?php echo $is_active ? '停用' : '启用'; ?>
                                        </button>
                                    </form>
                                    
                                    <form method="POST" style="display:inline;">
                                        <input type="hidden" name="delete_uid" value="<?php echo $row['mobile_no']; ?>">
                                        <button type="submit" name="delete_user" class="btn btn-sm btn-outline-danger ml-1" style="font-size: 10px;" onclick="return confirm('您确定要删除此用户吗？此操作无法撤销。');">
                                            删除
                                        </button>
                                    </form>

                                    <a href="usraction.php?type=update&usrid=<?php echo $row['id']; ?>" class="btn btn-sm btn-info ml-1" style="font-size: 10px;">资料</a>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <div class="col-12 text-center">
                                <h4 class="text-muted">未找到用户。</h4>
                            </div>
                        <?php endif; ?>
                    </div>

                <?php } else { 
                    // Regular User View: Show only their own merchant
                    ?>
                    <div class="box-container">
                        <a href="rspay-settings.php" class="box">
                            <div class="box-header">
                                <div class="badge-btn-live">Live</div>
                            </div>
                            <div class="box-content">
                                <div class="logo">
                                    <img src="../assets/logo/pay2inr.png" alt="pay2inr" class="rounded-logo"> 
                                </div>
                                <div class="text">
                                    <div class="name">pay2inr</div>
                                    <div class="subtitle">我的默认网关</div>
                                </div>
                            </div>
                        </a>
                    </div>
                <?php } ?>

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

<?php require_once('../database/footer.php'); ?>
