<?php

namespace App\Controllers;

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

// Models
use App\Models\CompanyDepartmentModel;
use App\Models\CompanyBranchModel;
use App\Models\JobTitleModel;
use App\Models\EmploymentStatusModel;
use App\Models\EmployeeModel;
use App\Models\PayrollGroupModel;

// Entities
use App\Entities\CompanyDepartment;
use App\Entities\CompanyBranch;
use App\Entities\JobTitle;
use App\Entities\EmploymentStatus;
use App\Entities\Employee;
use App\Entities\PayrollGroup;
use CodeIgniter\Shield\Entities\User;

// Class Library
use App\ClassLib\MiscLib;

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

    public function companyDepartment()
    {
        $companyDepartments = (new CompanyDepartmentModel())->findAll();

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

        if($companyDepartments == null)
            $data['tblCompanyDept'] = '<p>No departments found.</p>';
        else
        {
            $companyDeptHTMLTable->setHeading('Department ID', 'Department Code', 'Department Name', 'Action');

            foreach($companyDepartments as $department)
            {
                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Department Information"><i class="fas fa-eye "></i></a>';

                $companyDeptHTMLTable->addRow($department->dept_id, $department->department_code, $department->department_name, "$iconView");
            }

            $data['tblCompanyDept'] = $companyDeptHTMLTable->generate();

        }

        return view('hr/departmentview', $data);
    }

    public function addCompanyDepartment()
    {
        $companyDepartment = new CompanyDepartment();
        $companyDeptModel = new CompanyDepartmentModel();

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

        $companyDepartment->fill($rawData);
        $companyDepartment->parent_dept_id = 0;
        $companyDeptModel->save($companyDepartment);

        if($companyDeptModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add Department');
        else
            return redirect()->to('/hr/dept')->with('message', 'Department Added');
        
    }

    public function companyBranch()
    {
        $companyBranches = (new CompanyBranchModel())->findAll();

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

        if($companyBranches == null)
            $data['tblCompanyBranch'] = '<p>No branches found.</p>';
        else
        {
            $companyBranchHTMLTable->setHeading('Branch Code', 'Branch Name', 'Address', 'Contact Number', 'Email Address', 'Action');

            foreach($companyBranches as $branch)
            {
                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Branch Information"><i class="fas fa-eye "></i></a>';

                $companyBranchHTMLTable->addRow($branch->branch_code, $branch->branch_name, $branch->address, $branch->contact_number, $branch->email_address, "$iconView");
            }

            $data['tblCompanyBranch'] = $companyBranchHTMLTable->generate();
        }

        return view('hr/branchview', $data);
    }

    public function addCompanyBranch()
    {
        $companyBranch = new CompanyBranch();
        $companyBranchModel = new CompanyBranchModel();

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

        $companyBranch->fill($rawData);
        $companyBranchModel->save($companyBranch);

        if($companyBranchModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add branch');
        else
            return redirect()->to('/hr/branch')->with('message', 'Branch Added ');
    }

    public function jobTitle()
    {
        $jobTitles = (new JobTitleModel())->findAll();

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

        if($jobTitles == null)
            $data['tblJobTitle'] = '<p>No job titles found.</p>';
        else
        {
            $jobTitleHTMLTable->setHeading('Job Title ID', 'Job Title Name', 'Action');

            foreach($jobTitles as $jobTitle)
            {
                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Job Title Information"><i class="fas fa-eye "></i></a>';

                $jobTitleHTMLTable->addRow($jobTitle->job_title_id, $jobTitle->job_title_name, "$iconView");
            }

            $data['tblJobTitle'] = $jobTitleHTMLTable->generate();
        }

        return view('hr/jobtitleview', $data);
    }

    public function addJobTitle()
    {
        $jobTitle = new JobTitle();
        $jobTitleModel = new JobTitleModel();

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

        $jobTitle->fill($rawData);
        $jobTitleModel->save($jobTitle);

        if($jobTitleModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add job title');
        else
            return redirect()->to('/hr/jobtitle')->with('message', 'Job Title Added');
    }

    public function employmentStatus()
    {
        $employmentStatus = (new EmploymentStatusModel())->findAll();

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

        if($employmentStatus == null)
            $data['tblEmploymentStatus'] = '<p>No employment status found.</p>';
        else
        {
            $empStatusHTMLTable->setHeading('Status ID', 'Status Name', 'Action');

            foreach($employmentStatus as $empStatus)
            {    
                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Employment Status Information"><i class="fas fa-eye "></i></a>';

                $empStatusHTMLTable->addRow($empStatus->emp_status_id, $empStatus->status_name, "$iconView");
            }

            $data['tblEmploymentStatus'] = $empStatusHTMLTable->generate();
        }

        return view('hr/empstatusview', $data);
    }

    public function addEmploymentStatus()
    {
        $employmentStatus = new EmploymentStatus();
        $employmentStatusModel = new EmploymentStatusModel();

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

        $employmentStatus->fill($rawData);
        $employmentStatusModel->save($employmentStatus);

        if($employmentStatusModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add employment status');
        else
            return redirect()->to('/hr/empstatus')->with('message', 'Employment Status Added');
    }

    public function employee()
    {
        $employees = (new EmployeeModel())->findAll();
        $data['branches'] = (new CompanyBranchModel())->findAll();
        $data['departments'] = (new CompanyDepartmentModel())->findAll();
        $data['jobTitles'] = (new JobTitleModel())->findAll();
        $data['employmentStatus'] = (new EmploymentStatusModel())->findAll();
        $data['payGroups'] = (new PayrollGroupModel())->findAll();

        $employeeHTMLTable = new \CodeIgniter\View\Table();
        $employeeHTMLTable->setTemplate(MiscLib::adminLTEDataTable1Template("tblEmployee"));

        if($employees == null)
            $data['tblEmployee'] = '<p>No employees found.</p>';
        else    
        {
            $employeeHTMLTable->setHeading('Employee ID', 'First Name', 'Last Name', 'Action');
            
            foreach($employees as $employee)
            {
                $empHTMLData = 'data-employee_id="'.$employee->employee_id.
                             '" data-company_id="'.$employee->company_id.
                             '" data-branch_code="'.$employee->branch_code.
                             '" data-dept_id="'.$employee->dept_id.
                             '" data-job_title_id="'.$employee->job_title_id.
                             '" data-emp_status_id="'.$employee->emp_status_id.
                             '" data-pay_group_id="'.$employee->pay_group_id.
                             '" data-company_issued_id="'.$employee->company_issued_id.
                             '" data-dtr_emp_id="'.$employee->dtr_emp_id.
                             '" data-last_name="'.$employee->last_name.
                             '" data-first_name="'.$employee->first_name.
                             '" data-middle_name="'.$employee->middle_name.
                             '" data-suffix="'.$employee->suffix.
                             '" data-email_address="'.$employee->email_address.
                             '" data-contact_number="'.$employee->contact_number.'"';
                
                $iconView = '<a href="#" class="ml-3" data-toggle="tooltip" title="View Employee Information" '.$empHTMLData.'><i class="fas fa-eye "></i></a>';
                $iconEdit = '<a href="#" class="ml-3" data-toggle="tooltip" title="Edit Employee Information" '.$empHTMLData.' onclick="editEmployee(this)"><i class="fas fa-edit "></i></a>';
                $iconDelete = '<a href="#" class="ml-3" data-toggle="tooltip" title="Delete Employee Information"><i class="fas fa-trash "></i></a>';

                $employeeHTMLTable->addRow($employee->company_issued_id, $employee->first_name, $employee->last_name, "$iconView $iconEdit $iconDelete");
            }

            $data['tblEmployee'] = $employeeHTMLTable->generate();
        }

        return view('hr/employeeview', $data);
    }

    public function addEmployee()
    {
        $employee = new Employee();
        $employeeModel = new EmployeeModel();

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

        $employee->fill($rawData);
        $employeeModel->save($employee);

        if($employeeModel->getInsertID() == 0)
            return redirect()->back()->withInput()->with('error', 'Failed to add employee');
        else
            return redirect()->to('/hr/emp')->with('message', 'Employee Added');
    }

    public function editEmployee()
    {
        $employee = new Employee();
        $employeeModel = new EmployeeModel();

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

        $employee->fill($rawData);
        $employeeModel->save($employee);

        if($employeeModel->save($employee))
            return redirect()->to('/hr/emp')->with('message', 'Modified employee information');
        else
            return redirect()->back()->withInput()->with('error', 'Failed to add employee');
    }
}