<?php
session_start();
if(!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}

include('../includes/db.php');

$success_message = "";
$error_message = "";

// Get tanks for dropdown
$tanks_sql = "SELECT t.id, t.tank_name, f.name as fuel_name 
              FROM tanks t 
              LEFT JOIN fuel_types f ON t.fuel_type_id = f.id 
              ORDER BY t.tank_name";
$tanks_result = mysqli_query($conn, $tanks_sql);

// Add Nozzle
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_nozzle'])) {
    $tank_id = mysqli_real_escape_string($conn, $_POST['tank_id']);
    $nozzle_name = mysqli_real_escape_string($conn, $_POST['nozzle_name']);
    $meter_reading = mysqli_real_escape_string($conn, $_POST['meter_reading']);
    $date_time = date('Y-m-d H:i:s');
    
    if(empty($tank_id) || empty($nozzle_name)) {
        $error_message = "Tank and Nozzle Name are required!";
    } elseif(!is_numeric($meter_reading) || $meter_reading < 0) {
        $error_message = "Meter reading must be a valid number!";
    } else {
        $sql = "INSERT INTO nozzles (tank_id, nozzle_name, meter_reading, date_time) 
                VALUES ('$tank_id', '$nozzle_name', '$meter_reading', '$date_time')";
        
        if(mysqli_query($conn, $sql)) {
            $success_message = "Nozzle added successfully!";
        } else {
            $error_message = "Error: " . mysqli_error($conn);
        }
    }
}

// Edit Nozzle
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['edit_nozzle'])) {
    $nozzle_id = mysqli_real_escape_string($conn, $_POST['nozzle_id']);
    $tank_id = mysqli_real_escape_string($conn, $_POST['tank_id']);
    $nozzle_name = mysqli_real_escape_string($conn, $_POST['nozzle_name']);
    $meter_reading = mysqli_real_escape_string($conn, $_POST['meter_reading']);
    
    $sql = "UPDATE nozzles SET tank_id='$tank_id', nozzle_name='$nozzle_name', meter_reading='$meter_reading' WHERE id='$nozzle_id'";
    if(mysqli_query($conn, $sql)) {
        $success_message = "Nozzle updated successfully!";
    } else {
        $error_message = "Error: " . mysqli_error($conn);
    }
}

// Delete Nozzle
if(isset($_GET['delete'])) {
    $delete_id = mysqli_real_escape_string($conn, $_GET['delete']);
    
    // Check if nozzle has readings
    $check_readings = "SELECT id FROM nozzle_readings WHERE nozzle_id='$delete_id'";
    $readings_result = mysqli_query($conn, $check_readings);
    
    if(mysqli_num_rows($readings_result) > 0) {
        $error_message = "Cannot delete! This nozzle has reading records.";
    } else {
        $sql = "DELETE FROM nozzles WHERE id='$delete_id'";
        if(mysqli_query($conn, $sql)) {
            $success_message = "Nozzle deleted successfully!";
        } else {
            $error_message = "Error deleting: " . mysqli_error($conn);
        }
    }
}

// Get all nozzles
$sql = "SELECT n.*, t.tank_name, t.current_stock, t.capacity, f.name as fuel_name, f.sale_price
        FROM nozzles n
        LEFT JOIN tanks t ON n.tank_id = t.id
        LEFT JOIN fuel_types f ON t.fuel_type_id = f.id
        ORDER BY n.id DESC";
$result = mysqli_query($conn, $sql);

// Get statistics
$total_nozzles = mysqli_num_rows($result);
$total_meter_reading = 0;

mysqli_data_seek($result, 0);
while($row = mysqli_fetch_assoc($result)) {
    $total_meter_reading += $row['meter_reading'];
}
mysqli_data_seek($result, 0);

// Get single nozzle for edit
$edit_nozzle = null;
if(isset($_GET['edit'])) {
    $edit_id = mysqli_real_escape_string($conn, $_GET['edit']);
    $edit_sql = "SELECT * FROM nozzles WHERE id='$edit_id'";
    $edit_result = mysqli_query($conn, $edit_sql);
    $edit_nozzle = mysqli_fetch_assoc($edit_result);
}
?>

<?php include('../includes/log.php'); ?>
<?php include('../includes/sd.php'); ?>

<div class="nozzles-container">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-left">
            <h2><i class="fas fa-oil-can"></i> Nozzles Management</h2>
            <p>Manage fuel dispensing nozzles and track meter readings</p>
        </div>
        <div class="header-right">
            <div class="stats-badge">
                <i class="fas fa-chart-line"></i>
                <span><?php echo $total_nozzles; ?> Active Nozzles</span>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="stats-row">
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-oil-can"></i>
            </div>
            <div class="stat-info">
                <h3><?php echo $total_nozzles; ?></h3>
                <p>Total Nozzles</p>
                <span class="stat-trend">Active Dispensers</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-tachometer-alt"></i>
            </div>
            <div class="stat-info">
                <h3><?php echo number_format($total_meter_reading, 2); ?></h3>
                <p>Total Meter Reading</p>
                <span class="stat-trend">Combined Total</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-chart-line"></i>
            </div>
            <div class="stat-info">
                <h3><?php echo $total_nozzles > 0 ? round($total_meter_reading / $total_nozzles, 2) : 0; ?></h3>
                <p>Avg per Nozzle</p>
                <span class="stat-trend">Average Reading</span>
            </div>
        </div>
    </div>

    <!-- Alerts -->
    <?php if($success_message): ?>
        <div class="alert-custom alert-success">
            <i class="fas fa-check-circle"></i> <?php echo $success_message; ?>
            <button class="close-alert" onclick="this.parentElement.remove();">&times;</button>
        </div>
    <?php endif; ?>
    
    <?php if($error_message): ?>
        <div class="alert-custom alert-danger">
            <i class="fas fa-exclamation-triangle"></i> <?php echo $error_message; ?>
            <button class="close-alert" onclick="this.parentElement.remove();">&times;</button>
        </div>
    <?php endif; ?>
    
    <div class="content-grid">
        <!-- Add/Edit Nozzle Form -->
        <div class="form-card">
            <div class="form-card-header">
                <i class="fas fa-plus-circle"></i>
                <h4><?php echo isset($_GET['edit']) ? 'Edit Nozzle' : 'Add New Nozzle'; ?></h4>
            </div>
            <div class="form-card-body">
                <form method="POST" action="">
                    <?php if(isset($_GET['edit'])): ?>
                        <input type="hidden" name="nozzle_id" value="<?php echo $edit_nozzle['id']; ?>">
                    <?php endif; ?>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-tachometer-alt"></i> Select Tank <span class="required">*</span></label>
                        <select name="tank_id" class="form-control-modern" required>
                            <option value="">-- Select Tank --</option>
                            <?php 
                            mysqli_data_seek($tanks_result, 0);
                            while($tank = mysqli_fetch_assoc($tanks_result)): 
                                $selected = (isset($edit_nozzle) && $edit_nozzle['tank_id'] == $tank['id']) ? 'selected' : '';
                            ?>
                                <option value="<?php echo $tank['id']; ?>" <?php echo $selected; ?>>
                                    <?php echo htmlspecialchars($tank['tank_name']); ?>
                                    <?php if($tank['fuel_name']): ?>
                                        (<?php echo htmlspecialchars($tank['fuel_name']); ?>)
                                    <?php endif; ?>
                                </option>
                            <?php endwhile; ?>
                        </select>
                        <small class="input-help">Select the tank this nozzle is connected to</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-tag"></i> Nozzle Name <span class="required">*</span></label>
                        <input type="text" name="nozzle_name" class="form-control-modern" required 
                               value="<?php echo isset($edit_nozzle) ? htmlspecialchars($edit_nozzle['nozzle_name']) : ''; ?>"
                               placeholder="e.g., Nozzle 1, Nozzle A, Pump 1">
                        <small class="input-help">Unique identifier for this nozzle</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-chart-line"></i> Meter Reading (Liters)</label>
                        <input type="number" step="0.01" name="meter_reading" class="form-control-modern" 
                               value="<?php echo isset($edit_nozzle) ? $edit_nozzle['meter_reading'] : '0'; ?>"
                               placeholder="Initial meter reading">
                        <small class="input-help">Current meter reading in liters (default: 0)</small>
                    </div>
                    
                    <button type="submit" name="<?php echo isset($_GET['edit']) ? 'edit_nozzle' : 'add_nozzle'; ?>" 
                            class="btn-submit">
                        <i class="fas fa-save"></i> 
                        <?php echo isset($_GET['edit']) ? 'Update Nozzle' : 'Add Nozzle'; ?>
                    </button>
                    
                    <?php if(isset($_GET['edit'])): ?>
                        <a href="nozzles.php" class="btn-cancel-full">
                            <i class="fas fa-times"></i> Cancel Edit
                        </a>
                    <?php endif; ?>
                </form>
            </div>
        </div>
        
        <!-- Nozzles List -->
        <div class="table-card">
            <div class="table-card-header">
                <div class="header-info">
                    <i class="fas fa-list"></i>
                    <h4>Nozzles List</h4>
                </div>
                <div class="table-search">
                    <i class="fas fa-search"></i>
                    <input type="text" id="tableSearch" placeholder="Search nozzle..." class="search-input">
                </div>
            </div>
            <div class="table-card-body">
                <div class="table-responsive">
                    <table class="modern-table" id="nozzlesTable">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Nozzle Name</th>
                                <th>Tank Name</th>
                                <th>Fuel Type</th>
                                <th>Meter Reading</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(mysqli_num_rows($result) > 0): ?>
                                <?php while($row = mysqli_fetch_assoc($result)): 
                                    $status = $row['meter_reading'] > 0 ? 'Active' : 'Idle';
                                    $status_class = $row['meter_reading'] > 0 ? 'status-active' : 'status-idle';
                                    $status_icon = $row['meter_reading'] > 0 ? 'fa-play-circle' : 'fa-pause-circle';
                                ?>
                                    <tr>
                                        <td data-label="ID"><?php echo $row['id']; ?></td>
                                        <td data-label="Nozzle Name">
                                            <div class="nozzle-name-cell">
                                                <i class="fas fa-oil-can"></i>
                                                <strong><?php echo htmlspecialchars($row['nozzle_name']); ?></strong>
                                            </div>
                                        </div>
                                        <td data-label="Tank Name">
                                            <div class="tank-name-cell">
                                                <i class="fas fa-tachometer-alt"></i>
                                                <?php echo htmlspecialchars($row['tank_name']); ?>
                                            </div>
                                        </div>
                                        <td data-label="Fuel Type">
                                            <div class="fuel-type-cell">
                                                <i class="fas fa-gas-pump"></i>
                                                <?php echo htmlspecialchars($row['fuel_name']); ?>
                                            </div>
                                        </div>
                                        <td data-label="Meter Reading">
                                            <strong><?php echo number_format($row['meter_reading'], 2); ?></strong>
                                        </div>
                                        <td data-label="Status">
                                            <span class="status-badge <?php echo $status_class; ?>">
                                                <i class="fas <?php echo $status_icon; ?>"></i>
                                                <?php echo $status; ?>
                                            </span>
                                        </div>
                                        <td data-label="Actions">
                                            <div class="action-buttons">
                                                <a href="?edit=<?php echo $row['id']; ?>" class="btn-action btn-edit" title="Edit Nozzle">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="?delete=<?php echo $row['id']; ?>" 
                                                   class="btn-action btn-delete" 
                                                   onclick="return confirm('Delete this nozzle? This action cannot be undone!')"
                                                   title="Delete Nozzle">
                                                    <i class="fas fa-trash"></i>
                                                </a>
                                            </div>
                                        </div>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="7" class="empty-state">
                                        <i class="fas fa-oil-can"></i>
                                        <p>No nozzles found</p>
                                        <small>Click "Add New Nozzle" to get started</small>
                                    </div>
                                    </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* Nozzles Management Styles - Page Specific Only */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

body {
    background: #f5f7fb;
    overflow-x: hidden;
}

h1, h2, h3, h4, h5, h6,
.page-header h2,
.stat-info h3,
.form-card-header h4,
.header-info h4,
.modern-table th {
    font-family: 'Poppins', sans-serif;
    font-weight: 600;
}

/* Main Content */
.main-content {
    margin-left: 280px;
    padding: 15px 20px;
    transition: all 0.3s;
    background: #f5f7fb;
}

.nozzles-container {
    max-width: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    border-radius: 16px;
    padding: 16px 24px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    width: 100%;
}

.header-left h2 {
    font-size: 22px;
    margin-bottom: 4px;
}

.header-left p {
    font-size: 12px;
    opacity: 0.92;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.stats-badge {
    background: rgba(255,255,255,0.2);
    padding: 6px 18px;
    border-radius: 40px;
    font-weight: 600;
    font-size: 12px;
}

/* Statistics Cards */
.stats-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin-bottom: 20px;
    width: 100%;
}

.stat-card {
    background: #fff;
    border-radius: 16px;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    border: 1px solid #eef2f6;
    transition: all 0.3s;
}

.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 18px rgba(160,70,87,0.1);
}

.stat-icon {
    width: 52px;
    height: 52px;
    background: rgba(160,70,87,0.1);
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-icon i {
    font-size: 26px;
    color: #A04657;
}

.stat-info h3 {
    font-size: 24px;
    margin: 0;
    font-weight: 700;
    color: #1a2a3a;
}

.stat-info p {
    margin: 4px 0 0;
    font-weight: 500;
    color: #64748b;
    font-size: 11px;
}

.stat-trend {
    font-size: 10px;
    color: #94a3b8;
    display: block;
    margin-top: 3px;
}

/* Alerts */
.alert-custom {
    padding: 12px 20px;
    border-radius: 14px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 13px;
    width: 100%;
}

.alert-success {
    background: #e8f5e9;
    color: #2e7d32;
    border-left: 4px solid #28a745;
}

.alert-danger {
    background: #ffebee;
    color: #c62828;
    border-left: 4px solid #dc3545;
}

.close-alert {
    background: none;
    border: none;
    font-size: 22px;
    cursor: pointer;
    opacity: 0.5;
}

/* Content Grid */
.content-grid {
    display: grid;
    grid-template-columns: 380px 1fr;
    gap: 20px;
    width: 100%;
}

/* Form Card */
.form-card {
    background: #fff;
    border-radius: 16px;
    border: 1px solid #eef2f6;
    overflow: hidden;
}

.form-card-header {
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    padding: 14px 20px;
    display: flex;
    align-items: center;
    gap: 8px;
    color: #fff;
}

.form-card-header i {
    font-size: 18px;
}

.form-card-header h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.form-card-body {
    padding: 20px;
}

.input-group-custom {
    margin-bottom: 16px;
}

.input-group-custom label {
    display: block;
    margin-bottom: 6px;
    font-weight: 600;
    color: #1e293b;
    font-size: 12px;
}

.input-group-custom label i {
    color: #A04657;
    margin-right: 5px;
}

.required {
    color: #dc3545;
}

.form-control-modern {
    width: 100%;
    padding: 9px 12px;
    border: 2px solid #e2e8f0;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 500;
    background: #fefefe;
    transition: all 0.3s;
}

.form-control-modern:focus {
    border-color: #A04657;
    outline: none;
    background: #fff;
}

.input-help {
    display: block;
    font-size: 10px;
    color: #94a3b8;
    margin-top: 4px;
}

.btn-submit {
    width: 100%;
    padding: 10px;
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-weight: 600;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.btn-submit:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(160,70,87,0.3);
}

.btn-cancel-full {
    display: block;
    text-align: center;
    margin-top: 10px;
    padding: 8px;
    background: #f1f5f9;
    color: #475569;
    text-decoration: none;
    border-radius: 10px;
    font-weight: 600;
    font-size: 12px;
}

/* Table Card */
.table-card {
    background: #fff;
    border-radius: 16px;
    border: 1px solid #eef2f6;
    overflow: hidden;
    width: 100%;
}

.table-card-header {
    padding: 12px 20px;
    border-bottom: 1px solid #eef2f6;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}

.header-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-info i {
    font-size: 18px;
    color: #A04657;
}

.header-info h4 {
    margin: 0;
    font-size: 15px;
}

.table-search {
    position: relative;
}

.table-search i {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #94a3b8;
    font-size: 11px;
}

.search-input {
    padding: 7px 12px 7px 32px;
    border: 2px solid #e2e8f0;
    border-radius: 30px;
    width: 200px;
    font-size: 12px;
}

.search-input:focus {
    border-color: #A04657;
    outline: none;
}

.table-responsive {
    overflow-x: auto;
    width: 100%;
}

/* Modern Table */
.modern-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    min-width: 700px;
}

.modern-table th {
    padding: 10px 12px;
    background: #f8fafc;
    font-weight: 700;
    color: #475569;
    font-size: 11px;
    border-bottom: 2px solid #eef2f6;
    text-align: left;
}

.modern-table td {
    padding: 9px 12px;
    border-bottom: 1px solid #f0f4f9;
    font-size: 12px;
    font-weight: 500;
    color: #1e293b;
    vertical-align: middle;
}

.modern-table tr:hover td {
    background: #fefaf8;
}

/* Cell Components */
.nozzle-name-cell, .tank-name-cell, .fuel-type-cell {
    display: flex;
    align-items: center;
    gap: 6px;
}

.nozzle-name-cell i {
    color: #A04657;
    font-size: 13px;
}

.tank-name-cell i {
    color: #17a2b8;
    font-size: 12px;
}

.fuel-type-cell i {
    color: #28a745;
    font-size: 12px;
}

.meter-reading-cell strong {
    color: #A04657;
    font-weight: 700;
}

/* Status Badges */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 10px;
    border-radius: 30px;
    font-size: 10px;
    font-weight: 600;
}

.status-active {
    background: #e8f5e9;
    color: #28a745;
}

.status-idle {
    background: #f1f5f9;
    color: #64748b;
}

/* Action Buttons */
.action-buttons {
    display: flex;
    gap: 6px;
}

.btn-action {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: all 0.3s;
}

.btn-edit {
    background: #fff3e0;
    color: #f39c12;
}

.btn-edit:hover {
    background: #f39c12;
    color: #fff;
    transform: scale(1.05);
}

.btn-delete {
    background: #ffebee;
    color: #dc3545;
}

.btn-delete:hover {
    background: #dc3545;
    color: #fff;
    transform: scale(1.05);
}

.empty-state {
    text-align: center;
    padding: 40px !important;
}

.empty-state i {
    font-size: 48px;
    color: #A04657;
    opacity: 0.35;
    margin-bottom: 10px;
    display: block;
}

.empty-state p {
    font-size: 13px;
    font-weight: 600;
    color: #475569;
    margin-bottom: 4px;
}

.empty-state small {
    font-size: 11px;
    color: #94a3b8;
}

/* Responsive */
@media (max-width: 1100px) {
    .content-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 992px) {
    .main-content {
        margin-left: 0;
        padding: 12px 15px;
    }
}

@media (max-width: 768px) {
    .main-content {
        padding: 10px 12px;
    }
    .page-header {
        flex-direction: column;
        text-align: center;
        padding: 14px 18px;
    }
    .header-left h2 {
        font-size: 18px;
    }
    .header-right {
        justify-content: center;
    }
    .stats-row {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    .table-card-header {
        flex-direction: column;
        align-items: stretch;
    }
    .search-input {
        width: 100%;
    }
    .form-card-body {
        padding: 16px;
    }
    .stat-card {
        padding: 12px 16px;
    }
    .stat-icon {
        width: 44px;
        height: 44px;
    }
    .stat-icon i {
        font-size: 22px;
    }
    .stat-info h3 {
        font-size: 20px;
    }
    .modern-table th {
        display: none;
    }
    .modern-table td {
        display: block;
        text-align: right;
        padding-left: 45%;
        position: relative;
    }
    .modern-table td::before {
        content: attr(data-label);
        position: absolute;
        left: 10px;
        width: 40%;
        text-align: left;
        font-weight: 700;
        font-size: 10px;
        color: #A04657;
    }
    .modern-table tr {
        display: block;
        margin-bottom: 10px;
        border: 1px solid #eef2f6;
        border-radius: 10px;
        overflow: hidden;
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb {
    background: #A04657;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: #6b2a36;
}
</style>

<script>
// Table search functionality
document.getElementById('tableSearch')?.addEventListener('keyup', function() {
    let searchValue = this.value.toLowerCase().trim();
    let rows = document.querySelectorAll('#nozzlesTable tbody tr');
    
    rows.forEach(row => {
        if (row.querySelector('.empty-state')) {
            return;
        }
        let text = row.textContent.toLowerCase();
        row.style.display = text.includes(searchValue) ? '' : 'none';
    });
});

// Auto-hide alerts after 5 seconds
document.querySelectorAll('.alert-custom').forEach(alert => {
    setTimeout(() => {
        if (alert) alert.style.opacity = '0';
        setTimeout(() => {
            if (alert) alert.remove();
        }, 300);
    }, 5000);
});
</script>

<?php include('../includes/ft.php'); ?>