<?php
require_once 'functions.php';

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

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

$protocols = getAllProtocols();
 include_once 'header.php';
?>


<body>
    <div class="container">

        <?php if ($message): ?>
            <div class="alert alert-success"><?php echo $message; ?></div>
        <?php endif; ?>
        
        <?php if ($error): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
        <?php endif; ?>
        </br>
		</br>
        <div class="add-button">
            <a href="add_protocol.php" class="btn btn-success">Обследване</a>
        </div>
        
        <?php if (empty($protocols)): ?>
            <p>Няма създадени протоколи.</p>
        <?php else: ?>
		      </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>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($protocols as $protocol): ?>
                    <tr>
                            <td><?php echo htmlspecialchars($protocol['issuer']); ?></td>
							 <td><?php // echo htmlspecialchars($protocol['protocol_type']); ?></td>
							<td><?php echo date('d.m.Y H:i', strtotime($protocol['created_at'])); ?></td>
                        <td>
						 <a href="view_protocol.php?id=<?php echo $protocol['id']; ?>"  onclick="window.open(this.href, '_blank', 'left=20,top=20,width=1000,height=900,toolbar=0,resizable=1'); return false;" class="btn" style="background-color: #17a2b8; color: white;" class="btn">Преглед</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 $protocol['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>

