<?php
require_once __DIR__ . '/../config/config.php';
requireLogin();

// 獲取篩選項數據
$sites = $pdo->query("SELECT id, location, unit FROM sites ORDER BY location ASC")->fetchAll();
$employees = $pdo->query("SELECT id, name FROM employees ORDER BY name ASC")->fetchAll();
$subcontractors = $pdo->query("SELECT id, name FROM subcontractors ORDER BY name ASC")->fetchAll();

// 處理篩選
$where = ["1=1"];
$params = [];

if (!empty($_GET['site_id'])) {
    $where[] = "we.site_id = ?";
    $params[] = $_GET['site_id'];
}
if (!empty($_GET['worker_type'])) {
    $where[] = "we.worker_type = ?";
    $params[] = $_GET['worker_type'];
}
if (!empty($_GET['worker_id'])) {
    $where[] = "we.worker_id = ?";
    $params[] = $_GET['worker_id'];
}
if (!empty($_GET['start_date'])) {
    $where[] = "we.work_date >= ?";
    $params[] = $_GET['start_date'];
}
if (!empty($_GET['end_date'])) {
    $where[] = "we.work_date <= ?";
    $params[] = $_GET['end_date'];
}
if (!empty($_GET['work_type'])) {
    $where[] = "we.work_type LIKE ?";
    $params[] = "%" . $_GET['work_type'] . "%";
}

$where_sql = implode(" AND ", $where);

$query = "
    SELECT we.*, s.location, s.unit,
           CASE 
               WHEN we.worker_type = 'employee' THEN e.name 
               ELSE sub.name 
           END as worker_name
    FROM work_entries we
    JOIN sites s ON we.site_id = s.id
    LEFT JOIN employees e ON we.worker_type = 'employee' AND we.worker_id = e.id
    LEFT JOIN subcontractors sub ON we.worker_type = 'subcontractor' AND we.worker_id = sub.id
    WHERE $where_sql
    ORDER BY we.work_date DESC, we.created_at DESC
";

$stmt = $pdo->prepare($query);
$stmt->execute($params);
$records = $stmt->fetchAll();

include __DIR__ . '/../includes/header.php';
?>

<div class="mb-8">
    <h1 class="text-2xl font-bold text-slate-800">開工紀錄</h1>
    <p class="text-slate-500">查看及篩選所有員工及判工的開工歷史紀錄</p>
</div>

<!-- Filters -->
<div class="bg-white p-6 rounded-2xl shadow-sm border border-slate-100 mb-8">
    <form action="work_records.php" method="GET" class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-6 gap-4 items-end">
        <div>
            <label class="block text-xs font-semibold text-slate-500 uppercase mb-1">地盤</label>
            <select name="site_id" class="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm outline-none focus:ring-2 focus:ring-blue-500">
                <option value="">全部地盤</option>
                <?php foreach ($sites as $site): ?>
                <option value="<?php echo $site['id']; ?>" <?php echo ($_GET['site_id'] ?? '') == $site['id'] ? 'selected' : ''; ?>>
                    <?php echo htmlspecialchars($site['location']); ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="block text-xs font-semibold text-slate-500 uppercase mb-1">人員類型</label>
            <select name="worker_type" id="worker_type_select" class="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm outline-none focus:ring-2 focus:ring-blue-500">
                <option value="">全部類型</option>
                <option value="employee" <?php echo ($_GET['worker_type'] ?? '') == 'employee' ? 'selected' : ''; ?>>直屬員工</option>
                <option value="subcontractor" <?php echo ($_GET['worker_type'] ?? '') == 'subcontractor' ? 'selected' : ''; ?>>判工</option>
            </select>
        </div>
        <div>
            <label class="block text-xs font-semibold text-slate-500 uppercase mb-1">工種關鍵字</label>
            <input type="text" name="work_type" value="<?php echo htmlspecialchars($_GET['work_type'] ?? ''); ?>" placeholder="如：泥水"
                class="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <div>
            <label class="block text-xs font-semibold text-slate-500 uppercase mb-1">開始日期</label>
            <input type="date" name="start_date" value="<?php echo htmlspecialchars($_GET['start_date'] ?? ''); ?>"
                class="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <div>
            <label class="block text-xs font-semibold text-slate-500 uppercase mb-1">結束日期</label>
            <input type="date" name="end_date" value="<?php echo htmlspecialchars($_GET['end_date'] ?? ''); ?>"
                class="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <div class="flex gap-2">
            <button type="submit" class="flex-1 bg-blue-600 text-white py-2 rounded-lg text-sm font-bold hover:bg-blue-700 transition">
                <i class="fas fa-filter mr-1"></i> 篩選
            </button>
            <a href="work_records.php" class="bg-slate-100 text-slate-600 p-2 rounded-lg text-sm hover:bg-slate-200 transition">
                <i class="fas fa-sync-alt"></i>
            </a>
        </div>
    </form>
</div>

<!-- Records Table -->
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">
    <div class="overflow-x-auto">
        <table class="w-full text-left">
            <thead class="bg-slate-50 border-b border-slate-100 text-slate-500 text-sm font-semibold">
                <tr>
                    <th class="px-6 py-4">日期</th>
                    <th class="px-6 py-4">人員</th>
                    <th class="px-6 py-4">類型</th>
                    <th class="px-6 py-4">地盤</th>
                    <th class="px-6 py-4">工種 / 內容</th>
                    <th class="px-6 py-4">登記時間</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                <?php foreach ($records as $record): ?>
                <tr class="hover:bg-slate-50 transition">
                    <td class="px-6 py-4 font-medium text-slate-800"><?php echo htmlspecialchars($record['work_date']); ?></td>
                    <td class="px-6 py-4 text-slate-700 font-semibold"><?php echo htmlspecialchars($record['worker_name'] ?: '未知人員'); ?></td>
                    <td class="px-6 py-4">
                        <span class="px-2 py-1 rounded text-[10px] font-bold uppercase <?php echo $record['worker_type'] === 'employee' ? 'bg-blue-100 text-blue-600' : 'bg-indigo-100 text-indigo-600'; ?>">
                            <?php echo $record['worker_type'] === 'employee' ? '員工' : '判工'; ?>
                        </span>
                    </td>
                    <td class="px-6 py-4 text-slate-600 text-sm">
                        <?php echo htmlspecialchars($record['location']); ?>
                        <span class="text-slate-400 text-xs block"><?php echo htmlspecialchars($record['unit']); ?></span>
                    </td>
                    <td class="px-6 py-4 text-slate-600"><?php echo htmlspecialchars($record['work_type'] ?: '-'); ?></td>
                    <td class="px-6 py-4 text-slate-400 text-xs"><?php echo date('m-d H:i', strtotime($record['created_at'])); ?></td>
                </tr>
                <?php endforeach; ?>
                
                <?php if (empty($records)): ?>
                <tr>
                    <td colspan="6" class="px-6 py-10 text-center text-slate-400">沒有符合條件的紀錄</td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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