{% extends 'base.html' %}
{% block title %}Dashboard Workspace - LOGISRISET{% endblock %}

{% block content %}
<!-- WIDGET HEADER INFORMASI ROLE -->
<div class="bg-slate-900 text-white p-6 rounded-xl shadow-md my-4 flex justify-between items-center">
    <div>
        <h2 class="text-xl font-bold tracking-tight">Selamat Datang di LOGISRISET</h2>
        <p class="text-xs text-slate-400 mt-1">Kelola dokumen, restrukturisasi model riset, dan pelacakan review jurnal Scopus Anda.</p>
    </div>
    <div class="text-right">
        <span class="text-xs font-semibold tracking-wider uppercase block text-slate-400">Peran Anda</span>
        <span class="inline-block bg-indigo-600 text-white text-xs font-bold px-3 py-1 rounded-full mt-1 border border-indigo-400">
            {{ session.get('role', 'Peneliti') }}
        </span>
    </div>
</div>

<div class="grid grid-cols-1 md:grid-cols-3 gap-6 my-6">
    
    <!-- 1. AKSES KHUSUS PENELITI: FORM AJUKAN NASKAH BARU -->
    {% if session.get('role') == 'Peneliti' %}
    <div class="bg-white p-6 rounded-xl shadow-sm border border-slate-100 h-fit">
        <h3 class="text-base font-bold text-slate-900 mb-4 flex items-center gap-2">
            <span class="w-2 h-2 rounded-full bg-indigo-600"></span> Ajukan Naskah Baru
        </h3>
        <form action="{{ url_for('create_order') }}" method="POST" class="space-y-4">
            <div>
                <label class="block text-xs font-semibold text-slate-700 uppercase tracking-wider">Judul Disertasi / Artikel</label>
                <input type="text" name="title" placeholder="Contoh: Paradox of AI Fidelity in SMEs..." required class="mt-1 block w-full rounded-md border border-gray-300 p-2 text-sm shadow-sm focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500">
            </div>
            <div>
                <label class="block text-xs font-semibold text-slate-700 uppercase tracking-wider">Jenis Layanan Pendampingan</label>
                <select name="service_type" class="mt-1 block w-full rounded-md border border-gray-300 p-2 text-sm shadow-sm focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500 bg-white">
                    <option value="Proofreading & Editing Disertasi">Proofreading & Editing Disertasi</option>
                    <option value="Penyelarasan Selingkung Jurnal Scopus Q1/Q2">Penyelarasan Selingkung Jurnal Scopus Q1/Q2</option>
                    <option value="Restrukturisasi Model Statistik (SEM/AMOS)">Restrukturisasi Model Statistik (SEM/AMOS)</option>
                </select>
            </div>
            <button type="submit" class="w-full bg-indigo-600 text-white p-2.5 rounded-lg text-sm font-bold shadow hover:bg-indigo-500 transition-all">
                Kirim ke Tim Reviewer
            </button>
        </form>
    </div>
    {% else %}
    <!-- PANEL INFORMASI PERAN BUKAN PENELITI -->
    <div class="bg-slate-50 p-6 rounded-xl border border-dashed border-slate-200 h-fit text-center">
        <h3 class="text-sm font-bold text-slate-700 mb-2">Panel Kontrol Internal</h3>
        <p class="text-xs text-slate-500">Akun Anda terdaftar sebagai <span class="font-semibold text-slate-800">{{ session.get('role') }}</span>. Hak pengajuan dokumen baru dinonaktifkan secara otomatis.</p>
    </div>
    {% endif %}

    <!-- 2. TABEL / LIST PENGELOLAAN NASKAH (READ, UPDATE, DELETE BERDASARKAN ROLE) -->
    <div class="md:col-span-2 bg-white p-6 rounded-xl shadow-sm border border-slate-100">
        <h3 class="text-base font-bold text-slate-900 mb-4 flex items-center justify-between">
            <span>Daftar Pengajuan Naskah Aktif</span>
            <span class="text-xs bg-slate-100 px-2.5 py-1 rounded text-slate-600 font-normal">Total: {{ orders|length }} Naskah</span>
        </h3>
        
        {% if not orders %}
            <p class="text-gray-500 text-sm py-4 text-center border border-dashed border-gray-200 rounded-lg">Belum ada antrean pengajuan naskah di dalam database workspace ini.</p>
        {% else %}
            <div class="space-y-4">
                {% for order in orders %}
                    <div class="border border-slate-100 bg-slate-50/50 p-4 rounded-xl flex flex-col justify-between space-y-3 shadow-sm hover:border-slate-200 transition">
                        <div>
                            <div class="flex items-center gap-2 mb-2">
                                <span class="px-2 py-0.5 text-[10px] font-bold tracking-wide uppercase rounded bg-amber-100 text-amber-800">{{ order.status }}</span>
                                <span class="text-[11px] text-slate-400">ID Naskah: #{{ order.id }}</span>
                            </div>
                            
                            <!-- FORM UTAMA UPDATE DATA -->
                            <form action="{{ url_for('update_order', id=order.id) }}" method="POST" class="mt-2 space-y-2">
                                
                                <!-- Field Judul Naskah -->
                                <input type="text" name="title" value="{{ order.title }}" 
                                    {% if session.get('role') not in ['Peneliti', 'Super Admin'] %} readonly {% endif %}
                                    class="font-semibold text-slate-900 w-full border-b border-transparent focus:border-gray-300 p-1 bg-transparent rounded focus:outline-none focus:bg-white text-sm">
                                
                                <!-- Akses Khusus Ubah Status (Editor & Super Admin) -->
                                {% if session.get('role') in ['Editor', 'Super Admin'] %}
                                <div class="bg-amber-50 p-2 rounded border border-amber-200 my-2">
                                    <label class="block text-[10px] font-bold text-amber-800 uppercase tracking-wider mb-1">Perbarui Status Reviewer (Editor Only)</label>
                                    <select name="status" class="text-xs text-slate-700 w-full border border-gray-300 p-1.5 bg-white rounded focus:outline-none">
                                        <option value="Menunggu Review" {% if order.status == 'Menunggu Review' %}selected{% endif %}>Menunggu Review</option>
                                        <option value="Sedang Diperiksa" {% if order.status == 'Sedang Diperiksa' %}selected{% endif %}>Sedang Diperiksa</option>
                                        <option value="Selesai Proofreading" {% if order.status == 'Selesai Proofreading' %}selected{% endif %}>Selesai Proofreading</option>
                                    </select>
                                </div>
                                {% endif %}

                                <!-- Dropdown Pilihan Jenis Layanan -->
                                <select name="service_type" {% if session.get('role') not in ['Peneliti', 'Super Admin'] %} disabled {% endif %}
                                    class="text-xs text-slate-600 w-full border-b border-transparent focus:border-gray-300 p-1 bg-transparent rounded focus:outline-none focus:bg-white">
                                    <option value="Proofreading & Editing Disertasi" {% if order.service_type == 'Proofreading & Editing Disertasi' %}selected{% endif %}>Proofreading & Editing Disertasi</option>
                                    <option value="Penyelarasan Selingkung Jurnal Scopus Q1/Q2" {% if order.service_type == 'Penyelarasan Selingkung Jurnal Scopus Q1/Q2' %}selected{% endif %}>Penyelarasan Selingkung Jurnal Scopus Q1/Q2</option>
                                    <option value="Restrukturisasi Model Statistik (SEM/AMOS)" {% if order.service_type == 'Restrukturisasi Model Statistik (SEM/AMOS)' %}selected{% endif %}>Restrukturisasi Model Statistik (SEM/AMOS)</option>
                                </select>
                                
                                <!-- Group Aksi Tombol -->
                                <div class="flex space-x-2 pt-2 border-t border-slate-100 mt-2">
                                    {% if session.get('role') in ['Peneliti', 'Editor', 'Super Admin'] %}
                                    <button type="submit" class="text-xs bg-indigo-600 text-white px-3 py-1.5 rounded-lg hover:bg-indigo-700 transition font-medium">
                                        Simpan Perubahan
                                    </button>
                                    {% endif %}
                            </form> <!-- Penutup Form yang Benar -->
                            
                                    <!-- Form Pembatalan Naskah (Delete) ditaruh di dalam flex container yang sama agar rapi sejajar -->
                                    {% if session.get('role') in ['Peneliti', 'Super Admin'] %}
                                    <form action="{{ url_for('delete_order', id=order.id) }}" method="POST" onsubmit="return confirm('Apakah Anda yakin ingin membatalkan pengajuan naskah ini?');" class="inline">
                                        <button type="submit" class="text-xs bg-rose-600 text-white px-3 py-1.5 rounded-lg hover:bg-rose-700 transition font-medium">
                                            Batalkan Pengajuan
                                        </button>
                                    </form>
                                    {% endif %}
                                </div> 
                        </div>
                    </div>
                {% endfor %}
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}