<?php 

class User extends baseController
{
	
	
	  function add()
	  {
          
			$data = $this->getDataRequest();
			if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') :
			$name = $data->name;				
			$username = $data->username;			
			$password = $data->password;	
			$options = ['cost' => 10];			
			$pass = password_hash($password, PASSWORD_BCRYPT, $options);           
			$get = $this->db->query("select * from users where username ='$username'");	$get->execute();            
			$count = $get->rowCount();
			if($count > 0) :                 
			echo "Username alredy exist";                 
			else : 					
			$query = $this->db->query("insert into users (name,username,password)values ('$name','$username','$pass')"); 
			echo $query ? 'success' : 'fail';
        endif;
        endif;

	  }
     
        function Remove()
          {
			  
              $data = $this->getDataRequest();
              $qry = $this->db->query("delete from users where id =".$data->id);
              echo $qry ? "success" :  "fail";
          }

          function Update()
          {
			  $data = $this->getDataRequest();
               $name = !empty($data->name) ? $data->name : "";
               $username = !empty($data->username) ? $data->username : "";
               $password = !empty($data->password) ? $data->password : "";
              $query =  $this->db->query("update users set name = '".$name."',username='".$username."',password='".$password."' where id =".$data->edit_id."");
				echo $query ? "success" : "fail";
            
          }
		  function Upstatus()
          {
			 //error_reporting(-1);
			 $data = $this->getDataRequest();
			 
             $get = $this->getBy("users","id =".$data->id);
			 $data = $get[0];
			 
             if($data['status'] == "0" ){
				 $val = "1";
			 }else{
				$val = "0";
			 }
			$query = $this->db->query("update users set status = '".$val."' where id = ".$data['id']);
			echo $query ? "success" :  "fail";
			
			//print_r($query);
          }

}
?>