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

class Usermodel extends CI_Model {
	
    public function hosting()
    {
        $active='Y';
        $this->db->select('*');
        $this->db->where('service_id',3);
        $this->db->where('status',$active);
        $query=$this->db->get('tbl_service_plans');
        return $query->result();
    }
    public function email()
    {
        $active='Y';
        $this->db->select('*');
        $this->db->where('service_id',2);
        $this->db->where('status',$active);
        $query=$this->db->get('tbl_service_plans');
        return $query->result();
    }

   /* public function check_login($username,$password)
    {
        $this->db->select('*');
		$this->db->from('tbl_usr');
		$this->db->where('email',$username);
		$this->db->where('pswd',$password);
		$query=$this->db->get();
		if($query->num_rows()==1)
		{
            $logindata= [ 
                'userid'=> $query->id,
                'user' => $query->usr,
            ];
            $this->session->set_userdata($logindata);
			return true;
		}
		else
		{
			return false;
		}
    }*/
    public function check_login($username, $password) {
        $user = $this->db
            ->select("*")
            ->where(
                 [
                    'email' => $username,
                    'pswd' => $password
                 ]
             )
            ->get("tbl_usr")
            ->row();
            
       if ($user) {
             $logindata = [
                 'userid' => $user->id,
                 'name'   => $user->usr,
                 'email' => $user->email,
                 'loggedin'=> TRUE,
                 'ip' => $this->input->ip_address()
             ];
             $this->session
                  ->set_userdata($logindata);
                 
             return true;
       }
       else {
             return false;
       }
    } 
   
    public function getcountry()
	{
		$this->db->select('*');
		$query=$this->db->get('tbl_country');
		return $query->result();
	}
	public function fetchState($country_id)
	{
		$this->db->where('country_id',$country_id);
		$this->db->order_by('state_name','ASC');
		$query=$this->db->get('tbl_state');
		$output='<option value="">Select State</option>';
		foreach($query->result() as  $row)
		{
			$output .= '<option value="'.$row->state_id.'">'.$row->state_name.'</option>';
		}
		return $output;
	}
    public function check_emailID($email)
    {
        $this->db->select('*');
        $this->db->from('tbl_usr');
        $this->db->where('email',$email);
        $query=$this->db->get();
        if($query->num_rows()>0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public function addUser($data)
    {
        $this->db->insert('tbl_usr',$data);
    }
    public function getUser($id)
    {
        $this->db->select('*');
        $this->db->from('tbl_usr');
        $this->db->join('tbl_country','tbl_country.country_id=tbl_usr.country');
        $this->db->join('tbl_state','tbl_state.state_id=tbl_usr.state');
        $this->db->where('tbl_usr.id',$id);
        $query=$this->db->get();
        return $query->result();
    }
    public function updateUser($id,$data)
    {
        $this->db->where('id',$id);
        $this->db->update('tbl_usr',$data);
    }
    public function updatePassword($data,$id)
    {
        $this->db->where('id',$id);
        $this->db->update('tbl_usr',$data);
    }
    public function serviceList()
    {
        $active='Y';
        $this->db->select('*');
        $this->db->where('status',$active);
        $query=$this->db->get('tbl_service');
        return $query->result();
    }
    public function planListing($id)
    {
        $active='Y';
        $this->db->select('*');
        $this->db->where('service_id',$id);
        $this->db->where('status',$active);
        $query=$this->db->get('tbl_service_plans');
        return $query->result();
    }
    public function orderdetails($id)
    {
        $this->db->select('*');
        $this->db->from('tbl_order');
        $this->db->order_by('tbl_order.order_id','desc');
        $this->db->join('tbl_orderdetails','tbl_orderdetails.order_id=tbl_order.order_id');
        $this->db->join('tbl_service_plans','tbl_service_plans.id=tbl_orderdetails.product_id');
        $this->db->join('tbl_usr','tbl_usr.id=tbl_order.user_id');
        $this->db->where('tbl_order.order_id',$id);
        $query=$this->db->get();
        return $query->result();
    }
    public function allorderdetails($id)
    {
        $this->db->select('*');
        $this->db->from('tbl_order');
        $this->db->order_by('tbl_order.order_id','desc');
        $this->db->join('tbl_orderdetails','tbl_orderdetails.order_id=tbl_order.order_id');
        $this->db->join('tbl_service_plans','tbl_service_plans.id=tbl_orderdetails.product_id');
        $this->db->join('tbl_usr','tbl_usr.id=tbl_order.user_id');
        $this->db->where('tbl_order.user_id',$id);
        $query=$this->db->get();
        return $query->result();
    }

    public function renewOrder($userid)
    {
       
        $this->db->select('*');
        $this->db->from('tbl_orderdetails');
        $this->db->join('tbl_order','tbl_order.order_id=tbl_orderdetails.order_id');
        $this->db->join('tbl_service_plans','tbl_service_plans.id=tbl_orderdetails.product_id');
        $this->db->where('tbl_order.user_id',$userid);
        $todate=date("Y-m-d");
        $this->db->where("DATEDIFF(tbl_orderdetails.exp_date,'$todate') <=",15);
        //$this->db->where('tbl_orderdetails.exp_date'>= DATE_SUB(CURDATE(),INTERVAL 15 DAY );
        //$this->db->where('tbl_orderdetails.exp-date BETWEEN "'.DATE('Y-m-d',strtotime(tbl_orderdetails.exp_date)).'"and"'.date('Y-m-d',strtotime(now())).'"');      
        $query=$this->db->get();
        return $query->result_array();
    }
    
}
