@extends('Dashboard.layouts.main')

@section('title', 'Daftar Penilaian Kepuasan')

@section('content')
<div class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="page-title-box">
                    <div class="page-title-right">
                        <ol class="breadcrumb m-0">
                            <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                            <li class="breadcrumb-item active">Penilaian Kepuasan</li>
                        </ol>
                    </div>
                    <h4 class="page-title">Riwayat Penilaian Kepuasan</h4>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-12">
                @if(session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('success') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                @endif

                @if(session('warning'))
                <div class="alert alert-warning alert-dismissible fade show" role="alert">
                    {{ session('warning') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                @endif

                <div class="card">
                    <div class="card-body">
                        <div class="row mb-2">
                            <div class="col-sm-4">
                                @can('customer')
                                <a href="{{ route('satisfactions.create') }}" class="btn btn-primary mb-2">
                                    <i class="mdi mdi-plus-circle mr-2"></i> Isi Kuisioner Kepuasan
                                </a>
                                @endcan
                            </div>
                            
                            @can('admin')
                            <div class="col-sm-8">
                                <div class="float-right">
                                    {{-- <button type="button" class="btn btn-info mb-2" data-toggle="collapse" data-target="#filterForm">
                                        <i class="fe-filter"></i> Filter
                                    </button> --}}
                                    <button type="button" class="btn btn-danger mb-2 ml-1" onclick="cetakPDF()">
                                        <i class="far fa-file-pdf"></i> Cetak PDF
                                    </button>
                                </div>
                            </div>
                            @endcan
                        </div>

                        @can('admin')
                        <div class="collapse mb-3" id="filterForm">
                            <div class="card card-body">
                                <form action="{{ route('satisfactions.index') }}" method="GET" id="filter-form">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <div class="form-group">
                                                <label for="tanggal_mulai">Tanggal Mulai</label>
                                                <input type="date" name="tanggal_mulai" id="tanggal_mulai" class="form-control" value="{{ request('tanggal_mulai') }}">
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group">
                                                <label for="tanggal_selesai">Tanggal Selesai</label>
                                                <input type="date" name="tanggal_selesai" id="tanggal_selesai" class="form-control" value="{{ request('tanggal_selesai') }}">
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group">
                                                <label for="rating_min">Rating Minimal</label>
                                                <select name="rating_min" id="rating_min" class="form-control">
                                                    <option value="">Semua</option>
                                                    <option value="1" {{ request('rating_min') == '1' ? 'selected' : '' }}>1</option>
                                                    <option value="2" {{ request('rating_min') == '2' ? 'selected' : '' }}>2</option>
                                                    <option value="3" {{ request('rating_min') == '3' ? 'selected' : '' }}>3</option>
                                                    <option value="4" {{ request('rating_min') == '4' ? 'selected' : '' }}>4</option>
                                                    <option value="5" {{ request('rating_min') == '5' ? 'selected' : '' }}>5</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group">
                                                <label for="rating_max">Rating Maksimal</label>
                                                <select name="rating_max" id="rating_max" class="form-control">
                                                    <option value="">Semua</option>
                                                    <option value="1" {{ request('rating_max') == '1' ? 'selected' : '' }}>1</option>
                                                    <option value="2" {{ request('rating_max') == '2' ? 'selected' : '' }}>2</option>
                                                    <option value="3" {{ request('rating_max') == '3' ? 'selected' : '' }}>3</option>
                                                    <option value="4" {{ request('rating_max') == '4' ? 'selected' : '' }}>4</option>
                                                    <option value="5" {{ request('rating_max') == '5' ? 'selected' : '' }}>5</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-12 text-right">
                                            <button type="submit" class="btn btn-primary">
                                                <i class="fe-filter"></i> Terapkan Filter
                                            </button>
                                            <a href="{{ route('satisfactions.index') }}" class="btn btn-secondary">
                                                <i class="fe-refresh-cw"></i> Reset Filter
                                            </a>
                                            <button type="button" class="btn btn-danger" onclick="cetakPDF()">
                                                <i class="far fa-file-pdf"></i> Cetak PDF dengan Filter
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                        
                        <script>
                            function cetakPDF() {
                                // Salin form filter saat ini
                                const form = document.getElementById('filter-form');
                                const formClone = form.cloneNode(true);
                                
                                // Ubah action dan tambahkan target _blank
                                formClone.action = "{{ route('satisfactions.all-pdf') }}";
                                formClone.target = "_blank";
                                formClone.style.display = 'none';
                                
                                // Tambahkan ke body, submit, lalu hapus
                                document.body.appendChild(formClone);
                                formClone.submit();
                                document.body.removeChild(formClone);
                            }
                        </script>
                        @endcan

                        <div class="table-responsive">
                            <table class="table table-centered table-striped dt-responsive nowrap w-100" id="datatable">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Tanggal Pengisian</th>
                                        <th>Nilai Rata-rata</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($satisfactions as $index => $satisfaction)
                                    <tr>
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ $satisfaction->created_at->format('d-m-Y H:i') }}</td>
                                        <td>
                                            <span class="badge {{ $satisfaction->average_score >= 4 ? 'badge-success' : ($satisfaction->average_score >= 3 ? 'badge-info' : 'badge-danger') }}">
                                                {{ number_format($satisfaction->average_score, 1) }}
                                            </span>
                                        </td>
                                        <td>
                                            <div class="btn-group">
                                                <a href="{{ route('satisfactions.show', $satisfaction->id) }}" class="btn btn-info btn-sm">
                                                    <i class="fe-eye"></i>
                                                </a>
                                                <a href="{{ route('satisfactions.pdf', $satisfaction->id) }}" class="btn btn-danger btn-sm ml-1" target="_blank" data-toggle="tooltip" title="Download PDF">
                                                    <i class="far fa-file-pdf"></i>
                                                </a>
                                                @can('customer')
                                                <a href="{{ route('satisfactions.edit', $satisfaction->id) }}" class="btn btn-primary btn-sm ml-1">
                                                    <i class="fe-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-danger btn-sm ml-1" onclick="confirmDelete('{{ route('satisfactions.destroy', $satisfaction->id) }}')">
                                                    <i class="fe-trash"></i>
                                                </button>
                                                @endcan
                                            </div>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        {{-- <div class="pagination justify-content-center mt-3">
                            {{ $satisfactions->links() }}
                        </div> --}}

                        @if($satisfactions->count() == 0)
                        <div class="text-center mt-3">
                            <p>Anda belum pernah mengisi kuisioner kepuasan. <a href="{{ route('satisfactions.create') }}">Isi kuisioner sekarang</a></p>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Sertakan Modal Konfirmasi Hapus -->
@include('layouts.delete-modal')
@endsection 