<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Anggota;
use Illuminate\Support\Facades\Mail;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use App\Mail\StatusAnggotaDiperbarui;

class AnggotaController extends Controller
{
    
    public function index(Request $request)
    {
        $query = $request->input('search');
        $status = $request->input('status');

        $members = Anggota::when($query, function ($q) use ($query) {
            $q->where('nama', 'like', "%{$query}%")
                ->orWhere('email', 'like', "%{$query}%");
        })
            ->when($status, function ($q) use ($status) {
                $q->where('status', $status);
            })
            ->orderByDesc('created_at')
            ->paginate(10)
            ->withQueryString();

        return view('admin.anggota.index', compact('members'));
    }

    
    public function create()
    {
        return view('admin.anggota.create');
    }

    
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'nama' => 'required|string|max:100',
            'email' => 'required|email|unique:anggota,email',
            'alamat' => 'required|string|max:255',
            'no_hp' => 'nullable|string|max:20',
        ]);

        $validatedData['status'] = 'disetujui'; 

        Anggota::create($validatedData);

        return redirect()->route('admin.anggota.index')
            ->with('success', 'Anggota baru berhasil didaftarkan dan disetujui!');
    }

    
    public function edit($id)
    {
        $member = Anggota::findOrFail($id);
        return view('admin.anggota.edit', compact('member'));
    }

   
    public function update(Request $request, $id)
    {
        $member = Anggota::findOrFail($id);

        $validatedData = $request->validate([
            'nama' => 'required|string|max:100',
            'email' => 'required|email|unique:anggota,email,' . $member->id,
            'alamat' => 'required|string|max:255',
            'no_hp' => 'nullable|string|max:20',
            'status' => 'required|in:menunggu,disetujui,ditolak',
        ]);

        $member->update($validatedData);

        return redirect()->route('admin.anggota.index')
            ->with('success', 'Data anggota berhasil diperbarui!');
    }

   
    public function destroy($id)
    {
        $member = Anggota::findOrFail($id);
        $member->delete();

        return redirect()->route('admin.anggota.index')
            ->with('success', 'Anggota berhasil dihapus!');
    }

    
    public function cetak($id)
    {
        $member = Anggota::findOrFail($id);
        $qrCode = base64_encode(QrCode::format('svg')->size(120)->generate($member->nama));

        return view('admin.anggota.cetak', compact('member', 'qrCode'));
    }

    
    public function approve($id)
    {
        $member = Anggota::findOrFail($id);
        $member->update(['status' => 'disetujui']);

        Mail::to($member->email)->send(new StatusAnggotaDiperbarui($member));

        return back()->with('success', "Anggota {$member->nama} telah disetujui.");
    }

    public function reject($id)
    {
        $member = Anggota::findOrFail($id);
        $member->update(['status' => 'ditolak']);

        Mail::to($member->email)->send(new StatusAnggotaDiperbarui($member));

        return back()->with('warning', "Anggota {$member->nama} telah ditolak.");
    }
}
