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


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

    public function getActivity_Suggestions($key) {
        try {
            $sql = "SELECT distinct activity FROM activity where activity like '%$key%' limit 20 ;";
//            print_r($sql);
//            exit();
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            $this->db->close();
            return 0;
        }
    }

    public function getDepartmentList() {
        try {
            $sql = "SELECT distinct department FROM `sys_users` where active=1";
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            $this->db->close();
            return 0;
        }
    }

    public function getAllActiveSupervisor() {
        try {
            $sql = "select id,name,username from sys_users  where userType='Supervisor' and active=1 order by name; ";
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            return 0;
        }
    }

    public function addEmpInfoActivity($empId, $empName,$designation,$department, $Supervisor, $duration, $activityarr, $frequentArr, $timeArr, $hourArr,$satisfactionArr,
	   $targetTextArr,$targetSatisfactionArr,$targetMonitorArr,$monitorFrequentArr,$suggestionArr) {
        try {
            $sql = "SELECT * FROM emp_info where empID='$empId'";
            $count = count($this->db->query($sql)->result());
            if($count>0){
                return 0;
            }

            $sql = "INSERT INTO `emp_info` (`empId`, `empName`, `department`, `Supervisor`, `duration`,`designation`) "
                    . "VALUES ('$empId','$empName','$department','$Supervisor','$duration','$designation')";
            $rs = $this->db->query($sql);

            if ($rs) {
                $rs2 = 0;
                for ($x = 0; $x < count($activityarr); $x++) {
					$sup_hr="00:00:00";
					$sup_exp_tar="";
					$sup_value="0";
					$sup_monitor="";
					$sup_monitor_frqu="";
					$sup_suggestion="";
					$sup_sql="select ac.activity,ac.sup_hr,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 ='$activityarr[$x]' and emp.Supervisor='$Supervisor' and sup_hr<>'00:00:00' LIMIT 1 ";
					$sup_data=$this->db->query($sup_sql)->result();
					if($sup_data){
						$sup_hr=$sup_data[0]->sup_hr;
						$sup_exp_tar=$sup_data[0]->sup_exp_tar;
						$sup_value=$sup_data[0]->sup_value;
						$sup_monitor=$sup_data[0]->sup_monitor;
						$sup_monitor_frqu=$sup_data[0]->sup_monitor_frqu;
						$sup_suggestion=$sup_data[0]->sup_suggestion;
					}

                    $sql = "INSERT INTO `activity` (`emp_id`, `activity`, `frequent`, `times`, `hours`,`satisfaction`,`targetText`, `targetSatisfaction`, `targetMonitor`, `monitorFrequent`, `suggestion`,`sup_hr`, `sup_exp_tar`, `sup_value`, `sup_monitor`, `sup_monitor_frqu`, `sup_suggestion`) "
                            . "VALUES ('$empId','$activityarr[$x]','$frequentArr[$x]','$timeArr[$x]','$hourArr[$x]','$satisfactionArr[$x]',
                            '$targetTextArr[$x]','$targetSatisfactionArr[$x]','$targetMonitorArr[$x]','$monitorFrequentArr[$x]','$suggestionArr[$x]','$sup_hr','$sup_exp_tar','$sup_value','$sup_monitor','$sup_monitor_frqu','$sup_suggestion')";
                    $rs2 = $this->db->query($sql);
                    if (!$rs2) {
                        $sql = "DELETE FROM `emp_info` WHERE ='$empId'";
                        $this->db->query($sql);
                        break;
                    }
                }
                $this->db->close();
                return $rs2;
            } else {
                $this->db->close();
                return 0;
            }
        } catch (Exception $exc) {
            $this->db->close();
            return 0;
        }
    }

//    public function testF($activityarr,$Supervisor){
//		$sup_hr="00:00:00";
//		$sup_exp_tar="";
//		$sup_value="0";
//		$sup_monitor="";
//		$sup_monitor_frqu="";
//		$sup_suggestion="";
//		$sup_sql="select ac.activity,ac.sup_hr,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 ='$activityarr' and emp.Supervisor='$Supervisor' and sup_hr<>'00:00:00' LIMIT 1 ";
//		$sup_data=$this->db->query($sup_sql)->result();
//		if($sup_data){
//			$sup_hr=$sup_data[0]->sup_hr;
//			$sup_exp_tar=$sup_data[0]->sup_exp_tar;
//			$sup_value=$sup_data[0]->sup_value;
//			$sup_monitor=$sup_data[0]->sup_monitor;
//			$sup_monitor_frqu=$sup_data[0]->sup_monitor_frqu;
//			$sup_suggestion=$sup_data[0]->sup_suggestion;
//		}
//		print_r($sup_monitor_frqu);
//	}
}
