<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function cetak()
    {
        $data['title'] = 'Laporan Data User';
        $data['no'] = 1;
        $data['rows'] = User::orderBy('id_user')->get();
        return view('user.cetak', $data);
    }
    public function register()
    {

        return view('user.register');
    }
    public function registerForm(Request $request)
{
    // Validasi data
    $request->validate([
        'nama_user' => 'required',
        'username' => 'required|unique:tb_user',
        'password' => 'required|confirmed', // Menambahkan aturan confirmed untuk memastikan password sesuai dengan password konfirmasi
    ], [
        'nama_user.required' => 'Nama user harus diisi',
        'username.required' => 'Username harus diisi',
        'username.unique' => 'Username harus unik',
        'password.required' => 'Password harus diisi',
        'password.confirmed' => 'Konfirmasi password tidak cocok', // Pesan kesalahan untuk konfirmasi password yang tidak cocok
    ]);

    // Buat instance User baru
    $user = new User();

    // Isi data user dari request
    $user->nama_user = $request->nama_user;
    $user->username = $request->username;
    $user->password = Hash::make($request->password);
    $user->level = 'karyawan'; // Tetapkan level sebagai 'admin'
    $user->status_user = '1'; // Tetapkan status sebagai 'aktif'

    // Simpan data user ke database
    $user->save();

    // Redirect ke halaman tertentu dengan pesan sukses atau melakukan tindakan lain sesuai kebutuhan aplikasi Anda
    return redirect('login')->with('message', 'berhasil mendaftar!');
}


    public function profil()
    {
        $data['title'] = 'Ubah Profil';
        $data['user'] = Auth::user();
        return view('user.profil', $data);
    }

    public function profilUpdate(Request $request)
    {
        $request->validate([
            'nama_user' => 'required',
            'username' => 'required',
        ], [
            'nama_user.required' => 'Nama user harus diisi',
            'username.required' => 'Username harus diisi',
        ]);
        $user = current_user();
        if (get_row("SELECT * FROM tb_user WHERE username='{$request->username}' AND id_user<>'$user->id_user'"))
            return back()->withErrors([
                'username' => 'Username sudah terdaftar!',
            ]);

        $user->nama_user = $request->nama_user;
        $user->username = $request->username;

        $user->save();
        $request->session()->regenerate();
        return back()->with('message', 'Data berhasil diubah!');
    }
    public function logout(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect('/');
    }
    public function password()
    {
        $data['title'] = 'Ubah Password';
        $data['user'] = Auth::user();
        return view('user.password', $data);
    }

    public function passwordUpdate(Request $request)
    {
        $request->validate([
            'pass1' => 'required',
            'pass2' => 'required|confirmed',
        ], [
            'pass1.required' => 'Password lama harus diisi',
            'pass2.required' => 'Password baru harus diisi',
            'pass2.confirmed' => 'Password baru dan konfirmasi password baru harus sama',
        ]);
        $user = current_user();
        if (!Hash::check($request->pass1, $request->user()->password))
            return back()->withErrors([
                'username' => 'Password lama salah!',
            ]);

        $user->password = Hash::make($request->pass2);
        $user->save();
        $request->session()->regenerate();
        return back()->with('message', 'Data berhasil diubah!');
    }
    public function loginForm()
    {
        return view('user.login');
    }
    public function loginAction(Request $request)
    {
        if (Auth::attempt(['username' => $request->username, 'password' => $request->password, 'status_user' => 1])) {
            $request->session()->regenerate();
            return redirect()->intended('/');
        }

        return back()->withErrors([
            'email' => 'Salah kombinasi username dan password',
        ]);
    }

    public function index(Request $request)
    {
        $data['q'] = $request->input('q');
        $data['title'] = 'Data User';
        $data['limit'] = 10;
        $data['rows'] = User::where('nama_user', 'like', '%' . $data['q'] . '%')
            ->where('level', 'karyawan')
            ->orderBy('id_user')
            ->paginate($data['limit'])->withQueryString();
        return view('user.index', $data);
    }

    public function create()
    {
        $data['title'] = 'Tambah User';
        return view('user.create', $data);
    }

    public function store(Request $request)
    {
        $request->validate([
            'nama_user' => 'required',
            'username' => 'required|unique:tb_user',
            'password' => 'required',
            'level' => 'required',
            'status_user' => 'required',
        ], [
            'nama_user.required' => 'Nama user harus diisi',
            'username.required' => 'Username harus diisi',
            'username.unique' => 'Username harus unik',
            'password.required' => 'Password harus diisi',
            'level.required' => 'Level harus diisi',
            'status_user.required' => 'Status harus diisi',
        ]);
        $user = new User($request->all());
        $user->password = Hash::make($request->password);
        $user->save();
        return redirect('user')->with('message', 'Data berhasil ditambah!');
    }

    public function edit(User $user)
    {
        $data['row'] = $user;
        $data['title'] = 'Ubah User';
        return view('user.edit', $data);
    }

    public function update(Request $request, User $user)
    {
        $request->validate([
            'nama_user' => 'required',
            'username' => 'required',
            'level' => 'required',
            'status_user' => 'required',
        ], [
            'nama_user.required' => 'Nama user harus diisi',
            'username.required' => 'Username harus diisi',
            'username.unique' => 'Username harus unik',
            'level.required' => 'Level harus diisi',
            'status_user.required' => 'Status harus diisi',
        ]);

        if (get_row("SELECT * FROM tb_user WHERE username='{$request->username}' AND id_user<>'$user->id_user'"))
            return back()->withErrors([
                'username' => 'Username sudah terdaftar!',
            ]);

        $user->nama_user = $request->nama_user;
        $user->username = $request->username;
        if ($request->password)
            $user->password = Hash::make($request->password);
        $user->level = $request->level;
        $user->status_user = $request->status_user;
        $user->save();
        return redirect('user')->with('message', 'Data berhasil diubah!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        $user->delete();
        return redirect('user')->with('message', 'Data berhasil dihapus!');
    }
}
