<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index_admin()
    {
        return view('pages.user.admin.index', [
            "title" => "Data Admin/Kader",
            "heading" => "Data Admin/Kader",
            "dataUser" => User::get()
        ]);
    }
    public function create_admin()
    {
        return view('pages.user.admin.action.create', [
            "title" => "Tambah Data Admin/Kader",
            "heading" => "Tambah Data Admin/Kader",

        ]);
    }
    public function store_admin(Request $request)
    {
        $customMessages = [
            'required' => 'Kolom :attribute wajib diisi.',
            'max' => 'Kolom :attribute maksimal harus :max karakter.',
            'unique' => 'Username telah digunakan',
        ];
        $validatedData = $request->validate([
            'nama_lengkap' => 'required|max:255',
            'username' => 'required|max:255|unique:users',
            'password' => 'required|max:255',
        ], $customMessages);

        $validatedData['is_admin'] = 1;

        User::create($validatedData);

        return redirect('/user/admin')->with('success', 'Berhasil menambah admin/kader');
    }

    public function edit_admin($id)
    {
        $data = User::findOrFail($id);

        return view('pages.user.admin.action.edit', [
            "title" => "Tambah Data Admin/Kader",
            "heading" => "Tambah Data Admin/Kader",
            "data" => $data

        ]);
    }
    public function update_admin(Request $request, $id)
    {
        $data = User::find($id);

        $customMessages = [
            'required' => 'Kolom :attribute wajib diisi.',
            'max' => 'Kolom :attribute maksimal harus :max karakter.',
        ];

        $validatedData = $request->validate([
            'nama_lengkap' => 'required|max:255',
            'username' => 'required|max:255',
            'password' => 'required|max:255',
        ], $customMessages);

        User::where('id', $data->id)
            ->update($validatedData);

        return redirect('/user/admin')->with('success', 'Data berhasil di Update');
    }

    public function destroy_admin($id)
    {
        $data = User::findOrFail($id);

        User::where('id', $data->id)->delete();

        return redirect('/user/admin')->with('success', 'Data Berhasil Dihapus');
    }

    public function index_bidan()
    {
        return view('pages.user.bidan.index', [
            "title" => "Data Bidan",
            "heading" => "Data Bidan",
            "dataUser" => User::get()
        ]);
    }

    public function create_bidan()
    {
        return view('pages.user.bidan.action.create', [
            "title" => "Tambah Data Bidan",
            "heading" => "Tambah Data Bidan",

        ]);
    }

    public function store_bidan(Request $request)
    {
        $customMessages = [
            'required' => 'Kolom :attribute wajib diisi.',
            'max' => 'Kolom :attribute maksimal harus :max karakter.',
        ];
        $validatedData = $request->validate([
            'nama_lengkap' => 'required|max:255',
            'username' => 'required|max:255',
            'password' => 'required|max:255',
        ], $customMessages);

        $validatedData['is_admin'] = 0;

        User::create($validatedData);

        return redirect('/user/bidan')->with('success', 'Berhasil menambah bidan');
    }

    public function edit_bidan($id)
    {
        $data = User::findOrFail($id);

        return view('pages.user.bidan.action.edit', [
            "title" => "Edit Data Bidan",
            "heading" => "Edit Data Bidan",
            "data" => $data

        ]);
    }
    public function update_bidan(Request $request, $id)
    {
        $data = User::find($id);

        $customMessages = [
            'required' => 'Kolom :attribute wajib diisi.',
            'max' => 'Kolom :attribute maksimal harus :max karakter.',
        ];

        $validatedData = $request->validate([
            'nama_lengkap' => 'required|max:255',
            'username' => 'required|max:255',
            'password' => 'required|max:255',
        ], $customMessages);

        User::where('id', $data->id)
            ->update($validatedData);

        return redirect('/user/bidan')->with('success', 'Data berhasil di Update');
    }

    public function destroy_bidan($id)
    {
        $data = User::findOrFail($id);

        User::where('id', $data->id)->delete();

        return redirect('/user/bidan')->with('success', 'Data Berhasil Dihapus');
    }
}
