<?php
require_once 'config/config.php';

session_start();

// حماية الصفحة
if (!isset($_SESSION['user_id'])) {
    header("Location: auth/login.php");
    exit;
}

// اللغة
$lang_code = $_SESSION['language'] ?? 'english';
$lang = require "languages/$lang_code.php";

// حفظ البيانات
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $name   = $_POST['company_name'] ?? '';
    $type   = $_POST['car_type'] ?? 'fuel';
    $min    = $_POST['min_price'] ?? 0;
    $per    = $_POST['percentage'] ?? 0;
    $extra  = $_POST['extra_amount'] ?? 0;

    if (!empty($name)) {

        $sql = "INSERT INTO insurance_companies 
        (company_name, car_type, min_price, percentage, extra_amount)
        VALUES (?, ?, ?, ?, ?)";

        $stmt = $conn->prepare($sql);
        $stmt->bind_param("ssddd", $name, $type, $min, $per, $extra);
        $stmt->execute();
    }
}

// جلب الشركات
$result = $conn->query("SELECT * FROM insurance_companies ORDER BY id DESC");
?>

<?php include 'header.php'; ?>

<div class="main-content p-4" style="flex:1;">

    <h2 class="mb-4"><?= $lang['add_company']; ?></h2>

    <!-- نموذج الإضافة -->
    <div class="card p-4 mb-4">

        <form method="POST">

            <div class="row mb-3">

                <div class="col">
                    <label class="form-label"><?= $lang['company_name']; ?></label>
                    <input type="text" name="company_name" class="form-control" required>
                </div>

                <div class="col">
                    <label class="form-label"><?= $lang['car_type']; ?></label>
                    <select name="car_type" class="form-control">
                        <option value="fuel"><?= $lang['fuel_hybrid']; ?></option>
                        <option value="electric"><?= $lang['electric']; ?></option>
                    </select>
                </div>

            </div>

            <div class="row mb-3">

                <div class="col">
                    <label class="form-label"><?= $lang['min_price']; ?></label>
                    <input type="number" step="0.01" name="min_price" class="form-control" required>
                </div>

                <div class="col">
                    <label class="form-label"><?= $lang['percentage']; ?></label>
                    <input type="number" step="0.01" name="percentage" class="form-control" required>
                </div>

                <div class="col">
                    <label class="form-label"><?= $lang['extra_amount']; ?></label>
                    <input type="number" step="0.01" name="extra_amount" class="form-control" required>
                </div>

            </div>

            <button class="btn btn-success">
                <?= $lang['save']; ?>
            </button>

        </form>

    </div>

    <!-- جدول الشركات -->
    <div class="card p-4">

        <table class="table table-bordered table-striped">

            <tr>
                <th><?= $lang['company']; ?></th>
                <th><?= $lang['car_type']; ?></th>
                <th><?= $lang['min_price']; ?></th>
                <th><?= $lang['percentage']; ?></th>
                <th><?= $lang['extra_amount']; ?></th>
            </tr>

            <?php while($row = $result->fetch_assoc()): ?>

            <tr>
                <td><?= htmlspecialchars($row['company_name']); ?></td>

                <td>
                    <?= $row['car_type'] == 'fuel' ? $lang['fuel_hybrid'] : $lang['electric']; ?>
                </td>

                <td><?= number_format($row['min_price'], 2); ?></td>
                <td><?= number_format($row['percentage'], 2); ?> %</td>
                <td><?= number_format($row['extra_amount'], 2); ?></td>
            </tr>

            <?php endwhile; ?>

        </table>

    </div>

</div>

<?php include 'includes/footer.php'; ?>