<?php
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/stone_core_common.php';
require_login();
require_role(['admin','production_manager','workshop_staff']);

$page_title = 'Taş Kartları';
$active_menu = 'stones';

$options = stone_active_options();
$filters = [
    'q' => trim($_GET['q'] ?? ''),
    'species_id' => (int)($_GET['species_id'] ?? 0),
    'cut_id' => (int)($_GET['cut_id'] ?? 0),
    'size_id' => (int)($_GET['size_id'] ?? 0),
    'color_id' => (int)($_GET['color_id'] ?? 0),
];
$rows = stone_get_summary_rows($filters);
include __DIR__ . '/includes/layout.php';
?>
<div class="ph">
  <div><h1>Taş Kartları</h1><p>Taş SKU, kesim, ölçü, renk ve stok özeti. Satıra/Kart butonuna tıklayıp alım geçmişini görebilirsiniz.</p></div>
  <a href="stone_purchase.php" class="btn btn-primary">＋ Taş Alımı Gir</a>
</div>

<div class="card">
  <div class="card-header"><span class="card-title">Filtrele</span></div>
  <div class="card-body">
    <form method="get" style="display:grid;grid-template-columns:2fr repeat(4,1fr) auto;gap:10px;align-items:end">
      <div><label class="form-label">Arama</label><input class="form-control" name="q" value="<?= e($filters['q']) ?>" placeholder="SKU / cins / kesim / ölçü / renk"></div>
      <div><label class="form-label">Cins</label><select class="form-control" name="species_id"><option value="0">Tümü</option><?php foreach($options['species'] as $o): ?><option value="<?= (int)$o['id'] ?>" <?= $filters['species_id']==$o['id']?'selected':'' ?>><?= e($o['species_name']) ?></option><?php endforeach; ?></select></div>
      <div><label class="form-label">Kesim</label><select class="form-control" name="cut_id" id="cutFilter"><option value="0">Tümü</option><?php foreach($options['cuts'] as $o): ?><option value="<?= (int)$o['id'] ?>" <?= $filters['cut_id']==$o['id']?'selected':'' ?>><?= e($o['cut_name']) ?></option><?php endforeach; ?></select></div>
      <div><label class="form-label">Ölçü</label><select class="form-control" name="size_id" id="sizeFilter"><option value="0">Tümü</option><?php foreach($options['sizes'] as $o): ?><option data-cut="<?= (int)$o['cut_id'] ?>" value="<?= (int)$o['id'] ?>" <?= $filters['size_id']==$o['id']?'selected':'' ?>><?= e($o['cut_name'].' — '.$o['size_label']) ?></option><?php endforeach; ?></select></div>
      <div><label class="form-label">Renk</label><select class="form-control" name="color_id"><option value="0">Tümü</option><?php foreach($options['colors'] as $o): ?><option value="<?= (int)$o['id'] ?>" <?= $filters['color_id']==$o['id']?'selected':'' ?>><?= e($o['color_name']) ?></option><?php endforeach; ?></select></div>
      <div style="display:flex;gap:8px"><button class="btn btn-secondary" type="submit">Ara</button><a class="btn btn-secondary" href="stones.php">Temizle</a></div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-header"><span class="card-title">Taş Listesi</span><span class="c-mt"><?= count($rows) ?> kayıt gösteriliyor</span></div>
  <div class="table-wrap">
    <table>
      <thead><tr><th>SKU</th><th>Cinsi</th><th>Kesim</th><th>Ölçü</th><th>Renk</th><th>1 Adet Ct</th><th class="tr">Stok Adet</th><th class="tr">Stok Ct</th><th>Uyarı</th><th>İşlem</th></tr></thead>
      <tbody>
      <?php foreach ($rows as $r): ?>
        <tr onclick="if(!event.target.closest('a,button,input,select')) location.href='stone_detail.php?id=<?= (int)$r['id'] ?>'" style="cursor:pointer">
          <td class="mn"><?= e($r['stone_sku']) ?></td>
          <td><?= e($r['species_name']) ?></td>
          <td><?= e($r['cut_name']) ?></td>
          <td><?= e($r['size_label']) ?></td>
          <td><?= e($r['color_name']) ?></td>
          <td><?= number_format((float)$r['carat_each'], 5) ?></td>
          <td class="tr fw6"><?= (int)$r['on_hand_qty'] ?></td>
          <td class="tr"><?= number_format((float)$r['on_hand_carat'], 4) ?></td>
          <td><?php if ((int)$r['on_hand_qty'] <= (int)$r['critical_stock_qty']): ?><span class="badge bg-yl">Kritik</span><?php else: ?><span class="badge bg-grn">OK</span><?php endif; ?></td>
          <td><a class="btn btn-secondary btn-sm" href="stone_detail.php?id=<?= (int)$r['id'] ?>">Kart</a></td>
        </tr>
      <?php endforeach; if(!$rows): ?><tr><td colspan="10" class="c-mt">Filtreye uygun taş bulunamadı.</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<script>
function filterSizeFilter(){
 const cut=document.getElementById('cutFilter').value;
 const size=document.getElementById('sizeFilter');
 [...size.options].forEach((o,i)=>{ if(i===0){o.hidden=false;return;} o.hidden = cut !== '0' && cut !== '' && o.dataset.cut !== cut; });
 if(size.selectedOptions[0] && size.selectedOptions[0].hidden) size.value='0';
}
document.getElementById('cutFilter').addEventListener('change', filterSizeFilter); filterSizeFilter();
</script>
<?php include __DIR__ . '/includes/layout_end.php'; ?>
