<?php

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

/**
 *
 *
 * @author Lalendra
 */
class Supervisor_model extends CI_Model
{

	public function getEmpListForSupervisor($supid)
	{
		try {
			$sql = "select * from emp_info where Supervisor='$supid'";
			$result = $this->db->query($sql)->result();
			$this->db->close();
			return $result;
		} catch (Exception $exc) {
			return 0;
		}
	}

	public function addPrediction($sup_id, $emp_id, $activityarr, $hourArr)
	{
		try {
			$sql = "delete from supervisor_prediction where emp_id='$emp_id' and sup_id='$sup_id' ";
			$this->db->query($sql);
			$rs2 = 0;
			for ($x = 0; $x < count($activityarr); $x++) {
				$sql2 = "INSERT INTO `supervisor_prediction`(`emp_id`,`sup_id`,`activity`,`time`)VALUES('$emp_id','$sup_id','$activityarr[$x]','$hourArr[$x]');";
				$rs2 = $this->db->query($sql2);
				if (!$rs2) {
					$sql = "delete from supervisor_prediction where emp_id='$emp_id' and sup_id='$sup_id'";
					$this->db->query($sql);
					break;
				}
			}
			$this->db->close();
			return $rs2;
		} catch (Exception $exc) {
			return 0;
		}
	}

	public function getPrediction($sup_id, $emp_id)
	{
		try {
			$sql = "select activity,time from supervisor_prediction where emp_id='$emp_id' and sup_id='$sup_id'";
			$result = $this->db->query($sql)->result();
			$this->db->close();
			return $result;
		} catch (Exception $exc) {
			return 0;
		}
	}

	public function getDISTINCTActivityList($sup_id)
	{
		try {
			$sql = "select DISTINCT ac.activity,TIME_FORMAT(ac.sup_hr,'%H:%i') as sup_hr from activity ac join emp_info emp on emp.empId=ac.emp_id where emp.Supervisor='$sup_id' order by ac.activity";
			$result = $this->db->query($sql)->result();
			$this->db->close();
			return $result;
		} catch (Exception $exc) {
			return 0;
		}
	}

	public function getSupActivityDetail($sup_id, $activity)
	{
		try {
			$sql = "select DISTINCT  ac.activity,ac.sup_exp_tar,ac.sup_value,ac.sup_monitor,ac.sup_monitor_frqu,ac.sup_suggestion from activity ac join emp_info emp on emp.empId=ac.emp_id where ac.activity ='$activity' and emp.Supervisor='$sup_id' limit 1";
			$result = $this->db->query($sql)->result();
			$this->db->close();
			return $result;
		} catch (Exception $exc) {
			return 0;
		}
	}

	public function updateSupActivityDetail($sup_id, $act, $sup_exp_tar, $sup_monitor, $sup_value, $sup_monitor_frqu, $sup_suggestion)
	{
		$sql = "update activity ac join emp_info emp  on ac.emp_id=emp.empId set ac.`sup_exp_tar`='$sup_exp_tar',ac.`sup_value`='$sup_value',ac.`sup_monitor`='$sup_monitor',ac.`sup_monitor_frqu`='$sup_monitor_frqu',ac.`sup_suggestion`='$sup_suggestion' where "
			. " emp.Supervisor='$sup_id' and ac.activity='$act';";
		$result = $this->db->query($sql);

		$this->db->close();
		return $result;
	}

	public function addSup_predictedTime($supid, $activity, $time)
	{
		try {
			for ($x = 0; $x < count($activity); $x++) {
				$sql = "update activity ac join emp_info emp  on ac.emp_id=emp.empId set ac.sup_hr='$time[$x]' where "
					. " emp.Supervisor='$supid' and ac.activity='$activity[$x]';";
				$result = $this->db->query($sql);
			}
			$this->db->close();
			return $result;
		} catch (Exception $exc) {
			return 0;
		}
	}

}
