<?php

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

class Project_costing extends AdminController
{
    public function __construct()
    {
        parent::__construct();

        if (!has_permission('project_costing', '', 'view')) {
            access_denied('project_costing');
        }

        $this->load->model('project_costing/project_costing_model');
    }

    public function index()
    {
        $data['title'] = 'My Submissions';
        $data['projects'] = $this->project_costing_model->get_my_projects(get_staff_user_id());

        $this->load->view('project_costing/user/manage', $data);
    }

    public function finance_list()
    {
        $data['title'] = 'Finance Review';
        $data['projects'] = $this->project_costing_model->get_finance_projects(get_staff_user_id());

        $this->load->view('project_costing/user/manage', $data);
    }

    public function hod_list()
    {
        $data['title'] = 'HOD Review';
        $data['projects'] = $this->project_costing_model->get_hod_projects(get_staff_user_id());

        $this->load->view('project_costing/user/manage', $data);
    }

    public function create()
    {
        if (!has_permission('project_costing', '', 'create')) {
            access_denied('project_costing');
        }

        if ($this->input->post()) {
            $projectId = $this->project_costing_model->create_project_submission($this->input->post());

            if ($projectId) {
                set_alert('success', 'Project costing submitted successfully.');
                redirect(admin_url('project_costing/project_costing/view/' . $projectId));
            }

            set_alert('danger', 'Failed to save project costing.');
            redirect(admin_url('project_costing/project_costing/create'));
        }

        $data['title'] = 'New Project';
        $data['client_categories'] = $this->project_costing_model->get_client_categories_active();
        $data['edit_project'] = null;
        $data['edit_items'] = null;

        $this->load->view('project_costing/user/form', $data);
    }

    public function edit($id)
    {
        $project = $this->project_costing_model->get_project($id);

        if (!$project) {
            show_404();
        }

        $isAdmin = is_admin();
        $isOwner = $project['submitted_by'] == get_staff_user_id();

        if (!$isAdmin && !$isOwner) {
            access_denied('project_costing');
        }

        if ($this->input->post()) {
            $updated = $this->project_costing_model->update_project_submission($id, $this->input->post());

            if ($updated) {
                set_alert('success', 'Project costing updated successfully.');
                redirect(admin_url('project_costing/project_costing/view/' . $id));
            }

            set_alert('danger', 'Failed to update project costing.');
            redirect(admin_url('project_costing/project_costing/edit/' . $id));
        }

        $data['title'] = 'Edit Project';
        $data['client_categories'] = $this->project_costing_model->get_client_categories_active();
        $data['edit_project'] = $project;
        $data['edit_items'] = $this->project_costing_model->get_project_items_grouped($id);

        $this->load->view('project_costing/user/form', $data);
    }

    public function view($id)
    {
        $project = $this->project_costing_model->get_project($id);

        if (!$project) {
            show_404();
        }

        $isAdmin = is_admin();
        $isOwner = $project['submitted_by'] == get_staff_user_id();
        $isFinance = $project['finance_reviewer_staff_id'] == get_staff_user_id();
        $isHod = $project['hod_approver_staff_id'] == get_staff_user_id();

        if (!$isAdmin && !$isOwner && !$isFinance && !$isHod) {
            access_denied('project_costing');
        }

        $data['title'] = 'Project Costing View';
        $data['project'] = $project;
        $data['items'] = $this->project_costing_model->get_project_items_grouped($id);
        $data['notifications'] = $this->project_costing_model->get_project_notifications($id);

        $this->load->view('project_costing/user/view', $data);
    }
    public function resend_notification($notificationId)
{
    $this->load->model('project_costing/project_costing_model');

    $done = $this->project_costing_model->resend_notification($notificationId);

    if ($done) {
        set_alert('success', 'Notification resent successfully.');
    } else {
        set_alert('danger', 'Unable to resend notification.');
    }

    $projectId = $this->input->get('project_id');
    if ($projectId) {
        redirect(admin_url('project_costing/project_costing/view/' . $projectId));
    }

    redirect(admin_url('project_costing/project_costing'));
}

    public function finance_decide($id)
    {
        $project = $this->project_costing_model->get_project($id);

        if (!$project || $project['finance_reviewer_staff_id'] != get_staff_user_id()) {
            access_denied('project_costing');
        }

        $action = $this->input->post('action');
        $comment = $this->input->post('comment');

        $done = $this->project_costing_model->finance_decide($id, $action, $comment);

        if ($done) {
            set_alert('success', 'Finance decision updated.');
        } else {
            set_alert('danger', 'Unable to process finance decision.');
        }

        redirect(admin_url('project_costing/project_costing/view/' . $id));
    }

    public function hod_decide($id)
    {
        $project = $this->project_costing_model->get_project($id);

        if (!$project || $project['hod_approver_staff_id'] != get_staff_user_id()) {
            access_denied('project_costing');
        }

        $action = $this->input->post('action');
        $comment = $this->input->post('comment');

        $done = $this->project_costing_model->hod_decide($id, $action, $comment);

        if ($done) {
            set_alert('success', 'HOD decision updated.');
        } else {
            set_alert('danger', 'Unable to process HOD decision.');
        }

        redirect(admin_url('project_costing/project_costing/view/' . $id));
    }

    public function get_engagement_types_by_category()
    {
        $categoryId = (int)$this->input->post('client_category_id');
        $types = $this->project_costing_model->get_engagement_types_by_category($categoryId);
        echo json_encode($types);
    }

    public function get_engagement_type_details()
    {
        $engagementTypeId = (int)$this->input->post('engagement_type_id');
        $type = $this->project_costing_model->get_engagement_type_row($engagementTypeId);
        echo json_encode($type);
    }


public function download_excel($id)
{
    $project = $this->project_costing_model->get_project($id);

    if (!$project) {
        show_404();
    }

    $isAdmin   = is_admin();
    $isOwner   = $project['submitted_by'] == get_staff_user_id();
    $isFinance = $project['finance_reviewer_staff_id'] == get_staff_user_id();
    $isHod     = $project['hod_approver_staff_id'] == get_staff_user_id();

    if (!$isAdmin && !$isOwner && !$isFinance && !$isHod) {
        access_denied('project_costing');
    }

    $items = $this->project_costing_model->get_project_items_grouped($id);

    $this->project_costing_model->export_project_excel_dynamic($project, $items, is_admin());
}
}