<?php
session_start();
include 'config.php';

if (!isset($_SESSION['username']) || !in_array($_SESSION['role'], ['admin','superadmin'])) {
    header("Location: index.php"); exit;
}

// Search functionality
$search = $_GET['search'] ?? '';
$where = "";
if ($search !== '') {
    $search = $conn->real_escape_string($search);
    $where = "WHERE emp_code LIKE '%$search%' OR name LIKE '%$search%' OR department LIKE '%$search%'";
}

// Fetch salary master
$sql = "SELECT * FROM salary_master $where ORDER BY department, name";
$result = $conn->query($sql);
$employees = $result->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Salary Master - AROSTAR</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <style>
        .dept{border-left:5px solid;}
        .dept-Directors{border-color:#ffc107;}
        .dept-Electrical{border-color:#0d6efd;}
        .dept-TAB{border-color:#198754;}
        .dept-AC{border-color:#6f42c1;}
        .dept-Automation{border-color:#fd7e14;}
        .dept-Admin{border-color:#d63384;}
        .dept-General{border-color:#6c757d;}
    </style>
</head>
<body class="bg-light">
<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="bi bi-table"></i> Salary Master List</h2>
        <a href="salary_upload.php" class="btn btn-success">
            <i class="bi bi-upload"></i> Upload New Salary Excel
        </a>
    </div>

    <!-- Search Bar -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="d-flex gap-2">
                <input type="text" name="search" value="<?= htmlspecialchars($search) ?>" class="form-control w-50" placeholder="Search by ID, Name or Department...">
                <button type="submit" class="btn btn-primary"><i class="bi bi-search"></i> Search</button>
                <?php if ($search !== ''): ?>
                <a href="salary_master.php" class="btn btn-outline-secondary">Clear</a>
                <?php endif; ?>
            </form>
        </div>
    </div>

    <div class="card shadow">
        <div class="card-header bg-dark text-white d-flex justify-content-between">
            <span><strong>Total Employees: <?= count($employees) ?></strong></span>
            <a href="salary_master_export.php" class="btn btn-light btn-sm"><i class="bi bi-download"></i> Export CSV</a>
        </div>
        <div class="table-responsive">
            <table class="table table-hover table-sm mb-0">
                <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>Emp Code</th>
                        <th>Name</th>
                        <th>Department</th>
                        <th>Gross Salary</th>
                        <th>Basic</th>
                        <th>DA</th>
                        <th>HRA</th>
                        <th>Medical</th>
                        <th>Conveyance</th>
                        <th>EPF</th>
                        <th>ESI</th>
                        <th>Travel</th>
                        <th>2W Maint.</th>
                        <th>TDS</th>
                        <th>Advance</th>
                        <th>Net Salary</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($employees as $i => $e): 
                        $cls = 'dept-' . str_replace([' ','&'], '', $e['department']);
                    ?>
                    <tr class="<?= $cls ?>">
                        <td><?= $i+1 ?></td>
                        <td><strong><?= $e['emp_code'] ?></strong></td>
                        <td><?= htmlspecialchars($e['name']) ?></td>
                        <td><?= $e['department'] ?: 'General' ?></td>
                        <td class="text-end fw-bold"><?= number_format($e['gross_salary']) ?></td>
                        <td class="text-end"><?= number_format($e['basic']) ?></td>
                        <td class="text-end"><?= number_format($e['da']) ?></td>
                        <td class="text-end"><?= number_format($e['hra']) ?></td>
                        <td class="text-end"><?= number_format($e['medical']) ?></td>
                        <td class="text-end"><?= number_format($e['conveyance']) ?></td>
                        <td class="text-end text-danger"><?= number_format($e['epf']) ?></td>
                        <td class="text-end text-danger"><?= number_format($e['esi']) ?></td>
                        <td class="text-end"><?= number_format($e['travel_allowance']) ?></td>
                        <td class="text-end"><?= number_format($e['tw_maintenance']) ?></td>
                        <td class="text-end text-danger"><?= number_format($e['tds']) ?></td>
                        <td class="text-end text-danger"><?= number_format($e['advance']) ?></td>
                        <td class="text-end text-success fw-bold fs-5"><?= number_format($e['net_salary']) ?></td>
                        <td><?= htmlspecialchars($e['email'] ?: '-') ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(empty($employees)): ?>
                    <tr><td colspan="18" class="text-center py-4 text-muted">No salary records found. Please upload salary Excel first.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <div class="card-footer text-center">
            <a href="attendance.php" class="btn btn-outline-primary">← Back to Attendance</a>
            <a href="payroll.php" class="btn btn-primary">Go to Payroll →</a>
        </div>
    </div>
</div>
</body>
</html>