<?php
namespace App\Controllers;

use App\Controllers\BaseController;

class User extends BaseController
{
    public function index()
    {
        $db = \Config\Database::connect();
        $data['users'] = $db->table('users u')
            ->join('roles r','r.id=u.role_id')
            ->select('u.*, r.role_name')
            ->get()->getResult();

        return view('user/index', $data);
    }

    public function create()
    {
        $db = \Config\Database::connect();
        $data['roles'] = $db->table('roles')->get()->getResult();
        return view('user/create', $data);
    }

    public function store()
    {
        $db = \Config\Database::connect();

        $db->table('users')->insert([
            'name' => $this->request->getPost('name'),
            'email' => $this->request->getPost('email'),
            'password' => password_hash($this->request->getPost('password'), PASSWORD_DEFAULT),
            'role_id' => $this->request->getPost('role_id'),
            'status' => $this->request->getPost('status'),
            'created_at' => date('Y-m-d H:i:s')
        ]);

        return redirect()->to('admin/users');
    }
}
