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

session_start();

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

// البيانات
$type = $_POST['type'] ?? 'fuel';
$car_value = $_POST['car_value'] ?? 0;

// حماية
if ($car_value <= 0) {
    echo "<div class='alert alert-danger'>Invalid value</div>";
    exit;
}

// جلب الشركات
$sql = "SELECT * FROM insurance_companies WHERE car_type=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $type);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 0) {
    echo "<div class='alert alert-warning'>No companies found</div>";
    exit;
}

echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>".$lang['company']."</th>";
echo "<th>".$lang['price']."</th>";
echo "</tr>";

while ($row = $result->fetch_assoc()) {

    // الحساب
    $price = ($car_value * $row['percentage'] / 100) + $row['extra_amount'];

    // شرط الحد الأدنى (المهم)
    if ($price < $row['min_price']) {
        $price = $row['min_price'];
    }

    // تنسيق الرقم
    $price = number_format($price, 2);

    echo "<tr>";
    echo "<td>".$row['company_name']."</td>";
    echo "<td>".$price."</td>";
    echo "</tr>";
}

echo "</table>";