<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 *
 *
 * @author Lalendra
 */
error_reporting(E_ALL);
ini_set('display_errors', 1);

class Welcome extends CI_Controller {

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

    public function index() {
        $sess_array = $this->session->userdata;
        if (isset($sess_array['uDetails'])) {
            $this->dashboardRedirect();
        } else {
            $this->loginPage();
        }
    }

    public function loginPage() {
        $this->load->view("template/login");
    }

    public function login() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'User Name', 'required');
        $this->form_validation->set_rules('password', ' Password', 'required');
        if ($this->form_validation->run()) {
            $user_name = $this->input->post('username');
            $password = $this->input->post('password');

            $result = $this->user_model->validateUser($user_name, $password);
//                        print_r($result);
//                        exit();
            if (count($result) > 0) {
                if ($result[0]->active == 1) {
                    $uDataArray = array(
                        'id'=>$result[0]->id,
                        'uName' => $result[0]->username,
                        'Name' => $result[0]->name,
                        'type' => $result[0]->userType,
                        'department' => $result[0]->department,
//					'imgpath'=>$result[0]->imagePath,
                    );

                    $this->session->set_userdata('uDetails', $uDataArray);

                    redirect(base_url() . "Welcome/dashboardRedirect");
                } else {
                    $this->session->set_flashdata('error', '<span style="color: red;">Your Account is not activated!<span>');
                    $this->index();
                }
            } else {

                $this->session->set_flashdata('error', '<span style="color: red;">Your Password or Username is wrong!<span>');
                $this->index();
            }
        } else {
            $this->index();
        }
    }

    public function dashboardRedirect() {
        $sess_array = $this->session->userdata('uDetails');

        if (empty($sess_array)) {
            redirect(base_url() . "Welcome/login");
        } else {
            if ($sess_array['type'] == "admin") {
                $this->load->model('Dashboard_model');
                $data['satisfaction']=$this->Dashboard_model->overallSatisfaction();
                $this->load->view("template/header");
                $this->load->view("template/navigation");
                $this->load->view("admin/dashboard",$data);
                $this->load->view("template/footer");
            } else {

                redirect(base_url() . "Supervisor_Contoller/home");
            }
        }
    }

    public function userRegistration() {
        $durationList=$this->user_model->getDurationList();
        $data['duration']=$durationList;
        $this->load->view("user_register",$data);
    }


    public function saveSupervisor() {
        $name = $this->input->post("name");
        $duration = $this->input->post("duration");
        $departmet = $this->input->post("departmet");
        $userName = $this->input->post("userName");
        $password = $this->input->post("password");
        $designation = $this->input->post("designation");
        if (trim($name) == "" || trim($userName) == "" || trim($duration) == "" || trim($password) == "" || trim($departmet) == "" || trim($designation) == "") {
            $this->session->set_flashdata('error', '<span style="color: red;">All fields required!<span>');
            redirect(base_url() . "Welcome/userRegistration");
        } else {
            $md5Pass = md5($password);
            //validate EMPID
            $result = $this->user_model->validateUserName($userName);
            if (count($result) == 0) {
                $insert = $this->user_model->addNewSupervisor($name, $userName, $md5Pass, "Supervisor", $duration, $departmet,$designation);
                if ($insert) {
                    $this->session->set_flashdata('error', '<span style="color: green;">Success, Please Wait for Approval !<span>');
                    redirect(base_url() . "Welcome/login");
                } else {
                    $this->session->set_flashdata('error', '<span style="color: red;">Internal Server Error!<span>');
                    redirect(base_url() . "Welcome/userRegistration");
                }
            } else {
                $this->session->set_flashdata('error', '<span style="color: red;">EMP ID is alredy Registerd !<span>');
                redirect(base_url() . "Welcome/userRegistration");
            }
        }
    }

    public function logout() {
        $this->session->unset_userdata('uDetails');
        redirect(base_url() . "Welcome");
    }

    public function dataCollectionform() {
        $data['department']=$this->Activity_model->getDepartmentList();
        $data['durationList']=$this->user_model->getDurationList();
        $data['supList']=$this->user_model->getAllActiveSupervisor();
        $this->load->view("template/header");
        $this->load->view("dataCollectForm",$data);
        $this->load->view("template/footer");
    }

}
