<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class UserController extends BaseController
{
    private $db;

    public function __construct()
    {
        $this->db = \Config\Database::connect();
    }

    public function index()
    {
        $builder = $this->db->table('users');
        $builder->select('users.*, bidang.nama_bidang, opd.nama_opd');
        $builder->join('bidang', 'bidang.id = users.bidang_id', 'left');
        $builder->join('opd', 'opd.id = users.opd_id', 'left');
        $users = $builder->get()->getResultArray();

        $data = [
            'title' => 'Kelola User',
            'users' => $users
        ];
        return view('superadmin/users/index', $data);
    }

    public function create()
    {
        $data = [
            'title' => 'Tambah User',
            'bidang' => $this->db->table('bidang')->get()->getResultArray(),
            'opd' => $this->db->table('opd')->get()->getResultArray()
        ];
        return view('superadmin/users/create', $data);
    }

    public function store()
    {
        $data = [
            'username' => $this->request->getPost('username'),
            'password' => password_hash($this->request->getPost('password'), PASSWORD_DEFAULT),
            'role' => $this->request->getPost('role'),
            'created_at' => date('Y-m-d H:i:s')
        ];

        // Conditional fields based on role
        if ($data['role'] == 'admin_bidang') {
            $data['bidang_id'] = $this->request->getPost('bidang_id');
            $data['opd_id'] = null;
        } elseif ($data['role'] == 'opd') {
            $data['opd_id'] = $this->request->getPost('opd_id');
            $data['bidang_id'] = null;
        } else {
            $data['bidang_id'] = null;
            $data['opd_id'] = null;
        }

        $this->db->table('users')->insert($data);
        
        return redirect()->to('/users')->with('success', 'User berhasil ditambahkan');
    }

    public function edit($id)
    {
        $user = $this->db->table('users')->where('id', $id)->get()->getRowArray();
        
        if (!$user) {
            return redirect()->to('/users')->with('error', 'Data tidak ditemukan');
        }

        $data = [
            'title' => 'Edit User',
            'user' => $user,
            'bidang' => $this->db->table('bidang')->get()->getResultArray(),
            'opd' => $this->db->table('opd')->get()->getResultArray()
        ];
        return view('superadmin/users/edit', $data);
    }

    public function update($id)
    {
        $data = [
            'username' => $this->request->getPost('username'),
            'role' => $this->request->getPost('role'),
            'updated_at' => date('Y-m-d H:i:s')
        ];

        if ($this->request->getPost('password')) {
            $data['password'] = password_hash($this->request->getPost('password'), PASSWORD_DEFAULT);
        }

        // Conditional fields based on role
        if ($data['role'] == 'admin_bidang') {
            $data['bidang_id'] = $this->request->getPost('bidang_id');
            $data['opd_id'] = null;
        } elseif ($data['role'] == 'opd') {
            $data['opd_id'] = $this->request->getPost('opd_id');
            $data['bidang_id'] = null;
        } else {
            $data['bidang_id'] = null;
            $data['opd_id'] = null;
        }

        $this->db->table('users')->where('id', $id)->update($data);
        
        return redirect()->to('/users')->with('success', 'User berhasil diperbarui');
    }

    public function delete($id)
    {
        $this->db->table('users')->where('id', $id)->delete();
        return redirect()->to('/users')->with('success', 'User berhasil dihapus');
    }
}
