<?php

namespace App\Controllers;

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

// Models
use App\Models\PayrollGroupModel;


// Entities
use App\Entities\PayrollGroup;

// 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('Group 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 employeePayrollGroupAssignment()
    {

        return view('payroll/empaygrpview');
    }

    public function addEmployeePayrollGroupAssignment()
    {

    }
}
