<?php

namespace App\Http\Controllers\Superadmin;

use App\Http\Controllers\Controller;
use App\Models\Employee;
use Illuminate\Http\Request;

class EmployeeController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $employees = Employee::orderBy('name')->get();
        return view('superadmin.employees.index', compact('employees'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('superadmin.employees.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:100',
            'nip' => 'required|string|max:20|unique:employees,nip',
            'rank' => 'nullable|string|max:50',
            'phone' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:100',
        ], [
            'name.required' => 'Nama harus diisi',
            'nip.required' => 'NIP harus diisi',
            'nip.unique' => 'NIP sudah terdaftar',
            'email.email' => 'Format email tidak valid',
        ]);

        Employee::create($request->all());

        return redirect()->route('superadmin.employees.index')
            ->with('success', 'Data pegawai berhasil ditambahkan');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Employee $employee)
    {
        return view('superadmin.employees.edit', compact('employee'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Employee $employee)
    {
        $request->validate([
            'name' => 'required|string|max:100',
            'nip' => 'required|string|max:20|unique:employees,nip,' . $employee->id,
            'rank' => 'nullable|string|max:50',
            'phone' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:100',
        ], [
            'name.required' => 'Nama harus diisi',
            'nip.required' => 'NIP harus diisi',
            'nip.unique' => 'NIP sudah terdaftar',
            'email.email' => 'Format email tidak valid',
        ]);

        $employee->update($request->all());

        return redirect()->route('superadmin.employees.index')
            ->with('success', 'Data pegawai berhasil diperbarui');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Employee $employee)
    {
        try {
            $employee->delete();
            return redirect()->route('superadmin.employees.index')
                ->with('success', 'Data pegawai berhasil dihapus');
        } catch (\Exception $e) {
            return redirect()->route('superadmin.employees.index')
                ->with('error', 'Gagal menghapus data pegawai');
        }
    }

    /**
     * Toggle active status.
     */
    public function toggleStatus(Employee $employee)
    {
        $employee->is_active = !$employee->is_active;
        $employee->save();

        $status = $employee->is_active ? 'diaktifkan' : 'dinonaktifkan';
        return redirect()->route('superadmin.employees.index')
            ->with('success', "Pegawai berhasil {$status}");
    }
}
