<?php
require_once '../includes/auth.php';
requirePermission('clients', 'view');
require_once '../db.php';
require_once '../includes/header.php';
require_once '../includes/sidebar.php';

$search = $_GET['search'] ?? '';
$sql = "SELECT * FROM clients WHERE status = 1 AND company_id = $cid";
if ($search !== '') {
    $s = $conn->real_escape_string($search);
    $sql .= " AND (name LIKE '%$s%' OR phone LIKE '%$s%' OR gstin LIKE '%$s%')";
}
$sql .= " ORDER BY name";
$result = $conn->query($sql);
?>

<?php
$missingGstin = (int)$conn->query("SELECT COUNT(*) FROM clients WHERE company_id = $cid AND customer_type = 'B2B' AND (gstin IS NULL OR gstin = '') AND status = 1")->fetch_row()[0];
?>
<div class="page-header">
    <h2><i class="fas fa-users" style="color:var(--primary);margin-right:8px;"></i>Clients</h2>
    <div style="display:flex;gap:8px;align-items:center;">
        <?php if ($missingGstin > 0): ?>
        <a href="fix_gstin.php" class="btn btn-warning btn-sm" style="background:#f59e0b;border-color:#f59e0b;color:#fff;">
            <i class="fas fa-triangle-exclamation"></i> <?= $missingGstin ?> B2B client(s) missing GSTIN
        </a>
        <?php endif; ?>
        <a href="create.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add Client</a>
    </div>
</div>

<div class="panel">
    <div class="panel-header">
        <span class="panel-title">Client List</span>
        <form method="get" class="search-bar">
            <input type="text" name="search" placeholder="Search name, phone, GSTIN..."
                   value="<?= htmlspecialchars($search) ?>">
            <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-search"></i></button>
            <?php if ($search): ?>
            <a href="index.php" class="btn btn-secondary btn-sm">Clear</a>
            <?php endif; ?>
        </form>
    </div>
    <div style="overflow-x:auto;">
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Phone</th>
                    <th>Type</th>
                    <th>GSTIN</th>
                    <th>State</th>
                    <th style="text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php if ($result->num_rows > 0): while ($row = $result->fetch_assoc()): ?>
            <tr>
                <td><strong><?= htmlspecialchars($row['name']) ?></strong></td>
                <td><?= htmlspecialchars($row['phone']) ?></td>
                <td>
                    <span class="badge <?= $row['customer_type'] === 'B2B' ? 'badge-primary' : 'badge-gray' ?>">
                        <?= htmlspecialchars($row['customer_type']) ?>
                    </span>
                </td>
                <td><span style="font-family:monospace;font-size:12px;"><?= htmlspecialchars($row['gstin']) ?></span></td>
                <td><?= htmlspecialchars($row['state']) ?></td>
                <td>
                    <div class="action-links" style="justify-content:center;">
                        <a href="edit.php?id=<?= $row['id'] ?>" class="action-btn edit" title="Edit">
                            <i class="fas fa-edit"></i>
                        </a>
                    </div>
                </td>
            </tr>
            <?php endwhile; else: ?>
            <tr><td colspan="6" style="text-align:center;padding:40px;color:var(--text-muted);">
                <i class="fas fa-inbox" style="font-size:28px;display:block;margin-bottom:8px;"></i>
                No clients found.
            </td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>
