<?php

class Job_Circular extends Admin_Controller
{

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

    public function jobs_posted()
    {
        $data['title'] = lang('job_posted_list');
        $data['page_title'] = lang('job_posted_list');
        $data['page_description'] = lang('manage') . ' ' . lang('jobs_posted');
        $data['header_actions'] = array(
            array(
                'label' => lang('new') . ' ' . lang('jobs_posted'),
                'icon' => 'plus',
                'href' => base_url('admin/job_circular/new_jobs_posted'),
                'attrs' => array(
                    'data-modal-url' => base_url('admin/job_circular/new_jobs_posted'),
                    'data-modal-title' => lang('new') . ' ' . lang('jobs_posted'),
                    'data-modal-size' => 'lg',
                ),
                'style' => 'primary',
            ),
            array(
                'label' => lang('jobs_application'),
                'icon' => 'briefcase-business',
                'href' => base_url('admin/job_circular/jobs_applications'),
                'style' => 'info',
            ),
        );
        $data['header_filters'] = array();
        $data['datatable'] = true;
        $data['subview'] = $this->load->view('admin/job_circular/jobs_posted', $data, TRUE);
        $this->load->view('admin/_layout_main', $data);
    }

    public function jobs_postedList()
    {
        if ($this->input->is_ajax_request()) {
            $this->load->model('datatables');
            $this->datatables->table = 'tbl_job_circular';
            $this->datatables->join_table = array('tbl_designations');
            $this->datatables->join_where = array('tbl_designations.designations_id=tbl_job_circular.designations_id');
            $this->datatables->column_order = array('job_title', 'tbl_designations.designations', 'employment_type', 'vacancy_no', 'experience', 'age', 'salary_range', 'posted_date', 'description', 'last_date', 'status');
            $this->datatables->column_search = array('job_title', 'tbl_designations.designations', 'employment_type', 'vacancy_no', 'experience', 'age', 'salary_range', 'posted_date', 'description', 'last_date', 'status');
            $this->datatables->order = array('job_circular_id' => 'desc');

            $fetch_data = $this->datatables->get_datatable_permission();

            $edited = can_action('103', 'edited');
            $deleted = can_action('103', 'deleted');
            $data = array();
            foreach ($fetch_data as $_key => $v_job_post) {
                if (!empty($v_job_post->designations_id)) {
                    $design_info = $this->db->where('designations_id', $v_job_post->designations_id)->get('tbl_designations')->row();
                    if (!empty($design_info)) {
                        $designation = $design_info->designations;
                    } else {
                        $designation = '-';
                    }
                } else {
                    $designation = '-';
                }
                $can_edit = $this->job_circular_model->can_action('tbl_job_circular', 'edit', array('job_circular_id' => $v_job_post->job_circular_id));
                $can_delete = $this->job_circular_model->can_action('tbl_job_circular', 'delete', array('job_circular_id' => $v_job_post->job_circular_id));


                $action = null;
                $sub_array = array();
                $title = null;
                $title .= btn_modal(
                    'admin/job_circular/view_circular_details/' . $v_job_post->job_circular_id,
                    $v_job_post->job_title,
                    lang('view_circular_details'),
                    'lg',
                    '',
                    array('class' => 'text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300')
                );
                $sub_array[] = $title;

                $sub_array[] = '<span class="tags">' . $designation . '</span>';
                $sub_array[] = $v_job_post->vacancy_no;
                $sub_array[] = display_date($v_job_post->last_date);

                $custom_form_table = custom_form_table(14, $v_job_post->job_circular_id);

                if (!empty($custom_form_table)) {
                    foreach ($custom_form_table as $c_label => $v_fields) {
                        $sub_array[] = $v_fields;
                    }
                }
                if ($v_job_post->status == 'unpublished') {
                    $status = '<span class="label label-danger">' . lang('unpublished') . '</span>';
                } else {
                    $status = '<span class="label label-success">' . lang('published') . '</span>';
                }
                $sub_array[] = $status;

                $action .= '<a href="' . base_url() . 'admin/job_circular/jobs_applications/' . $v_job_post->job_circular_id . '" title="' . lang('all_application_for') . '" class="inline-flex items-center justify-center rounded-lg border border-slate-200 bg-slate-50 p-2 text-slate-600 hover:bg-slate-100 h-7" data-toggle="tooltip"><i data-lucide="list" class="h-4 w-4"></i></a> ';
                if (!empty($can_edit) && !empty($edited)) {
                    if ($v_job_post->status == 'unpublished') {
                        $action .= btn_publish('admin/job_circular/change_status/published/' . $v_job_post->job_circular_id) . ' ';
                    } else {
                        $action .= btn_unpublish('admin/job_circular/change_status/unpublished/' . $v_job_post->job_circular_id) . ' ';
                    }
                    $action .= btn_modal(
                        'admin/job_circular/new_jobs_posted/' . $v_job_post->job_circular_id,
                        '',
                        lang('edit'),
                        'lg',
                        'pencil',
                        array('class' => 'inline-flex h-7 items-center justify-center rounded-lg border border-indigo-200 bg-indigo-50 p-2 text-indigo-600 hover:bg-indigo-100')
                    ) . ' ';
                }
                if (!empty($can_delete) && !empty($deleted)) {
                    $action .= ajax_anchor(base_url("admin/job_circular/delete_jobs_posted/" . $v_job_post->job_circular_id), '', array("class" => "inline-flex items-center justify-center rounded-lg border border-red-200 bg-red-50 p-2 text-red-600 hover:bg-red-100", "title" => lang('delete'), "data-fade-out-on-success" => "#table_" . $_key)) . ' ';
                }
                $action .= '<a target="_blank" href="' . base_url() . 'frontend/circular_details/' . $v_job_post->job_circular_id . '" title="' . lang('view_circular_details') . ' & ' . lang('apply_now') . '" class="inline-flex items-center justify-center rounded-lg border border-slate-200 bg-slate-50 p-2 text-slate-600 hover:bg-slate-100 h-7" data-placement="top" data-toggle="tooltip"><i data-lucide="send" class="h-4 w-4"></i></a> ';

                $sub_array[] = $action;
                $data[] = $sub_array;
            }

            render_table($data);
        } else {
            redirect('admin/dashboard');
        }
    }

    public function new_jobs_posted($id = null)
    {
        $data['title'] = lang('new') . ' ' . lang('jobs_posted');
        if (!empty($id)) {
            $can_edit = $this->job_circular_model->can_action('tbl_job_circular', 'edit', array('job_circular_id' => $id));
            if (!empty($can_edit)) {
                $data['job_posted'] = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();

                if (empty($data['job_posted'])) {
                    // messages for user
                    $type = "error";
                    $message = lang('not_found');
                    set_message($type, $message);
                    redirect('admin/job_circular/jobs_posted');
                }
            }
        }
        // get all department info and designation info
        $data['all_dept_info'] = $this->db->get('tbl_departments')->result();
        // get all department info and designation info
        foreach ($data['all_dept_info'] as $v_dept_info) {
            $data['all_department_info'][] = $this->job_circular_model->get_add_department_by_id($v_dept_info->departments_id);
        }

        $data['assign_user'] = $this->job_circular_model->allowed_user('103');

        $data['subview'] = $this->load->view('admin/job_circular/new_jobs_posted', $data, FALSE);
    }

    public function save_job_posted($id = NULL)
    {
        $created = can_action('103', 'created');
        $edited = can_action('103', 'edited');
        if (!empty($created) || !empty($edited) && !empty($id)) {
            $data = $this->job_circular_model->array_from_post(array('job_title', 'designations_id', 'employment_type', 'vacancy_no', 'experience', 'age', 'salary_range', 'posted_date', 'description', 'last_date', 'status'));
            $designation_info = $this->db->where('designations_id', $data['designations_id'])->get('tbl_designations')->row();
            $permission = $this->input->post('permission', true);
            // update root category
            $where = array('designations_id' => $data['designations_id']);
            // duplicate value check in DB
            if (!empty($id)) { // if id exist in db update data
                $job_circular_id = array('job_circular_id !=' => $id);
            } else { // if id is not exist then set id as null
                $job_circular_id = null;
            }
            if (!empty($permission)) {
                if ($permission == 'everyone') {
                    $assigned = 'all';
                } else {
                    $assigned_to = $this->job_circular_model->array_from_post(array('assigned_to'));
                    if (!empty($assigned_to['assigned_to'])) {
                        foreach ($assigned_to['assigned_to'] as $assign_user) {
                            $assigned[$assign_user] = $this->input->post('action_' . $assign_user, true);
                        }
                    }
                }
                if (!empty($assigned)) {
                    if ($assigned != 'all') {
                        $assigned = json_encode($assigned);
                    }
                } else {
                    $assigned = 'all';
                }
                $data['permission'] = $assigned;
            } else {
                set_message('error', sprintf(lang('field_is_required'), lang('assigned_to')));
                if (empty($_SERVER['HTTP_REFERER'])) {
                    redirect('admin/job_circular/jobs_posted');
                } else {
                    redirect($_SERVER['HTTP_REFERER']);
                }
            }

            $this->job_circular_model->_table_name = "tbl_job_circular"; // table name
            $this->job_circular_model->_primary_key = "job_circular_id"; // $id
            $return_id = $this->job_circular_model->save($data, $id);

            if (!empty($id)) {
                $activity = 'activity_update_job_posted';
                $msg = lang('job_posted_information_update');
            } else {
                $activity = 'activity_added_job_posted';
                $msg = lang('job_posted_information_saved');
                $id = $return_id;
            }
            save_custom_field(14, $id);

            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'job_circular',
                'module_field_id' => $id,
                'activity' => $activity,
                'icon' => 'fa-ticket',
                'value1' => $data['job_title'] . '[' . $designation_info->designations . ']',
            );

            // Update into tbl_project
            $this->job_circular_model->_table_name = "tbl_activities"; //table name
            $this->job_circular_model->_primary_key = "activities_id";
            $this->job_circular_model->save($activities);
            // messages for user
            $type = "success";
            $message = $msg;
            set_message($type, $message);
        }
		if ($this->input->is_ajax_request()) {
			echo json_encode(array("status" => $type, 'message' => $message));
			exit();
		}
        redirect('admin/job_circular/jobs_posted');
    }

    public function delete_jobs_posted($id)
    {
        $deleted = can_action('103', 'deleted');
        $type = "error";
        $message = lang('permission_denied');
        if (!empty($deleted)) {
            // check into tbl_job_allocations
            // if id exist delete this
            $check_existing_data = $this->job_circular_model->check_by(array('job_circular_id' => $id), 'tbl_job_appliactions');
            $job_posted_info = $this->job_circular_model->check_by(array('job_circular_id' => $id), 'tbl_job_circular');

            if (empty($job_posted_info)) {
                $type = "error";
                $message = lang('not_found');
            } else if (empty($check_existing_data)) {
                // save into activities
                $activities = array(
                    'user' => $this->session->userdata('user_id'),
                    'module' => 'job_circular',
                    'module_field_id' => $id,
                    'activity' => 'activity_delete_job_posted',
                    'icon' => 'fa-ticket',
                    'value1' => $job_posted_info->job_title,
                );
                // Update into tbl_project
                $this->job_circular_model->_table_name = "tbl_activities"; //table name
                $this->job_circular_model->_primary_key = "activities_id";
                $this->job_circular_model->save($activities);

                // delete into tbl_job_circular
                $this->job_circular_model->_table_name = "tbl_job_circular"; // table name
                $this->job_circular_model->_primary_key = "job_circular_id"; // $id
                $this->job_circular_model->delete($id);
                // messages for user
                $type = "success";
                $message = lang('job_posted_information_delete');
            } else {
                $type = "error";
                $message = lang('job_posted_information_used');
            }
            set_message($type, $message);
        }
        if ($this->input->is_ajax_request()) {
            echo json_encode(array("status" => $type, 'message' => $message));
            exit();
        }
        redirect('admin/job_circular/jobs_posted');
    }

    public
    function change_status($status, $id)
    {
        $edited = can_action('103', 'edited');
        if (!empty($edited)) {
            // if flag == 1 that means it is published to un pubslished
            // else unpublished to pubslished
            $this->job_circular_model->set_action(array('job_circular_id' => $id), array('status' => $status), 'tbl_job_circular');

            $type = "success";
            $message = lang('job_posted_status_change') . ' ' . $status . ' !';
            set_message($type, $message);
        }
        redirect('admin/job_circular/jobs_posted');
    }

    public
    function view_circular_details($id)
    {
        $data['title'] = lang('view_circular_details');
        $data['job_posted'] = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();
        $data['subview'] = $this->load->view('admin/job_circular/circular_details', $data, FALSE);
    }

    public
    function jobs_posted_pdf($id)
    {
        $data['job_posted'] = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();

        $this->load->helper('dompdf');
        $view_file = $this->load->view('admin/job_circular/jobs_posted_pdf', $data, true);
        pdf_create($view_file, slug_it(lang('jobs_posted') . '- ' . $data['job_posted']->job_title));
    }

    public
    function jobs_applications($id = null)
    {
        $data['title'] = lang('all_jobs_application');
        $job_posted = null;
        if (!empty($id)) {
            $job_posted = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();
        }
        $data['page_title'] = lang('all_jobs_application');
        $data['page_description'] = !empty($job_posted->job_title) ? $job_posted->job_title : (lang('manage') . ' ' . lang('jobs_application'));
        $data['header_actions'] = array(
            array(
                'label' => lang('jobs_posted'),
                'icon' => 'briefcase',
                'href' => base_url('admin/job_circular/jobs_posted'),
                'style' => 'info',
            ),
            array(
                'label' => lang('new') . ' ' . lang('jobs_posted'),
                'icon' => 'plus',
                'href' => base_url('admin/job_circular/new_jobs_posted'),
                'attrs' => array(
                    'data-modal-url' => base_url('admin/job_circular/new_jobs_posted'),
                    'data-modal-title' => lang('new') . ' ' . lang('jobs_posted'),
                    'data-modal-size' => 'lg',
                ),
                'style' => 'primary',
            ),
        );
        $data['header_filters'] = array();
        $is_department_head = is_department_head();
        if ($this->session->userdata('user_type') == 1 || !empty($is_department_head)) {
            $filter_items = array(
                array(
                    'label' => lang('all'),
                    'href' => base_url('admin/job_circular/jobs_applications'),
                    'active' => empty($id),
                ),
            );

            $job_circular_info = $this->job_circular_model->get_permission('tbl_job_circular');
            if (!empty($job_circular_info)) {
                foreach ($job_circular_info as $v_circular_info) {
                    $filter_items[] = array(
                        'label' => $v_circular_info->job_title,
                        'href' => base_url('admin/job_circular/jobs_applications/' . $v_circular_info->job_circular_id),
                        'active' => (string) $id === (string) $v_circular_info->job_circular_id,
                    );
                }
            }

            $data['header_filters'][] = array(
                'label' => lang('filter'),
                'icon' => 'filter',
                'selected_label' => !empty($job_posted->job_title) ? $job_posted->job_title : '',
                'active' => !empty($id),
                'items' => $filter_items,
            );
        }
        $data['datatable'] = true;
        // get salary template details
        $data['job_appliactions_id'] = $id;
        $data['subview'] = $this->load->view('admin/job_circular/jobs_applications', $data, TRUE);
        $this->load->view('admin/_layout_main', $data);
    }

    public function jobs_applicationsList($id = null)
    {
        if ($this->input->is_ajax_request()) {
            $this->load->model('datatables');
            $this->datatables->table = 'tbl_job_appliactions';
            $this->datatables->join_table = array('tbl_job_circular');
            $this->datatables->join_where = array('tbl_job_circular.job_circular_id=tbl_job_appliactions.job_circular_id');
            $this->datatables->column_order = array('tbl_job_circular.job_title', 'name', 'email', 'mobile', 'application_status', 'apply_date', 'interview_date');
            $this->datatables->column_search = array('tbl_job_circular.job_title', 'name', 'email', 'mobile', 'application_status', 'apply_date', 'interview_date');
            $this->datatables->order = array('tbl_job_appliactions.job_appliactions_id' => 'desc');
            $where = null;
            if (!empty($id)) {
                $where = array('tbl_job_appliactions.job_circular_id' => $id);
            }
            $fetch_data = make_datatables($where);
            $edited = can_action('102', 'edited');
            $deleted = can_action('102', 'deleted');
            $data = array();
            foreach ($fetch_data as $_key => $v_job_application) {

                $action = null;
                $sub_array = array();
                $title = null;
                $title .= btn_modal(
                    'admin/job_circular/view_circular_details/' . $v_job_application->job_circular_id,
                    $v_job_application->job_title,
                    lang('view_circular_details'),
                    'lg',
                    '',
                    array('class' => 'text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300')
                );
                $sub_array[] = $title;

                $sub_array[] = '<span class="tags">' . $v_job_application->name . '</span>';
                $sub_array[] = $v_job_application->email;
                $sub_array[] = $v_job_application->mobile;
                $sub_array[] = display_date($v_job_application->apply_date);

                if ($v_job_application->application_status == 0) {
                    $status = '<span class="label label-warning">' . lang('unread') . '</span>';
                } elseif ($v_job_application->application_status == 1) {
                    $status = '<span class="label label-success">' . lang('approved') . '</span>';
                } elseif ($v_job_application->application_status == 2) {
                    $status = '<span class="label label-primary">' . lang('primary_selected') . '</span>';
                } elseif ($v_job_application->application_status == 3) {
                    $status = '<span class="label label-purple">' . lang('call_for_interview') . '</span>';
                } else {
                    $status = '<span class="label label-danger">' . lang('rejected') . '</span>';
                }
                $sub_array[] = $status;

                $action .= '<a href="' . base_url() . 'admin/job_circular/download_resume/' . $v_job_application->job_appliactions_id . '" title="' . lang('download') . ' ' . lang('resume') . '" class="inline-flex items-center justify-center rounded-lg border border-slate-200 bg-slate-50 p-2 text-slate-600 hover:bg-slate-100 h-7" data-toggle="tooltip"><i data-lucide="download" class="h-4 w-4"></i></a> ';
                if (!empty($edited)) {
                    $action .= btn_modal(
                        'admin/job_circular/change_application_status/' . $v_job_application->job_appliactions_id,
                        lang('status'),
                        lang('change_status'),
                        'md',
                        'check-circle',
                        array('class' => 'inline-flex items-center gap-2 rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-xs font-semibold text-emerald-600 hover:bg-emerald-100')
                    ) . ' ';
                }
                $action .= btn_modal(
                    'admin/job_circular/jobs_application_details/' . $v_job_application->job_appliactions_id,
                    '',
                    lang('view'),
                    'lg',
                    'eye',
                    array('class' => 'inline-flex items-center justify-center rounded-lg border border-indigo-200 bg-indigo-50 p-2 text-indigo-600 hover:bg-indigo-100 h-7')
                ) . ' ';
                if (!empty($deleted)) {
                    $action .= ajax_anchor(base_url("admin/job_circular/delete_jobs_application/" . $v_job_application->job_appliactions_id), '', array("class" => "inline-flex items-center justify-center rounded-lg border border-red-200 bg-red-50 p-2 text-red-600 hover:bg-red-100", "title" => lang('delete'), "data-fade-out-on-success" => "#table_" . $_key)) . ' ';
                }
                $sub_array[] = $action;
                $data[] = $sub_array;
            }

            render_table($data, $where);
        } else {
            redirect('admin/dashboard');
        }
    }

    public
    function change_application_status($id)
    {
        $flag = $this->input->post('flag', true);
        if (empty($flag)) {
            $data['title'] = lang('change_status');
            // get salary template deatails
            $data['job_application_info'] = $this->db->where('job_appliactions_id', $id)->get('tbl_job_appliactions')->row();

            $data['subview'] = $this->load->view('admin/job_circular/application_status', $data, FALSE);
        } else {
            // if flag == 1 that means it is published to un pubslished
            // else unpublished to pubslished
            $status = $this->input->post('status', true);
            $where = array('application_status' => $status);
            if ($status == 3) {
                $send_email = $this->input->post('send_email', true);
                $interview_date = $this->input->post('interview_date', true);
                $interview_note = $this->input->post('interview_note', true);
                if (!empty($send_email)) {
                    $where = array('application_status' => $status, 'send_email' => $send_email, 'interview_note' => $interview_note, 'interview_date' => $interview_date);
                    $this->call_for_interview($id, $where);
                }
            }
            $this->job_circular_model->set_action(array('job_appliactions_id' => $id), $where, 'tbl_job_appliactions');
            // messages for user
            $type = "success";
            $message = lang('job_posted_status_change');
            set_message($type, $message);

            if ($this->input->is_ajax_request()) {
                $this->output
                    ->set_content_type('application/json')
                    ->set_output(json_encode(array(
                        'status' => $type,
                        'message' => $message,
                    )));
                return;
            }

            redirect('admin/job_circular/jobs_applications');
        }
    }

    function call_for_interview($id, $data)
    {
        $job_application_info = $this->db->where('job_appliactions_id', $id)->get('tbl_job_appliactions')->row();
        if (!empty($job_application_info)) {
            $job_circular_info = $this->db->where('job_circular_id', $job_application_info->job_circular_id)->get('tbl_job_circular')->row();
            $designation = '-';
            if (!empty($job_circular_info->designations_id)) {
                $design_info = $this->db->where('designations_id', $job_circular_info->designations_id)->get('tbl_designations')->row();
                if (!empty($design_info)) {
                    $designation = $design_info->designations;
                }
            }

            $email_template = email_templates(array('email_group' => 'call_for_interview'));

            $message = $email_template->template_body;
            $subject = $email_template->subject;
            $title = str_replace("{NAME}", $job_application_info->name, $message);
            $job_title = str_replace("{JOB_TITLE}", $job_circular_info->job_title, $title);
            $designation = str_replace("{DESIGNATION}", $designation, $job_title);
            $date = str_replace("{DATE}", !empty($data['interview_date']) ? display_date($data['interview_date']) : '-', $designation);
            $interview_note = str_replace("{NOTE}", $data['interview_note'], $designation);
            $Link = str_replace("{LINK}", base_url() . 'frontend/circular_details/' . $job_application_info->job_circular_id, $interview_note);
            $message = str_replace("{SITE_NAME}", config_item('company_name'), $Link);
            $data['message'] = $message;
            $message = $this->load->view('email_template', $data, TRUE);

            $params['subject'] = $subject;
            $params['message'] = $message;
            $params['resourceed_file'] = '';

            $params['recipient'] = $job_application_info->email;
            $this->job_circular_model->send_email($params);
        }
        return true;
    }

    public
    function jobs_application_details($id)
    {
        $data['title'] = lang('jobs_application_details');
        // get salary template deatails
        $data['job_application_info'] = $this->job_circular_model->get_job_application_info($id);
        $data['subview'] = $this->load->view('admin/job_circular/jobs_applications_details', $data, FALSE);
    }

    public
    function view_jobs_application($id)
    {
        $data['title'] = lang('jobs_application_details');
        // get salary template deatails
        $data['job_application_info'] = $this->job_circular_model->get_job_application_info($id);
        $data['page_title'] = !empty($data['job_application_info']->name) ? $data['job_application_info']->name : lang('jobs_application_details');
        $job_title = !empty($data['job_application_info']->job_title) ? $data['job_application_info']->job_title : lang('job_title');
        $apply_date = !empty($data['job_application_info']->apply_date) ? display_date($data['job_application_info']->apply_date) : '-';
        $data['page_description'] = $job_title . ' . ' . lang('apply_date') . ': ' . $apply_date;
        $data['header_badge'] = array(
            'label' => !empty($data['job_application_info']->application_status) ? lang('status') : ('#' . $id),
            'tone' => 'primary',
        );
        $data['header_actions'] = array(
            array(
                'label' => lang('download') . ' ' . lang('resume'),
                'icon' => 'download',
                'style' => 'primary',
                'href' => base_url('admin/job_circular/download_resume/' . $data['job_application_info']->job_appliactions_id),
            ),
        );
        $data['header_filters'] = array();
        $data['subview'] = $this->load->view('admin/job_circular/jobs_applications_details', $data, TRUE);
        $this->load->view('admin/_layout_main', $data);
    }

    public
    function delete_jobs_application($id)
    {
        $jobs_application = $this->job_circular_model->get_job_application_info($id);
        // save into activities
        $activities = array(
            'user' => $this->session->userdata('user_id'),
            'module' => 'job_circular',
            'module_field_id' => $id,
            'activity' => 'activity_delete_job_application',
            'icon' => 'fa-ticket',
            'value1' => $jobs_application->name,
        );
        // Update into tbl_project
        $this->job_circular_model->_table_name = "tbl_activities"; //table name
        $this->job_circular_model->_primary_key = "activities_id";
        $this->job_circular_model->save($activities);

        $this->job_circular_model->_table_name = "tbl_job_appliactions"; // table name
        $this->job_circular_model->_primary_key = "job_appliactions_id"; // $id
        $this->job_circular_model->delete($id);

        // messages for user
        $type = "success";
        $message = lang('deleted_job_application');
        set_message($type, $message);
        redirect('admin/job_circular/jobs_applications');
    }

    public
    function download_resume($id)
    {
        $job_application_info = $this->job_circular_model->get_job_application_info($id);

        if (empty($job_application_info) || empty($job_application_info->resume)) {
            set_message('error', lang('not_found'));
            redirect('admin/job_circular/jobs_applications');
        }

        $resume_path = $job_application_info->resume;
        $file_path = is_file($resume_path) ? $resume_path : FCPATH . ltrim($resume_path, '/\\');
        if (!is_file($file_path)) {
            log_message('error', 'Resume file not found for job application #' . $id . ': ' . $resume_path);
            set_message('error', lang('resume') . ' ' . lang('not_found'));
            redirect('admin/job_circular/jobs_applications');
        }

        $path = file_get_contents($file_path); // Read the file's contents
        $this->load->helper('download');
        force_download($job_application_info->name . ' - ' . basename($resume_path), $path);
    }
}
