<?php
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/product_core_common.php';
require_once __DIR__ . '/design_core_common.php';
require_login();
require_role(['admin','production_manager','workshop_staff','designer']);
$user = current_user();
$role = (string)($user['role'] ?? '');
$page_title = 'Tasarım Kuyruğu';
$active_menu = 'design_queue';
$statusOptions = design_status_options();
$stores = design_stores();
$types = design_product_type_options();
$designers = design_users_for_designer();
$q = trim((string)($_GET['q'] ?? ''));
$status = trim((string)($_GET['status'] ?? ''));
$storeId = (int)($_GET['store_id'] ?? 0);
$productType = trim((string)($_GET['product_type_code'] ?? ''));
$designerId = (int)($_GET['designer_user_id'] ?? 0);
$sameLink = !empty($_GET['same_link']);
$sort = trim((string)($_GET['sort'] ?? 'oldest'));
$view = trim((string)($_GET['view'] ?? 'waiting'));
$allowedViews = ['waiting','sent','admin_revision','production_revision','revision','completed','all'];
if (!in_array($view, $allowedViews, true)) $view = 'waiting';
$filters = ['q'=>$q, 'status'=>$status, 'store_id'=>$storeId, 'product_type_code'=>$productType, 'designer_user_id'=>$designerId, 'same_link'=>$sameLink, 'sort'=>$sort, 'queue_unique_groups'=>1];
if ($view !== 'all' && $status === '') $filters['statuses'] = design_bucket_statuses($view);
if ($role === 'designer') {
    if ($designerId <= 0) $filters['designer_user_id'] = (int)($user['id'] ?? 0);
}
[$sql, $params] = design_request_list_sql($user, $filters);
$stmt = db()->prepare($sql); $stmt->execute($params); $rows = $stmt->fetchAll();
include __DIR__ . '/includes/layout.php';
?>
<div class="ph">
  <div>
    <h1><?= e(design_workflow_bucket_label($view)) ?></h1>
    <p>Çizimci burada kendi iş akışını görür. Admin gruplama, red/revize, numune ve ürün kartı kararları ayrı admin ekranındadır.</p>
  </div>
  <div style="display:flex;gap:10px">
    <?php if (in_array($role, ['admin','production_manager'], true)): ?><a class="btn btn-secondary" href="design_admin.php">Tasarım Admin Takibi</a><?php endif; ?>
    <a class="btn btn-secondary" href="design_requests.php">Tüm Talepler</a>
  </div>
</div>

<div class="tabs" style="display:flex;gap:8px;margin-bottom:14px;flex-wrap:wrap">
  <?php foreach(['waiting'=>'Bekleyen / Üzerimdeki','sent'=>'Admine Gönderilenler','admin_revision'=>'Admin Revizesi','production_revision'=>'Üretim Revizesi','completed'=>'Tamamlanan','all'=>'Tümü'] as $k=>$label): ?>
    <a class="btn <?= $view===$k?'btn-primary':'btn-secondary' ?>" href="design_queue.php?view=<?= e($k) ?>"><?= e($label) ?></a>
  <?php endforeach; ?>
</div>

<div class="card"><div class="card-header"><span class="card-title">Filtrele / Sırala</span></div><div class="card-body">
<form method="get" style="display:grid;grid-template-columns:2fr 1fr 1fr 1fr 1fr 1fr auto auto;gap:12px;align-items:end">
<input type="hidden" name="view" value="<?= e($view) ?>">
<div><label class="form-label">Arama</label><input class="form-control" name="q" value="<?= e($q) ?>" placeholder="DR no / ürün / link / mağaza / çizim dosyası"></div>
<div><label class="form-label">Durum</label><select class="form-control" name="status"><option value="">Sekmeye göre</option><?php foreach($statusOptions as $k=>$v): ?><option value="<?= e($k) ?>" <?= $status===$k?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
<div><label class="form-label">Mağaza</label><select class="form-control" name="store_id"><option value="0">Tümü</option><?php foreach($stores as $s): ?><option value="<?= (int)$s['id'] ?>" <?= $storeId===(int)$s['id']?'selected':'' ?>><?= e($s['store_code']) ?></option><?php endforeach; ?></select></div>
<div><label class="form-label">Tip</label><select class="form-control" name="product_type_code"><option value="">Tümü</option><?php foreach($types as $k=>$t): ?><option value="<?= e($k) ?>" <?= $productType===$k?'selected':'' ?>><?= e($t['label']) ?></option><?php endforeach; ?></select></div>
<div><label class="form-label">Çizimci</label><select class="form-control" name="designer_user_id"><option value="0">Tümü</option><?php foreach($designers as $d): ?><option value="<?= (int)$d['id'] ?>" <?= $designerId===(int)$d['id']?'selected':'' ?>><?= e($d['full_name']) ?></option><?php endforeach; ?></select></div>
<div><label class="form-label">Sırala</label><select class="form-control" name="sort"><option value="oldest" <?= $sort==='oldest'?'selected':'' ?>>En eski üstte</option><option value="newest" <?= $sort==='newest'?'selected':'' ?>>En yeni üstte</option><option value="request_no" <?= $sort==='request_no'?'selected':'' ?>>Talep no</option><option value="store" <?= $sort==='store'?'selected':'' ?>>Mağaza</option><option value="status" <?= $sort==='status'?'selected':'' ?>>Durum</option><option value="type" <?= $sort==='type'?'selected':'' ?>>Ürün tipi</option><option value="designer" <?= $sort==='designer'?'selected':'' ?>>Çizimci</option></select></div>
<div><label style="display:block;padding:11px 0"><input type="checkbox" name="same_link" value="1" <?= $sameLink?'checked':'' ?>> Çoklu link</label></div>
<div><button class="btn btn-secondary" type="submit">Ara</button></div><div><a class="btn btn-secondary" href="design_queue.php?view=<?= e($view) ?>">Temizle</a></div>
</form>
</div></div>

<div class="card"><div class="card-header"><span class="card-title">Liste</span><span class="c-mt"><?= count($rows) ?> kayıt</span></div><div class="table-wrap"><table><thead><tr><th>Sıra</th><th>Görsel</th><th>Talep</th><th>Mağaza</th><th>Durum</th><th>Zaman</th><th>Çoklu Talep</th><th>Çizim Dosyası</th><th>Ürün Kartı</th><th>İşlem</th></tr></thead><tbody>
<?php if (!$rows): ?><tr><td colspan="10" class="c-mt">Bu sekmede talep yok.</td></tr><?php endif; ?>
<?php $i=1; foreach($rows as $r): $existing = design_find_existing_product_by_url((string)$r['source_url']); $sameCnt=(int)($r['same_url_count'] ?? 0); $storesText = $sameCnt>1 ? design_same_link_store_summary((string)$r['source_url']) : ''; ?>
<tr>
<td><strong><?= $i++ ?></strong></td>
<td><?php if($r['reference_image_path']): ?><img src="<?= e($r['reference_image_path']) ?>" style="width:64px;height:64px;object-fit:cover;border-radius:8px;border:1px solid var(--border)"><?php else: ?><span class="c-mt">-</span><?php endif; ?></td>
<td><strong><?= e($r['request_no'].' — '.$r['title']) ?></strong><?php if($r['source_url']): ?><div><a class="c-gold" href="<?= e($r['source_url']) ?>" target="_blank" rel="noopener">Kaynak linki aç</a></div><?php endif; ?></td>
<td><span class="badge"><?= e($r['store_code']) ?></span></td>
<td><span class="<?= e(design_status_badge_class($r['status'])) ?>"><?= e(design_status_label($r['status'])) ?></span></td>
<td class="c-mt">Açıldı: <?= e(substr((string)$r['created_at'],0,16)) ?><br><?php if($r['design_started_at']): ?>Başladı: <?= e(substr((string)$r['design_started_at'],0,16)) ?><br><?php endif; ?><?php if($r['design_completed_at']): ?>Bitti: <?= e(substr((string)$r['design_completed_at'],0,16)) ?><?php endif; ?></td>
<td><?php if($sameCnt>1): ?><span class="badge badge-gold">Aynı link: <?= $sameCnt ?> mağaza</span><div class="c-mt"><?= e($storesText) ?></div><?php else: ?><span class="c-mt">-</span><?php endif; ?></td>
<td><?= design_file_badge_html($r['group_no'] ?? null, isset($r['group_member_count']) ? (int)$r['group_member_count'] : null, $r['group_status'] ?? null) ?></td>
<td><?php if($existing): ?><span class="badge badge-green">Mevcut: <?= e($existing['master_code']) ?></span><?php elseif($r['created_master_code']): ?><span class="badge badge-green"><?= e($r['created_master_code']) ?></span><?php else: ?><span class="c-mt">-</span><?php endif; ?></td>
<td><a class="btn btn-secondary" href="design_request_detail.php?id=<?= (int)$r['id'] ?>">Detay / Çiz</a></td>
</tr>
<?php endforeach; ?>
</tbody></table></div></div>
<?php include __DIR__ . '/includes/layout_end.php'; ?>
