@extends('layouts.app')

@section('title', 'Politeknik Negeri Jember')
@section('topbar', 'Dashboard')

@section('css')
<style>
    :root {
        --primary: rgb(0, 122, 122);
        --secondary: rgb(136, 163, 186);
        --success: #28a745;
        --danger: #dc3545;
        --warning: #ffc107;
        --teal-light: #4da6a6;
        --info: #17a2b8;
        --light: #f8f9fa;
        --dark: #343a40;
    }
    
    .welcome-card {
        background: linear-gradient(135deg, var(--primary), rgb(0, 122, 153));
        color: white;
        border-radius: 15px;
        box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        overflow: hidden;
        position: relative;
        z-index: 1;
    }
    
    .welcome-card::before {
        content: '';
        position: absolute;
        top: -50%;
        right: -50%;
        width: 100%;
        height: 200%;
        background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
        z-index: -1;
    }
    
    .metric-card {
        border: none;
        border-radius: 12px;
        transition: all 0.3s ease;
        overflow: hidden;
        box-shadow: 0 5px 15px rgba(0,0,0,0.08);
        position: relative;
    }
    
    .metric-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 15px 30px rgba(0,0,0,0.15);
    }
    
    .metric-icon {
        position: absolute;
        right: 20px;
        top: 50%;
        transform: translateY(-50%);
        font-size: 2.5rem;
        opacity: 0.2;
        color: inherit;
    }
    
    .metric-card-primary { background-color: #e6f2ff; border-left: 4px solid var(--primary); }
    .metric-card-success { background-color: #e6ffee; border-left: 4px solid var(--success); }
    .metric-card-info { background-color: #e6f9ff; border-left: 4px solid var(--info); }
    .metric-card-warning { background-color: #fff8e6; border-left: 4px solid var(--warning); }
    
    .status-badge {
        padding: 6px 12px;
        border-radius: 20px;
        font-weight: 500;
        font-size: 0.8rem;
        display: inline-flex;
        align-items: center;
    }
    
    .badge-success { background-color: rgba(40, 167, 69, 0.1); color: var(--success); }
    .badge-warning { background-color: rgba(255, 193, 7, 0.1); color: var(--warning); }
    .badge-danger { background-color: rgba(220, 53, 69, 0.1); color: var(--danger); }
    
    .data-table-card {
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 5px 15px rgba(0,0,0,0.08);
    }
    
    .table-header {
        background: linear-gradient(to right, var(--primary), rgb(40, 68, 99));
        color: white;
    }
    
    /* Formulir Aktif Styles */
    .form-item {
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        padding: 15px;
        margin-bottom: 15px;
        background: white;
    }
    
    .form-item-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
    }
    
    .form-item-title {
        font-size: 1.1rem;
        margin: 0;
        color: var(--dark);
    }
    
    .active-badge {
        background-color: rgba(40, 167, 69, 0.1);
        color: var(--success);
        padding: 4px 8px;
        border-radius: 12px;
        font-size: 0.75rem;
    }
    
    .form-item-meta {
        margin-bottom: 10px;
    }
    
    .form-item-dates {
        display: flex;
        flex-wrap: wrap;
        gap: 15px;
        font-size: 0.85rem;
        color: var(--secondary);
    }
    
    .no-active-form {
        text-align: center;
        padding: 30px;
        color: var(--secondary);
    }
    
    @media (max-width: 768px) {
        .metric-card {
            margin-bottom: 20px;
        }
        
        .welcome-card {
            text-align: center;
            padding: 10px;
        }
        
        .metric-icon {
            font-size: 2rem;
            opacity: 0.3;
        }
        
        .form-item-header {
            flex-direction: column;
            align-items: flex-start;
        }
        
        .active-badge {
            margin-top: 8px;
        }
    }

    @media (max-width: 576px) {
        .container-fluid {
            padding: 0 10px;
        }
        
        .welcome-card h2 {
            font-size: 1.3rem;
        }
        
        .metric-card {
            padding: 10px !important;
        }
        
        .progress {
            width: 100% !important;
        }
        
        .form-item-dates {
            flex-direction: column;
            gap: 8px;
        }
    }
</style>
<link href="{{ asset('vendor/datatables/dataTables.bootstrap4.min.css') }}" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
@endsection

@section('content')
<div class="container-fluid">
    <!-- Welcome Header -->
    <div class="card welcome-card mb-4">
        <div class="card-body p-4">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h2 class="h3 mb-2 text-white fw-bold">Selamat Datang, {{ auth()->user()->name }}!</h2>
                    <p class="mb-0 text-white fw-bold">Sistem Penyesuaian UKT Politeknik Negeri Jember</p>
                </div>
                <div class="col-md-4 text-md-right text-center mt-3 mt-md-0">
                    <i class="fas fa-university fa-4x opacity-10"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Metric Cards -->
    <div class="row g-2">
        <!-- Total Mahasiswa -->
        <div class="col-6 col-sm-6 col-md-3 mb-3">
            <a href="{{ route('user.index') }}" class="text-decoration-none">
                <div class="card metric-card metric-card-primary h-100">
                    <div class="card-body">
                        <div class="d-flex flex-column">
                            <div class="metric-value h3 fw-bold text-metric-primary mb-1">{{ $totalMahasiswa ?? 0 }}</div>
                            <div class="metric-title text-dark fw-bold">Total Mahasiswa</div>
                        </div>
                        <i class="fas fa-users metric-icon fw-bold"></i>
                    </div>
                </div>
            </a>
        </div>

        <!-- Total Kriteria -->
        <div class="col-6 col-sm-6 col-md-3 mb-3">
            <a href="{{ route('kriteria.index') }}" class="text-decoration-none">
                <div class="card metric-card metric-card-success h-100">
                    <div class="card-body">
                        <div class="d-flex flex-column">
                            <div class="metric-value h3 fw-bold text-metric-success mb-1">{{ $totalKriteria ?? 0 }}</div>
                            <div class="metric-title text-dark fw-bold">Kriteria</div>
                        </div>
                        <i class="fas fa-list-alt metric-icon text-success"></i>
                    </div>
                </div>
            </a>
        </div>

        <!-- Sub Kriteria -->
        <div class="col-6 col-sm-6 col-md-3 mb-3">
            <a href="{{ route('subkriteria.index') }}" class="text-decoration-none">
                <div class="card metric-card metric-card-info h-100">
                    <div class="card-body">
                        <div class="d-flex flex-column">
                            <div class="metric-value h3 fw-bold text-metric-info mb-1">{{ $totalSubkriteria ?? 0 }}</div>
                            <div class="metric-title text-dark fw-bold">Sub Kriteria</div>
                        </div>
                        <i class="fas fa-layer-group metric-icon fw-bold"></i>
                    </div>
                </div>
            </a>
        </div>

        <!-- Total Pengajuan -->
        <div class="col-6 col-sm-6 col-md-3 mb-3">
            <a href="{{ route('admin.pengajuan') }}" class="text-decoration-none">
                <div class="card metric-card metric-card-warning h-100">
                    <div class="card-body">
                        <div class="d-flex flex-column">
                            <div class="metric-value h3 fw-bold text-metric-warning mb-1">{{ $totalPengajuan ?? 0 }}</div>
                            <div class="metric-title text-dark fw-bold">Total Pengajuan</div>
                        </div>
                        <i class="fas fa-file-alt metric-icon fw-bold"></i>
                    </div>
                </div>
            </a>
        </div>
    </div>

    <!-- Pengajuan Charts -->
    <div class="row g-2 mt-2">
        <div class="col-12 col-md-6 mb-3">
            <div class="card h-100">
                <div class="card-header bg-white border-bottom-0 py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Penurunan UKT</h6>
                </div>
                @php
                    $total = $totalPengajuan ?? 0;
                    $penurunan = $penurunanUKT ?? 0;
                    $percentage = $total ? ($penurunan/$total)*100 : 0;
                @endphp
                <div class="card-body">
                    <div class="d-flex align-items-center justify-content-between">
                        <div class="h2 font-weight-bold text-primary">{{ $penurunan }}</div>
                        <div>
                            <div class="progress" style="height: 10px; width: 150px;">
                                <div class="progress-bar bg-primary" style="width: {{ $percentage }}%"></div>
                            </div>
                            <small class="text-muted">{{ round($percentage, 1) }}% dari total</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-md-6 mb-3">
            <div class="card h-100">
                <div class="card-header bg-white border-bottom-0 py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Pengangsuran UKT</h6>
                </div>
                @php
                    $total = $totalPengajuan ?? 0;
                    $pengangsuran = $pengangsuranUKT ?? 0;
                    $percentage = $total ? ($pengangsuran/$total)*100 : 0;
                @endphp
                <div class="card-body">
                    <div class="d-flex align-items-center justify-content-between">
                        <div class="h2 font-weight-bold text-primary">{{ $pengangsuran }}</div>
                        <div>
                            <div class="progress" style="height: 10px; width: 150px;">
                                <div class="progress-bar bg-primary" style="width: {{ $percentage }}%"></div>
                            </div>
                            <small class="text-muted">{{ round($percentage, 1) }}% dari total</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Laporan Section -->
    <div class="card data-table-card mb-4 mt-3">
        <div class="card-header table-header">
            <h5 class="section-title m-0">
                <i class="fas fa-file-pdf mr-2"></i> Manajemen Laporan Ranking
            </h5>
        </div>
        <div class="card-body">
            @if(session('report_status'))
                <div class="alert alert-{{ session('report_status')['type'] }} alert-dismissible fade show">
                    {{ session('report_status')['message'] }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            @php
                $rankingFile = \App\Models\RankingFile::latest()->first();
            @endphp

            <div class="row">
                <div class="col-md-6">
                    <h5 class="mb-3">Status Laporan</h5>
                    @if($rankingFile)
                        <div class="alert alert-success">
                            <i class="fas fa-check-circle mr-2"></i>
                            Laporan tersedia (diunggah {{ $rankingFile->created_at->diffForHumans() }})
                        </div>
                        <div class="d-flex align-items-center">
                            <a href="{{ route('admin.ranking-report.view', $rankingFile->id) }}" 
                                target="_blank"
                                class="btn btn-primary btn-sm mr-2">
                                <i class="fas fa-eye mr-2"></i>Lihat Laporan
                            </a>
                            <form action="{{ route('admin.ranking-report.delete') }}" method="POST">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-danger btn-sm">
                                    <i class="fas fa-trash mr-2"></i>Hapus
                                </button>
                            </form>
                        </div>
                    @else
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-circle mr-2"></i>
                            Belum ada laporan ranking yang diunggah
                        </div>
                    @endif
                </div>
            
                <div class="col-md-6">
                    <h5 class="mb-3">Unggah Laporan Baru</h5>
                    @if(!$rankingFile)
                        <form action="{{ route('admin.ranking-report.store') }}" method="POST" enctype="multipart/form-data">
                            @csrf
                            <div class="form-group mb-3">
                                <div class="custom-file">
                                    <input type="file" class="custom-file-input" id="rankingFile" 
                                            name="ranking_file" accept=".pdf" required>
                                    <label class="custom-file-label" for="rankingFile">Pilih file PDF</label>
                                </div>
                                <small class="form-text text-muted">
                                    Maksimal ukuran: 10MB. Format: PDF
                                </small>
                            </div>
                            <button type="submit" class="btn btn-success">
                                <i class="fas fa-upload mr-2"></i>Unggah Laporan
                            </button>
                        </form>
                    @else
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle mr-2"></i>
                            Untuk mengunggah laporan baru, hapus terlebih dahulu laporan yang ada
                        </div>
                        <button class="btn btn-secondary" disabled>
                            <i class="fas fa-upload mr-2"></i>Unggah Laporan
                        </button>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <!-- Formulir Aktif -->
    <div class="card data-table-card mb-4 mt-3">
        <div class="card-header table-header d-flex justify-content-between align-items-center">
            <div>
                <h5 class="section-title m-0">
                    <i class="fas fa-file-alt mr-2"></i> Formulir Aktif
                </h5>
            </div>
            <a href="{{ route('form.pendaftaran') }}" class="btn btn-sm btn-light">
                <i class="fas fa-eye mr-1"></i> Lihat Semua
            </a>
        </div>
        <div class="card-body card-body-content">
            @php
                $activeForms = App\Models\Form::where('status', 'Dibuka')
                    ->orderBy('tahun', 'desc')
                    ->orderBy('semester', 'desc')
                    ->get();
            @endphp
            
            @if($activeForms->count() > 0)
                <div class="form-list">
                    @foreach($activeForms as $form)
                        <div class="form-item">
                            <div class="form-item-header">
                                <h5 class="form-item-title">
                                    <i class="fas fa-file-signature mr-2 text-primary"></i>
                                    {{ $form->nama_form }}
                                </h5>
                                <span class="active-badge" style="font-weight: bold; color: green; font-size: 0.95rem;">
                                    <i class="fas fa-circle" style="margin-right: 6px; color: green;"></i> Aktif
                                </span>
                            </div>
                            <div class="form-item-meta">
                                <span class="badge badge-primary mr-2">
                                    {{ ucfirst($form->jenis_form) }}
                                </span>
                                <span class="badge badge-secondary">
                                    {{ $form->semester }} {{ $form->tahun }}
                                </span>
                            </div>
                            <div class="form-item-dates">
                                <div class="form-item-date">
                                    <i class="fas fa-calendar-plus"></i>
                                    <span class="fw-bold text-dark">Dibuka: {{ $form->tanggal_pembukaan ? \Carbon\Carbon::parse($form->tanggal_pembukaan)->format('d M Y') : '-' }}</span>
                                </div>
                                <div class="form-item-date">
                                    <i class="fas fa-calendar-plus"></i>
                                    <span class="fw-bold text-dark">Ditutup: {{ $form->tanggal_penutupan ? \Carbon\Carbon::parse($form->tanggal_pembukaan)->format('d M Y') : '-' }}</span>
                                </div>
                                 <div class="form-item-date">
                                    <i class="fas fa-calendar-plus"></i>
                                    <span class="fw-bold text-dark">Verifikasi: {{ $form->tanggal_verifikasi ? \Carbon\Carbon::parse($form->tanggal_pembukaan)->format('d M Y') : '-' }}</span>
                                </div>
                                 <div class="form-item-date">
                                    <i class="fas fa-calendar-plus"></i>
                                    <span class="fw-bold text-dark">Pengumuman: {{ $form->tanggal_pengumuman ? \Carbon\Carbon::parse($form->tanggal_pembukaan)->format('d M Y') : '-' }}</span>
                                </div>
                                <div class="form-item-date">
                                    <i class="fas fa-users"></i>
                                    <span class="fw-bold text-dark">Pengajuan: {{ App\Models\PengajuanUkt::where('status_form_id', $form->id)->count() }}</span>
                                </div>
                            </div>
                            <div class="mt-3 d-flex flex-wrap">
                                <a href="{{ route('admin.pengajuan') }}?status_form_id={{ $form->id }}" class="btn btn-sm btn-outline-primary mr-2 mb-2">
                                    <i class="fas fa-list mr-1"></i> Lihat Pengajuan
                                </a>
                                <a href="{{ route('form.pendaftaran') }}" class="btn btn-sm btn-outline-secondary mb-2">
                                    <i class="fas fa-cog mr-1"></i> Kelola Form
                                </a>
                            </div>
                        </div>
                    @endforeach
                </div>
            @else
                <div class="no-active-form">
                    <i class="fas fa-info-circle"></i>
                    <h5 class="mt-3 mb-2">Tidak ada formulir aktif</h5>
                    <p class="mb-0">Saat ini tidak ada formulir yang dibuka untuk pengajuan</p>
                    <a href="{{ route('form.pendaftaran') }}" class="btn btn-primary mt-3">
                        <i class="fas fa-plus mr-1"></i> Buat Formulir Baru
                    </a>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection

@section('js')
<script src="{{ asset('vendor/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('vendor/datatables/dataTables.bootstrap4.min.js') }}"></script>

<script>
    $(document).ready(function() {
        // Inisialisasi sidebar
        $('#sidebarToggle').click(function(e) {
            e.preventDefault();
            $('.sidebar').toggleClass('show');
            $('.sidebar-overlay').toggleClass('show');
            
            if ($('.sidebar').hasClass('show')) {
                $('.welcome-card').css('visibility', 'hidden');
            } else {
                $('.welcome-card').css('visibility', 'visible');
            }
        });
        
        // Tutup sidebar saat klik overlay
        $('.sidebar-overlay').click(function() {
            $('.sidebar').removeClass('show');
            $('.sidebar-overlay').removeClass('show');
            $('.welcome-card').css('visibility', 'visible');
        });
        
        // Inisialisasi dropdown menu
        $('.sidebar .nav-link[data-toggle="collapse"]').click(function(e) {
            e.preventDefault();
            $(this).attr('aria-expanded', function(i, attr) {
                return attr === 'true' ? 'false' : 'true';
            });
            
            var target = $(this).attr('href');
            $(target).collapse('toggle');
        });
        
    });
</script>
@endsection