@extends('layouts.app')

@section('title', 'Jadwal & Shift Kerja')
@section('page-title', 'Manajemen Shift & Jadwal Kerja')
@section('page-subtitle', 'Kelola jam masuk, jam pulang, toleransi keterlambatan, dan penetapan jadwal kerja karyawan.')

@section('page-actions')
<div class="d-flex justify-content-md-end gap-2 mt-2 mt-md-0">
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalCreateShift">
        <i class="bi bi-plus-circle-fill me-1"></i> Tambah Shift Baru
    </button>
</div>
@endsection

@section('content')
{{-- Shift Cards Row --}}
<div class="row mb-4">
    @forelse($shifts as $shift)
        <div class="col-12 col-md-4 mb-3">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-body p-4">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <span class="badge bg-light-primary text-primary fs-6 px-3 py-2 fw-bold">
                            <i class="bi bi-clock-history me-1"></i> {{ $shift->name }}
                        </span>
                        @if($shift->is_active)
                            <span class="badge bg-success">Aktif</span>
                        @else
                            <span class="badge bg-secondary">Nonaktif</span>
                        @endif
                    </div>

                    <div class="d-flex align-items-center justify-content-between bg-light p-3 rounded-3 mb-3">
                        <div>
                            <small class="text-muted d-block">JAM MASUK</small>
                            <h4 class="text-success fw-bold mb-0">{{ \Carbon\Carbon::parse($shift->start_time)->format('H:i') }}</h4>
                        </div>
                        <div class="text-center text-muted"><i class="bi bi-arrow-right fs-4"></i></div>
                        <div class="text-end">
                            <small class="text-muted d-block">JAM PULANG</small>
                            <h4 class="text-danger fw-bold mb-0">{{ \Carbon\Carbon::parse($shift->end_time)->format('H:i') }}</h4>
                        </div>
                    </div>

                    <div class="d-flex justify-content-between text-xs text-muted mb-4">
                        <span><i class="bi bi-shield-check text-warning me-1"></i> Toleransi: <strong>{{ $shift->late_tolerance_minutes }} Menit</strong></span>
                        <span><i class="bi bi-people text-info me-1"></i> <strong>{{ $shift->schedules_count }} Karyawan</strong></span>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="button" class="btn btn-outline-warning btn-sm w-100" data-bs-toggle="modal" data-bs-target="#modalEditShift{{ $shift->id }}">
                            <i class="bi bi-pencil-square me-1"></i> Edit Shift
                        </button>
                        <button type="button" class="btn btn-outline-danger btn-sm" onclick="confirmDeleteShift('{{ $shift->id }}', '{{ $shift->name }}')">
                            <i class="bi bi-trash"></i>
                        </button>
                        <form id="delete-shift-{{ $shift->id }}" action="{{ route('admin.shifts.destroy', $shift->id) }}" method="POST" class="d-none">
                            @csrf
                            @method('DELETE')
                        </form>
                    </div>
                </div>
            </div>
        </div>

        {{-- Modal Edit Shift --}}
        <div class="modal fade" id="modalEditShift{{ $shift->id }}" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content border-0 shadow">
                    <div class="modal-header bg-warning text-dark">
                        <h5 class="modal-title"><i class="bi bi-pencil-square me-2"></i> Edit Shift: {{ $shift->name }}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <form action="{{ route('admin.shifts.update', $shift->id) }}" method="POST">
                        @csrf
                        @method('PUT')
                        <div class="modal-body p-4">
                            <div class="mb-3">
                                <label class="form-label fw-bold">Nama Shift <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control" value="{{ $shift->name }}" required>
                            </div>
                            <div class="row g-2 mb-3">
                                <div class="col-6">
                                    <label class="form-label fw-bold">Jam Masuk <span class="text-danger">*</span></label>
                                    <input type="time" name="start_time" class="form-control" value="{{ \Carbon\Carbon::parse($shift->start_time)->format('H:i') }}" required>
                                </div>
                                <div class="col-6">
                                    <label class="form-label fw-bold">Jam Pulang <span class="text-danger">*</span></label>
                                    <input type="time" name="end_time" class="form-control" value="{{ \Carbon\Carbon::parse($shift->end_time)->format('H:i') }}" required>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Toleransi Keterlambatan (Menit) <span class="text-danger">*</span></label>
                                <input type="number" name="late_tolerance_minutes" class="form-control" value="{{ $shift->late_tolerance_minutes }}" min="0" required>
                            </div>
                            <div class="form-check form-switch mb-3">
                                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="shiftActive{{ $shift->id }}" {{ $shift->is_active ? 'checked' : '' }}>
                                <label class="form-check-label fw-bold" for="shiftActive{{ $shift->id }}">Shift Aktif Digunakan</label>
                            </div>
                        </div>
                        <div class="modal-footer bg-light">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                            <button type="submit" class="btn btn-warning"><i class="bi bi-save me-1"></i> Simpan Perubahan</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    @empty
        <div class="col-12">
            <div class="alert alert-info">Belum ada master shift kerja. Silakan buat shift baru.</div>
        </div>
    @endforelse
</div>

{{-- Assignment Matrix Table Card --}}
<div class="card shadow-sm border-0">
    <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
        <h5 class="card-title mb-0 fw-bold"><i class="bi bi-calendar3 me-2 text-primary"></i> Penugasan Jadwal Shift Karyawan</h5>
        <span class="badge bg-light-primary text-primary">Total: {{ $employees->total() }} Karyawan</span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="bg-light">
                    <tr>
                        <th class="ps-4">Karyawan</th>
                        <th>Departemen</th>
                        <th>Shift Saat Ini</th>
                        <th>Jam Kerja Shift</th>
                        <th>Mulai Berlaku</th>
                        <th class="text-end pe-4">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($employees as $emp)
                        @php $current = $emp->currentSchedule(); @endphp
                        <tr>
                            <td class="ps-4">
                                <div class="d-flex align-items-center">
                                    <div class="avatar avatar-md bg-light-primary text-primary me-3 flex-shrink-0">
                                        @if($emp->photo)
                                            <img src="{{ asset($emp->photo) }}" alt="{{ $emp->name }}" style="object-fit: cover; width: 40px; height: 40px; border-radius: 50%;">
                                        @else
                                            <span class="avatar-content">{{ strtoupper(substr($emp->name, 0, 2)) }}</span>
                                        @endif
                                    </div>
                                    <div>
                                        <h6 class="mb-0 fw-bold">{{ $emp->name }}</h6>
                                        <small class="text-muted">{{ $emp->nik }}</small>
                                    </div>
                                </div>
                            </td>
                            <td>{{ $emp->department?->name ?? '-' }}</td>
                            <td>
                                @if($current && $current->shift)
                                    <span class="badge bg-light-success text-success fw-bold"><i class="bi bi-clock me-1"></i> {{ $current->shift->name }}</span>
                                @else
                                    <span class="badge bg-light-danger text-danger">Belum Ditugaskan</span>
                                @endif
                            </td>
                            <td>
                                @if($current && $current->shift)
                                    <strong>{{ \Carbon\Carbon::parse($current->shift->start_time)->format('H:i') }} - {{ \Carbon\Carbon::parse($current->shift->end_time)->format('H:i') }} WIB</strong>
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                            <td>{{ $current ? \Carbon\Carbon::parse($current->effective_from)->format('d M Y') : '-' }}</td>
                            <td class="text-end pe-4">
                                <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#modalAssignShift{{ $emp->id }}">
                                    <i class="bi bi-arrow-repeat me-1"></i> Ubah Shift
                                </button>
                            </td>
                        </tr>

                        {{-- Modal Assign Shift --}}
                        <div class="modal fade" id="modalAssignShift{{ $emp->id }}" tabindex="-1" aria-hidden="true">
                            <div class="modal-dialog modal-dialog-centered">
                                <div class="modal-content border-0 shadow">
                                    <div class="modal-header bg-primary text-white">
                                        <h5 class="modal-title text-white"><i class="bi bi-clock me-2"></i> Tetapkan Shift: {{ $emp->name }}</h5>
                                        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <form action="{{ route('admin.shifts.assign') }}" method="POST">
                                        @csrf
                                        <input type="hidden" name="employee_id" value="{{ $emp->id }}">
                                        <div class="modal-body p-4">
                                            <div class="mb-3">
                                                <label class="form-label fw-bold">Pilih Shift Kerja <span class="text-danger">*</span></label>
                                                <select name="shift_id" class="form-select" required>
                                                    @foreach($shifts as $s)
                                                        <option value="{{ $s->id }}" {{ ($current && $current->shift_id == $s->id) ? 'selected' : '' }}>
                                                            {{ $s->name }} ({{ \Carbon\Carbon::parse($s->start_time)->format('H:i') }} - {{ \Carbon\Carbon::parse($s->end_time)->format('H:i') }} WIB)
                                                        </option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            <div class="mb-3">
                                                <label class="form-label fw-bold">Tanggal Mulai Efektif <span class="text-danger">*</span></label>
                                                <input type="date" name="effective_from" class="form-control" value="{{ date('Y-m-d') }}" required>
                                            </div>
                                        </div>
                                        <div class="modal-footer bg-light">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                            <button type="submit" class="btn btn-primary"><i class="bi bi-check2-circle me-1"></i> Simpan Penugasan</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </tbody>
            </table>
        </div>

        @if($employees->hasPages())
            <div class="p-3 border-top d-flex justify-content-end">
                {{ $employees->links('pagination::bootstrap-5') }}
            </div>
        @endif
    </div>
</div>

{{-- Modal Create Shift --}}
<div class="modal fade" id="modalCreateShift" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title text-white"><i class="bi bi-plus-circle me-2"></i> Tambah Master Shift Baru</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('admin.shifts.store') }}" method="POST">
                @csrf
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold">Nama Shift <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control" placeholder="Contoh: Shift Pagi (08:00 - 17:00)" required>
                    </div>
                    <div class="row g-2 mb-3">
                        <div class="col-6">
                            <label class="form-label fw-bold">Jam Masuk <span class="text-danger">*</span></label>
                            <input type="time" name="start_time" class="form-control" value="08:00" required>
                        </div>
                        <div class="col-6">
                            <label class="form-label fw-bold">Jam Pulang <span class="text-danger">*</span></label>
                            <input type="time" name="end_time" class="form-control" value="17:00" required>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold">Toleransi Keterlambatan (Menit) <span class="text-danger">*</span></label>
                        <input type="number" name="late_tolerance_minutes" class="form-control" value="10" min="0" required>
                    </div>
                    <div class="form-check form-switch mb-3">
                        <input class="form-check-input" type="checkbox" name="is_active" value="1" id="createShiftActive" checked>
                        <label class="form-check-label fw-bold" for="createShiftActive">Shift Aktif Digunakan</label>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary"><i class="bi bi-check2-circle me-1"></i> Tambahkan Shift</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    function confirmDeleteShift(id, name) {
        Swal.fire({
            title: 'Hapus Shift?',
            text: `Apakah Anda yakin ingin menghapus master shift "${name}"?`,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#dc3545',
            cancelButtonColor: '#6c757d',
            confirmButtonText: 'Ya, Hapus',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) {
                document.getElementById(`delete-shift-${id}`).submit();
            }
        });
    }
</script>
@endpush
