<?php
require_once '../../includes/auth.php';
require_once '../../db.php';
require_once '../../includes/header.php';
require_once '../../includes/sidebar.php';

$companies = $conn->query("SELECT * FROM company_master ORDER BY company_name ASC")->fetch_all(MYSQLI_ASSOC);
?>

<div class="page-header">
    <h2><i class="fas fa-building" style="color:var(--primary);margin-right:8px;"></i>Companies</h2>
    <a href="form.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add Company</a>
</div>

<?php if (isset($_GET['success'])): ?>
<div class="alert alert-success"><i class="fas fa-check-circle"></i> Company saved successfully.</div>
<?php elseif (isset($_GET['deleted'])): ?>
<div class="alert alert-warning"><i class="fas fa-trash"></i> Company deleted.</div>
<?php elseif (isset($_GET['error'])): ?>
<div class="alert alert-danger"><i class="fas fa-exclamation-circle"></i> <?= htmlspecialchars($_GET['error']) ?></div>
<?php endif; ?>

<div class="panel">
    <div class="panel-header">
        <span class="panel-title">All Companies</span>
        <span style="font-size:12px;color:var(--text-muted);"><?= count($companies) ?> total</span>
    </div>
    <div style="overflow-x:auto;">
        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>Company Name</th>
                    <th>GST No</th>
                    <th>Phone</th>
                    <th>City / State</th>
                    <th>Email</th>
                    <th style="text-align:center;">Status</th>
                    <th style="text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php if (empty($companies)): ?>
                <tr>
                    <td colspan="8" style="text-align:center;padding:40px;color:var(--text-muted);">
                        <i class="fas fa-building" style="font-size:32px;margin-bottom:8px;display:block;opacity:0.3;"></i>
                        No companies found. <a href="form.php">Add your first company</a>.
                    </td>
                </tr>
            <?php else: ?>
                <?php foreach ($companies as $i => $co): ?>
                <tr>
                    <td style="color:var(--text-muted);font-size:12px;"><?= $i + 1 ?></td>
                    <td>
                        <div style="display:flex;align-items:center;gap:10px;">
                            <?php if (!empty($co['logo'])): ?>
                            <img src="../../<?= htmlspecialchars($co['logo']) ?>"
                                 style="width:32px;height:32px;object-fit:contain;border-radius:6px;border:1px solid var(--border);padding:2px;background:#f8fafc;">
                            <?php else: ?>
                            <div style="width:32px;height:32px;border-radius:6px;background:var(--primary-light);display:flex;align-items:center;justify-content:center;color:var(--primary);font-size:14px;">
                                <i class="fas fa-building"></i>
                            </div>
                            <?php endif; ?>
                            <div>
                                <strong><?= htmlspecialchars($co['company_name']) ?></strong>
                                <?php if ($co['is_default']): ?>
                                <span class="badge badge-primary" style="margin-left:6px;font-size:10px;">Default</span>
                                <?php endif; ?>
                                <?php if (!empty($co['pan_no'])): ?>
                                <br><small style="color:var(--text-muted);">PAN: <?= htmlspecialchars($co['pan_no']) ?></small>
                                <?php endif; ?>
                            </div>
                        </div>
                    </td>
                    <td><?= !empty($co['gst_no']) ? htmlspecialchars($co['gst_no']) : '<span style="color:var(--text-muted)">—</span>' ?></td>
                    <td><?= !empty($co['phone']) ? htmlspecialchars($co['phone']) : '<span style="color:var(--text-muted)">—</span>' ?></td>
                    <td>
                        <?php
                        $loc = array_filter([$co['city'] ?? '', $co['state'] ?? '']);
                        echo $loc ? htmlspecialchars(implode(', ', $loc)) : '<span style="color:var(--text-muted)">—</span>';
                        ?>
                    </td>
                    <td><?= !empty($co['email']) ? htmlspecialchars($co['email']) : '<span style="color:var(--text-muted)">—</span>' ?></td>
                    <td style="text-align:center;">
                        <span class="badge badge-success">Active</span>
                    </td>
                    <td>
                        <div class="action-links" style="justify-content:center;">
                            <a href="form.php?id=<?= $co['id'] ?>" class="action-btn edit" title="Edit Company">
                                <i class="fas fa-edit"></i>
                            </a>
                            <?php if (!$co['is_default']): ?>
                            <a href="delete.php?id=<?= $co['id'] ?>"
                               class="action-btn delete" title="Delete Company"
                               onclick="return confirm('Delete company &quot;<?= htmlspecialchars(addslashes($co['company_name'])) ?>&quot;? This cannot be undone.')">
                                <i class="fas fa-trash-alt"></i>
                            </a>
                            <?php else: ?>
                            <span class="action-btn" title="Cannot delete the default company" style="opacity:0.3;cursor:not-allowed;">
                                <i class="fas fa-trash-alt"></i>
                            </span>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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