<?php
function biz_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
?>

<div class="container-fluid py-3">

<div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
    <div class="card-body p-4">
        <h3 class="fw-bold mb-1">Conference Business Logic</h3>
        <div class="opacity-75">Committees, timetable, sessions, production, issues, and certificates</div>
    </div>
</div>

<div id="committees" class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
<div class="card-body">
<h5 class="fw-bold">Committees / Reviewers / Patrons / Chairs / Editors / TPC Photos</h5>

<form method="post" action="/easychair/admin/conference-business/committee/store" enctype="multipart/form-data" class="row g-2 mb-3">
<div class="col-md-2">
<select name="committee_type" class="form-select">
<option value="chair">Chair</option>
<option value="co_chair">Co-chair</option>
<option value="editor">Editor</option>
<option value="section_editor">Section Editor</option>
<option value="patron">Patron</option>
<option value="keynote">Keynote</option>
<option value="tpc">TPC</option>
<option value="reviewer">Reviewer</option>
<option value="panel">Panelist</option>
<option value="organizing">Organizing</option>
<option value="scientific">Scientific</option>
<option value="steering">Steering</option>
<option value="sponsor">Sponsor</option>
<option value="other">Other</option>
</select>
</div>
<div class="col-md-2"><input name="full_name_ar" class="form-control" placeholder="الاسم"></div>
<div class="col-md-2"><input name="full_name_en" class="form-control" placeholder="Name"></div>
<div class="col-md-2"><input name="email" class="form-control" placeholder="Email"></div>
<div class="col-md-2"><input type="file" name="image" class="form-control" accept=".png,.jpg,.jpeg,.webp"></div>
<div class="col-md-2"><button class="btn btn-success w-100">Add Person</button></div>
<div class="col-md-3"><input name="title_ar" class="form-control" placeholder="الصفة"></div>
<div class="col-md-3"><input name="title_en" class="form-control" placeholder="Title"></div>
<div class="col-md-3"><input name="affiliation_ar" class="form-control" placeholder="المؤسسة"></div>
<div class="col-md-3"><input name="affiliation_en" class="form-control" placeholder="Affiliation"></div>
</form>

<table class="table table-bordered align-middle">
<thead class="table-dark"><tr><th>Photo</th><th>Type</th><th>Name</th><th>Title</th><th>Email</th><th>Action</th></tr></thead>
<tbody>
<?php foreach(($committees ?? []) as $m): ?>
<tr>
<td>
<?php if(!empty($m['image_path'])): ?>
<img src="/easychair/storage/<?= biz_h($m['image_path']) ?>" style="width:55px;height:55px;object-fit:cover;border-radius:50%;">
<?php else: ?>
<span class="badge bg-secondary">No photo</span>
<?php endif; ?>
</td>
<td><?= biz_h($m['committee_type']) ?></td>
<td><?= biz_h(($m['full_name_ar'] ?? '') ?: ($m['full_name_en'] ?? '')) ?></td>
<td><?= biz_h(($m['title_ar'] ?? '') ?: ($m['title_en'] ?? '')) ?></td>
<td><?= biz_h($m['email'] ?? '') ?></td>
<td><a class="btn btn-sm btn-danger" onclick="return confirm('Delete?')" href="/easychair/admin/conference-business/committee/delete?id=<?= (int)$m['id'] ?>">Delete</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>

<div id="themes" class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
<div class="card-body">
<h5 class="fw-bold">Conference Themes / Panels / Section Editors</h5>
<form method="post" action="/easychair/admin/conference-business/theme/store" class="row g-2 mb-3">
    <div class="col-md-2">
        <select name="theme_type" class="form-select">
            <option value="theme">Theme</option>
            <option value="track">Track</option>
            <option value="panel">Panel</option>
            <option value="workshop">Workshop</option>
            <option value="topic">Topic</option>
        </select>
    </div>
    <div class="col-md-2"><input name="title_ar" class="form-control" placeholder="العنوان"></div>
    <div class="col-md-2"><input name="title_en" class="form-control" placeholder="Title"></div>
    <div class="col-md-3">
        <select name="section_editor_id" class="form-select">
            <option value="">Section Editor</option>
            <?php foreach($users as $u): ?>
                <option value="<?= (int)$u['id'] ?>"><?= biz_h(trim(($u['first_name'] ?? '').' '.($u['last_name'] ?? '')).' - '.$u['email']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="col-md-1"><input name="sort_order" class="form-control" value="1"></div>
    <div class="col-md-2"><button class="btn btn-success w-100">Add</button></div>
</form>

<table class="table table-bordered">
<thead class="table-dark"><tr><th>Type</th><th>Title</th><th>Editor</th><th>Active</th></tr></thead>
<tbody>
<?php foreach($themes as $t): ?>
<tr>
<td><?= biz_h($t['theme_type']) ?></td>
<td><?= biz_h($t['title_ar'] ?: $t['title_en']) ?></td>
<td><?= biz_h($t['editor_name'] ?? '-') ?></td>
<td><?= !empty($t['is_active']) ? 'Yes' : 'No' ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>

<div id="timetable" class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
<div class="card-body">
<h5 class="fw-bold">Conference Timetable / Days / Venues / Sessions</h5>

<div class="row g-3 mb-3">
<div class="col-lg-4">
<form method="post" action="/easychair/admin/conference-business/day/store" class="border rounded-4 p-3 h-100">
<h6 class="fw-bold">Add Day</h6>
<input type="date" name="day_date" class="form-control mb-2" required>
<input name="title_ar" class="form-control mb-2" placeholder="اليوم">
<input name="title_en" class="form-control mb-2" placeholder="Day title">
<button class="btn btn-primary w-100">Add Day</button>
</form>
</div>

<div class="col-lg-4">
<form method="post" action="/easychair/admin/conference-business/venue/store" class="border rounded-4 p-3 h-100">
<h6 class="fw-bold">Add Venue</h6>
<input name="name_ar" class="form-control mb-2" placeholder="اسم القاعة">
<input name="name_en" class="form-control mb-2" placeholder="Venue name">
<input name="capacity" class="form-control mb-2" placeholder="Capacity">
<button class="btn btn-primary w-100">Add Venue</button>
</form>
</div>

<div class="col-lg-4">
<form method="post" action="/easychair/admin/conference-business/session/store" class="border rounded-4 p-3 h-100">
<h6 class="fw-bold">Add Session</h6>
<select name="day_id" class="form-select mb-2">
<?php foreach($days as $d): ?><option value="<?= (int)$d['id'] ?>"><?= biz_h($d['day_date'].' - '.($d['title_ar'] ?: $d['title_en'])) ?></option><?php endforeach; ?>
</select>
<select name="venue_id" class="form-select mb-2">
<?php foreach($venues as $v): ?><option value="<?= (int)$v['id'] ?>"><?= biz_h($v['name_ar'] ?: $v['name_en']) ?></option><?php endforeach; ?>
</select>
<select name="theme_id" class="form-select mb-2">
<?php foreach($themes as $t): ?><option value="<?= (int)$t['id'] ?>"><?= biz_h($t['title_ar'] ?: $t['title_en']) ?></option><?php endforeach; ?>
</select>
<input name="title_ar" class="form-control mb-2" placeholder="عنوان الجلسة">
<input name="title_en" class="form-control mb-2" placeholder="Session title">
<div class="d-flex gap-2"><input type="time" name="start_time" class="form-control"><input type="time" name="end_time" class="form-control"></div>
<button class="btn btn-success w-100 mt-2">Add Session</button>
</form>
</div>
</div>

<form method="post" action="/easychair/admin/conference-business/session/assign-paper" class="row g-2 mb-3">
<div class="col-md-4">
<select name="session_id" class="form-select"><?php foreach($sessions as $s): ?><option value="<?= (int)$s['id'] ?>"><?= biz_h($s['title_ar'] ?: $s['title_en']) ?></option><?php endforeach; ?></select>
</div>
<div class="col-md-4">
<select name="submission_id" class="form-select"><?php foreach($acceptedPapers as $p): ?><option value="<?= (int)$p['id'] ?>"><?= biz_h($p['submission_no'].' - '.$p['title']) ?></option><?php endforeach; ?></select>
</div>
<div class="col-md-2"><input name="duration_minutes" class="form-control" value="15"></div>
<div class="col-md-2"><button class="btn btn-primary w-100">Assign Paper</button></div>
</form>

<table class="table table-bordered">
<thead class="table-dark"><tr><th>Day</th><th>Time</th><th>Session</th><th>Venue</th><th>Theme</th><th>Papers</th></tr></thead>
<tbody>
<?php foreach($sessions as $s): ?>
<tr>
<td><?= biz_h($s['day_date']) ?></td>
<td><?= biz_h(($s['start_time'] ?? '').' - '.($s['end_time'] ?? '')) ?></td>
<td><?= biz_h($s['title_ar'] ?: $s['title_en']) ?></td>
<td><?= biz_h($s['venue_name']) ?></td>
<td><?= biz_h($s['theme_title']) ?></td>
<td><?= (int)$s['papers_count'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>

<div id="production" class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
<div class="card-body">
<h5 class="fw-bold">Galley Production Ready</h5>
<form method="post" action="/easychair/admin/conference-business/galley/upload" enctype="multipart/form-data" class="row g-2 mb-3">
<div class="col-md-4"><select name="submission_id" class="form-select"><?php foreach($acceptedPapers as $p): ?><option value="<?= (int)$p['id'] ?>"><?= biz_h($p['submission_no'].' - '.$p['title']) ?></option><?php endforeach; ?></select></div>
<div class="col-md-2"><select name="galley_type" class="form-select"><option>pdf</option><option>docx</option><option>html</option><option>xml</option><option>other</option></select></div>
<div class="col-md-4"><input type="file" name="galley_file" class="form-control" required></div>
<div class="col-md-2"><button class="btn btn-success w-100">Upload</button></div>
</form>

<table class="table table-bordered">
<thead class="table-dark"><tr><th>Submission</th><th>File</th><th>Status</th><th>Action</th></tr></thead>
<tbody>
<?php foreach($galleys as $g): ?>
<tr>
<td><?= biz_h($g['submission_no'].' - '.$g['submission_title']) ?></td>
<td><a target="_blank" href="/easychair/storage/<?= biz_h($g['file_path']) ?>"><?= biz_h($g['original_filename']) ?></a></td>
<td><?= biz_h($g['status']) ?></td>
<td><a class="btn btn-sm btn-success" href="/easychair/admin/conference-business/galley/approve?id=<?= (int)$g['id'] ?>">Approve</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>

<div id="issues" class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
<div class="card-body">
<h5 class="fw-bold">Issuing / Proceedings</h5>
<form method="post" action="/easychair/admin/conference-business/issue/store" class="row g-2 mb-3">
<div class="col-md-3"><input name="title_ar" class="form-control" placeholder="عنوان العدد"></div>
<div class="col-md-3"><input name="title_en" class="form-control" placeholder="Issue title"></div>
<div class="col-md-2"><input name="volume_no" class="form-control" placeholder="Volume"></div>
<div class="col-md-2"><input name="issue_no" class="form-control" placeholder="Issue"></div>
<div class="col-md-2"><button class="btn btn-success w-100">Create Issue</button></div>
</form>

<form method="post" action="/easychair/admin/conference-business/issue/add-article" class="row g-2 mb-3">
<div class="col-md-4"><select name="issue_id" class="form-select"><?php foreach($issues as $i): ?><option value="<?= (int)$i['id'] ?>"><?= biz_h($i['title_ar'] ?: $i['title_en']) ?></option><?php endforeach; ?></select></div>
<div class="col-md-4"><select name="submission_id" class="form-select"><?php foreach($acceptedPapers as $p): ?><option value="<?= (int)$p['id'] ?>"><?= biz_h($p['submission_no'].' - '.$p['title']) ?></option><?php endforeach; ?></select></div>
<div class="col-md-2"><input name="doi" class="form-control" placeholder="DOI"></div>
<div class="col-md-2"><button class="btn btn-primary w-100">Add Article</button></div>
</form>

<table class="table table-bordered">
<thead class="table-dark"><tr><th>Issue</th><th>Articles</th><th>Status</th><th>Action</th></tr></thead>
<tbody>
<?php foreach($issues as $i): ?>
<tr>
<td><?= biz_h($i['title_ar'] ?: $i['title_en']) ?></td>
<td><?= (int)$i['articles_count'] ?></td>
<td><?= biz_h($i['status']) ?></td>
<td><a class="btn btn-sm btn-success" href="/easychair/admin/conference-business/issue/publish?id=<?= (int)$i['id'] ?>">Publish</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>

<div id="certificates" class="card border-0 shadow-sm" style="border-radius:22px;">
<div class="card-body">
<h5 class="fw-bold">Certificates</h5>

<form method="post" action="/easychair/admin/conference-business/certificate/bulk" class="row g-2 mb-3">
<div class="col-md-4">
<select name="certificate_type" class="form-select">
<option value="author">Bulk Authors Certificates</option>
<option value="reviewer">Bulk Reviewers Certificates</option>
<option value="chair">Bulk Chairs Certificates</option>
<option value="tpc">Bulk TPC Certificates</option>
<option value="editor">Bulk Editors Certificates</option>
<option value="committee">Bulk Committee Certificates</option>
</select>
</div>
<div class="col-md-2"><button class="btn btn-primary w-100">Issue Bulk</button></div>
</form>

<form method="post" action="/easychair/admin/conference-business/certificate/issue" class="row g-2 mb-3">
<div class="col-md-2"><select name="certificate_type" class="form-select"><option>author</option><option>reviewer</option><option>chair</option><option>tpc</option><option>editor</option><option>speaker</option><option>attendee</option><option>committee</option><option>other</option></select></div>
<div class="col-md-3"><input name="recipient_name" class="form-control" placeholder="Recipient name" required></div>
<div class="col-md-3"><input name="recipient_email" class="form-control" placeholder="Email"></div>
<div class="col-md-2"><input name="title_en" class="form-control" value="Certificate of Participation"></div>
<div class="col-md-2"><button class="btn btn-success w-100">Issue Single</button></div>
</form>

<table class="table table-bordered">
<thead class="table-dark"><tr><th>No</th><th>Name</th><th>Type</th><th>Status</th><th>Date</th><th>Action</th></tr></thead>
<tbody>
<?php foreach($certificates as $c): ?>
<tr>
<td><?= biz_h($c['certificate_no']) ?></td>
<td><?= biz_h($c['recipient_name']) ?></td>
<td><?= biz_h($c['certificate_type']) ?></td>
<td><?= biz_h($c['status']) ?></td>
<td><?= biz_h($c['issued_at']) ?></td>
<td>
<a class="btn btn-sm btn-outline-primary" target="_blank" href="/easychair/admin/conference-business/certificate/print?id=<?= (int)$c['id'] ?>">Print</a>
<?php if(!empty($c['verify_token'])): ?>
<a class="btn btn-sm btn-outline-success" target="_blank" href="/easychair/certificates/verify?token=<?= biz_h($c['verify_token']) ?>">Verify</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>

</div>
