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

// 검색 조건 받기
$searchItem = $_GET['searchItem'] ?? '';
$searchUA = $_GET['searchUA'] ?? '';
$searchGuiderailModel = $_GET['searchGuiderailModel'] ?? '';
$searchGuiderailType = $_GET['searchGuiderailType'] ?? '';
$searchGuiderailFinishing = $_GET['searchGuiderailFinishing'] ?? '';
$search = $_GET['search'] ?? '';

// guiderail.json 파일 읽기
$jsonFile = $_SERVER['DOCUMENT_ROOT'].'/guiderail/guiderail.json';
$guiderailData = [];

if (file_exists($jsonFile)) {
    $jsonContent = file_get_contents($jsonFile);
    $guiderailData = json_decode($jsonContent, true);
    if (!is_array($guiderailData)) {
        $guiderailData = [];
    }
}

// 검색 조건에 따라 필터링
$filteredData = [];
$originalIndexes = [];

foreach ($guiderailData as $originalIndex => $item) {
    // 대분류 필터
    if (!empty($searchItem) && $item['firstitem'] !== $searchItem) {
        continue;
    }
    
    // 인정/비인정 필터
    if (!empty($searchUA) && $item['UA'] !== $searchUA) {
        continue;
    }
    
    // 제품모델 필터
    if (!empty($searchGuiderailModel) && $item['model_name'] !== $searchGuiderailModel) {
        continue;
    }
    
    // 형태 필터
    if (!empty($searchGuiderailType) && $item['check_type'] !== $searchGuiderailType) {
        continue;
    }
    
    // 마감 필터
    if (!empty($searchGuiderailFinishing) && $item['finishing_type'] !== $searchGuiderailFinishing) {
        continue;
    }
    
    // 검색어 필터
    if (!empty($search)) {
        $searchTrimmed = str_replace(' ', '', $search);
        $searchFields = [
            $item['search_keyword'] ?? '',
            $item['model_name'] ?? '',
            $item['check_type'] ?? '',
            $item['finishing_type'] ?? ''
        ];
        
        $found = false;
        foreach ($searchFields as $field) {
            if (stripos($field, $searchTrimmed) !== false) {
                $found = true;
                break;
            }
        }
        
        if (!$found) {
            continue;
        }
    }
    
    // 필터링을 통과한 항목 저장
    $filteredData[] = $item;
    $originalIndexes[] = $originalIndex;
}

// 결과 출력
if (empty($filteredData)) {
    echo '<tr><td colspan="9" class="text-center text-muted">검색 결과가 없습니다.</td></tr>';
} else {
    $index = 0;
    foreach ($filteredData as $filteredIndex => $item) {
        $index++;
        $originalIndex = $originalIndexes[$filteredIndex];
        echo '<tr class="guiderail-item-row" data-index="' . ($originalIndex + 1) . '" style="cursor: pointer;">';
        echo '<td class="text-center">' . htmlspecialchars($item['firstitem'] ?? '', ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td class="text-center">' . htmlspecialchars($item['UA'] ?? '', ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td class="text-center">' . htmlspecialchars($item['model_name'] ?? '', ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td class="text-center">' . htmlspecialchars($item['check_type'] ?? '', ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td class="text-center">' . htmlspecialchars($item['finishing_type'] ?? '', ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td class="text-center">' . htmlspecialchars($item['search_keyword'] ?? '', ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td class="text-center">';
        if (!empty($item['image'])) {
            echo '<img src="' . htmlspecialchars($item['image'], ENT_QUOTES, 'UTF-8') . '" alt="이미지" style="width:50px;height:50px;">';
        }
        echo '</td>';
        echo '</tr>';
    }
}
?> 