<?php
include $_SERVER['DOCUMENT_ROOT'].'/include/session_include.php';

$c_uid  = intval($_GET['customer_uid'] ?? 0);
$i_type = trim($_GET['i_type'] ?? 'install_order');
$showInactive = intval($_GET['show_inactive'] ?? 0);

if (!$c_uid || !$i_type) {
  echo '<div class="text-danger">Invalid request.</div>';
  exit;
}

$i_type = mysqli_real_escape_string($jdb->DBConn, $i_type);

// type
$whereType = "";
if (strtoupper($i_type) !== 'ALL') {
  $whereType = "AND i.i_type = '{$i_type}'";
}

// status
$statusWhere = "AND i.i_status = 'A'";
if ($showInactive) {
    $statusWhere = "AND i.i_status IN ('A','I')";
}

$sql = "
  SELECT i.*,
         m.m_firstname,
         m.m_lastname,
         m.m_initial
  FROM tbl_customer_image i
  LEFT JOIN tbl_member m ON m.m_uid = i.i_createdby
  WHERE i.i_customeruid = '{$c_uid}'
    {$whereType}
    {$statusWhere}
  ORDER BY i.i_createddate DESC
";

// 
$rlt = mysqli_query($jdb->DBConn, $sql);
if (!$rlt) {
  echo '<div class="text-danger">Query error</div>';
  exit;
}

// 결과 없을 때 체크
if (mysqli_num_rows($rlt) === 0) {
  echo '<div class="text-muted text-center py-4">No images.</div>';
  exit;
}

// 
$images = [];
$meta   = [];

while ($row = mysqli_fetch_assoc($rlt)) {

  $files = explode(',', $row['i_filename']);

  foreach ($files as $f) {
    $f = trim($f);
    if (!$f) continue; // 빈값

    $images[] = $row['i_filepath'] . $f;

    $meta[] = [
      'note'    => $row['i_note'] ?? '',
      'created' => $row['i_createddate'] ?? '',
      'by'      => trim(($row['m_firstname'] ?? '').' '.($row['m_lastname'] ?? '')),
      'type'    => $row['i_type'] ?? ''
    ];
  }
}

$total = count($images);
if ($total === 0) {
  echo '<div class="text-muted text-center py-4">No images.</div>';
  exit;
}

// JSON을 data-attribute로 심기
$imagesJson = htmlspecialchars(json_encode($images), ENT_QUOTES);
$metaJson   = htmlspecialchars(json_encode($meta), ENT_QUOTES);
?>

<style>
.customer-image-slider { text-align:center; }
.customer-image-slider img {
  max-width:100%;
  max-height:60vh;
  object-fit:contain;
  border-radius:8px;
  border:1px solid #ddd;
}
.slider-nav { display:flex; justify-content:space-between; margin-top:10px; }
.slider-nav button { min-width:100px; }
.slider-indicator { text-align:center; margin-top:6px; font-size:13px; color:#666; }
.slider-meta { margin-top:10px; font-size:13px; color:#555; line-height:1.4; }
</style>

<div id="customerImageSlider"
     class="customer-image-slider"
     data-images="<?=$imagesJson?>"
     data-meta="<?=$metaJson?>">

  <img id="sliderImage" src="<?=htmlspecialchars($images[0])?>" alt="Image">

  <div class="slider-nav">
    <button type="button"
            id="btnPrevImage"
            class="btn btn-outline-secondary btn-sm"
            title="Previous">
      <i class="bi bi-chevron-left"></i>
    </button>

    <button type="button"
            id="btnNextImage"
            class="btn btn-outline-secondary btn-sm"
            title="Next">
      <i class="bi bi-chevron-right"></i>
    </button>
  </div>

  <div class="slider-indicator">
    <span id="sliderIndex">1</span> / <?=$total?>
  </div>

  <div class="slider-meta" id="sliderMeta">
    <b>Type:</b> <?=nl2br(htmlspecialchars($meta[0]['type']))?><br>
    <b>Note:</b> <?=nl2br(htmlspecialchars($meta[0]['note']))?><br>
    <b>Created:</b> <?=($meta[0]['created'] ? date('Y-m-d H:i', strtotime($meta[0]['created'])) : '')?><br>
    <b>By:</b> <?=htmlspecialchars($meta[0]['by'])?>
  </div>

</div>
