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

class Admin extends CI_Controller {


  public function __construct()
        {
			parent::__construct();
			$this->load->helper(array('form', 'url'));
			$this->load->database();
			$this->load->library('form_validation');
			$this->load->model('Model_admin','admin');
			$this->load->library('session');
			$this->load->library('email');
			$this->form_validation->set_rules('email', 'Email', 'required');   
				
        }
	//login page.
  public function index()
	     {
		 $this->load->view('backend/login');
	     }
		 
	 
  // subscription page
  public function subscription(){
		 if($this->session->userdata('username-admin1'))
	         {
		 	 $this->load->view('backend/subscription');
		      }
	     else
		  {
	        redirect('admin/index');
		   }
	   
         }
  // client page		 
 public function clients(){
			  if($this->session->userdata('username-admin1'))
	    {
			 $this->load->view('backend/clients'); 
		}
		else
		  {
	        redirect('admin/index');
		   }
			 }

// contractor page
 public function contractor(){
			 if($this->session->userdata('username-admin1'))
	    {
		 	$this->load->view('backend/contractor'); 	
		}
		
				 else
		  {
	        redirect('admin/index');
		   }
	   
         }
	
 // job categories page
 public function jobCategories(){
		 if($this->session->userdata('username-admin1'))
	           {
				$this->load->view('backend/job-categories'); 
		       }
		  else
		   {
	        redirect('admin/index');
		   }
		}
					
  //job details page
  public function job_details(){
	if($this->session->userdata('username-admin1'))
	    {
			$this->load->view('backend/jobs'); 		
		 }
			  else
		   {
	        redirect('admin/index');
		   }
 }
	
	//payment page
  public function payment(){
	if($this->session->userdata('username-admin1'))
	    {
			$this->load->view('backend/payment'); 	
						
		}
   else
		   {
	        redirect('admin/index');
		   }
  }
	
 //	message pages
  public function messages(){
	  if($this->session->userdata('username-admin1'))
	    {
			$this->load->view('backend/message'); 		
	    }
		 else
		   {
	        redirect('admin/index');
		   }
 }
								
	// rating reviews page							
	public function rating_reviews(){
	 if($this->session->userdata('username-admin1'))
	    {
			$this->load->view('backend/rating-reviews'); 		
         }
	  else
		   {
	        redirect('admin/index');
		   }
   }
							 
	// report page						 
  public function  report(){
	  if($this->session->userdata('username-admin1'))
	    {
		$this->load->view('backend/reports'); 	 
	  }
	   else
		   {
	        redirect('admin/index');
		   }
   }
		 
//login code.
 public function adminlogin()
     {
		$data  = $_POST;
		$result = $this->admin->login($data);
		if($result == "admin")
		    {
		         redirect('admin/subscription');
            }
		else
		   {
			$this->session->set_flashdata('response',"Username and Password does not Exist");
	         redirect('admin/index');
		  }
     }

public function forgot_password(){
	$this->load->view('backend/forgot-password'); 	 
	}
	
// logout functionality
 public function logout()
   {
	 $this->session->unset_userdata('username-admin1');
	 redirect('admin/index');
   }
 

  //get the job information pop up page 
  public function edit_jobInfo()
	{
       $job_information_id     = $_POST['job_information_id'];
	   $table	    = 'jobdetails_tbl';	
	   $select_data	= "*";
	   $this->db->select($select_data);
	   $this->db->where("(job_information_id = '$job_information_id')");
	   $query  = $this->db->get($table);  
	   $result = $query->row_array();
	   $data   =  json_encode($result,true);
	   echo $data;
    }
	
public function forget_password()
	 {
        $email = $_POST['email'];
	  	$this->db->where('email',$email);
        $q = $this->db->get('admin_login');
	    if ($q->num_rows() > 0)
		  {
		     $r    = $q->result();
			 $user = $r[0];
             $this->load->helper('string');
			 $password1= random_string('alnum',6);
			 $password = md5($password1);
			 $this->db->where('email', $user->email);
			 $query = $this->db->query("update admin_login set password='$password' where email='" . $email . "'");
			 $this->email->from('johncollins0977@gmail.com', 'sampletest');
			 $this->email->to($user->email); 	
			 $this->email->subject('Password reset');
			 $this->email->message('You have requested the new password, Here is you new password:'. $password1);	
			 $this->email->send();
			 echo 1;
            }

	     }

  //delete selected job information record
 public function delete_job_information()
	{
	  $job_information_id    = $_POST['job_information_id'];
	  $result = $result = $this->admin->delete_job_information($job_information_id);
	  if($result)
		 {
		    return true;
		 }else {
		         return false;
			   }
    }

// update job information details
 public function update_jobInfo_details()
   {
	   $data   = $_POST;
	   $result = $this->admin->update_jobInfo_details($data);
	   if($result){
					echo 1;
				  }
   }
 
//add subscription 
 public function add_subscription()
   {
	  $data=$_POST;
	  $result = $this->admin->add_subscription($data);
	  if($result){
					echo 1;
				 }
  }
 // add client 
  public function add_client(){
	  $data=$_POST;
	  $result = $this->admin->add_client($data);
	  if($result){
					echo 1;
				 } 
	  }
 //get the  client pop up page 
 public function edit_client()
   {
      $client_id = $_POST['client_id'];
	  $table		   = 'clients_tbl';	
	  $select_data	   = "*";
	  $this->db->select($select_data);
	  $this->db->where("(client_id = '$client_id')");
	  $query  = $this->db->get($table);  
      $result = $query->row_array();
      $data   =  json_encode($result,true);
	  echo $data;
  }
  //update client details
 public function update_client_details()
   {
	  $data   = $_POST;
	  $result = $this->admin->update_client_details($data);
	  if($result){
			       echo 1;
		          }
  }
//delete client  record
 public function delete_client()
   {
	  $client_id = $_POST['client_id'];
	  $result    = $result = $this->admin->delete_client($client_id);
	  if($result)
		{
		   return true;
		}else {
	       return false;
		      }
  }
 //add contractor  details 
 public function add_contractor()
	{
		
	  $data   = $_POST;
	  $result = $this->admin->add_contractor($data);
	  if($result){
					echo 1;
				 }
   }
   
//get the contractor pop up page	  
 public function edit_contractor()
   {
      $contractor_id = $_POST['contractor_id'];
	  $table		    = 'contractors_tbl';	
	  $select_data	    = "*";
	  $this->db->select($select_data);
	  $this->db->where("(contractor_id = '$contractor_id')");
	  $query		= $this->db->get($table);  
	  $result		= $query->row_array();
	  $data =  json_encode($result,true);
      echo $data;
	   }
//update contractor details	   
  public function update_contractor_details()
	{ 
      $data   = $_POST;
	  $result = $this->admin->update_contractor_details($data);
	  if($result){
		          echo 1;
			     }
		}
//delete contractor record	 
 public function delete_contractor()
	{
	   $contractor_id    = $_POST['contractor_id'];
	   $result = $result = $this->admin->delete_contractor($contractor_id);
	   if($result)
	    {
		   return true;
		}else {
			return false;
		      }
   }
 // add job categories details
 public function add_jobCategory()
	{
	   $data=$_POST;
	   $result = $this->admin->add_jobCategory($data);
	   if($result){
					echo 1;
				   }
	}
// get the job details pop up page
 public function edit_job()
	{
      $job_category_id = $_POST['job_category_id'];
	  $table  = 'jobcategories_tbl';	
	  $select_data	= "*";
	  $this->db->select($select_data);
      $this->db->where("(job_category_id = '$job_category_id')");
	  $query  = $this->db->get($table);  
      $result = $query->row_array();
	  $data   =  json_encode($result,true);
	  echo $data;
	   }

	   
//update jobcategories details	   
 public function update_jobCategoryDetails()
  {
	 $data   = $_POST;
	 $result = $this->admin->update_jobCategoryDetails($data);
	 if($result){
		          echo 1;
				}
  }
 
 //delete job job category record
 public function delete_jobCategory()
	{
	  $job_category_id = $_POST['job_category_id'];
	  $result    = $result = $this->admin->delete_jobCategory($job_category_id);
	  if($result)
	     {
		    return true;
		 }else
		  {
		    return false;
		  }
    }
//add job job information details
 public function add_jobsInfo()
	{
		   
		 $data   = $_POST;
	   	 $result = $this->admin->add_jobsInfo($data);
	   if($result){
					echo 1;
				   }
	}

          public function setting(){
            
           $this->load->view('backend/setting'); 	  
        }
public function update_fee()
   {
	   $data   = $_POST;
	   $result = $this->admin->update_fee($data);
	   if($result){
					echo 1;
				  }
   }
   
     public function edit_fee()
	{
      $setting_id = $_POST['setting_id'];
	  $table  = 'setting_tbl';	
	  $select_data	= "*";
	  $this->db->select($select_data);
      $this->db->where("(setting_id = '$setting_id')");
	  $query  = $this->db->get($table);  
      $result = $query->row_array();
	  $data   =  json_encode($result,true);
	  echo $data;
	   }
           
        public function get_approve_denied(){
            $approve_denied_status_id = $_POST['approve_denied_id'];
	  $table  = 'contractors_tbl';	
	  $select_data	= "*";
	  $this->db->select($select_data);
      $this->db->where("(contractor_id = '$approve_denied_status_id')");
	  $query  = $this->db->get($table);  
      $result = $query->row_array();
	  $data   =  json_encode($result,true);
	  echo $data;  
               
           }
           
            public function get_approve_denied2(){
            $approve_denied_status_id = $_POST['denied_id'];
	  $table  = 'contractors_tbl';	
	  $select_data	= "*";
	  $this->db->select($select_data);
      $this->db->where("(contractor_id = '$approve_denied_status_id')");
	  $query  = $this->db->get($table);  
      $result = $query->row_array();
	  $data   =  json_encode($result,true);
	  echo $data;  
               
           }
           
            public function get_approve_denied4(){
            $approve_denied_status_id = $_POST['approved_id'];
	  $table  = 'contractors_tbl';	
	  $select_data	= "*";
	  $this->db->select($select_data);
      $this->db->where("(contractor_id = '$approve_denied_status_id')");
	  $query  = $this->db->get($table);  
      $result = $query->row_array();
	  $data   =  json_encode($result,true);
	  echo $data;  
               
           }
            public function status_approved(){
         $approve_denied_id = $_POST['approve_denied'];  
         $query = $this->db->query("update contractors_tbl set approve='1' where contractor_id='" . $approve_denied_id . "'");
         if($query){
             echo 1;
         }
         else{
             echo 2;
         }
         
        }
        
        public function status_to_approve(){
            
            $approve_denied_status_id= $_POST['approve_denied_id'];
            $query = $this->db->query("update contractors_tbl set approve='1' where contractor_id='" . $approve_denied_status_id . "'");
           
            if($query){
                echo 1;
            }
            else{
                echo 2;
            }
        }
        
        public function status_to_denied(){
        $denied_status_id= $_POST['denied_status_id'];
            $query = $this->db->query("update contractors_tbl set denied='1',approve='0' where contractor_id='" . $denied_status_id . "'");
            if($query){
                echo 1;
            }
            else{
                echo 2;
            }     
        }
        //denied to approve status
      public function denied_to_approve(){
         $denied_to_approve_status_id = $_POST['denied_to_approve_status_id'];
          $query = $this->db->query("update contractors_tbl set denied='0',approve='1' where contractor_id='" . $denied_to_approve_status_id . "'");
            if($query){
                 
                echo 1;
            }
            else{
                echo 2;
            }     
       
      }
      
      public function pendind_to_denied(){
       $approve_denied_status_id = $_POST['approve_denied_status_id'];
          $query = $this->db->query("update contractors_tbl set denied='1' where contractor_id='" . $approve_denied_status_id . "'");
            if($query){
                echo 1;
            }
            else{
                echo 2;
            }        
      }
		}
		
		
		?>
