<?php

namespace App\Http\Controllers;

use App\Models\KeanggotaanModel;
use Illuminate\Http\Request;
use Illuminate\Support\Str;


class RegisterController extends Controller
{
    public function index()
    {
        return view('register');
    }
    public function submit(Request $request)
    {
        $validated = $request->except('_token');
        $validated['kode_anggota'] = Str::random(8);

        if ($request->file('ktp')) {
            $imageName = time() . '.' . $request->ktp->extension();
            $request->ktp->move(public_path('storage/ktp'), $imageName);
            $validated['ktp'] = $imageName;
        }
        $result = KeanggotaanModel::insert($validated);

        if ($result) {
            return redirect('register')->with('response', ['status' => 200, 'message' => "Pendaftaran akun sedang dalam proses, kami akan menghubungi Anda jika pendaftaran Anda diterima."]);
        }
    }
}
