<?php
session_start();
include 'layouts/dbconn.php';
date_default_timezone_set('Africa/Nairobi');

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$user_id = $_SESSION['user_id'];

// Fetch user details
$sql_user_details = "SELECT u.*, ur.role_name FROM users u 
                     INNER JOIN user_roles ur ON u.role_id = ur.role_id 
                     WHERE u.user_id = ?";
$stmt_user = $conn->prepare($sql_user_details);
$stmt_user->bind_param("i", $user_id);
$stmt_user->execute();
$user_details = $stmt_user->get_result()->fetch_assoc();

if ($user_details) {
    $username = $user_details['username'];
    $role_name = $user_details['role_name'];
    $full_name = $user_details['full_name'];
}

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'update_tax_settings':
                foreach ($_POST['settings'] as $key => $value) {
                    $sql = "INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) 
                            ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)";
                    $stmt = $conn->prepare($sql);
                    $stmt->bind_param("ss", $key, $value);
                    $stmt->execute();
                }
                echo json_encode(['success' => true, 'message' => 'Tax settings updated successfully']);
                exit();
                break;
                
            case 'add_tax_rate':
                $country_code = $_POST['country_code'];
                $tax_name = $_POST['tax_name'];
                $tax_rate = $_POST['tax_rate'];
                $is_default = isset($_POST['is_default']) ? 1 : 0;
                $is_active = isset($_POST['is_active']) ? 1 : 0;
                
                // If this is set as default, update others to not default
                if ($is_default) {
                    $update_sql = "UPDATE settings SET setting_value = '0' WHERE setting_key LIKE 'tax_%_default'";
                    $conn->query($update_sql);
                }
                
                // Add new tax rate settings
                $tax_key_prefix = 'tax_' . strtolower($country_code) . '_' . strtolower(str_replace(' ', '_', $tax_name));
                
                $settings = [
                    $tax_key_prefix . '_name' => $tax_name,
                    $tax_key_prefix . '_rate' => $tax_rate,
                    $tax_key_prefix . '_country' => $country_code,
                    $tax_key_prefix . '_default' => $is_default,
                    $tax_key_prefix . '_active' => $is_active
                ];
                
                foreach ($settings as $key => $value) {
                    $sql = "INSERT INTO settings (setting_key, setting_value, category) VALUES (?, ?, 'tax_rates')
                            ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)";
                    $stmt = $conn->prepare($sql);
                    $stmt->bind_param("ss", $key, $value);
                    $stmt->execute();
                }
                
                echo json_encode(['success' => true, 'message' => 'Tax rate added successfully']);
                exit();
                break;
        }
    }
}

// Get current tax settings
$sql_settings = "SELECT setting_key, setting_value FROM settings 
                 WHERE category = 'tax' OR setting_key LIKE 'tax_%'
                 ORDER BY setting_key";
$settings_result = $conn->query($sql_settings);

$tax_settings = [];
while ($row = $settings_result->fetch_assoc()) {
    $tax_settings[$row['setting_key']] = $row['setting_value'];
}

// Get countries for tax rates
$sql_countries = "SELECT country_code, country_name FROM countries WHERE is_active = 1 ORDER BY country_name";
$countries_result = $conn->query($sql_countries);

// Parse tax rates from settings
$tax_rates = [];
foreach ($tax_settings as $key => $value) {
    if (strpos($key, 'tax_') === 0 && strpos($key, '_name') !== false) {
        $rate_key = str_replace('_name', '', $key);
        $tax_rates[$rate_key] = [
            'name' => $value,
            'rate' => $tax_settings[$rate_key . '_rate'] ?? 0,
            'country' => $tax_settings[$rate_key . '_country'] ?? '',
            'default' => $tax_settings[$rate_key . '_default'] ?? 0,
            'active' => $tax_settings[$rate_key . '_active'] ?? 1
        ];
    }
}

// Default tax settings
$default_settings = [
    'tax_calculation_method' => $tax_settings['tax_calculation_method'] ?? 'inclusive',
    'tax_rounding_method' => $tax_settings['tax_rounding_method'] ?? 'round',
    'tax_display_prices' => $tax_settings['tax_display_prices'] ?? 'inclusive',
    'tax_invoice_display' => $tax_settings['tax_invoice_display'] ?? 'separate',
    'tax_exemption_enabled' => $tax_settings['tax_exemption_enabled'] ?? '1',
    'tax_number_required' => $tax_settings['tax_number_required'] ?? '0',
    'default_tax_rate' => $tax_settings['default_tax_rate'] ?? '16.00'
];

$conn->close();
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Tax Settings - Freight Management System')); ?>
    <link href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css" rel="stylesheet">
    <link href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.32/dist/sweetalert2.min.css" rel="stylesheet">
    <?php include 'layouts/head-css.php'; ?>
    
    <style>
        .tax-settings-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            overflow: hidden;
            margin-bottom: 2rem;
        }
        .setting-group {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
        }
        .setting-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.75rem 0;
            border-bottom: 1px solid #e2e8f0;
        }
        .setting-item:last-child {
            border-bottom: none;
        }
        .tax-rate-card {
            background: white;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1rem;
            transition: all 0.3s ease;
        }
        .tax-rate-card:hover {
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            border-color: #3b82f6;
        }
        .tax-rate-card.default {
            border-color: #10b981;
            background: #f0fdf4;
        }
        .country-flag {
            width: 24px;
            height: 18px;
            border-radius: 3px;
            margin-right: 8px;
        }
        .tax-rate-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 50px;
            font-size: 0.75rem;
            font-weight: 600;
        }
        .rate-default { background: #d1fae5; color: #065f46; }
        .rate-active { background: #dbeafe; color: #1e40af; }
        .rate-inactive { background: #fee2e2; color: #991b1b; }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0"><i class="ri-calculator-line me-2"></i>Tax Settings</h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                                        <li class="breadcrumb-item"><a href="#">Financial</a></li>
                                        <li class="breadcrumb-item active">Tax Settings</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- General Tax Configuration -->
                    <div class="row">
                        <div class="col-xl-8">
                            <div class="tax-settings-card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">
                                        <i class="ri-settings-3-line me-2"></i>General Tax Configuration
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <form id="taxSettingsForm">
                                        <div class="setting-group">
                                            <h6 class="mb-3"><i class="ri-calculator-fill me-2"></i>Tax Calculation</h6>
                                            
                                            <div class="setting-item">
                                                <div>
                                                    <label class="form-label mb-0">Tax Calculation Method</label>
                                                    <small class="text-muted d-block">How should tax be calculated?</small>
                                                </div>
                                                <div>
                                                    <select class="form-select" name="settings[tax_calculation_method]" style="width: 200px;">
                                                        <option value="inclusive" <?php echo $default_settings['tax_calculation_method'] == 'inclusive' ? 'selected' : ''; ?>>
                                                            Tax Inclusive
                                                        </option>
                                                        <option value="exclusive" <?php echo $default_settings['tax_calculation_method'] == 'exclusive' ? 'selected' : ''; ?>>
                                                            Tax Exclusive
                                                        </option>
                                                    </select>
                                                </div>
                                            </div>
                                            
                                            <div class="setting-item">
                                                <div>
                                                    <label class="form-label mb-0">Default Tax Rate (%)</label>
                                                    <small class="text-muted d-block">Default rate when no specific rate is set</small>
                                                </div>
                                                <div>
                                                    <input type="number" class="form-control" name="settings[default_tax_rate]" 
                                                           value="<?php echo $default_settings['default_tax_rate']; ?>" 
                                                           step="0.01" min="0" max="100" style="width: 120px;">
                                                </div>
                                            </div>
                                            
                                            <div class="setting-item">
                                                <div>
                                                    <label class="form-label mb-0">Tax Rounding Method</label>
                                                    <small class="text-muted d-block">How to round tax calculations</small>
                                                </div>
                                                <div>
                                                    <select class="form-select" name="settings[tax_rounding_method]" style="width: 150px;">
                                                        <option value="round" <?php echo $default_settings['tax_rounding_method'] == 'round' ? 'selected' : ''; ?>>
                                                            Round
                                                        </option>
                                                        <option value="up" <?php echo $default_settings['tax_rounding_method'] == 'up' ? 'selected' : ''; ?>>
                                                            Round Up
                                                        </option>
                                                        <option value="down" <?php echo $default_settings['tax_rounding_method'] == 'down' ? 'selected' : ''; ?>>
                                                            Round Down
                                                        </option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="setting-group">
                                            <h6 class="mb-3"><i class="ri-eye-line me-2"></i>Display Settings</h6>
                                            
                                            <div class="setting-item">
                                                <div>
                                                    <label class="form-label mb-0">Price Display</label>
                                                    <small class="text-muted d-block">How to display prices to customers</small>
                                                </div>
                                                <div>
                                                    <select class="form-select" name="settings[tax_display_prices]" style="width: 200px;">
                                                        <option value="inclusive" <?php echo $default_settings['tax_display_prices'] == 'inclusive' ? 'selected' : ''; ?>>
                                                            Tax Inclusive
                                                        </option>
                                                        <option value="exclusive" <?php echo $default_settings['tax_display_prices'] == 'exclusive' ? 'selected' : ''; ?>>
                                                            Tax Exclusive
                                                        </option>
                                                        <option value="both" <?php echo $default_settings['tax_display_prices'] == 'both' ? 'selected' : ''; ?>>
                                                            Show Both
                                                        </option>
                                                    </select>
                                                </div>
                                            </div>
                                            
                                            <div class="setting-item">
                                                <div>
                                                    <label class="form-label mb-0">Invoice Tax Display</label>
                                                    <small class="text-muted d-block">How to show tax on invoices</small>
                                                </div>
                                                <div>
                                                    <select class="form-select" name="settings[tax_invoice_display]" style="width: 200px;">
                                                        <option value="separate" <?php echo $default_settings['tax_invoice_display'] == 'separate' ? 'selected' : ''; ?>>
                                                            Separate Line
                                                        </option>
                                                        <option value="included" <?php echo $default_settings['tax_invoice_display'] == 'included' ? 'selected' : ''; ?>>
                                                            Included in Price
                                                        </option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="setting-group">
                                            <h6 class="mb-3"><i class="ri-shield-check-line me-2"></i>Tax Exemptions</h6>
                                            
                                            <div class="setting-item">
                                                <div>
                                                    <label class="form-label mb-0">Enable Tax Exemptions</label>
                                                    <small class="text-muted d-block">Allow customers to be exempt from tax</small>
                                                </div>
                                                <div>
                                                    <div class="form-check form-switch">
                                                        <input class="form-check-input" type="checkbox" name="settings[tax_exemption_enabled]" 
                                                               value="1" <?php echo $default_settings['tax_exemption_enabled'] ? 'checked' : ''; ?>>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            <div class="setting-item">
                                                <div>
                                                    <label class="form-label mb-0">Tax Number Required</label>
                                                    <small class="text-muted d-block">Require tax ID for corporate customers</small>
                                                </div>
                                                <div>
                                                    <div class="form-check form-switch">
                                                        <input class="form-check-input" type="checkbox" name="settings[tax_number_required]" 
                                                               value="1" <?php echo $default_settings['tax_number_required'] ? 'checked' : ''; ?>>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="d-flex justify-content-end">
                                            <button type="submit" class="btn btn-primary">
                                                <i class="ri-save-line me-1"></i>Save Tax Settings
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-4">
                            <div class="tax-settings-card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">
                                        <i class="ri-information-line me-2"></i>Tax Configuration Guide
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <div class="alert alert-info">
                                        <h6><i class="ri-lightbulb-line me-1"></i>Quick Tips</h6>
                                        <ul class="mb-0">
                                            <li><strong>Tax Inclusive:</strong> Tax is included in the displayed price</li>
                                            <li><strong>Tax Exclusive:</strong> Tax is added to the displayed price</li>
                                            <li><strong>Country-specific:</strong> Set different rates for different countries</li>
                                            <li><strong>Exemptions:</strong> Some customers may be tax-exempt</li>
                                        </ul>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <h6>Common Tax Rates</h6>
                                        <table class="table table-sm">
                                            <tr><td>Kenya (VAT)</td><td>16%</td></tr>
                                            <tr><td>Uganda (VAT)</td><td>18%</td></tr>
                                            <tr><td>Tanzania (VAT)</td><td>18%</td></tr>
                                            <tr><td>Rwanda (VAT)</td><td>18%</td></tr>
                                            <tr><td>UK (VAT)</td><td>20%</td></tr>
                                            <tr><td>US (Sales Tax)</td><td>Varies</td></tr>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Country-Specific Tax Rates -->
                    <div class="row">
                        <div class="col-12">
                            <div class="tax-settings-card">
                                <div class="card-header">
                                    <div class="d-flex align-items-center justify-content-between">
                                        <h5 class="card-title mb-0">
                                            <i class="ri-global-line me-2"></i>Country-Specific Tax Rates
                                        </h5>
                                        <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addTaxRateModal">
                                            <i class="ri-add-line me-1"></i>Add Tax Rate
                                        </button>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <?php if (empty($tax_rates)): ?>
                                    <div class="text-center py-4">
                                        <i class="ri-tax-line display-4 text-muted"></i>
                                        <h5 class="mt-3">No Tax Rates Configured</h5>
                                        <p class="text-muted">Add country-specific tax rates to get started.</p>
                                        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addTaxRateModal">
                                            <i class="ri-add-line me-1"></i>Add Your First Tax Rate
                                        </button>
                                    </div>
                                    <?php else: ?>
                                    <div class="row">
                                        <?php foreach ($tax_rates as $key => $rate): ?>
                                        <div class="col-md-6 col-lg-4">
                                            <div class="tax-rate-card <?php echo $rate['default'] ? 'default' : ''; ?>">
                                                <div class="d-flex justify-content-between align-items-start mb-2">
                                                    <div>
                                                        <h6 class="mb-1"><?php echo htmlspecialchars($rate['name']); ?></h6>
                                                        <p class="text-muted mb-0"><?php echo $rate['country']; ?></p>
                                                    </div>
                                                    <div class="dropdown">
                                                        <button class="btn btn-sm btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown">
                                                            <i class="ri-more-2-line"></i>
                                                        </button>
                                                        <ul class="dropdown-menu">
                                                            <li><a class="dropdown-item" href="#" onclick="editTaxRate('<?php echo $key; ?>')">
                                                                <i class="ri-edit-line me-2"></i>Edit
                                                            </a></li>
                                                            <li><a class="dropdown-item text-danger" href="#" onclick="deleteTaxRate('<?php echo $key; ?>')">
                                                                <i class="ri-delete-bin-line me-2"></i>Delete
                                                            </a></li>
                                                        </ul>
                                                    </div>
                                                </div>
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <div>
                                                        <span class="fs-4 fw-bold text-primary"><?php echo $rate['rate']; ?>%</span>
                                                    </div>
                                                    <div>
                                                        <?php if ($rate['default']): ?>
                                                        <span class="tax-rate-badge rate-default">Default</span>
                                                        <?php endif; ?>
                                                        <span class="tax-rate-badge rate-<?php echo $rate['active'] ? 'active' : 'inactive'; ?>">
                                                            <?php echo $rate['active'] ? 'Active' : 'Inactive'; ?>
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php endforeach; ?>
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Tax Reports Preview -->
                    <div class="row">
                        <div class="col-12">
                            <div class="tax-settings-card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">
                                        <i class="ri-bar-chart-line me-2"></i>Tax Collection Summary (Last 30 Days)
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <div class="row text-center">
                                        <div class="col-md-3">
                                            <div class="p-3">
                                                <h4 class="text-primary">KSh 125,340</h4>
                                                <p class="text-muted mb-0">Total Tax Collected</p>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="p-3">
                                                <h4 class="text-success">1,247</h4>
                                                <p class="text-muted mb-0">Taxable Shipments</p>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="p-3">
                                                <h4 class="text-warning">16.0%</h4>
                                                <p class="text-muted mb-0">Average Tax Rate</p>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="p-3">
                                                <h4 class="text-info">45</h4>
                                                <p class="text-muted mb-0">Tax-Exempt Shipments</p>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="text-center mt-3">
                                        <a href="tax_reports.php" class="btn btn-outline-primary">
                                            <i class="ri-external-link-line me-1"></i>View Detailed Tax Reports
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <!-- Add Tax Rate Modal -->
    <div class="modal fade" id="addTaxRateModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="ri-add-line me-2"></i>Add Tax Rate
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form id="addTaxRateForm">
                    <div class="modal-body">
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">Country <span class="text-danger">*</span></label>
                                <select class="form-select" name="country_code" required>
                                    <option value="">Select Country</option>
                                    <?php while ($country = $countries_result->fetch_assoc()): ?>
                                    <option value="<?php echo $country['country_code']; ?>">
                                        <?php echo $country['country_name']; ?>
                                    </option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Tax Name <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" name="tax_name" placeholder="e.g., VAT, Sales Tax" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Tax Rate (%) <span class="text-danger">*</span></label>
                                <input type="number" class="form-control" name="tax_rate" step="0.01" min="0" max="100" required>
                            </div>
                            <div class="col-md-6 d-flex align-items-end">
                                <div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="is_default" id="is_default">
                                        <label class="form-check-label" for="is_default">
                                            Set as Default
                                        </label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="is_active" id="is_active" checked>
                                        <label class="form-check-label" for="is_active">
                                            Active
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">
                            <i class="ri-save-line me-1"></i>Add Tax Rate
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.32/dist/sweetalert2.all.min.js"></script>
    
    <script>
    $(document).ready(function() {
        // Handle tax settings form submission
        $('#taxSettingsForm').on('submit', function(e) {
            e.preventDefault();
            saveTaxSettings();
        });

        // Handle add tax rate form submission
        $('#addTaxRateForm').on('submit', function(e) {
            e.preventDefault();
            addTaxRate();
        });
    });

    function saveTaxSettings() {
        const formData = new FormData($('#taxSettingsForm')[0]);
        formData.append('action', 'update_tax_settings');
        
        Swal.fire({
            title: 'Saving...',
            text: 'Please wait while we save your tax settings.',
            allowOutsideClick: false,
            didOpen: () => {
                Swal.showLoading();
            }
        });
        
        $.ajax({
            url: '',
            method: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                const result = JSON.parse(response);
                if (result.success) {
                    Swal.fire('Success!', result.message, 'success');
                } else {
                    Swal.fire('Error!', result.message, 'error');
                }
            },
            error: function() {
                Swal.fire('Error!', 'Something went wrong. Please try again.', 'error');
            }
        });
    }

    function addTaxRate() {
        const formData = new FormData($('#addTaxRateForm')[0]);
        formData.append('action', 'add_tax_rate');
        
        $.ajax({
            url: '',
            method: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                const result = JSON.parse(response);
                if (result.success) {
                    Swal.fire('Success!', result.message, 'success').then(() => {
                        $('#addTaxRateModal').modal('hide');
                        location.reload();
                    });
                } else {
                    Swal.fire('Error!', result.message, 'error');
                }
            },
            error: function() {
                Swal.fire('Error!', 'Something went wrong. Please try again.', 'error');
            }
        });
    }

    function editTaxRate(rateKey) {
        // Implementation for editing tax rates
        Swal.fire({
            title: 'Edit Tax Rate',
            text: 'Tax rate editing functionality will be implemented here.',
            icon: 'info'
        });
    }

    function deleteTaxRate(rateKey) {
        Swal.fire({
            title: 'Delete Tax Rate',
            text: 'Are you sure you want to delete this tax rate?',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Yes, Delete',
            cancelButtonText: 'Cancel',
            confirmButtonColor: '#dc3545'
        }).then((result) => {
            if (result.isConfirmed) {
                // Implementation for deleting tax rates
                Swal.fire('Deleted!', 'Tax rate has been deleted.', 'success').then(() => {
                    location.reload();
                });
            }
        });
    }
    </script>

    <script src="assets/js/app.js"></script>
</body>
</html>