<?php
session_start();
require_once('../includes/admin-security.php');
include_once('includes/config.php');

// Handle vendor approval
if (isset($_GET['approve']) && isset($_GET['id'])) {
    $vendorId = intval($_GET['id']);
    $adminId = $_SESSION['id'];

    // Fetch store name for logging
    $q = mysqli_query($con, "SELECT store_name FROM vendors WHERE id='$vendorId'");
    $r = mysqli_fetch_assoc($q);
    $store = $r['store_name'] ?? 'Unknown';

    mysqli_query($con, "UPDATE vendors SET status='active', approved_at=NOW(), approved_by='$adminId' WHERE id='$vendorId'");
    log_activity("Approved Vendor", "Store: $store, ID: $vendorId");
    $_SESSION['msg'] = "Vendor approved successfully!";
    header('location: pending-vendors.php');
    exit;
}

// Handle vendor rejection
if (isset($_GET['reject']) && isset($_GET['id'])) {
    $vendorId = intval($_GET['id']);

    // Fetch store name for logging
    $q = mysqli_query($con, "SELECT store_name FROM vendors WHERE id='$vendorId'");
    $r = mysqli_fetch_assoc($q);
    $store = $r['store_name'] ?? 'Unknown';

    mysqli_query($con, "UPDATE vendors SET status='rejected' WHERE id='$vendorId'");
    log_activity("Rejected Vendor", "Store: $store, ID: $vendorId");
    $_SESSION['msg'] = "Vendor application rejected.";
    header('location: pending-vendors.php');
    exit;
}

// Get pending vendors
$pendingQuery = mysqli_query($con, "SELECT v.*, u.username, u.email, ud.name 
                                    FROM vendors v 
                                    JOIN users u ON v.user_id = u.id 
                                    LEFT JOIN user_details ud ON ud.user_id = u.id 
                                    WHERE v.status='pending' 
                                    ORDER BY v.created_at DESC");
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>Pending Vendors - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>

    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>

        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <!-- Page Header -->
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-user-clock me-2"></i> Pending Vendor Applications</h1>
                            <p>Review and approve vendor applications</p>
                        </div>
                    </div>

                    <!-- Messages -->
                    <?php if (isset($_SESSION['msg'])) { ?>
                        <div class="alert alert-success alert-dismissible fade show">
                            <i class="fas fa-check-circle me-2"></i>
                            <?php echo $_SESSION['msg'];
                            unset($_SESSION['msg']); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php } ?>

                    <!-- Pending Vendors Table -->
                    <div class="table-card">
                        <div class="card-header">
                            <h5 class="mb-0"><i class="fas fa-list me-2"></i>Pending Applications</h5>
                        </div>
                        <div class="card-body">
                            <?php if (mysqli_num_rows($pendingQuery) > 0) { ?>
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Store Name</th>
                                            <th>Owner</th>
                                            <th>Email</th>
                                            <th>Phone</th>
                                            <th>Applied Date</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $cnt = 1;
                                        while ($vendor = mysqli_fetch_array($pendingQuery)) {
                                            ?>
                                            <tr>
                                                <td>
                                                    <?php echo $cnt; ?>
                                                </td>
                                                <td><strong>
                                                        <?php echo htmlentities($vendor['store_name']); ?>
                                                    </strong></td>
                                                <td>
                                                    <?php echo htmlentities($vendor['name'] ?: $vendor['username']); ?>
                                                </td>
                                                <td>
                                                    <?php echo htmlentities($vendor['business_email']); ?>
                                                </td>
                                                <td>
                                                    <?php echo htmlentities($vendor['business_phone']); ?>
                                                </td>
                                                <td>
                                                    <?php echo date('M d, Y', strtotime($vendor['created_at'])); ?>
                                                </td>
                                                <td>
                                                    <button class="btn btn-sm btn-info" data-bs-toggle="modal"
                                                        data-bs-target="#viewModal<?php echo $vendor['id']; ?>">
                                                        <i class="fas fa-eye"></i> View
                                                    </button>
                                                    <a href="?approve=1&id=<?php echo $vendor['id']; ?>"
                                                        class="btn btn-sm btn-success"
                                                        onclick="return confirm('Approve this vendor?');">
                                                        <i class="fas fa-check"></i> Approve
                                                    </a>
                                                    <a href="?reject=1&id=<?php echo $vendor['id']; ?>"
                                                        class="btn btn-sm btn-danger"
                                                        onclick="return confirm('Reject this vendor?');">
                                                        <i class="fas fa-times"></i> Reject
                                                    </a>
                                                </td>
                                            </tr>

                                            <!-- View Details Modal -->
                                            <div class="modal fade" id="viewModal<?php echo $vendor['id']; ?>" tabindex="-1">
                                                <div class="modal-dialog modal-lg">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <h5 class="modal-title">Vendor Application Details</h5>
                                                            <button type="button" class="btn-close"
                                                                data-bs-dismiss="modal"></button>
                                                        </div>
                                                        <div class="modal-body">
                                                            <h6>Store Information</h6>
                                                            <table class="table table-bordered">
                                                                <tr>
                                                                    <th width="30%">Store Name</th>
                                                                    <td>
                                                                        <?php echo htmlentities($vendor['store_name']); ?>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Business Email</th>
                                                                    <td>
                                                                        <?php echo htmlentities($vendor['business_email']); ?>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Business Phone</th>
                                                                    <td>
                                                                        <?php echo htmlentities($vendor['business_phone']); ?>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Business Address</th>
                                                                    <td>
                                                                        <?php echo htmlentities($vendor['business_address']); ?>
                                                                    </td>
                                                                </tr>
                                                            </table>

                                                            <h6 class="mt-3">Owner Information</h6>
                                                            <table class="table table-bordered">
                                                                <tr>
                                                                    <th width="30%">Full Name</th>
                                                                    <td>
                                                                        <?php echo htmlentities($vendor['name']); ?>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Username</th>
                                                                    <td>
                                                                        <?php echo htmlentities($vendor['username']); ?>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Email</th>
                                                                    <td>
                                                                        <?php echo htmlentities($vendor['email']); ?>
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary"
                                                                data-bs-dismiss="modal">Close</button>
                                                            <a href="?approve=1&id=<?php echo $vendor['id']; ?>"
                                                                class="btn btn-success">
                                                                <i class="fas fa-check me-2"></i>Approve Vendor
                                                            </a>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                            <?php
                                            $cnt++;
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            <?php } else { ?>
                                <div class="alert alert-info text-center">
                                    <i class="fas fa-info-circle me-2"></i>
                                    No pending vendor applications at this time.
                                </div>
                            <?php } ?>
                        </div>
                    </div>

                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="js/scripts.js"></script>
</body>

</html>