<?php require_once('header.php'); ?>

<?php
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
    $valid = 1;
    $id = $_GET['id'];  

    if ($valid == 1) {
        $statement = $pdo->prepare("DELETE FROM tbl_locations WHERE id = ?");
        $statement->execute([$id]);

        $success_message = 'Location deleted successfully!';
    }
}
?>

<section class="content-header">
    <div class="content-header-left">
        <h1>View Locations</h1>
    </div>
    <div class="content-header-right">
        <a href="location-add.php" class="btn btn-primary btn-sm">Add New Location</a>
    </div>
</section>

<section class="content">
  <div class="row">
    <div class="col-md-12">
      <?php if (isset($success_message)): ?>
            <div class="<?php if($success_message){ echo 'callout callout-success'; } ?>">
                <p><?php echo $success_message; ?></p>
            </div>
        <?php endif; ?>

      <div class="box box-info">
        <div class="box-body table-responsive">
          <table id="example1" class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Location Name</th>
                    <!-- <th>Status</th> -->
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $i = 0;
                $statement = $pdo->prepare("SELECT * FROM tbl_locations ORDER BY id DESC");
                $statement->execute();
                $locations = $statement->fetchAll(PDO::FETCH_ASSOC);

                foreach ($locations as $location) {
                    $i++;
                    ?>
                    <tr>
                        <td><?php echo $i; ?></td>
                        <td><?php echo $location['location_name']; ?></td>
                        <!-- <td>
                            <?php
                            // if ($location['status'] == 1) {
                                // echo 'Active';
                            // } else {
                                // echo 'Deactive';
                            // }
                            ?>
                        </td> -->
                        <td>
                            <a href="location-edit.php?id=<?php echo $location['id']; ?>" class="btn btn-primary btn-xs">Edit</a>
                            <a href="#" class="btn btn-danger btn-xs" data-href="location.php?action=delete&id=<?php echo $location['id']; ?>" data-toggle="modal" data-target="#confirm-delete">Delete</a>
                        </td>
                    </tr>
                    <?php
                }
                ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</section>

<!-- Modal for Delete Confirmation -->
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Delete Confirmation</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete this location?</p>
                <p style="color:red;">Be careful! Deleting this location will remove all associated data, including products and references linked to this location.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>

<?php require_once('footer.php'); ?>
