<?php


// Check if the user is logged in and has the correct role
if ($_SESSION['role'] != 'Auditor' && $_SESSION['role'] != 'Legal' && $_SESSION['role'] != 'Initiator' && $_SESSION['role'] != 'Approver') {
    header("Location: index.php");
    exit();
}

$status = $permit['status'];
$userRole = $_SESSION['role'];

// Determine which buttons to show (default to true)
$showEdit = $showDelete = $showReview = $showPrint = $showEmail = $showWhatsApp = true;

// Initiator logic
if ($userRole === 'Initiator') {
    switch ($status) {
        case 'Approved':
            $showEdit = $showDelete = $showReview = false;
            break;
        case 'Audited':
        case 'Verified':
            $showEdit = $showDelete = $showReview = $showPrint = $showEmail = $showWhatsApp = false;
            break;
        case 'Pending':
            $showReview = $showPrint = $showEmail = $showWhatsApp = false;
            break;
    }
}

// Auditor and Legal logic: Only show Review
if ($userRole === 'Auditor' || $userRole === 'Legal') {
    $showEdit = $showDelete = $showPrint = $showEmail = $showWhatsApp = false;
    $showReview = true;
}

// Utility function to apply disabled styling
if (!function_exists('disabledBtn')) {
    function disabledBtn($iconClass) {
        return '<button type="button" class="btn btn-secondary btn-sm" disabled><i class="' . $iconClass . '"></i></button>';
    }
}
?>

<div class="hstack gap-2 fs-1">
    <!-- Edit Button -->
    <?php if ($showEdit): ?>
        
            <a href="edit_business_permit.php?id=<?= $permit['id']; ?>" class="btn btn-icon btn-sm btn-secondary-light btn-wave" title="Edit"><i class="ri-edit-line"></i></a>
        
    <?php endif; ?>

    <!-- Delete Button -->
    <?php if ($showDelete): ?>
        
            <a href="delete_permit.php?id=<?= $permit['id']; ?>" class="btn bg-danger-transparent btn-sm" onclick="return confirm('Are you sure you want to delete this permit?');" title="Delete"><i class="bi bi-trash"></i>
            </a>
        
    <?php endif; ?>

    <!-- Review Button -->
    <?php if ($showReview): ?>
        <a href="edit_business_permit.php?id=<?= $permit['id']; ?>" class="btn btn-warning btn-sm" title="Review">
            <i class="fas fa-eye"></i>
        </a>
    <?php endif; ?>

    <!-- Print Button -->
    <?php if ($showPrint): ?>
      
            <a href="reports/RptHealthPermit.php?id=<?= $permit['id']; ?>" class="btn bg-success-transparent btn-sm" target="_blank" title="Print"><i class="bi bi-printer-fill"></i>
        
    </a>

    <?php endif; ?>

    <!-- Send Email Button -->
    <?php if ($showEmail): ?>
        <button type="button" class="btn bg-info-transparent btn-sm" data-id="<?= $permit['id']; ?>" title="Send Email">
        <i class="bi bi-envelope-fill"></i>
        </button>
    <?php endif; ?>
</div>

