<?php

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

class user_model extends CI_Model {

    public function validateUser($username, $pass) {
        try {
            $md5pass = md5($pass);
            $sql = "SELECT * FROM sys_users where username='$username' and password='$md5pass'  ";
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            return 0;
        }
    }

    public function addNewSupervisor($name, $userName, $md5Pass, $userType, $duration, $departmet,$designation) {
//        print_r($data);
        try {
            $sql = "INSERT INTO `sys_users` (`name`, `username`, `password`, `userType`, `department`, `duration`,`designation`) "
                    . "VALUES ( '$name', '$userName', '$md5Pass','$userType', '$departmet', '$duration','$designation')";
//            exit($sql);
            $return = $this->db->query($sql);
            $this->db->close();
            return $return;
        } catch (Exception $exc) {
            return 0;
        }
    }

    public function validateUserName($userId) {
        try {
            $sql = "SELECT * FROM sys_users where username='$userId'";
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            return 0;
        }
    }

    public function getAllSupervisor() {
        try {
            $sql = "SELECT su.*,du.text  FROM `sys_users` su join duration du  on du.id=su.duration WHERE su.userType='Supervisor' ";
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            return 0;
        }
    }

    public function SupervisorStatus($id, $st) {
        try {
            $sql = "UPDATE `sys_users` SET `active`='$st' WHERE `id`='$id'; ";
            $result = $this->db->query($sql);
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            return 0;
        }
    }

    public function getDurationList() {
        try {
            $sql = "select * from duration; ";
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            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 validateEmpID($empID) {
        try {
            $sql = "SELECT * FROM emp_info where empID='$empID'";
            $result = $this->db->query($sql)->result();
            $this->db->close();
            return $result;
        } catch (Exception $exc) {
            return 0;
        }
    }

}
