<?php  
 class Main_model extends CI_Model  
 {  
      function can_login($username, $password)  
      {  
           $this->db->where('username', $username);  
           $this->db->where('password', $password);  
           $query = $this->db->get('adminlogin');  
           //SELECT * FROM users WHERE UserName = '$username' AND Password = '$password'  
           if($query->num_rows() > 0)  
           {  
                return true;  
           }  
           else  
           {  
                return false;       
           }  
      }
	  
	  function update_login_info($user_id)
		{
			$query=$this->db->query("update users set IsLoggedIn = 1 and LastLogin = ".date('Y-m-d H:i:s')." where UserID = ".$user_id."");
			//return $query->result();
		}
		
		function get_subcriptions()
		{
			$query=$this->db->query("select * from subscriptions");
			return $query->result();
		}
		
		function get_clients()
		{
			$query=$this->db->query("select * from customer");
			return $query->result();
		}
		
		function get_intervals()
		{
			$query=$this->db->query("select * from intervals where Enabled=1");
			return $query->result();
		}
		
		
		function get_contractors()
		{
			$query=$this->db->query("select * from contractor");
			return $query->result();
		}
		
		function get_jobcats()
		{
			$query=$this->db->query("select * from jobs");
			return $query->result();
		}
		
		function get_jobDetails()
		{
			/*$query=$this->db->query("select * from job_details");
			return $query->result();*/
			
			$this->db->select('a.*,b.*'); 
			$this->db->from('job_details a');
			$this->db->join('jobs b', 'b.id = a.job_type', 'left'); 
			$query = $this->db->get();
			return $query->result();
			
			
		}
		
		function get_jobTypes()
		{
			$query=$this->db->query("select id,job_type from jobs");
			return $query->result();
			
		}
		
		function get_jobPeriodTypes()
		{
			$query=$this->db->query("select job_type_ID,job_type from job_types Where Enabled=1");
			return $query->result();
			
		}
		
		function add_sub($plan_title,$plan_id,$interval_type,$interval_nos,$amount,$trail_period_days,$grp_to_activate)
		{
			$data = array( 
				'subscription_type'	=>  $plan_title, 
				'subscription_duration' =>  $interval_nos,
				'subscription_price' =>  $amount,
				'subscription_interval' =>  $interval_type,
				'trail_period_days' =>  $trail_period_days,
				'group_to_activate' =>  $grp_to_activate,
				'subscription_plan_id' =>  $plan_id
			);
		$this->db->insert('subscriptions', $data);
		}
		
		function add_client($add_fname,$add_lname,$add_email,$add_pass,$add_phone)
		{
			$data = array( 
				'first_name'	=>  $add_fname, 
				'last_name' =>  $add_lname,
				'email' =>  $add_email,
				'phone_no' =>  $add_phone,
				'password' =>  $add_pass
			);
		$this->db->insert('customer', $data);
		}
		
		function edit_client($edit_id,$edit_fname,$edit_lname,$edit_email,$edit_pass,$edit_ph)
		{
			$data = array(
				'first_name' => $edit_fname,
				'last_name' => $edit_lname,
				'email' => $edit_email,
				'phone_no' => $edit_ph
			);
			
			
			$this->db->where('id', $edit_id);
			$this->db->update('customer', $data);
			
			if(!empty($edit_pass)){
				$data = array(
					'password' => $edit_pass
				);
				$this->db->where('id', $edit_id);
				$this->db->update('customer', $data);
			}
		}
		
		function delete_client($del_id)
		{
			$this->db->where('id', $del_id);
			$this->db->delete('customer');
			
			
		}
		
		
		
		function add_contractor($add_fname,$add_lname,$add_email,$add_pass,$add_phone,$add_Uname)
		{
			$data = array( 
				'first_name'	=>  $add_fname, 
				'last_name' =>  $add_lname,
				'email' =>  $add_email,
				'phone_no' =>  $add_phone,
				'password' =>  $add_pass,
				'username' =>  $add_Uname
			);
		$this->db->insert('contractor', $data);
		}
		
		function edit_contractor($edit_id,$edit_fname,$edit_lname,$edit_email,$edit_pass,$edit_ph,$edit_Uname)
		{
			$data = array(
				'first_name' => $edit_fname,
				'last_name' => $edit_lname,
				'email' => $edit_email,
				'phone_no' => $edit_ph,
				'username' => $edit_Uname
			);
			
			
			$this->db->where('id', $edit_id);
			$this->db->update('contractor', $data);
			
			if(!empty($edit_pass)){
				$data = array(
					'password' => $edit_pass
				);
				$this->db->where('id', $edit_id);
				$this->db->update('contractor', $data);
			}
		}
		
		function delete_contractor($del_id)
		{
			$this->db->where('id', $del_id);
			$this->db->delete('contractor');
			
			
		}
		
		
		function add_jobcat($cat_name,$cat_qualification,$cat_add,$cat_sal,$cat_cmp)
		{
			$data = array( 
				'job_type'	=>  $cat_name, 
				'qualification' =>  $cat_qualification,
				'address' =>  $cat_add,
				'salary' =>  $cat_sal,
				'company_name' =>  $cat_cmp,
				'date_added' =>  date('Y-m-d')
			);
		$this->db->insert('jobs', $data);
		}
		
		function edit_jobcat($edit_cat_id,$edit_cat_name,$edit_cat_qualification,$edit_cat_add,$edit_cat_sal,$edit_cat_cmp)
		{
			$data = array(
				'job_type' => $edit_cat_name,
				'qualification' => $edit_cat_qualification,
				'address' => $edit_cat_add,
				'salary' => $edit_cat_sal,
				'company_name' => $edit_cat_cmp,
				'date_added' => date('Y-m-d')
			);
			
			
			$this->db->where('id', $edit_cat_id);
			$this->db->update('jobs', $data);
			
			if(!empty($edit_pass)){
				$data = array(
					'password' => $edit_pass
				);
				$this->db->where('id', $edit_id);
				$this->db->update('customer', $data);
			}
		}
		
		function delete_jobcat($del_id)
		{
			$this->db->where('id', $del_id);
			$this->db->delete('jobs');
			
			
		}
		
		
		function add_job($job_cat,$job_title,$job_type,$job_hourly,$job_desc,$job_start,$job_end)
		{
			$data = array( 
				'job_category'	=>  $job_cat, 
				'job_title' =>  $job_title,
				'job_type' =>  $job_type,
				'job_hourly_rate' =>  $job_hourly,
				'job_description' =>  $job_desc,
				'job_start_date' =>  $job_start,
				'job_end_date' =>  $job_end,
				'job_posted_on' =>  date('Y-m-d')
			);
		$this->db->insert('job_details', $data);
		}
		
		function edit_job($job_id,$edit_job_cat,$edit_job_title,$edit_job_type,$edit_job_hourly,$edit_job_desc,$edit_job_start,$edit_job_end)
		{
			$data = array(
				'job_category'	=>  $edit_job_cat, 
				'job_title' =>  $edit_job_title,
				'job_type' =>  $edit_job_type,
				'job_hourly_rate' =>  $edit_job_hourly,
				'job_description' =>  $edit_job_desc,
				'job_start_date' =>  $edit_job_start,
				'job_end_date' =>  $edit_job_end
			);
			
			
			$this->db->where('job_detail_ID', $job_id);
			$this->db->update('job_details', $data);
			
			if(!empty($edit_pass)){
				$data = array(
					'password' => $edit_pass
				);
				$this->db->where('id', $edit_id);
				$this->db->update('customer', $data);
			}
		}
		
		function delete_job($del_id)
		{
			$this->db->where('job_detail_ID', $del_id);
			$this->db->delete('job_details');
			
			
		}
		
		
		function get_payments()
		{
			$this->db->select('a.*,b.*'); 
			$this->db->from('payments a');
			$this->db->join('payment_categories b', 'b.payment_category_ID = a.payment_category', 'left'); 
			$query = $this->db->get();
			return $query->result();
			
		}
		
		function get_ratings_reviews()
		{
			$query=$this->db->query("select * from ratings_reviews");
			return $query->result();
			
		}
		
		function get_messages()
		{
			$query=$this->db->query("select * from view_messages");
			return $query->result();
			
		}
		
		function delete_mail($del_id)
		{
			$this->db->where('id', $del_id);
			$this->db->delete('view_messages');
			
			
		}
	  
 }  