<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");  

$tablename = 'juilcarlog';
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();

$car_table = 'juilcar';

// initial_distance 컬럼이 있는지 확인
$check_column_sql = "SHOW COLUMNS FROM " . $DB . "." . $car_table . " LIKE 'initial_distance'";
$check_stmh = $pdo->query($check_column_sql);
$column_exists = $check_stmh->rowCount() > 0;

if (!$column_exists) {
    // initial_distance 컬럼 추가
    $add_column_sql = "ALTER TABLE " . $DB . "." . $car_table . " ADD COLUMN initial_distance DECIMAL(10,2) DEFAULT 0.00 COMMENT '최초 주행거리'";
    $pdo->exec($add_column_sql);
}

// 차량 정보 불러오기 (사용자, 차종, 차량번호, 최초주행거리)
$car_sql = "SELECT 
    c.vehicle_number,
    c.responsible_person,
    c.vehicle_type,
    IFNULL(c.initial_distance, 0) as initial_distance,
    m.part as department,
    m.position
FROM " . $DB . "." . $car_table . " c
LEFT JOIN " . $DB . ".member m ON c.responsible_person = m.name
WHERE c.is_deleted IS NULL 
ORDER BY c.manufacturing_date ASC, c.purchase_date ASC";

$car_stmh = $pdo->query($car_sql);
$car_list = $car_stmh->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid">
    <div class="d-flex align-items-center justify-content-center">
        <div class="card w-100">
            <div class="card-header text-center align-items-center">
                <div class="d-flex align-items-center justify-content-center m-2">
                    <span class="text-center fs-5 mx-3">최초주행거리 설정</span>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead class="table-primary">
                            <tr>
                                <th class="text-center">사용자명</th>
                                <th class="text-center">부서</th>
                                <th class="text-center">직책</th>
                                <th class="text-center">차종</th>
                                <th class="text-center">차량번호</th>
                                <th class="text-center">최초주행거리(km)</th>
                                <th class="text-center">저장</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($car_list as $car): ?>
                            <tr>
                                <td class="text-center"><?= htmlspecialchars($car['responsible_person'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                                <td class="text-center"><?= htmlspecialchars($car['department'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                                <td class="text-center"><?= htmlspecialchars($car['position'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                                <td class="text-center"><?= htmlspecialchars($car['vehicle_type'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                                <td class="text-center"><?= htmlspecialchars($car['vehicle_number'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                                <td class="text-center">
                                    <input type="number" 
                                           class="form-control text-end initial-distance-input" 
                                           data-vehicle="<?= htmlspecialchars($car['vehicle_number'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                                           value="<?= htmlspecialchars($car['initial_distance'] ?? '0', ENT_QUOTES, 'UTF-8') ?>"
                                           step="0.01" 
                                           min="0" 
                                           style="width: 120px;">
                                </td>
                                <td class="text-center">
                                    <button type="button" 
                                            class="btn btn-success btn-sm save-initial-distance" 
                                            data-vehicle="<?= htmlspecialchars($car['vehicle_number'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <i class="bi bi-check"></i>
                                    </button>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <div class="d-flex justify-content-center mt-3">
                    <button type="button" id="closeInitialDistanceBtn" class="btn btn-outline-dark btn-sm">
                        &times; 닫기
                    </button>
                </div>
            </div>
        </div>
    </div>
</div> 