<?php 

namespace App\Controllers\Owner; 

use App\Controllers\BaseController; 
use App\Models\PayrollModel; 
use App\Models\UserModel; 

class Payroll extends BaseController { 

    protected $payroll; 
    protected $user; 

    public function __construct() { 
        helper(['form', 'url']); 
        $this->payroll = new PayrollModel(); 
        $this->user    = new UserModel(); 
    } 

    public function index() { 
        $payrolls = $this->payroll 
            ->select('payrolls.*, users.nama_lengkap') 
            ->join('users', 'users.id_user = payrolls.id_user', 'left') 
            ->orderBy('periode', 'DESC') 
            ->findAll(); 

        return view('owner/payroll/index', [ 
            'title'    => 'Penggajian', 
            'payrolls' => $payrolls 
        ]); 
    } 

    public function create() { 
        return view("owner/payroll/create", [ 
            "title"           => "Tambah Penggajian", 
            "users"           => $this->user->where("is_active", 1)->findAll(), 
            "default_periode" => date('Y-m') 
        ]); 
    } 

    public function store() { 
        // Ambil input periode (misal: "2026-07")
        $periode_input = $this->request->getPost("periode"); 
        // Tambahkan "-01" agar menjadi format DATE yang valid: "2026-07-01"
        $periode_valid = !empty($periode_input) ? $periode_input . "-01" : null; 

        $this->payroll->insert([ 
            "id_user"    => $this->request->getPost("id_user"), 
            "periode"    => $periode_valid, 
            "gaji_pokok" => $this->request->getPost("gaji_pokok"), 
            "bonus"      => $this->request->getPost("bonus"), 
            "potongan"   => $this->request->getPost("potongan"), 
            "total_gaji" => $this->request->getPost("total_gaji"), 
            "status"     => "Pending" 
        ]); 

        return redirect()->to("/owner/payroll")->with("success", "Data penggajian berhasil disimpan."); 
    } 

    public function detail($id) { 
        $payroll = $this->payroll 
            ->select('payrolls.*, users.nama_lengkap') 
            ->join('users', 'users.id_user = payrolls.id_user', 'left') 
            ->where('id_payroll', $id) 
            ->first(); 

        if (!$payroll) { 
            return redirect()->to('/owner/payroll'); 
        } 

        return view('owner/payroll/detail', [ 
            'title'   => 'Detail Penggajian', 
            'payroll' => $payroll 
        ]); 
    } 

    public function edit($id) { 
        return view("owner/payroll/edit", [ 
            "title"   => "Edit Penggajian", 
            "payroll" => $this->payroll->find($id), 
            "users"   => $this->user->where("is_active", 1)->findAll() 
        ]); 
    } 

    public function update($id) { 
        // Tangkap periode dan pastikan formatnya YYYY-MM-01
        $periode_input = $this->request->getPost("periode"); 
        $periode_valid = !empty($periode_input) ? $periode_input . "-01" : null; 

        $this->payroll->update($id, [ 
            "periode"    => $periode_valid, 
            "gaji_pokok" => $this->request->getPost("gaji_pokok"), 
            "bonus"      => $this->request->getPost("bonus"), 
            "potongan"   => $this->request->getPost("potongan"), 
            "total_gaji" => $this->request->getPost("total_gaji") 
        ]); 

        return redirect()->to("/owner/payroll")->with("success", "Data berhasil diperbarui."); 
    } 

    public function pay($id) { 
        $this->payroll->update($id, [ 
            "status"  => "Paid", 
            "paid_at" => date("Y-m-d H:i:s") 
        ]); 
        return redirect()->back()->with("success", "Gaji berhasil dibayarkan."); 
    } 

    public function delete($id) { 
        $this->payroll->delete($id); 
        return redirect()->to("/owner/payroll")->with("success", "Data berhasil dihapus."); 
    } 

    public function history() { 
        $history = $this->payroll 
            ->select('payrolls.*, users.nama_lengkap') 
            ->join('users', 'users.id_user = payrolls.id_user', 'left') 
            ->orderBy("updated_at", "DESC") 
            ->findAll(); 

        return view("owner/payroll/history", [ 
            "title"   => "Riwayat Penggajian", 
            "history" => $history 
        ]); 
    } 

    public function recap() { 
        $total = $this->payroll->selectSum("total_gaji")->first(); 
        return view("owner/payroll/recap", [ 
            "title" => "Rekap Penggajian", 
            "total" => $total 
        ]); 
    } 
}