<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;

// Models
use App\Models\PayrollGroupModel;
use App\Models\IncomeDeductionModel;
use App\Models\PayrollTypeModel;
use App\Models\EmployeePayrollInfoModel;
use App\Models\EmployeeModel;


// Entities
use App\Entities\PayrollGroup;
use App\Entities\IncomeDeduction;
use App\Entities\PayrollType;
use App\Entities\EmployeePayrollInfo;
use App\Entities\Employee;

// Class Library
use App\ClassLib\MiscLib;

class PayrollController extends BaseController
{
    public function index()
    {
        return redirect()->to(base_url('/login'));
    }


    public function payrollGroup()
    {
        $payGroups = (new PayrollGroupModel())->findAll();

        $payGroupHTMLTable = new \CodeIgniter\View\Table();
        $payGroupHTMLTable->setTemplate(MiscLib::adminLTETableTemplate());

        if($payGroups == null)
            $data['tblPayGroup'] = '<p>No groups found.</p>';
        else
        {
            foreach($payGroups as $group)
            {
                $payGroupHTMLTable->setHeading('ID', 'Group Name', 'Action');
                
                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Group Information"><i class="fas fa-eye "></i></a>';

                $payGroupHTMLTable->addRow($group->pay_group_id, $group->pay_group_code, $group->pay_group_name, "$iconView");
            }

            $data['tblPayGroup'] = $payGroupHTMLTable->generate();
        }

        return view('payroll/paygroupview', $data);
    }

    public function addPayrollGroup()
    {
        $payrollGroup = new PayrollGroup();
        $payrollGroupModel = new PayrollGroupModel();

        $rawData = $this->request->getPost();

        $payrollGroup->fill($rawData);
        $payrollGroupModel->save($payrollGroup);

        if($payrollGroupModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add payroll group');
        else
            return redirect()->to('/payroll/paygroup')->with('message', 'Payroll Group Added');
    }

    public function incomeDeduction()
    {
        $incomeDeductions = (new IncomeDeductionModel())->findAll();

        $inDedHTMLTable = new \CodeIgniter\View\Table();
        $inDedHTMLTable->setTemplate(MiscLib::adminLTETableTemplate());

        if($incomeDeductions == null)
            $data['tblIncomeDeduction'] = '<p>No income and deduction found.</p>';
        else
        {
            foreach($incomeDeductions as $incomeDeduction)
            {
                $inDedHTMLTable->setHeading('ID', 'Payslip Display', 'COA Code', 'Deduction Name', 'Income', 'Taxable', 'Include in Gross', 'Action');

                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Information"><i class="fas fa-eye "></i></a>';

                $inDedHTMLTable->addRow($incomeDeduction->inded_id, $incomeDeduction->payslip_display, $incomeDeduction->coa_code, $incomeDeduction->income_deduction_name, ($incomeDeduction->is_income) ? 'Yes' : 'No', ($incomeDeduction->is_taxable) ? 'Yes' : 'No', ($incomeDeduction->include_in_gross) ? 'Yes' : 'No', $iconView);
            }

            $data['tblIncomeDeduction'] = $inDedHTMLTable->generate();
        }

        return view('payroll/incomedeductionview', $data);
    }

    public function addIncomeDeduction()
    {
        $incomeDeduction = new IncomeDeduction();
        $incomeDeductionModel = new IncomeDeductionModel();

        $rawData = $this->request->getPost();

        // Handle checkbox inputs
        $rawData['is_income'] = isset($rawData['is_income']) ? 1 : 0; // Set to 1 if checked, 0 if not
        $rawData['is_taxable'] = isset($rawData['is_taxable']) ? 1 : 0; // Same for taxable
        $rawData['include_in_gross'] = isset($rawData['include_in_gross']) ? 1 : 0; // Same for include in gross

        $incomeDeduction->fill($rawData);
        $incomeDeductionModel->save($incomeDeduction);

        if($incomeDeductionModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add income or deduction');
        else
            return redirect()->to('/payroll/inded')->with('message', 'Income or Deduction Added');
    }

    public function payrollType()
    {
        $payrollTypes = (new PayrollTypeModel())->findAll();

        $payTypeHTMLTable = new \CodeIgniter\View\Table();
        $payTypeHTMLTable->setTemplate(MiscLib::adminLTETableTemplate());

        if($payrollTypes == null)
            $data['tblPayrollType'] = '<p>No payroll type found.</p>';
        else
        {
            foreach($payrollTypes as $payrollType)
            {
                $payTypeHTMLTable->setHeading('ID', 'Code', 'Name', 'Monthly', 'Semi-Monthly', 'Daily', 'Hourly', 'Action');

                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Information"><i class="fas fa-eye "></i></a>';

                $payTypeHTMLTable->addRow($payrollType->paytype_id, $payrollType->paytype_code, $payrollType->paytype_name, ($payrollType->is_monthly) ? 'Yes' : 'No', ($payrollType->is_semi_monthly) ? 'Yes' : 'No', ($payrollType->is_daily) ? 'Yes' : 'No', ($payrollType->is_hourly) ? 'Yes' : 'No', $iconView);
            }

            $data['tblPayrollType'] = $payTypeHTMLTable->generate();
        }

        return view('payroll/paytypeview', $data);
    }

    public function addPayrollType()
    {
        $payrollType = new PayrollType();
        $payrollTypeModel = new PayrollTypeModel();

        $rawData = $this->request->getPost();

        // Initialize all payroll type fields to 0
        $rawData['is_monthly'] = 0;
        $rawData['is_semi_monthly'] = 0;
        $rawData['is_daily'] = 0;
        $rawData['is_hourly'] = 0;

        // Handle radio button input
        if (isset($rawData['paytype_sched'])) {
            switch ($rawData['paytype_sched']) {
                case 'monthly':
                    $rawData['is_monthly'] = 1;
                    break;
                case 'semi_monthly':
                    $rawData['is_semi_monthly'] = 1;
                    break;
                case 'daily':
                    $rawData['is_daily'] = 1;
                    break;
                case 'hourly':
                    $rawData['is_hourly'] = 1;
                    break;
            }
        }

        $payrollType->fill($rawData);
        $payrollTypeModel->save($payrollType);

        if($payrollTypeModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add payroll type');
        else
            return redirect()->to('/payroll/paytype')->with('message', 'Payroll Type Added');
    }

    public function employeePayrollInfo()
    {
        $empPayInfoModel = new EmployeePayrollInfoModel();
        $empPayInfos = $empPayInfoModel->getAllEmpPayInfoJoinedEmpPayType();

        $data['employees'] = (new EmployeeModel())->findAll();
        $data['paytypes'] = (new PayrollTypeModel())->findAll();

        $empPayInfoHTMLTable = new \CodeIgniter\View\Table();
        $empPayInfoHTMLTable->setTemplate(MiscLib::adminLTETableTemplate());

        if($empPayInfos == null)
            $data['tblEmpPayInfo'] = '<p>No employee payroll type found.</p>';
        else
        {
            foreach($empPayInfos as $empPayInfo)
            {
                $empPayInfoHTMLTable->setHeading('ID', 'Payroll Type', 'Employee ID', 'Employee Name', 'Monthly', 'Semi-Monthly', 'Daily', 'Hourly', 'Action');

                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Information"><i class="fas fa-eye "></i></a>';

                $empPayInfoHTMLTable->addRow($empPayInfo->emppay_id, $empPayInfo->paytype_name, $empPayInfo->company_issued_id, $empPayInfo->last_name . ', ' . $empPayInfo->first_name, $empPayInfo->basic_monthly_pay, $empPayInfo->basic_semi_monthly_pay, $empPayInfo->basic_daily_pay, $empPayInfo->basic_hourly_pay, $iconView);
            }

            $data['tblEmpPayInfo'] = $empPayInfoHTMLTable->generate();
        }

        return view('payroll/empinfoview', $data);
    }

    public function addEmployeePayrollInfo()
    {
        $empPayInfo = new EmployeePayrollInfo();
        $empPayInfoModel = new EmployeePayrollInfoModel();

        $rawData = $this->request->getPost();

        $rawData['is_ATM'] = isset($rawData['is_ATM']) ? 1 : 0;
        $rawData['has_cola'] = isset($rawData['has_cola']) ? 1 : 0;
        $rawData['has_philhealth'] = isset($rawData['has_philhealth']) ? 1 : 0;
        $rawData['has_hdmf'] = isset($rawData['has_hdmf']) ? 1 : 0;
        $rawData['has_sss'] = isset($rawData['has_sss']) ? 1 : 0;
        $rawData['has_gsis'] = isset($rawData['has_gsis']) ? 1 : 0;

        $empPayInfo->fill($rawData);
        $empPayInfoModel->save($empPayInfo);

        if($empPayInfoModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add employee payroll type');
        else
            return redirect()->to('/payroll/emppayinfo')->with('message', 'Employee Payroll Type Added');
    }
}
