<?php

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

/**
 *
 *
 * @author Lalendra
 */
class Supervisor_Contoller extends MY_Controller {

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

    public function index() {
        $sess_array = $this->session->userdata('uDetails');
        if ($sess_array['type'] == "admin") {
            $data['SupList'] = $this->user_model->getAllSupervisor();
            $this->load->view("template/header");
            $this->load->view("template/navigation");
            $this->load->view("admin/supervisorList", $data);
            $this->load->view("template/footer");
        } else {
            redirect(base_url() . "Welcome/dashboardRedirect");
        }
    }

    public function home() {
        $sess_array = $this->session->userdata('uDetails');
        $data['emplist'] = $this->Supervisor_model->getEmpListForSupervisor($sess_array['id']);
        $this->load->view("template/header");
        $this->load->view("template/navigation");
        $this->load->view("supervisor/dashboard", $data);
        $this->load->view("template/footer");
    }

    public function updateSupervisorSt() {
        $sess_array = $this->session->userdata('uDetails');
        if ($this->input->is_ajax_request() && $this->input->method() == "post" && $sess_array['type'] == "admin") {

            $id = $this->input->post('id');
            $st = $this->input->post('st');
            $res = $this->user_model->SupervisorStatus($id, $st);
            if ($res) {
                $this->data['message'] = 'Success';
            } else {
                $this->output->set_status_header('400');
                $this->data['message'] = 'Internal Server error';
            }
        } else {
            $this->output->set_status_header('400');
            $this->data['message'] = 'You can not access';
        }

        echo json_encode($this->data);
    }

    public function addPrediction() {
        $sess_array = $this->session->userdata('uDetails');
        if ($this->input->is_ajax_request() && $this->input->method() == "post" && $sess_array['type'] == "Supervisor") {
            $empId = $this->input->post('empId');
            $sup_id = $sess_array['id'];
            $activityarr = $this->input->post('activityarr');
            $hourArr = $this->input->post('hourArr');
            $res = $this->Supervisor_model->addPrediction($sup_id, $empId, $activityarr, $hourArr);
            if ($res) {
                echo json_encode($res);
            } else {
                $this->output->set_status_header('400');
                $this->data['message'] = 'Internal Server error';
                echo json_encode($this->data);
            }
        } else {
            $this->output->set_status_header('400');
            $this->data['message'] = 'You can not access';
            echo json_encode($this->data);
        }
    }

    public function loadPrediction() {
        $sess_array = $this->session->userdata('uDetails');
        if ($this->input->is_ajax_request() && $this->input->method() == "post" && $sess_array['type'] == "Supervisor") {
            $empId = $this->input->post('empId');
            $sup_id = $sess_array['id'];
            $res = $this->Supervisor_model->getPrediction($sup_id, $empId);
            echo json_encode($res);
        } else {
            $this->output->set_status_header('400');
            $this->data['message'] = 'You can not access';
            echo json_encode($this->data);
        }
    }

    public function loadSupActivityDetail(){
		$sess_array = $this->session->userdata('uDetails');
		if ($this->input->is_ajax_request() && $this->input->method() == "get" && $sess_array['type'] == "Supervisor") {
			$activity = $this->input->get('activity');
			$sup_id = $sess_array['id'];
			$res = $this->Supervisor_model->getSupActivityDetail($sup_id, $activity);
			echo json_encode($res);
		} else {
			$this->output->set_status_header('400');
			$this->data['message'] = 'You can not access';
			echo json_encode($this->data);
		}
	}

	function updateSupActivityDetail(){
		$sess_array = $this->session->userdata('uDetails');
		if ($this->input->is_ajax_request() && $this->input->method() == "post" && $sess_array['type'] == "Supervisor") {
			$act = $this->input->post('act');
			$sup_exp_tar = $this->input->post('sup_exp_tar');
			$sup_monitor = $this->input->post('sup_monitor');
			$sup_monitor_frqu = $this->input->post('sup_monitor_frqu');
			$sup_suggestion = $this->input->post('sup_suggestion');
			$sup_value = $this->input->post('sup_value');
			$sup_id = $sess_array['id'];
			$res = $this->Supervisor_model->updateSupActivityDetail($sup_id,$act,$sup_exp_tar,$sup_monitor,$sup_value,$sup_monitor_frqu,$sup_suggestion);
			echo json_encode($res);
		} else {
			$this->output->set_status_header('400');
			$this->data['message'] = 'You can not access';
			echo json_encode($this->data);
		}
	}

    public function addTime() {
        $sess_array = $this->session->userdata('uDetails');
        if ($sess_array['type'] == "Supervisor") {
            $sup_id = $sess_array['id'];
            $data['acList'] = $this->Supervisor_model->getDISTINCTActivityList($sup_id);
            $this->load->view("template/header");
            $this->load->view("template/navigation");
            $this->load->view("supervisor/add_time", $data);
            $this->load->view("template/footer");
        } else {
            redirect(base_url() . "Welcome/dashboardRedirect");
        }
    }

    public function addTimeAjax() {
        $sess_array = $this->session->userdata('uDetails');
        if ($this->input->is_ajax_request() && $this->input->method() == "post" && $sess_array['type'] == "Supervisor") {
            $sup_id = $sess_array['id'];
            $activityarr = $this->input->post('activityArr');
            $hourArr = $this->input->post('hourArr');
            if (!(is_array($activityarr) && is_array($hourArr))) {
                $this->output->set_status_header('400');
                $this->data['message'] = 'Invalid data';
                echo json_encode($this->data);
            } elseif (count($activityarr) != count($hourArr)) {
                $this->output->set_status_header('400');
                $this->data['message'] = 'Invalid data';
                echo json_encode($this->data);
            } else {
                $res=$this->Supervisor_model->addSup_predictedTime($sup_id,$activityarr,$hourArr);
                echo json_encode($res);
            }
        } else {
            $this->output->set_status_header('400');
            $this->data['message'] = 'You can not access';
            echo json_encode($this->data);
        }
    }

}
