<?php
require_once 'functions.php';

$message = '';
$error = '';

// Handle delete action
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'delete') {
    $id = (int)$_POST['id'];
    if (deleteOffer($id)) {
        $message = "Офертата беше изтрита успешно.";
    } else {
        $error = "Грешка при изтриване на офертата.";
    }
}

$offers = getAllOffers();
?>

<!DOCTYPE html>
<html lang="bg">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

	<style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
		
		myInput {
			background-image: url('/css/searchicon.png');
			background-position: 10px 10px;
			background-repeat: no-repeat;
			width: 100%;
			font-size: 16px;
			padding: 12px 20px 12px 40px;
			border: 1px solid #ddd;
			margin-bottom: 12px;
					}
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
 
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .btn {
            background-color: #007bff;
            color: white;
            padding: 8px 16px;
            text-decoration: none;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            margin: 2px;
            display: inline-block;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        .btn-warning {
            background-color: #ffc107;
            color: #212529;
        }
        .btn-warning:hover {
            background-color: #e0a800;
        }
        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border: 1px solid transparent;
            border-radius: 4px;
        }
        .alert-success {
            color: #155724;
            background-color: #d4edda;
            border-color: #c3e6cb;
        }
        .alert-danger {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }
        .add-button {
            text-align: center;
            margin-bottom: 20px;
        }
        @media (max-width: 768px) {
            .nav {
                flex-direction: column;
                align-items: center;
            }
            table {
                font-size: 12px;
            }
            .container {
                padding: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
  
     <?php
   include_once 'header.php';
   ?>

        <?php if ($message): ?>
            <div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

</br>
</br>
       
            <a href="add_offer.php" class="btn btn-success">Създай нова оферта</a>
       
        <?php if (empty($offers)): ?>
            <p>Няма създадени оферти.</p>
        <?php else: ?>
        </br>
		</br>
		<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Търсене в оферти..." class="search-input" autofocus>
</br>		
		
            <table id="myTable">
                <thead>
                    <tr>
                        <th>Номер</th>
                        <th>Получател</th>
                        <th>Дата</th>
                        <th>Обща сума</th>

                        <th>Действия</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($offers as $offer): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($offer['offer_number']); ?></td>
                        <td><?php echo htmlspecialchars($offer['recipient']); ?></td>
                        <td><?php echo date('d.m.Y', strtotime($offer['offer_date'])); ?></td>
                        <td><?php echo number_format($offer['total_amount'], 2, '.', ''); ?> лв.</td>
              
                        <td>
                            <a href="view_offer.php?id=<?php echo $offer['id']; ?>"  onclick="window.open(this.href, '_blank', 'left=20,top=20,width=1000,height=600,toolbar=1,resizable=0'); return false;" class="btn" style="background-color: #17a2b8; color: white;" class="btn">Преглед</a>
                            <a href="edit_offer.php?id=<?php echo $offer['id']; ?>" class="btn btn-warning">Редакция</a>
                            <form method="POST" style="display: inline;" onsubmit="return confirm('Сигурни ли сте, че искате да изтриете тази оферта?');">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="id" value="<?php echo $offer['id']; ?>">
                                <!-- button type="submit" class="btn btn-danger">Delete</button -->
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
		<script>
function myFunction() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }       
  }
}
</script>
</body>
</html>

