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

// 검색 조건 받기
$searchItem = $_GET['searchItem'] ?? '';
$searchUA = $_GET['searchUA'] ?? '';
$searchBottombarModel = $_GET['searchBottombarModel'] ?? '';
$searchBottombarType = $_GET['searchBottombarType'] ?? '';
$searchBottombarFinishing = $_GET['searchBottombarFinishing'] ?? '';
$search = $_GET['search'] ?? '';

// Bottombar.json 파일 읽기
$jsonFile = $_SERVER['DOCUMENT_ROOT'].'/bottombar/bottombar.json';
$bottombarData = [];

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

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

foreach ($bottombarData as $originalIndex => $item) {
    // 대분류 필터
    if (!empty($searchItem) && $item['firstitem'] !== $searchItem) {
        continue;
    }
    
    // 인정/비인정 필터
    if (!empty($searchUA) && $item['model_UA'] !== $searchUA) {
        continue;
    }
    
    // 제품모델 필터
    if (!empty($searchBottombarModel) && $item['model_name'] !== $searchBottombarModel) {
        continue;
    }
    
    // 마감 필터
    if (!empty($searchBottombarFinishing) && $item['finishing_type'] !== $searchBottombarFinishing) {
        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="bottombar-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['model_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['bar_width'] ?? '', ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td class="text-center">' . htmlspecialchars($item['bar_height'] ?? '', 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:150px;height:auto;" class="search-zoomable-image">';
        }
        echo '</td>';
        echo '</tr>';
    }
}
?> 