<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Utilities extends Admin_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('utilities_model');
    }

    public function overtime()
    {
		
        $data['title'] = lang('overtime_details');
        $data['page_title'] = lang('overtime_details');
        $data['page_description'] = lang('manage') . ' ' . lang('overtime_details');
        $data['header_actions'] = array(
            array(
                'label' => lang('add_overtime'),
                'icon' => 'plus',
                'href' => base_url('admin/utilities/add_overtime'),
                'attrs' => array(
                    'data-modal-url' => base_url('admin/utilities/add_overtime'),
                    'data-modal-title' => lang('add_overtime'),
                    'data-modal-size' => 'lg',
                ),
                'style' => 'primary',
            ),
        );
        $data['header_filters'] = array();
        // active check with current month
        $data['current_month'] = date('m');
        if ($this->input->post('year', TRUE)) { // if input year
            $data['year'] = $this->input->post('year', TRUE);
        } else { // else current year
            $data['year'] = date('Y'); // get current year
        }
        // get all expense list by year and month
        $data['all_overtime_info'] = $this->get_overtime_info($data['year']);

        $data['subview'] = $this->load->view('admin/utilities/overtime/overtime_list', $data, TRUE);
        $this->load->view('admin/_layout_main', $data);
    }

    public function add_overtime($id = null)
    {
        // active check with current month
        $data['current_month'] = date('m');
        if ($this->input->post('year', TRUE)) { // if input year
            $data['year'] = $this->input->post('year', TRUE);
        } else { // else current year
            $data['year'] = date('Y'); // get current year
        }

        if (!empty($id)) {
            $data['overtime_info'] = $this->utilities_model->get_overtime_info_by_emp_id($id);
            $data['selected_employee_label'] = $this->get_employee_lookup_label($data['overtime_info']->user_id ?? null);
        }
        $data['modal_subview'] = $this->load->view('admin/utilities/overtime/new_overtime', $data, FALSE);
    }

    private function get_employee_lookup_label($user_id)
    {
        if (empty($user_id)) {
            return '';
        }

        $this->db->select('tbl_account_details.fullname, tbl_designations.designations');
        $this->db->from('tbl_account_details');
        $this->db->join('tbl_designations', 'tbl_designations.designations_id = tbl_account_details.designations_id', 'left');
        $this->db->where('tbl_account_details.user_id', $user_id);
        $employee = $this->db->get()->row();

        if (empty($employee)) {
            return '';
        }

        return $employee->fullname . (!empty($employee->designations) ? ' (' . $employee->designations . ')' : '');
    }

    public function get_overtime_info($year, $month = NULL)
    {// this function is to create get monthy recap report
        if (!empty($month)) {
            $date = new DateTime($year . '-' . $month . '-01');
            $start_date = $date->modify('first day of this month')->format('Y-m-d');
            $end_date = $date->modify('last day of this month')->format('Y-m-d');

            $get_expense_list = $this->utilities_model->get_overtime_info_by_date($start_date, $end_date); // get all report by start date and in date
        } else {
            for ($i = 1; $i <= 12; $i++) { // query for months
                $date = new DateTime($year . '-' . $i . '-01');
                $start_date = $date->modify('first day of this month')->format('Y-m-d');
                $end_date = $date->modify('last day of this month')->format('Y-m-d');
                $get_expense_list[$i] = $this->utilities_model->get_overtime_info_by_date($start_date, $end_date); // get all report by start date and in date
            }
        }
        return $get_expense_list; // return the result
    }

    public function overtime_report_pdf($year, $month)
    {
        $data['overtime_info'] = $this->get_overtime_info($year, $month);
        $month_name = date('F', safe_strtotime($year . '-' . $month)); // get full name of month by date query
        $data['monthyaer'] = $month_name . '  ' . $year;

        $this->load->helper('dompdf');
        $viewfile = $this->load->view('admin/utilities/overtime/overtime_report_pdf', $data, TRUE);
        pdf_create($viewfile, slug_it('Overtime Report  - ' . $data['monthyaer']));
    }

    public function save_overtime($id = NULL)
    {
        $is_ajax = $this->input->is_ajax_request();
        $data = $this->utilities_model->array_from_post(array('user_id', 'overtime_date', 'overtime_hours', 'notes'));
        if (empty($data['user_id'])) {
            $data['user_id'] = $this->session->userdata('user_id');
        }
        // check existing data by employee id and date
        $where = array('user_id' => $data['user_id'], 'overtime_date' => $data['overtime_date']);
        // duplicate value check in DB
        if (!empty($id)) { // if id exist in db update data
            $overtime_id = array('overtime_id !=' => $id);
        } else { // if id is not exist then set id as null
            $overtime_id = null;
        }
        $check_existing_data = $this->utilities_model->check_update('tbl_overtime', $where, $overtime_id);
        if (!empty($check_existing_data)) {
            $type = "error";
            $message = lang('overtime_already_exist');
            if ($is_ajax) {
                echo json_encode(array(
                    'status' => $type,
                    'message' => $message,
                ));
                exit;
            }
        } else {
            if ($this->session->userdata('user_type') == 1) {
                $data['status'] = 'approved';
            } elseif (empty($id)) {
                $data['status'] = 'pending';

            }

            $this->utilities_model->_table_name = "tbl_overtime"; //table name
            $this->utilities_model->_primary_key = "overtime_id";
            $id = $this->utilities_model->save($data, $id);

            $overtime_info = $this->db->where('overtime_id', $id)->get('tbl_overtime')->row();
            $profile_info = $this->utilities_model->check_by(array('user_id' => $overtime_info->user_id), 'tbl_account_details');
            if (!empty($profile_info->designations_id)) {
                // get departments head user id
                $designation_info = $this->utilities_model->check_by(array('designations_id' => $profile_info->designations_id), 'tbl_designations');
                // get departments head by departments id
                $dept_head = $this->utilities_model->check_by(array('departments_id' => $designation_info->departments_id), 'tbl_departments');

                if (!empty($dept_head->department_head_id)) {
                    $overtime_email = config_item('overtime_email');
                    if (!empty($overtime_email) && $overtime_email == 1) {
                        $email_template = email_templates(array('email_group' => 'overtime_request_email'), $overtime_info->user_id, true);
                        $user_info = $this->utilities_model->check_by(array('user_id' => $dept_head->department_head_id), 'tbl_users');
                        $message = $email_template->template_body;
                        $subject = $email_template->subject;
                        $username = str_replace("{NAME}", $profile_info->fullname, $message);
                        $message = str_replace("{SITE_NAME}", config_item('company_name'), $username);
                        $odata['message'] = $message;
                        $message = $this->load->view('email_template', $odata, TRUE);

                        $params['subject'] = $subject;
                        $params['message'] = $message;
                        $params['resourceed_file'] = '';
                        $params['recipient'] = $user_info->email;
                        $this->utilities_model->send_email($params);
                    }

                    $notifyUser = array($dept_head->department_head_id);
                    if (!empty($notifyUser)) {
                        foreach ($notifyUser as $v_user) {
                            add_notification(array(
                                'to_user_id' => $v_user,
                                'description' => 'not_overtime_request',
                                'icon' => 'clock-o',
                                'link' => 'admin/utilities/view_overtime/' . $overtime_info->overtime_id,
                                'value' => lang('by') . ' ' . $this->session->userdata('name'),
                            ));
                        }
                    }
                    if (!empty($notifyUser)) {
                        show_notification($notifyUser);
                    }
                }
            }
            $type = "success";
            $message = lang('overtime_saved');

            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'overtime',
                'module_field_id' => $id,
                'activity' => lang('activity_new_overtime'),
                'icon' => 'clock-o',
                'link' => 'admin/utilities/view_overtime/' . $overtime_info->overtime_id,
                'value1' => $data['overtime_date'],
            );
            // Update into tbl_project
            $this->utilities_model->_table_name = "tbl_activities"; //table name
            $this->utilities_model->_primary_key = "activities_id";
            $this->utilities_model->save($activities);
        }
        if ($is_ajax) {
            echo json_encode(array(
                'status' => $type,
                'message' => $message,
                'url' => 'admin/utilities/overtime',
            ));
            exit;
        }
        set_message($type, $message);
        redirect('admin/utilities/overtime'); //redirect page
    }

    public function change_overtime_status($status = null, $id = null)

    {
        $posted_status = $this->input->post('status', true);
        $posted_id = $this->input->post('overtime_id', true);
        if (!empty($posted_status)) {
            $status = $posted_status;
        }
        if (!empty($posted_id)) {
            $id = $posted_id;
        }

        $allowed_statuses = array('approved', 'rejected');
        $fallback_url = 'admin/utilities/overtime';

        if (empty($id) || !in_array($status, $allowed_statuses, true)) {
            set_message('error', lang('unexpected_error'));
            redirect($fallback_url);
        }

        $overtime_info = $this->db
            ->select('overtime_id,overtime_date,status,user_id')
            ->where('overtime_id', $id)
            ->get('tbl_overtime')
            ->row();

        if (empty($overtime_info)) {
            set_message('error', lang('unexpected_error'));
            redirect($fallback_url);
        }

        if ($overtime_info->status !== $status) {
            $data = array('status' => $status);

            if ($status == 'approved') {
                // $this->send_overtime_status_by_email($overtime_info, true);
            } elseif ($status == 'rejected') {
                // $this->send_overtime_status_by_email($overtime_info);
            }

            $this->utilities_model->_table_name = "tbl_overtime"; //table name
            $this->utilities_model->_primary_key = "overtime_id";
            $this->utilities_model->save($data, $id);

            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'overtime',
                'module_field_id' => $id,
                'activity' => lang('activity_new_overtime'),
                'icon' => 'clock-o',
                'link' => 'admin/utilities/view_overtime/' . $overtime_info->overtime_id,
                'value1' => $overtime_info->overtime_date,
            );
            $this->utilities_model->_table_name = "tbl_activities"; //table name
            $this->utilities_model->_primary_key = "activities_id";
            $this->utilities_model->save($activities);
        }

        set_message('success', lang('overtime_change_status'));
        redirect($fallback_url);
    }

    function send_overtime_status_by_email($appl_info, $approve = null)
    {
        $overtime_email = config_item('overtime_email');
        $user_info = $this->utilities_model->check_by(array('user_id' => $appl_info->user_id), 'tbl_users');
        if (!empty($overtime_email) && $overtime_email == 1) {
            if (!empty($approve)) {
                $email_template = email_templates(array('email_group' => 'overtime_approved_email'), $appl_info->user_id, true);
                $description = 'not_overtime_request_approve';
            } else {
                $email_template = email_templates(array('email_group' => 'overtime_reject_email'), $appl_info->user_id, true);
                $description = 'not_overtime_request_reject';
            }
            $message = $email_template->template_body;
            $subject = $email_template->subject;
            $Date = str_replace("{DATE}", $appl_info->overtime_date, $message);
            $hours = str_replace("{HOUR}", $appl_info->overtime_hours, $Date);
            $message = str_replace("{SITE_NAME}", config_item('company_name'), $hours);
            $data['message'] = $message;
            $message = $this->load->view('email_template', $data, TRUE);

            $params['subject'] = $subject;
            $params['message'] = $message;
            $params['resourceed_file'] = '';
            $params['recipient'] = $user_info->email;

            $this->utilities_model->send_email($params);
        } else {
            return true;
        }
        $notifyUser = array($user_info->user_id);
        if (!empty($notifyUser)) {
            foreach ($notifyUser as $v_user) {
                add_notification(array(
                    'to_user_id' => $v_user,
                    'description' => $description,
                    'icon' => 'clock-o',
                    'link' => 'admin/utilities/view_overtime/' . $appl_info->overtime_id,
                    'value' => lang('by') . ' ' . $this->session->userdata('name'),
                ));
            }
        }
        if (!empty($notifyUser)) {
            show_notification($notifyUser);
        }
    }

    public function delete_overtime($id)
    {
        $this->utilities_model->_table_name = "tbl_overtime"; //table name
        $this->utilities_model->_primary_key = "overtime_id";
        $this->utilities_model->delete($id);
        // save into activities
        $activities = array(
            'user' => $this->session->userdata('user_id'),
            'module' => 'overtime',
            'module_field_id' => $id,
            'activity' => lang('activity_delete_overtime'),
            'icon' => 'clock-o',
            'value1' => $this->db->where('overtime_id', $id)->get('tbl_overtime')->row()->overtime_date,
        );
        // Update into tbl_project
        $this->utilities_model->_table_name = "tbl_activities"; //table name
        $this->utilities_model->_primary_key = "activities_id";
        $this->utilities_model->save($activities);

        $type = "success";
        $message = lang('overtime_deleted');
        set_message($type, $message);
        redirect('admin/utilities/overtime'); //redirect page
    }

    public function view_overtime($id)
    {
        $data['title'] = lang('overtime') . ' ' . lang('details');
        $data['overtime_info'] = $this->db->where('overtime_id', $id)->get('tbl_overtime')->row();
        $data['subview'] = $this->load->view('admin/utilities/overtime/view_overtime', $data, true);
        $this->load->view('admin/_layout_main', $data);
    }

}
