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

class User extends CI_Controller {

	public function __construct()
	{
		
		parent::__construct();
		$this->load->model('usermodel');
		$this->load->model('cart');
		$this->load->helper(array('url','form'));
		$this->load->library('form_validation','session');
		
		/*$this->load->helper('ckeditor_helper');
		$data 	= 	array();
 
		//Ckeditor's configuration
		$this->data['ckeditor'] = array(
 
			//ID of the textarea that will be replaced
			'id' 	=> 	'content',
			'path'	=>	'js/ckeditor',
 
			//Optionnal values
			'config' => array(
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 	=> 	"550px",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
 
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array(
 
				//Creating a new style named "style 1"
				'style 1' => array (
					'name' 		=> 	'Blue Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 	=> 	'Blue',
						'font-weight' 	=> 	'bold'
					)
				),
 
				//Creating a new style named "style 2"
				'style 2' => array (
					'name' 	=> 	'Red Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 		=> 	'Red',
						'font-weight' 		=> 	'bold',
						'text-decoration'	=> 	'underline'
					)
				)				
			)
		);
 
		$this->data['ckeditor_2'] = array(
 
			//ID of the textarea that will be replaced
			'id' 	=> 	'content_2',
			'path'	=>	'js/ckeditor',
 
			//Optionnal values
			'config' => array(
				'width' 	=> 	"100%",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
				'toolbar' 	=> 	array(	//Setting a custom toolbar
					array('Bold', 'Italic'),
					array('Underline', 'Strike', 'FontSize'),
					array('Smiley'),
					'/'
				)
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array(
 
				//Creating a new style named "style 1"
				'style 3' => array (
					'name' 		=> 	'Green Title',
					'element' 	=> 	'h3',
					'styles' => array(
						'color' 	=> 	'Green',
						'font-weight' 	=> 	'bold'
					)
				)
 
			)
		);	*/	
 
	}
	
	
	public function index()
	{
		
		$this->load->view('header');
		$this->load->view('index');
		$this->load->view('footer');
	}
	public function contact()
	{
		$this->load->view('header');
		$this->load->view('contact');
		$this->load->view('footer');
	}
	public function quote()
	{
		$this->load->view('header');
		$this->load->view('get-a-quote');
		$this->load->view('footer');
	}
	public function user_home()
	{
		$this->enter();
		$this->load->view('header');
		$this->load->view('user-home');
	}
	public function user_profile()
	{
		$this->enter();
		$this->load->view('header');
		
		$result['data']=$this->usermodel->getUser($this->session->userid);
		$this->load->view('user-home-profile',$result);
		if($this->input->post('Update'))
		{
			$id=$this->session->userid;
			$data= array(
				'phone'=>$this->input->post('phone'),
				'address'=>$this->input->post('address'),
				'street'=>$this->input->post('street'),
				'city'=>$this->input->post('city'),
				'pincode'=>$this->input->post('pincode'),
				'state'=>$this->input->post('state'),
				'country'=>$this->input->post('country'),
				
			);
			$this->usermodel->updateUser($id,$data);
			header('location:'.SITE_PATH.'user_profile');
		}
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('password','Password','required');
			$this->form_validation->set_rules('confirm','Confrim','required|matches[password]');
			if($this->form_validation->run())
			{
				$data['pswd']=$this->input->post('password');
				$id=$this->session->userid;
				$this->usermodel->updatePassword($data,$id);
				$this->session->set_flashdata('success','Password Updated');
				header('location:'.SITE_PATH.'user_profile');
			}
			else{
				$this->session->set_flashdata('error','Password not matching. Please Try Again');
			}
		}
	}
	public function user_email()
	{
		$this->enter();
		$this->load->view('header');
		$this->load->view('user-home-email');
	}
	public function support()
	{
		$this->enter();
		$this->load->view('header');
		$this->load->view('user-home-support');
	}
	public function email_plans()
	{
		$this->enter();
		$this->load->view('header');
		$this->load->view('user-home-email-plan');
	}
	public function billing()
	{
		$this->enter();
		$this->load->view('header');
		$this->load->view('user-home-billing');
	}
	public function dashboard()
	{
		$this->enter();
	
		$this->load->view('header');
		$userid=$this->session->userdata('userid');
		$result['data']=$this->usermodel->renewOrder($userid);
		$this->load->view('user-home',$result);

	}
	public function orders()
	{
		
		$this->enter();
		$this->load->view('header');
		$id=$this->session->userdata('userid');
		$result['data']=$this->usermodel->allorderdetails($id);
		$this->load->view('orders',$result);
	}
	public function Service()
	{
		//$this->enter();
		$this->load->view('header');
		$result['data']=$this->usermodel->serviceList();
		$this->load->view('services',$result);
		
	}
	public function servicePlan()
	{
		//$this->enter();
		$this->load->view('header');
		$id=$this->uri->segment(2);
		$result['data']=$this->usermodel->planListing($id);
		$this->load->view('service_plans',$result);
		
	}
	public function LinuxService()
	{
		$this->load->view('header');
		$result['data']=$this->usermodel->hosting();
		$this->load->view('sdhlinux',$result);
		$this->load->view('footer');
	}
	public function EmailService()
	{
		$this->load->view('header');
		$result['data']=$this->usermodel->email();
		$this->load->view('email_plans',$result);
		$this->load->view('footer');
	}
	public function login()
	{
		$this->load->view('login');
	}
	public function check_user()
	{
		$this->form_validation->set_rules('username','Username','required');
		$this->form_validation->set_rules('password','Password','required');
		if($this->form_validation->run())
		{
			$ipAddr = $this->input->ip_address();
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$result=$this->usermodel->check_login($username,$password);
			if($result)
			{
				header('location:'.SITE_PATH.'dashboard');
			}
			else
			{
				$this->session->set_flashdata('error','Incorrect Username/Password');
				header('location:'.SITE_PATH.'login');
			}
		}
		else
		{
			$this->session->set_flashdata('error','Enter Username and Password ');
			header('location:'.SITE_PATH.'login');
		}
	}
	public function enter()
	{
		if($this->session->userdata('userid')!='')
		{

		}
		else
		{
			header('location:'.SITE_PATH.'login');
		}
	}
	public function logout()
	{
		$this->session->unset_userdata('userid');
		$this->session->unset_userdata('name');
		
		header('location:'.SITE_PATH.'index');
	}
	public function SignUp()
	{
		$this->load->view('header');
		$result['country']=$this->usermodel->getcountry();
		$this->load->view('register',$result);
	}
	public function fetch_state()
	{
		if($this->input->post('country_id'))
		{
			echo $this->usermodel->fetchState($this->input->post('country_id'));
		}
	}
	public function register()
	{
		$this->form_validation->set_rules('name','Name','required');
		$this->form_validation->set_rules('email','Email','required');
		$this->form_validation->set_rules('Phone','phone','required');
		$this->form_validation->set_rules('address','Address','required');
		$this->form_validation->set_rules('pin','Pincode','required');
		$this->form_validation->set_rules('country','Country','required');
		$this->form_validation->set_rules('state','State','required');
		$this->form_validation->set_rules('pwd','Password','required');
		if($this->form_validation->run())
		{
			$email=$this->input->post('email');
			$result=$this->usermodel->check_emailID($email);
			if($result==true)
			{
				$data= array(
					'usr' => $this->input->post('name'),
					'address' => $this->input->post('address'),
					'street' => $this->input->post('street'),
					'city' =>$this->input->post('city'),
					'email' => $this->input->post('email'),
					'phone' => $this->input->post('phone'),
					'pswd' => $this->input->post('pwd'),
					'pincode' => $this->input->post('pin'),
					'country' =>$this->input->post('country'),
					'state' =>$this->input->post('state'),
					'gst' => $this->input->post('gst')
				);
				$this->usermodel->addUser($data);
				
				header('location:'.SITE_PATH.'login');
			}
			else
			{
				$this->session->set_flashdata('error', "Account already exist");
				header('location:'.SITE_PATH.'SignUp');
			}
		

		}
		header('location:'.SITE_PATH.'SignUp');
	}
	public function addToCart()
	{
		if($this->input->post('submit'))
		{
			$ipAddr = $this->input->ip_address();

			$plnid= $this->input->post('plnid');
			$price =$this->input->post('price');
			$duration =$this->input->post('duration');
			if($duration=="1")
                          {
                            $date=date("Y-m-d");
                            $exDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " + 30 days"));
                           // echo $new=date("d-m-Y",strtotime($exDate));
                          } 
                          if($duration=="3")
                          {
                            $date=date("Y-m-d");
                            $exDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " + 90 days"));
                           // echo $new=date("d-m-Y",strtotime($exDate));
                          }
                          if($duration=="6")
                          {
                            $date=date("Y-m-d");
                            $exDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " + 180 days"));
                           // echo $new=date("d-m-Y",strtotime($exDate));
                          }
                          if($duration=="12")
                          {
                            $date=date("Y-m-d");
                            $exDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " + 365 days"));
                            //echo $new=date("d-m-Y",strtotime($exDate));
                          }
                          if($duration=="24")
                          {
                            $date=date("Y-m-d");
                            $exDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " + 730 days"));
                           // echo $new=date("d-m-Y",strtotime($exDate));
                          }
                          if($duration=="36")
                          {
                            $date=date("Y-m-d");
                            $exDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " + 1095 days"));
                            //echo $new=date("d-m-Y",strtotime($exDate));
                          }
			$userid=NULL;
			$userid=$this->session->userdata('userid');
		
			if (isset($userId)) { //if session exist
				$checkCart = $this->cart->checkCartProduct($pid, $userid, $duration); // checks if same product exists or not by userID
			} else {
				$checkCart =  $this->cart->checkCart($pid, $ipAddr, $duration); // checks if same product exists or not by IP
			}
			
			$cartData= array(
				'product_id' => $plnid,
				'price' => $price,
				'user_id' => $userid,
				'ip_address' => $ipAddr,
				'duration' => $duration,
				'exp_date'=>$exDate
			);
			
			$this->cart->insertcart($cartData);
			
			header('location:'.SITE_PATH.'cart');
			
		}
	}
	public function cart()
	{
		$ipAddr = $this->input->ip_address();

        $userid = $this->session->userdata('userid');

        $sessionIPAddr = $this->session->userdata('ip');

        if ($sessionIPAddr === $ipAddr) {

            //if session ip address == local ipaddress update cart by entering the user id in the cart
            $this->cart->updateCartID($userid, $ipAddr);
            // $this->cart();
        }

        if (!isset($userid)) {
            $showCart = $this->cart->showCart($ipAddr);
			
        } else {
            $showCart = $this->cart->showCartByID($userid);
			
        }

        if (!empty($showCart)) {
            //   print_r($showCart);

            foreach ($showCart as $values) {
                $implodedValue[] = $values['product_id'];
            }
            $implodedValue = implode(',', $implodedValue);
            //echo $implodedValue;
            $productDetail['productDetail'] = $this->cart->getProductsForCart($implodedValue);
			$productDetail['taxRate'] = $this->cart->fetchTaxRate();
        } else {
            $productDetail['productDetail'] = NULL;
        }
		
		
		$this->load->view('header');
		
		$this->load->view('cart',$productDetail);
	}
	public function updatevalue()
	{
		if($this->input->post('submit'))
		{
			$pid=$this->input->post('plnid');
			$data['duration']=$this->input->post('dtn');
			$data['users']= $this->input->post('user');
			//$data['subtotal']= $this->input->post('sub');
			$this->cart->updatevalue($pid,$data);
			header('location:'.SITE_PATH.'cart');
		}
		
		header('location:'.SITE_PATH.'cart');
	}
	public function deleteItem($cid)
	{
		$this->cart->deleteCartItem($cid);
		header('location:'.SITE_PATH.'cart');
	}
	public function confirmOrder()
	{
		if($this->session->userdata('userid'))
		{
			$userid=$this->session->userdata('userid');
			
			$data['user_id']=$userid;
			$data['total']=$this->input->post('total');
			$orderID=$this->cart->insertOrder($data);
			
			$showCart=$this->cart->showCartById($userid);
			foreach($showCart as $cart)
			{
				$order['order_id']=$orderID;
				$order['product_id']=$cart['product_id'];
				//$order['exp_date']=$this->input->post('exp_date');
				$order['users']=$cart['users'];
				$order['price']=$cart['price'];
				$order['period']=$cart['duration'];
				if($cart['duration']=="1")
				{
					$date=date('Y-m-d');
					$exDate=date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) ." + 30 days"));
					$order['exp_date']=$exDate;
				}
				if($cart['duration']=="3")
				{
					$date=date("Y-m-d");
					$exDate=date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)). " + 90 days"));
					$order['exp_date']=$exDate;
				}
				if($cart['duration']=="6")
				{
					$date=date('Y-m-d');
					$exDate=date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) ." + 180 days"));
					$order['exp_date']=$exDate;
				}
				if($cart['duration']=="12")
				{
					$date=date("Y-m-d");
					$exDate=date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)). " + 365 days"));
					$order['exp_date']=$exDate;
				}
				if($cart['duration']=="24")
				{
					$date=date("Y-m-d");
					$exDate=date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)). " + 730 days"));
					$order['exp_date']=$exDate;
				}
				if($cart['duration']=="36")
				{
					$date=date("Y-m-d");
					$exDate=date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)). " + 1095 days"));
					$order['exp_date']=$exDate;
				}
				$order['subtotal']=$order['price']*$order['period']* $order['users'];
				$orderDetailsId=$this->cart->insertOrderDetails($order);
			}
			if(!empty($orderDetailsId))
			{
				$deleteCart=$this->cart->deleteCartByUserId($userid);
				//header('location:'.SITE_PATH.'orderDetails');
				$this->load->view('header');
			//$id=$this->session->userdata('userid');
			$result['order']=$this->usermodel->orderdetails($orderID);
				$this->load->view('orderdetails',$result);
			}
		}
		else
		{
			header('location:'.SITE_PATH.'login');
		}
		

	}
	/*public function orderDetails()
	{
		$this->load->view('header');
		$id=$this->session->userdata('userid');
		$result['order']=$this->usermodel->orderdetails($id);
		$this->load->view('orderdetails',$result);
	}*/
	public function bankdetails()
	{
		$this->load->view('header');
		$this->load->view('bankdetails');
		$this->load->view('footer');
	}
	public function femail()
	{
	   $this->load->view('header');
		$this->load->view('email');
		$this->load->view('footer'); 
	}
	public function send_email()
	{
	    
         //Load email library 
        // $this->load->library('email'); 
       /* $to_mail='rrumedia@gmail.com';
        $from='elizabethg089@gmail.com';
      
            $config = Array(
				'protocol'  => 'mail',
				'smtp_host' => 'smtpout.secureserver.net',
				'smtp_port' => 80,
				'smtp_user' => 'xxxxxxxxxx', 
				'smtp_pass' => 'xxxxxxxxxx', 
				'mailtype'  => 'html',
				'charset'  => 'iso-8859-1',
				'wordwrap'  => TRUE
			 );
        
       // $this->email->initialize($config);
       $this->load->library('email',$config); 
        
        $htmlcon = '<p>This is a test mail</p>';
        $this->email->to($to_mail);
        $this->email->from($from,'rru');
        $this->email->subject('Test Mail');
        $this->email->message($htmlcon);
        if($this->email->send())
        {
            $this->session->set_flashdata('msg','<div class="alert alert-success">Mail sent!</div>');
            
            header('location:'.SITE_PATH.'femail');
        }
        else
        {
            $this->session->set_flashdata('msg','<div class="alert alert-danger">Problem in sending</div>');
             header('location:'.SITE_PATH.'femail');
        }
       // $this->email->send();
       // echo $this->email->print_debugger(array('headers','subject','body'));
	}*/
	$this->load->library('phpmailer_lib');
       
        /* PHPMailer object */
        $mail = $this->phpmailer_lib->load();
       
        /* SMTP configuration */
        $mail->isMail();
        $mail->Host     = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'web@asianetweb.net';
        $mail->Password = 'G9aM4cF<';
        $mail->SMTPSecure = 'ssl';
        $mail->Port     = 465;
       
        $mail->From = 'web@asianetweb.net';
        $mail->FromName = 'asianet';
      //  $mail->addReplyTo('info@example.com', 'CodexWorld');
       
        /* Add a recipient */
        $mail->addAddress('rrumedia@gmail.com');
       
        /* Add cc or bcc */
       // $mail->addCC('cc@example.com');
       // $mail->addBCC('bcc@example.com');
       
        /* Email subject */
        $mail->Subject = 'Send Email via SMTP using PHPMailer in CodeIgniter';
       
        /* Set email format to HTML */
        $mail->isHTML(true);
       
        /* Email body content */
        $mailContent = "<h1>Send HTML Email using SMTP in CodeIgniter</h1>
        <p>This is a test email sending using SMTP mail server with PHPMailer.</p>";
        $mail->Body = $mailContent;
       
        /* Send email */
        if(!$mail->send()){
            echo 'Mail could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }else{
            echo 'Mail has been sent';
            
        }
	}
}

