<?php
$page_title = 'Supplier List';
require_once('includes/load.php');
page_require_level(5);

// Mengambil data supplier
function get_all_vendors() {
    global $db;
    $sql = "SELECT * FROM vendor_list"; // Pastikan nama tabel sesuai
    return $db->query($sql);
}

// Mendapatkan data supplier
$vendors = get_all_vendors();

// Cek apakah data supplier kosong
$error_msg = '';
if (!$vendors || $db->num_rows($vendors) == 0) {
    $error_msg = "No vendors found.";
}
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
        <?php if ($error_msg): ?>
            <div class="alert alert-danger"><?php echo $error_msg; ?></div>
        <?php endif; ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-th"></span>
                    <span>Supplier List</span>
                </strong>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th>Vendor Name</th>
                                <th>Contact Info</th>
                                <th>Address</th>
                                <th>Phone Number</th>
                                <th>Email</th>
                                <th>Bank Account</th>
                                <th>Bank Name</th>
                                <th>Term of Payment</th>
                                <th>Attachment</th>
                                <th>Date Added</th>
                                <th class="text-center">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (isset($vendors) && $db->num_rows($vendors) > 0): ?>
                                <?php foreach ($vendors as $vendor): ?>
                                    <tr>
                                        <td class="text-center"><?php echo count_id(); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['vendor_name']); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['contact_info']); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['address']); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['phone_number']); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['email']); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['bank_account']); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['bank_name']); ?></td>
                                        <td><?php echo htmlspecialchars($vendor['term_of_payment']); ?></td>
                                        <td>
                                            <?php if (!empty($vendor['attachment'])): ?>
                                                <a href="<?php echo htmlspecialchars($vendor['attachment']); ?>" target="_blank" class="btn btn-xs btn-info">
                                                    View
                                                </a>
                                            <?php else: ?>
                                                No Attachment
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo htmlspecialchars($vendor['date_added']); ?></td>
                                        <td class="text-center">
                                            <button class="btn btn-xs btn-warning" data-toggle="modal" data-target="#editVendorModal" 
                                                    data-id="<?php echo (int)$vendor['id']; ?>" 
                                                    data-name="<?php echo htmlspecialchars($vendor['vendor_name']); ?>">
                                                <span class="glyphicon glyphicon-edit"></span>
                                            </button>
                                            <a href="delete_vendor.php?id=<?php echo (int)$vendor['id']; ?>" class="btn btn-xs btn-danger" onclick="return confirm('Are you sure you want to delete this vendor?');">
                                                <span class="glyphicon glyphicon-trash"></span>
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="12" class="text-center">No vendors found.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="editVendorModal" tabindex="-1" role="dialog" aria-labelledby="editVendorLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="editVendorLabel">Edit Vendor Name</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form method="post" action="update_vendor.php">
        <div class="modal-body">
          <input type="hidden" name="vendor_id" id="vendor-id">
          <div class="form-group">
            <label for="vendor-name">Vendor Name</label>
            <input type="text" class="form-control" name="vendor_name" id="vendor-name" required>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="submit" name="update" class="btn btn-primary">Save changes</button>
        </div>
      </form>
    </div>
  </div>
</div>

<script>
  $('#editVendorModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // Button that triggered the modal
    var id = button.data('id'); // Extract info from data-* attributes
    var name = button.data('name');

    // Update the modal's content
    var modal = $(this);
    modal.find('#vendor-id').val(id);
    modal.find('#vendor-name').val(name);
  });
</script>

<?php include_once('layouts/footer.php'); ?>
