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

class Account extends CI_Controller {
	
	

public function index(){

        $this->load->library('form_validation');
        $this->load->library('user_agent');
        
        $data['current']  = "signin";     
        
        $this->form_validation->set_rules('email', 'E-Mail', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');                
        $this->form_validation->set_error_delimiters(' <p class="red"> ', ' </p> ');
		        
        
        if ($this->form_validation->run() == TRUE)
	{
            $username = $this->input->post("email");
            $password = md5($this->input->post("password"));

            $adminData = $this->general->getRecordWhere(CUSTOMERS, array("Email"=>$username, "Password"=>$password));

            if(count($adminData))
            {
                if($adminData->is_active){
                    
               
                    
                $newdata = array(
                                   'CustomerID'         => $adminData->CustomerID,
                                   'FirstName'  	=> $adminData->FirstName,
                                   'LastName'           => $adminData->LastName                                   
                                   );
                
                $this->session->set_userdata($newdata);

                $updateData = array(
                                                "CustomerID" 	=> $adminData->CustomerID,
                                                "DateEntered"   => date("Y-m-d H:i:s")
                                    );
                $this->general->updateRecord(CUSTOMERS, $updateData, "CustomerID");
                                                
                redirect();
                
                
                }else{
                        $data['message'] = "Sorry ... Your account status is not activated yet !!!";
                }
            }
            else
            {
                    $data['message'] = "Invalid credentials provided";
            }                         
        }
        
        
        $this->load->view('signin',$data);
    }        
        
        
    public function myaccount(){
                $data['current']  = "myaccount"; 
                $this->load->view('myaccount',$data);
    }
    
    
        public function signup()
        {
            $data['current']  = "signup";                   
            $this->load->library('form_validation');
            
            //$users = $this->general->getAllRecordsWhere(CUSTOMERS , array("is_active" => 1),"CustomerID" , "desc");                                    
            
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique['.CUSTOMERS.'.Email]');
            $this->form_validation->set_rules('password', 'Password', 'trim|required');
            $this->form_validation->set_rules('cnfpassword', 'Confirm Password', 'trim|required|matches[password]');
            
            
            if ($this->form_validation->run() == TRUE) {
                // add user to database and send activation code email
                
                // Generate Activation Code
                $code = "Mohammad"+$this->input->post("fname")+$this->input->post("email")+$this->input->post("lname")+"Allah";
                $addData = array(
                            "FirstName"=>$this->input->post("fname"),
                            "LastName"=>$this->input->post("lname"),
                            "Email"=>$this->input->post("email"),                            
                            "Password"=>md5($this->input->post("password")),                            
                            "ActivationCode"=> md5($code),                    
                            "date_added"   => date("Y-m-d H:i:s"),
                            "date_modified"   => date("Y-m-d H:i:s")
                            );

                $this->general->insertRecord(CUSTOMERS, $addData, "CustomerID");
                
                // send Email
                 $subject = "Thank You For Registration";
            
                $message="
                                <html>
                                <body>
                                <h2>Thank You For Registration</h2><br>                                
                                <p>
                                To activate your account you need to click on the following link :
                                </p>
                                <p>Link : <a href='".base_url()."/actacc?c=".$code."'></a></p>
                                </body>
                                </html>";

                $this->general->send_email("emady877@gmail.com" , $this->input->post("email") ,$subject ,"Web Store" , $message);                        
            
                
                
                $data['messagetitle']  = "Account Created Successfully";
                $data['message']  = "activate link sent to your email, please click on link to activate your account"; 
                $this->load->view('message',$data);
            }
            else
            {
                $this->load->view('signup',$data);
            }
        }
        
        
        public function activate()
        {
            $data['current']  = "activate"; 
            $sentcode = $this->input->get("c");
            if($sentcode != "")
            {
                $resultitem =  $this->general->getRecordWhere(CUSTOMERS,array("ActivationCode" => $sentcode));  
                
                if(Count($resultitem) > 0)
                {    
                    $id = $resultitem->CustomerID;

                    $updateData = array(
                                        "CustomerID" 	  => $id,                
                                        "is_active"       => 1,                
                                        "date_modified"   => date("Y-m-d H:i:s")
                                        );

                   $this->general->updateRecord(CUSTOMERS, $updateData, "CustomerID"); 
                   
                    $data['messagetitle']  = "Account Activated Successfully";
                    $data['message']  = "thank you for your activation <a href='".site_url("signin")."'> Go To Sign In Page</a>"; 
                    $this->load->view('message',$data);
                }
                else {
                    echo "Failed";
                }
            }
            else
            {
                echo "Failed";
            }
        }
        
        
        public function forgotpassword()
        {                     
                $this->load->library('form_validation');                               
                $data['current']  = "forget";  
                $data['message']  = "";
                
                $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

                if ($this->form_validation->run() == TRUE) {
                    
                    // send change password link to customer
                    $email = $this->input->post("email");
                   
                    // get activation code
                    $resultitem =  $this->general->getRecordWhere(CUSTOMERS,array("Email" => $email));  
                    
                    if(Count($resultitem) > 0)
                    {
                       $code = $resultitem->ActivationCode;
                    
                    // send Email
                       $subject = "Forget Password Reqguest";

                       $message="
                                       <html>
                                       <body>
                                       <h2>Forget Password Reqguest</h2><br>                                
                                       <p>
                                       To change password for your account you need to click on the following link :
                                       </p>
                                       <p>Link : <a href='".base_url()."/chpacc?c=".$code."'></a></p>
                                       </body>
                                       </html>";

                       $this->general->send_email("emady877@gmail.com" , $this->input->post("email") ,$subject ,"Web Store" , $message);                        



                       $data['messagetitle']  = "Password Change Request";
                       $data['message']  = "change password link sent to your email, please click on link to complete process"; 
                       $this->load->view('message',$data);
                    }                    
                    else
                    {
                        $data['message']  = "Email Not Available !!!";
                        $this->load->view('forgotpassword',$data); 
                    }
                }
                else {
                $this->load->view('forgotpassword',$data);    
                }                            			
        }
        
        
         public function changepassword()
        {
            $data['current']  = "changepassword"; 
            $data['message']  = "";
            
            $sentcode = $this->input->get("c");
            if($sentcode != "")
            {
                $resultitem =  $this->general->getRecordWhere(CUSTOMERS,array("ActivationCode" => $sentcode));  
                
                if(Count($resultitem) > 0)
                {    
                    $this->load->library('form_validation'); 
                    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
                    $this->form_validation->set_rules('password', 'Password', 'trim|required');
                    $this->form_validation->set_rules('cnfpassword', 'Confirm Password', 'trim|required|matches[password]');

                    if ($this->form_validation->run() == TRUE) {
                        
                        if($resultitem->Email == $this->input->post("email"))
                        {
                        $id = $resultitem->CustomerID;

                        $updateData = array(
                                            "CustomerID" 	  => $id,                
                                            "Password"       => md5($this->input->post("password")),                
                                            "date_modified"   => date("Y-m-d H:i:s")
                                            );

                        $this->general->updateRecord(CUSTOMERS, $updateData, "CustomerID"); 

                        $data['messagetitle']  = "Password Changed Successfully";
                        $data['message']  = "thank you <a href='".site_url("signin")."'> Go To Sign In Page</a>"; 
                        $this->load->view('message',$data);
                        }
                        else
                        {
                            $data['message']  = "Email Not Valid !!!";
                            $this->load->view('changepassword',$data);                             
                        }
                        
                    }
                    else
                    {
                    $this->load->view('changepassword',$data); 
                    }
                                                                                                   
                }
                else {
                    echo "Failed";
                }
            }
            else
            {
                echo "Failed";
            }
        }
        
        
        public function signout()
        {
           $this->session->sess_destroy();                    
           redirect();
        }
        
        
        public function editprofile($cusid)        
        {
            $data['current']  = "editprofile";                   
            
            
            $resultitem =  $this->general->getRecordWhere(CUSTOMERS,array("CustomerID" => $cusid));  
                
                if(Count($resultitem) > 0)
                {                       
                   $this->load->library('form_validation');
                    $this->form_validation->set_rules('fname', 'First Name', 'trim|required');
                    $this->form_validation->set_rules('lname', 'Last Name', 'trim|required');
                    
                    $fname = "";
                    $lname = "";
                    $data['message']  = "";
                    if ($this->form_validation->run() == TRUE) {                                                                       
                                $updateData = array(
                                                    "CustomerID" 	  => $cusid,                
                                                    "FirstName"       => $this->input->post("fname"),                
                                                    "LastName"       => $this->input->post("lname"),                
                                                    "date_modified"   => date("Y-m-d H:i:s")
                                                );
                                $fname = $this->input->post("fname");
                                $lname = $this->input->post("lname");
                                $this->general->updateRecord(CUSTOMERS, $updateData, "CustomerID"); 
                                $data['message']  = "Your Information Changed Successfully";                                               
                        }
                        else
                        {
                                $fname = $resultitem->FirstName;
                                $lname = $resultitem->LastName;
                        }
                                
                        $data['fname']  =$fname;
                        $data['lname']  =$lname;
                        
                        $this->session->set_userdata('FirstName', $fname);
                                                
                        $this->load->view('editprofile',$data);
                        
                    }
                    else
                    {
                            $data['message']  = "ID Not Valid !!!";
                            $this->load->view('message',$data);  
                    }                                                                                                                  
        }
        
          
}
