<?php
session_start();
include 'layouts/main.php';
include 'layouts/dbconn.php';

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

// Handle Form Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $vat_rate = $_POST['vat_rate'] ?? 0;
    $wht_rate = $_POST['wht_rate'] ?? 0;
    $tax_inclusive = isset($_POST['tax_inclusive']) ? 'true' : 'false';

    // Helper function to update setting
    function updateSetting($conn, $key, $value, $type, $category)
    {
        $check = $conn->prepare("SELECT setting_id FROM system_settings WHERE setting_key = ?");
        $check->bind_param("s", $key);
        $check->execute();
        if ($check->get_result()->num_rows > 0) {
            $sql = "UPDATE system_settings SET setting_value = ?, updated_at = NOW(), updated_by = ? WHERE setting_key = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("sis", $value, $_SESSION['user_id'], $key);
        } else {
            $sql = "INSERT INTO system_settings (setting_key, setting_value, setting_type, category, is_public, updated_by) VALUES (?, ?, ?, ?, 0, ?)";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("ssssi", $key, $value, $type, $category, $_SESSION['user_id']);
        }
        $stmt->execute();
    }

    updateSetting($conn, 'tax_vat_rate', $vat_rate, 'DECIMAL', 'financial');
    updateSetting($conn, 'tax_withholding_rate', $wht_rate, 'DECIMAL', 'financial');
    updateSetting($conn, 'tax_inclusive_prices', $tax_inclusive, 'BOOLEAN', 'financial');

    $success_msg = "Tax settings updated successfully.";
}

// Fetch current settings
$settings = [];
$res = $conn->query("SELECT setting_key, setting_value FROM system_settings WHERE category = 'financial'");
while ($row = $res->fetch_assoc()) {
    $settings[$row['setting_key']] = $row['setting_value'];
}
$vat_rate = $settings['tax_vat_rate'] ?? 16;
$wht_rate = $settings['tax_withholding_rate'] ?? 5;
$tax_inclusive = ($settings['tax_inclusive_prices'] ?? 'true') === 'true';
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Tax Configuration')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">

                    <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">Tax Configuration</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-6">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">General Tax Settings</h5>
                                </div>
                                <div class="card-body">
                                    <form>
                                        <div class="mb-3">
                                            <label class="form-label">Default VAT Rate (%)</label>
                                            <input type="number" class="form-control" value="16">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Withholding Tax Rate (%)</label>
                                            <input type="number" class="form-control" value="5">
                                        </div>
                                        <div class="form-check form-switch mb-3">
                                            <input class="form-check-input" type="checkbox" role="switch"
                                                id="taxInclusive" checked>
                                            <label class="form-check-label" for="taxInclusive">Prices enter are Tax
                                                Inclusive</label>
                                        </div>
                                        <button type="submit" class="btn btn-primary">Save Settings</button>
                                    </form>
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-6">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">Category Specific Rules</h5>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>Category</th>
                                                <th>Tax Status</th>
                                                <th>Rate</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>Consultation</td>
                                                <td>Exempt</td>
                                                <td>0%</td>
                                            </tr>
                                            <tr>
                                                <td>Medicine / Pharmacy</td>
                                                <td>Taxable</td>
                                                <td>16%</td>
                                            </tr>
                                            <tr>
                                                <td>Procedures</td>
                                                <td>Exempt</td>
                                                <td>0%</td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    <button class="btn btn-soft-primary w-100">Configure Categories</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>