@extends('layouts.app')

@section('title', 'Data Karyawan')
@section('page-title', 'Manajemen Karyawan')
@section('page-subtitle', 'Kelola informasi profil, departemen, jabatan, dan jadwal shift karyawan.')

@section('page-actions')
<div class="d-flex justify-content-md-end gap-2 mt-2 mt-md-0">
    <a href="{{ route('admin.employees.export') }}" class="btn btn-outline-success">
        <i class="bi bi-file-earmark-excel me-1"></i> Export Data
    </a>
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalCreateEmployee">
        <i class="bi bi-person-plus-fill me-1"></i> Tambah Karyawan
    </button>
</div>
@endsection

@section('content')
{{-- Stats Row --}}
<div class="row g-3 mb-4">
    <div class="col-12 col-sm-6 col-xl-3">
        <div class="material-stat-card">
            <div class="stat-icon-glow stat-icon-purple">
                <i class="bi bi-people-fill"></i>
            </div>
            <div class="stat-info-box">
                <div class="stat-info-label">Total Karyawan</div>
                <h3 class="stat-info-val">{{ $employees->total() }}</h3>
                <small class="text-muted text-xs font-semibold">Tercatat di Database</small>
            </div>
        </div>
    </div>
    <div class="col-12 col-sm-6 col-xl-3">
        <div class="material-stat-card">
            <div class="stat-icon-glow stat-icon-green">
                <i class="bi bi-person-check-fill"></i>
            </div>
            <div class="stat-info-box">
                <div class="stat-info-label">Status Aktif</div>
                <h3 class="stat-info-val text-success">{{ $totalActive }}</h3>
                <small class="text-muted text-xs font-semibold">Karyawan Aktif Bekerja</small>
            </div>
        </div>
    </div>
    <div class="col-12 col-sm-6 col-xl-3">
        <div class="material-stat-card">
            <div class="stat-icon-glow stat-icon-rose">
                <i class="bi bi-person-x-fill"></i>
            </div>
            <div class="stat-info-box">
                <div class="stat-info-label">Non-Aktif</div>
                <h3 class="stat-info-val text-danger">{{ $totalInactive }}</h3>
                <small class="text-muted text-xs font-semibold">Resigned / Non-Aktif</small>
            </div>
        </div>
    </div>
    <div class="col-12 col-sm-6 col-xl-3">
        <div class="material-stat-card">
            <div class="stat-icon-glow stat-icon-orange">
                <i class="bi bi-building"></i>
            </div>
            <div class="stat-info-box">
                <div class="stat-info-label">Departemen</div>
                <h3 class="stat-info-val text-primary">{{ $departments->count() }}</h3>
                <small class="text-muted text-xs font-semibold">Divisi Operasional</small>
            </div>
        </div>
    </div>
</div>

{{-- Filter & Search Card --}}
<div class="card shadow-sm border-0 mb-4">
    <div class="card-body p-3">
        <form method="GET" action="{{ route('admin.employees.index') }}" class="row g-2 align-items-center">
            <div class="col-12 col-md-5">
                <div class="input-group">
                    <span class="input-group-text bg-light border-0"><i class="bi bi-search"></i></span>
                    <input type="text" name="search" class="form-control bg-light border-0" placeholder="Cari NIK, Nama, Email, atau WhatsApp..." value="{{ request('search') }}">
                </div>
            </div>
            <div class="col-6 col-md-3">
                <select name="department_id" class="form-select bg-light border-0">
                    <option value="">Semua Departemen</option>
                    @foreach($departments as $dept)
                        <option value="{{ $dept->id }}" {{ request('department_id') == $dept->id ? 'selected' : '' }}>{{ $dept->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-6 col-md-2">
                <select name="status" class="form-select bg-light border-0">
                    <option value="">Semua Status</option>
                    <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Aktif</option>
                    <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>Nonaktif</option>
                </select>
            </div>
            <div class="col-12 col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-primary w-100"><i class="bi bi-funnel me-1"></i> Filter</button>
                @if(request()->hasAny(['search', 'department_id', 'status']))
                    <a href="{{ route('admin.employees.index') }}" class="btn btn-light" title="Reset Filter"><i class="bi bi-arrow-counterclockwise"></i></a>
                @endif
            </div>
        </form>
    </div>
</div>

{{-- Main Employee Table Card --}}
<div class="card shadow-sm border-0">
    <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>Kontak & WhatsApp</th>
                        <th>Departemen & Jabatan</th>
                        <th>Shift Kerja</th>
                        <th>Status</th>
                        <th class="text-end pe-4">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($employees as $emp)
                        <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: 45px; height: 45px; border-radius: 50%;">
                                        @else
                                            <span class="avatar-content fw-bold">{{ strtoupper(substr($emp->name, 0, 2)) }}</span>
                                        @endif
                                    </div>
                                    <div>
                                        <h6 class="mb-0 fw-bold text-dark">{{ $emp->name }}</h6>
                                        <small class="text-muted"><span class="badge bg-light text-secondary">{{ $emp->nik }}</span> • {{ $emp->gender == 'male' ? 'L' : 'P' }}</small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div><i class="bi bi-envelope text-muted me-1"></i> <small>{{ $emp->email }}</small></div>
                                <div>
                                    <i class="bi bi-whatsapp text-success me-1"></i> 
                                    <small>{{ $emp->phone ?? '-' }}</small>
                                </div>
                            </td>
                            <td>
                                <span class="badge bg-light-primary text-primary fw-semibold">{{ $emp->department?->name ?? 'Tanpa Departemen' }}</span>
                                <br><small class="text-muted">{{ $emp->position?->name ?? '-' }}</small>
                            </td>
                            <td>
                                @php $sch = $emp->currentSchedule(); @endphp
                                @if($sch && $sch->shift)
                                    <span class="badge bg-light-info text-info"><i class="bi bi-clock me-1"></i>{{ $sch->shift->name }}</span>
                                    <br><small class="text-muted">{{ \Carbon\Carbon::parse($sch->shift->start_time)->format('H:i') }} - {{ \Carbon\Carbon::parse($sch->shift->end_time)->format('H:i') }}</small>
                                @else
                                    <span class="badge bg-light-secondary text-secondary">Belum ada shift</span>
                                @endif
                            </td>
                            <td>
                                @if($emp->status === 'active')
                                    <span class="badge bg-light-success text-success"><i class="bi bi-check-circle me-1"></i> Aktif</span>
                                @else
                                    <span class="badge bg-light-danger text-danger"><i class="bi bi-x-circle me-1"></i> Nonaktif</span>
                                @endif
                                <br><small class="text-muted">{{ ucfirst($emp->employment_status) }}</small>
                            </td>
                            <td class="text-end pe-4">
                                <div class="btn-group">
                                    <button type="button" class="btn btn-sm btn-outline-info" data-bs-toggle="modal" data-bs-target="#modalDetailEmployee{{ $emp->id }}" title="Detail Lengkap">
                                        <i class="bi bi-eye"></i>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-outline-warning" data-bs-toggle="modal" data-bs-target="#modalEditEmployee{{ $emp->id }}" title="Edit Data">
                                        <i class="bi bi-pencil-square"></i>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-outline-danger" onclick="confirmDelete('{{ $emp->id }}', '{{ $emp->name }}')" title="Hapus Karyawan">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </div>
                                <form id="delete-form-{{ $emp->id }}" action="{{ route('admin.employees.destroy', $emp->id) }}" method="POST" class="d-none">
                                    @csrf
                                    @method('DELETE')
                                </form>
                            </td>
                        </tr>

                        {{-- Modal Detail --}}
                        <div class="modal fade" id="modalDetailEmployee{{ $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-light">
                                        <h5 class="modal-title"><i class="bi bi-person-badge me-2 text-primary"></i> Detail Karyawan</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <div class="modal-body text-center pt-4">
                                        <div class="avatar avatar-xl bg-light-primary mx-auto mb-3">
                                            @if($emp->photo)
                                                <img src="{{ asset($emp->photo) }}" alt="{{ $emp->name }}" style="width: 90px; height: 90px; object-fit: cover; border-radius: 50%;">
                                            @else
                                                <span class="avatar-content fs-3">{{ strtoupper(substr($emp->name, 0, 2)) }}</span>
                                            @endif
                                        </div>
                                        <h4 class="fw-bold mb-1">{{ $emp->name }}</h4>
                                        <p class="text-muted mb-3">{{ $emp->email }} • <span class="badge bg-light text-dark">{{ $emp->nik }}</span></p>

                                        <div class="text-start bg-light p-3 rounded-3 mb-3">
                                            <div class="row g-2">
                                                <div class="col-6"><small class="text-muted d-block">Departemen:</small><strong>{{ $emp->department?->name ?? '-' }}</strong></div>
                                                <div class="col-6"><small class="text-muted d-block">Jabatan:</small><strong>{{ $emp->position?->name ?? '-' }}</strong></div>
                                                <div class="col-6"><small class="text-muted d-block">No. WhatsApp:</small><strong>{{ $emp->phone ?? '-' }}</strong></div>
                                                <div class="col-6"><small class="text-muted d-block">Jenis Kelamin:</small><strong>{{ $emp->gender == 'male' ? 'Laki-laki' : 'Perempuan' }}</strong></div>
                                                <div class="col-6"><small class="text-muted d-block">Tempat/Tgl Lahir:</small><strong>{{ $emp->birth_place ?? '-' }}, {{ $emp->birth_date ? $emp->birth_date->format('d/m/Y') : '-' }}</strong></div>
                                                <div class="col-6"><small class="text-muted d-block">Sisa Hak Cuti:</small><strong class="text-primary">{{ $emp->leave_balance }} Hari</strong></div>
                                                <div class="col-12"><small class="text-muted d-block">Alamat Tinggal:</small><span>{{ $emp->address ?? '-' }}</span></div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer bg-light">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        {{-- Modal Edit --}}
                        <div class="modal fade" id="modalEditEmployee{{ $emp->id }}" tabindex="-1" aria-hidden="true">
                            <div class="modal-dialog modal-lg modal-dialog-centered">
                                <div class="modal-content border-0 shadow">
                                    <div class="modal-header bg-light">
                                        <h5 class="modal-title"><i class="bi bi-pencil-square text-warning me-2"></i> Edit Data Karyawan</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <form action="{{ route('admin.employees.update', $emp->id) }}" method="POST" enctype="multipart/form-data">
                                        @csrf
                                        @method('PUT')
                                        <div class="modal-body p-4">
                                            <div class="row g-3">
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">NIK Karyawan <span class="text-danger">*</span></label>
                                                    <input type="text" name="nik" class="form-control" value="{{ old('nik', $emp->nik) }}" required>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Nama Lengkap <span class="text-danger">*</span></label>
                                                    <input type="text" name="name" class="form-control" value="{{ old('name', $emp->name) }}" required>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Email Akun <span class="text-danger">*</span></label>
                                                    <input type="email" name="email" class="form-control" value="{{ old('email', $emp->email) }}" required>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Password Baru <small class="text-muted">(Kosongkan jika tidak diganti)</small></label>
                                                    <input type="password" name="password" class="form-control" placeholder="Minimal 6 karakter">
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Nomor WhatsApp</label>
                                                    <input type="text" name="phone" class="form-control" value="{{ old('phone', $emp->phone) }}" placeholder="0812xxxx">
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Jenis Kelamin <span class="text-danger">*</span></label>
                                                    <select name="gender" class="form-select" required>
                                                        <option value="male" {{ $emp->gender == 'male' ? 'selected' : '' }}>Laki-laki</option>
                                                        <option value="female" {{ $emp->gender == 'female' ? 'selected' : '' }}>Perempuan</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Departemen</label>
                                                    <select name="department_id" class="form-select">
                                                        <option value="">Pilih Departemen</option>
                                                        @foreach($departments as $dept)
                                                            <option value="{{ $dept->id }}" {{ $emp->department_id == $dept->id ? 'selected' : '' }}>{{ $dept->name }}</option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Jabatan</label>
                                                    <select name="position_id" class="form-select">
                                                        <option value="">Pilih Jabatan</option>
                                                        @foreach($positions as $pos)
                                                            <option value="{{ $pos->id }}" {{ $emp->position_id == $pos->id ? 'selected' : '' }}>{{ $pos->name }}</option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Jadwal Shift Kerja</label>
                                                    <select name="shift_id" class="form-select">
                                                        <option value="">Pilih Shift</option>
                                                        @foreach($shifts as $s)
                                                            <option value="{{ $s->id }}" {{ $emp->currentSchedule()?->shift_id == $s->id ? 'selected' : '' }}>{{ $s->name }} ({{ $s->start_time }} - {{ $s->end_time }})</option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Status Kepegawaian</label>
                                                    <select name="employment_status" class="form-select">
                                                        <option value="permanent" {{ $emp->employment_status == 'permanent' ? 'selected' : '' }}>Tetap (Permanent)</option>
                                                        <option value="contract" {{ $emp->employment_status == 'contract' ? 'selected' : '' }}>Kontrak (Contract)</option>
                                                        <option value="internship" {{ $emp->employment_status == 'internship' ? 'selected' : '' }}>Magang (Internship)</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Status Akun</label>
                                                    <select name="status" class="form-select">
                                                        <option value="active" {{ $emp->status == 'active' ? 'selected' : '' }}>Aktif</option>
                                                        <option value="inactive" {{ $emp->status == 'inactive' ? 'selected' : '' }}>Nonaktif</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Tempat Lahir</label>
                                                    <input type="text" name="birth_place" class="form-control" value="{{ old('birth_place', $emp->birth_place) }}" placeholder="Kota Lahir">
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Tanggal Lahir</label>
                                                    <input type="date" name="birth_date" class="form-control" value="{{ old('birth_date', $emp->birth_date ? $emp->birth_date->format('Y-m-d') : '') }}">
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Tanggal Bergabung</label>
                                                    <input type="date" name="join_date" class="form-control" value="{{ old('join_date', $emp->join_date ? $emp->join_date->format('Y-m-d') : '') }}">
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Sisa Kuota Cuti (Hari)</label>
                                                    <input type="number" name="leave_balance" class="form-control" value="{{ old('leave_balance', $emp->leave_balance) }}" min="0">
                                                </div>
                                                <div class="col-md-6">
                                                    <label class="form-label fw-bold">Foto Profil Baru</label>
                                                    <input type="file" name="photo" class="form-control" accept="image/*">
                                                </div>
                                                <div class="col-12">
                                                    <label class="form-label fw-bold">Alamat Lengkap</label>
                                                    <textarea name="address" class="form-control" rows="2">{{ old('address', $emp->address) }}</textarea>
                                                </div>
                                            </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
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="bi bi-people fs-1 d-block mb-2 text-muted"></i>
                                Tidak ada data karyawan yang sesuai dengan kriteria pencarian.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Pagination --}}
        @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 Employee --}}
<div class="modal fade" id="modalCreateEmployee" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg 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-person-plus-fill me-2"></i> Tambah Karyawan Baru</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('admin.employees.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="modal-body p-4">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label fw-bold">NIK Karyawan <span class="text-danger">*</span></label>
                            <input type="text" name="nik" class="form-control" placeholder="Contoh: EMP-2026-011" value="{{ old('nik') }}" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Nama Lengkap <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" placeholder="Nama lengkap karyawan" value="{{ old('name') }}" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Email Login <span class="text-danger">*</span></label>
                            <input type="email" name="email" class="form-control" placeholder="karyawan@company.com" value="{{ old('email') }}" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Password Login</label>
                            <input type="password" name="password" class="form-control" placeholder="Default: password (jika kosong)">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Nomor WhatsApp</label>
                            <input type="text" name="phone" class="form-control" placeholder="08123456789" value="{{ old('phone') }}">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Jenis Kelamin <span class="text-danger">*</span></label>
                            <select name="gender" class="form-select" required>
                                <option value="male">Laki-laki</option>
                                <option value="female">Perempuan</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Departemen</label>
                            <select name="department_id" class="form-select">
                                <option value="">Pilih Departemen</option>
                                @foreach($departments as $dept)
                                    <option value="{{ $dept->id }}">{{ $dept->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Jabatan</label>
                            <select name="position_id" class="form-select">
                                <option value="">Pilih Jabatan</option>
                                @foreach($positions as $pos)
                                    <option value="{{ $pos->id }}">{{ $pos->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Jadwal Shift Kerja</label>
                            <select name="shift_id" class="form-select">
                                <option value="">Pilih Shift</option>
                                @foreach($shifts as $s)
                                    <option value="{{ $s->id }}">{{ $s->name }} ({{ $s->start_time }} - {{ $s->end_time }})</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Status Kepegawaian</label>
                            <select name="employment_status" class="form-select">
                                <option value="permanent">Tetap (Permanent)</option>
                                <option value="contract">Kontrak (Contract)</option>
                                <option value="internship">Magang (Internship)</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Status Akun</label>
                            <select name="status" class="form-select">
                                <option value="active">Aktif</option>
                                <option value="inactive">Nonaktif</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold">Foto Profil</label>
                            <input type="file" name="photo" class="form-control" accept="image/*">
                        </div>
                        <div class="col-12">
                            <label class="form-label fw-bold">Alamat Lengkap</label>
                            <textarea name="address" class="form-control" rows="2" placeholder="Alamat domisili saat ini"></textarea>
                        </div>
                    </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 Karyawan</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    function confirmDelete(id, name) {
        Swal.fire({
            title: 'Hapus Data Karyawan?',
            text: `Apakah Anda yakin ingin menghapus "${name}" beserta akun loginnya? Tindakan ini tidak dapat dibatalkan.`,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#dc3545',
            cancelButtonColor: '#6c757d',
            confirmButtonText: 'Ya, Hapus Data',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) {
                document.getElementById(`delete-form-${id}`).submit();
            }
        });
    }
</script>
@endpush
