<?php
/**
 * RUHONDO BEACH RESORT HMS + POS System
 * Tax & Currency Settings
 * Developer: JOACHIM'S INNOTECH
 */

// Initialize system
require_once dirname(__DIR__, 2) . '/config/config.php';
require_once dirname(__DIR__, 2) . '/config/constants.php';
require_once dirname(__DIR__, 2) . '/config/database.php';
require_once dirname(__DIR__, 2) . '/config/helpers.php';
require_once dirname(__DIR__, 2) . '/config/auth.php';
require_once dirname(__DIR__, 2) . '/config/csrf.php';

// Require authentication and permission
require_auth();
require_permission('settings.edit');

// Set page title
$page_title = 'Tax & Currency Settings';

// Get current user
$current_user = current_user();

// Handle actions
$message = '';
$error = '';

// Tax Rate CRUD
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tax_action'])) {
    if (!verify_csrf_token($_POST[CSRF_TOKEN_NAME] ?? '')) {
        $error = 'Invalid security token. Please try again.';
    } else {
        $action = $_POST['tax_action'];
        
        switch ($action) {
            case 'add_tax':
                $tax_name = sanitize_input($_POST['tax_name'] ?? '');
                $tax_rate = (float)($_POST['tax_rate'] ?? 0);
                $tax_type = sanitize_input($_POST['tax_type'] ?? TAX_TYPE_EXCLUSIVE);
                $applies_to_rooms = isset($_POST['applies_to_rooms']) ? 1 : 0;
                $applies_to_fnb = isset($_POST['applies_to_fnb']) ? 1 : 0;
                $applies_to_services = isset($_POST['applies_to_services']) ? 1 : 0;
                
                if (empty($tax_name)) {
                    $error = 'Tax name is required';
                } elseif ($tax_rate < 0 || $tax_rate > 100) {
                    $error = 'Tax rate must be between 0 and 100';
                } else {
                    try {
                        db_insert('tax_rates', [
                            'tax_name' => $tax_name,
                            'tax_rate' => $tax_rate,
                            'tax_type' => $tax_type,
                            'applies_to_rooms' => $applies_to_rooms,
                            'applies_to_fnb' => $applies_to_fnb,
                            'applies_to_services' => $applies_to_services,
                            'is_active' => 1
                        ]);
                        $message = 'Tax rate added successfully';
                        audit_log($current_user['user_id'], AUDIT_CREATE, 'tax_rates', null, null, $_POST);
                    } catch (Exception $e) {
                        $error = 'Failed to add tax rate: ' . $e->getMessage();
                    }
                }
                break;
                
            case 'edit_tax':
                $tax_id = (int)($_POST['tax_id'] ?? 0);
                $tax_name = sanitize_input($_POST['tax_name'] ?? '');
                $tax_rate = (float)($_POST['tax_rate'] ?? 0);
                $tax_type = sanitize_input($_POST['tax_type'] ?? TAX_TYPE_EXCLUSIVE);
                $applies_to_rooms = isset($_POST['applies_to_rooms']) ? 1 : 0;
                $applies_to_fnb = isset($_POST['applies_to_fnb']) ? 1 : 0;
                $applies_to_services = isset($_POST['applies_to_services']) ? 1 : 0;
                $is_active = isset($_POST['is_active']) ? 1 : 0;
                
                if (!$tax_id) {
                    $error = 'Invalid tax ID';
                } elseif (empty($tax_name)) {
                    $error = 'Tax name is required';
                } elseif ($tax_rate < 0 || $tax_rate > 100) {
                    $error = 'Tax rate must be between 0 and 100';
                } else {
                    try {
                        $old = db_fetch_one("SELECT * FROM tax_rates WHERE tax_id = ?", [$tax_id]);
                        db_update('tax_rates', [
                            'tax_name' => $tax_name,
                            'tax_rate' => $tax_rate,
                            'tax_type' => $tax_type,
                            'applies_to_rooms' => $applies_to_rooms,
                            'applies_to_fnb' => $applies_to_fnb,
                            'applies_to_services' => $applies_to_services,
                            'is_active' => $is_active
                        ], 'tax_id = ?', [$tax_id]);
                        $message = 'Tax rate updated successfully';
                        audit_log($current_user['user_id'], AUDIT_UPDATE, 'tax_rates', $tax_id, $old, $_POST);
                    } catch (Exception $e) {
                        $error = 'Failed to update tax rate: ' . $e->getMessage();
                    }
                }
                break;
                
            case 'delete_tax':
                $tax_id = (int)($_POST['tax_id'] ?? 0);
                
                if (!$tax_id) {
                    $error = 'Invalid tax ID';
                } else {
                    // Check if tax is in use
                    $in_use = db_fetch_value("SELECT COUNT(*) FROM pos_products WHERE tax_id = ?", [$tax_id]);
                    $in_use += db_fetch_value("SELECT COUNT(*) FROM pos_outlets WHERE tax_id = ?", [$tax_id]);
                    
                    if ($in_use > 0) {
                        $error = 'Cannot delete tax rate that is in use';
                    } else {
                        try {
                            db_delete('tax_rates', 'tax_id = ?', [$tax_id]);
                            $message = 'Tax rate deleted successfully';
                            audit_log($current_user['user_id'], AUDIT_DELETE, 'tax_rates', $tax_id);
                        } catch (Exception $e) {
                            $error = 'Failed to delete tax rate: ' . $e->getMessage();
                        }
                    }
                }
                break;
        }
    }
}

// Currency CRUD
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['currency_action'])) {
    if (!verify_csrf_token($_POST[CSRF_TOKEN_NAME] ?? '')) {
        $error = 'Invalid security token. Please try again.';
    } else {
        $action = $_POST['currency_action'];
        
        switch ($action) {
            case 'add_currency':
                $currency_code = strtoupper(sanitize_input($_POST['currency_code'] ?? ''));
                $currency_name = sanitize_input($_POST['currency_name'] ?? '');
                $currency_symbol = sanitize_input($_POST['currency_symbol'] ?? '');
                $exchange_rate = (float)($_POST['exchange_rate'] ?? 1);
                $is_base_currency = isset($_POST['is_base_currency']) ? 1 : 0;
                
                if (empty($currency_code) || strlen($currency_code) != 3) {
                    $error = 'Valid 3-letter currency code is required';
                } elseif (empty($currency_name)) {
                    $error = 'Currency name is required';
                } elseif ($exchange_rate <= 0) {
                    $error = 'Exchange rate must be greater than 0';
                } else {
                    // Check if currency exists
                    $exists = db_fetch_value("SELECT COUNT(*) FROM currency_rates WHERE currency_code = ?", [$currency_code]);
                    if ($exists) {
                        $error = 'Currency code already exists';
                    } else {
                        try {
                            if ($is_base_currency) {
                                // Clear any existing base currency
                                db_update('currency_rates', ['is_base_currency' => 0], '1=1', []);
                            }
                            
                            db_insert('currency_rates', [
                                'currency_code' => $currency_code,
                                'currency_name' => $currency_name,
                                'currency_symbol' => $currency_symbol,
                                'exchange_rate' => $exchange_rate,
                                'is_base_currency' => $is_base_currency,
                                'is_active' => 1
                            ]);
                            $message = 'Currency added successfully';
                            audit_log($current_user['user_id'], AUDIT_CREATE, 'currency_rates', null, null, $_POST);
                        } catch (Exception $e) {
                            $error = 'Failed to add currency: ' . $e->getMessage();
                        }
                    }
                }
                break;
                
            case 'edit_currency':
                $currency_id = (int)($_POST['currency_id'] ?? 0);
                $currency_code = strtoupper(sanitize_input($_POST['currency_code'] ?? ''));
                $currency_name = sanitize_input($_POST['currency_name'] ?? '');
                $currency_symbol = sanitize_input($_POST['currency_symbol'] ?? '');
                $exchange_rate = (float)($_POST['exchange_rate'] ?? 1);
                $is_base_currency = isset($_POST['is_base_currency']) ? 1 : 0;
                $is_active = isset($_POST['is_active']) ? 1 : 0;
                
                if (!$currency_id) {
                    $error = 'Invalid currency ID';
                } elseif (empty($currency_code) || strlen($currency_code) != 3) {
                    $error = 'Valid 3-letter currency code is required';
                } elseif (empty($currency_name)) {
                    $error = 'Currency name is required';
                } elseif ($exchange_rate <= 0) {
                    $error = 'Exchange rate must be greater than 0';
                } else {
                    // Check if currency code exists for another record
                    $exists = db_fetch_value("SELECT COUNT(*) FROM currency_rates WHERE currency_code = ? AND currency_id != ?", 
                                            [$currency_code, $currency_id]);
                    if ($exists) {
                        $error = 'Currency code already exists';
                    } else {
                        try {
                            if ($is_base_currency) {
                                // Clear any existing base currency
                                db_update('currency_rates', ['is_base_currency' => 0], '1=1', []);
                            }
                            
                            $old = db_fetch_one("SELECT * FROM currency_rates WHERE currency_id = ?", [$currency_id]);
                            db_update('currency_rates', [
                                'currency_code' => $currency_code,
                                'currency_name' => $currency_name,
                                'currency_symbol' => $currency_symbol,
                                'exchange_rate' => $exchange_rate,
                                'is_base_currency' => $is_base_currency,
                                'is_active' => $is_active
                            ], 'currency_id = ?', [$currency_id]);
                            $message = 'Currency updated successfully';
                            audit_log($current_user['user_id'], AUDIT_UPDATE, 'currency_rates', $currency_id, $old, $_POST);
                        } catch (Exception $e) {
                            $error = 'Failed to update currency: ' . $e->getMessage();
                        }
                    }
                }
                break;
                
            case 'delete_currency':
                $currency_id = (int)($_POST['currency_id'] ?? 0);
                
                if (!$currency_id) {
                    $error = 'Invalid currency ID';
                } else {
                    // Check if it's base currency
                    $is_base = db_fetch_value("SELECT is_base_currency FROM currency_rates WHERE currency_id = ?", [$currency_id]);
                    if ($is_base) {
                        $error = 'Cannot delete base currency';
                    } else {
                        try {
                            db_delete('currency_rates', 'currency_id = ?', [$currency_id]);
                            $message = 'Currency deleted successfully';
                            audit_log($current_user['user_id'], AUDIT_DELETE, 'currency_rates', $currency_id);
                        } catch (Exception $e) {
                            $error = 'Failed to delete currency: ' . $e->getMessage();
                        }
                    }
                }
                break;
        }
    }
}

// Get all tax rates
$tax_rates = db_fetch_all("SELECT * FROM tax_rates ORDER BY tax_name");

// Get all currencies
$currencies = db_fetch_all("SELECT * FROM currency_rates ORDER BY is_base_currency DESC, currency_code");

// Get tax to edit
$edit_tax = null;
if (isset($_GET['edit_tax'])) {
    $edit_tax = db_fetch_one("SELECT * FROM tax_rates WHERE tax_id = ?", [(int)$_GET['edit_tax']]);
}

// Get currency to edit
$edit_currency = null;
if (isset($_GET['edit_currency'])) {
    $edit_currency = db_fetch_one("SELECT * FROM currency_rates WHERE currency_id = ?", [(int)$_GET['edit_currency']]);
}

// Include header
require_once dirname(__DIR__, 2) . '/views/partials/header.php';
?>

<!-- Content Header -->
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0">Tax & Currency Settings</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="<?php echo base_url('dashboard.php'); ?>">Home</a></li>
                    <li class="breadcrumb-item"><a href="<?php echo base_url('admin/dashboard.php'); ?>">Admin</a></li>
                    <li class="breadcrumb-item"><a href="<?php echo base_url('admin/settings/index.php'); ?>">Settings</a></li>
                    <li class="breadcrumb-item active">Tax & Currency</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <!-- Messages -->
        <?php if ($message): ?>
            <div class="alert alert-success alert-dismissible fade show">
                <i class="fas fa-check-circle mr-2"></i>
                <?php echo $message; ?>
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
        <?php endif; ?>
        
        <?php if ($error): ?>
            <div class="alert alert-danger alert-dismissible fade show">
                <i class="fas fa-exclamation-circle mr-2"></i>
                <?php echo $error; ?>
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
        <?php endif; ?>

        <div class="row">
            <!-- Tax Rates Section -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-percent mr-1"></i>
                            Tax Rates
                        </h3>
                        <div class="card-tools">
                            <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#addTaxModal">
                                <i class="fas fa-plus"></i> Add Tax
                            </button>
                        </div>
                    </div>
                    
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Rate</th>
                                        <th>Type</th>
                                        <th>Applies To</th>
                                        <th>Status</th>
                                        <th width="100">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($tax_rates as $tax): ?>
                                    <tr>
                                        <td><?php echo escape_html($tax['tax_name']); ?></td>
                                        <td><?php echo $tax['tax_rate']; ?>%</td>
                                        <td>
                                            <span class="badge badge-<?php echo $tax['tax_type'] == TAX_TYPE_INCLUSIVE ? 'info' : 'warning'; ?>">
                                                <?php echo ucfirst($tax['tax_type']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php 
                                            $applies = [];
                                            if ($tax['applies_to_rooms']) $applies[] = 'Rooms';
                                            if ($tax['applies_to_fnb']) $applies[] = 'F&B';
                                            if ($tax['applies_to_services']) $applies[] = 'Services';
                                            echo $applies ? implode(', ', $applies) : 'None';
                                            ?>
                                        </td>
                                        <td>
                                            <?php if ($tax['is_active']): ?>
                                                <span class="badge badge-success">Active</span>
                                            <?php else: ?>
                                                <span class="badge badge-danger">Inactive</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <a href="?edit_tax=<?php echo $tax['tax_id']; ?>" class="btn btn-sm btn-warning">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <?php if ($tax['tax_id'] > 3): // Don't delete default taxes ?>
                                                <button type="button" class="btn btn-sm btn-danger" 
                                                        onclick="deleteTax(<?php echo $tax['tax_id']; ?>, '<?php echo escape_html($tax['tax_name']); ?>')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- Edit Tax Form -->
                <?php if ($edit_tax): ?>
                <div class="card card-warning mt-3">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-edit mr-1"></i>
                            Edit Tax: <?php echo escape_html($edit_tax['tax_name']); ?>
                        </h3>
                    </div>
                    <div class="card-body">
                        <form method="POST">
                            <?php echo csrf_field(); ?>
                            <input type="hidden" name="tax_action" value="edit_tax">
                            <input type="hidden" name="tax_id" value="<?php echo $edit_tax['tax_id']; ?>">
                            
                            <div class="form-group">
                                <label>Tax Name</label>
                                <input type="text" class="form-control" name="tax_name" 
                                       value="<?php echo escape_html($edit_tax['tax_name']); ?>" required>
                            </div>
                            
                            <div class="form-group">
                                <label>Tax Rate (%)</label>
                                <input type="number" class="form-control" name="tax_rate" step="0.01" min="0" max="100"
                                       value="<?php echo $edit_tax['tax_rate']; ?>" required>
                            </div>
                            
                            <div class="form-group">
                                <label>Tax Type</label>
                                <select class="form-control" name="tax_type">
                                    <option value="<?php echo TAX_TYPE_EXCLUSIVE; ?>" <?php echo $edit_tax['tax_type'] == TAX_TYPE_EXCLUSIVE ? 'selected' : ''; ?>>
                                        Exclusive (added to price)
                                    </option>
                                    <option value="<?php echo TAX_TYPE_INCLUSIVE; ?>" <?php echo $edit_tax['tax_type'] == TAX_TYPE_INCLUSIVE ? 'selected' : ''; ?>>
                                        Inclusive (included in price)
                                    </option>
                                </select>
                            </div>
                            
                            <div class="form-group">
                                <label>Applies To</label>
                                <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="applies_rooms_edit" name="applies_to_rooms"
                                           <?php echo $edit_tax['applies_to_rooms'] ? 'checked' : ''; ?>>
                                    <label class="custom-control-label" for="applies_rooms_edit">Rooms</label>
                                </div>
                                <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="applies_fnb_edit" name="applies_to_fnb"
                                           <?php echo $edit_tax['applies_to_fnb'] ? 'checked' : ''; ?>>
                                    <label class="custom-control-label" for="applies_fnb_edit">Food & Beverage</label>
                                </div>
                                <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="applies_services_edit" name="applies_to_services"
                                           <?php echo $edit_tax['applies_to_services'] ? 'checked' : ''; ?>>
                                    <label class="custom-control-label" for="applies_services_edit">Services</label>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <div class="custom-control custom-switch">
                                    <input type="checkbox" class="custom-control-input" id="is_active_edit" name="is_active"
                                           <?php echo $edit_tax['is_active'] ? 'checked' : ''; ?>>
                                    <label class="custom-control-label" for="is_active_edit">Active</label>
                                </div>
                            </div>
                            
                            <button type="submit" class="btn btn-warning">Update Tax</button>
                            <a href="?" class="btn btn-secondary">Cancel</a>
                        </form>
                    </div>
                </div>
                <?php endif; ?>
            </div>

            <!-- Currencies Section -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-coins mr-1"></i>
                            Currencies
                        </h3>
                        <div class="card-tools">
                            <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#addCurrencyModal">
                                <i class="fas fa-plus"></i> Add Currency
                            </button>
                        </div>
                    </div>
                    
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>Code</th>
                                        <th>Name</th>
                                        <th>Symbol</th>
                                        <th>Exchange Rate</th>
                                        <th>Base</th>
                                        <th>Status</th>
                                        <th width="100">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($currencies as $currency): ?>
                                    <tr>
                                        <td><strong><?php echo $currency['currency_code']; ?></strong></td>
                                        <td><?php echo escape_html($currency['currency_name']); ?></td>
                                        <td><?php echo escape_html($currency['currency_symbol']); ?></td>
                                        <td>
                                            <?php if ($currency['is_base_currency']): ?>
                                                <span class="text-muted">Base</span>
                                            <?php else: ?>
                                                <?php echo number_format($currency['exchange_rate'], 4); ?>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php if ($currency['is_base_currency']): ?>
                                                <span class="badge badge-success">Base</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php if ($currency['is_active']): ?>
                                                <span class="badge badge-success">Active</span>
                                            <?php else: ?>
                                                <span class="badge badge-danger">Inactive</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <a href="?edit_currency=<?php echo $currency['currency_id']; ?>" class="btn btn-sm btn-warning">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <?php if (!$currency['is_base_currency']): ?>
                                                <button type="button" class="btn btn-sm btn-danger" 
                                                        onclick="deleteCurrency(<?php echo $currency['currency_id']; ?>, '<?php echo $currency['currency_code']; ?>')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- Edit Currency Form -->
                <?php if ($edit_currency): ?>
                <div class="card card-warning mt-3">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-edit mr-1"></i>
                            Edit Currency: <?php echo $edit_currency['currency_code']; ?>
                        </h3>
                    </div>
                    <div class="card-body">
                        <form method="POST">
                            <?php echo csrf_field(); ?>
                            <input type="hidden" name="currency_action" value="edit_currency">
                            <input type="hidden" name="currency_id" value="<?php echo $edit_currency['currency_id']; ?>">
                            
                            <div class="form-group">
                                <label>Currency Code (3 letters)</label>
                                <input type="text" class="form-control" name="currency_code" maxlength="3" 
                                       value="<?php echo $edit_currency['currency_code']; ?>" required>
                            </div>
                            
                            <div class="form-group">
                                <label>Currency Name</label>
                                <input type="text" class="form-control" name="currency_name" 
                                       value="<?php echo escape_html($edit_currency['currency_name']); ?>" required>
                            </div>
                            
                            <div class="form-group">
                                <label>Currency Symbol</label>
                                <input type="text" class="form-control" name="currency_symbol" 
                                       value="<?php echo escape_html($edit_currency['currency_symbol']); ?>" required>
                            </div>
                            
                            <div class="form-group">
                                <label>Exchange Rate (to base currency)</label>
                                <input type="number" class="form-control" name="exchange_rate" step="0.0001" min="0.0001"
                                       value="<?php echo $edit_currency['exchange_rate']; ?>" 
                                       <?php echo $edit_currency['is_base_currency'] ? 'readonly' : ''; ?>>
                                <?php if ($edit_currency['is_base_currency']): ?>
                                    <small class="text-muted">Base currency always has rate 1.0000</small>
                                <?php endif; ?>
                            </div>
                            
                            <div class="form-group">
                                <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="is_base_edit" name="is_base_currency"
                                           <?php echo $edit_currency['is_base_currency'] ? 'checked' : ''; ?>>
                                    <label class="custom-control-label" for="is_base_edit">Set as base currency</label>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <div class="custom-control custom-switch">
                                    <input type="checkbox" class="custom-control-input" id="is_active_currency_edit" name="is_active"
                                           <?php echo $edit_currency['is_active'] ? 'checked' : ''; ?>>
                                    <label class="custom-control-label" for="is_active_currency_edit">Active</label>
                                </div>
                            </div>
                            
                            <button type="submit" class="btn btn-warning">Update Currency</button>
                            <a href="?" class="btn btn-secondary">Cancel</a>
                        </form>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>

<!-- Add Tax Modal -->
<div class="modal fade" id="addTaxModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary">
                <h5 class="modal-title">Add Tax Rate</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form method="POST">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="tax_action" value="add_tax">
                
                <div class="modal-body">
                    <div class="form-group">
                        <label>Tax Name</label>
                        <input type="text" class="form-control" name="tax_name" required>
                    </div>
                    
                    <div class="form-group">
                        <label>Tax Rate (%)</label>
                        <input type="number" class="form-control" name="tax_rate" step="0.01" min="0" max="100" required>
                    </div>
                    
                    <div class="form-group">
                        <label>Tax Type</label>
                        <select class="form-control" name="tax_type">
                            <option value="<?php echo TAX_TYPE_EXCLUSIVE; ?>">Exclusive (added to price)</option>
                            <option value="<?php echo TAX_TYPE_INCLUSIVE; ?>">Inclusive (included in price)</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label>Applies To</label>
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" id="applies_rooms" name="applies_to_rooms">
                            <label class="custom-control-label" for="applies_rooms">Rooms</label>
                        </div>
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" id="applies_fnb" name="applies_to_fnb">
                            <label class="custom-control-label" for="applies_fnb">Food & Beverage</label>
                        </div>
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" id="applies_services" name="applies_to_services">
                            <label class="custom-control-label" for="applies_services">Services</label>
                        </div>
                    </div>
                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Add Tax</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Add Currency Modal -->
<div class="modal fade" id="addCurrencyModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary">
                <h5 class="modal-title">Add Currency</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form method="POST">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="currency_action" value="add_currency">
                
                <div class="modal-body">
                    <div class="form-group">
                        <label>Currency Code (3 letters)</label>
                        <input type="text" class="form-control" name="currency_code" maxlength="3" required>
                    </div>
                    
                    <div class="form-group">
                        <label>Currency Name</label>
                        <input type="text" class="form-control" name="currency_name" required>
                    </div>
                    
                    <div class="form-group">
                        <label>Currency Symbol</label>
                        <input type="text" class="form-control" name="currency_symbol" required>
                    </div>
                    
                    <div class="form-group">
                        <label>Exchange Rate (to base currency)</label>
                        <input type="number" class="form-control" name="exchange_rate" step="0.0001" min="0.0001" value="1.0000" required>
                    </div>
                    
                    <div class="form-group">
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" id="is_base" name="is_base_currency">
                            <label class="custom-control-label" for="is_base">Set as base currency</label>
                        </div>
                        <small class="text-muted">Note: Only one currency can be base. Base currency has exchange rate 1.0000.</small>
                    </div>
                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Add Currency</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Tax Modal -->
<div class="modal fade" id="deleteTaxModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger">
                <h5 class="modal-title">Delete Tax Rate</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete tax rate <strong id="deleteTaxName"></strong>?</p>
            </div>
            <div class="modal-footer">
                <form method="POST" id="deleteTaxForm">
                    <?php echo csrf_field(); ?>
                    <input type="hidden" name="tax_action" value="delete_tax">
                    <input type="hidden" name="tax_id" id="deleteTaxId">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Delete Currency Modal -->
<div class="modal fade" id="deleteCurrencyModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger">
                <h5 class="modal-title">Delete Currency</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete currency <strong id="deleteCurrencyCode"></strong>?</p>
            </div>
            <div class="modal-footer">
                <form method="POST" id="deleteCurrencyForm">
                    <?php echo csrf_field(); ?>
                    <input type="hidden" name="currency_action" value="delete_currency">
                    <input type="hidden" name="currency_id" id="deleteCurrencyId">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function deleteTax(taxId, taxName) {
    $('#deleteTaxId').val(taxId);
    $('#deleteTaxName').text(taxName);
    $('#deleteTaxModal').modal('show');
}

function deleteCurrency(currencyId, currencyCode) {
    $('#deleteCurrencyId').val(currencyId);
    $('#deleteCurrencyCode').text(currencyCode);
    $('#deleteCurrencyModal').modal('show');
}
</script>

<?php
// Include footer
require_once dirname(__DIR__, 2) . '/views/partials/footer.php';
?>