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

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

$item_sep = $_GET['item_sep'] ?? ''; // 대분류
$item_bending = $_GET['item_bending'] ?? ''; 

$sql = "SELECT * FROM {$DB}.bending WHERE is_deleted IS NULL";
$params = [];

if($item_sep !== ''){
    $sql .= " AND item_sep = :item_sep";
    $params[':item_sep'] = $item_sep;
}
if($item_bending !== ''){
    $sql .= " AND item_bending = :item_bending";
    $params[':item_bending'] = $item_bending;
}

$sql .= " ORDER BY num DESC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<table class="table table-bordered">
  <thead class="table-secondary" >
    <tr>
      <th>대분류</th>
      <th>중분류</th>
      <th>품명</th>
      <th>규격(가로*세로)</th>
      <th>재질</th>
      <th>전체 폭</th>
      <th>이미지</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach($results as $row): ?>
      <tr onclick='selectBendingItem(<?php echo json_encode($row, JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT); ?>)'>
         <td><?php echo htmlspecialchars($row['item_sep']); ?></td>
         <td><?php echo htmlspecialchars($row['item_bending']); ?></td>
         <td><?php echo htmlspecialchars($row['itemName']); ?></td>
         <td><?php echo htmlspecialchars($row['item_spec']); ?></td>
         <td><?php echo htmlspecialchars($row['material']); ?></td>
		<td><?php 
			$sumList = json_decode($row['sumList'], true);
			echo htmlspecialchars(end($sumList)); 
		?></td>   
		  <td class="text-center">
			<?php 
			  $img = htmlspecialchars($row['imgdata'], ENT_QUOTES, 'UTF-8');
			  if ($img) {
				  $img_path = "../bending/img/" . $img;
				  echo '<img src="' . $img_path . '" alt="이미지" style="max-width:100px;max-height:50px;">';
			  } else {
				  echo '(선택)';
			  }
			?>
		  </td>
		
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
