<?php
App::uses('AppController', 'Controller');

class CustomersController extends AppController{
	public $components = array(
        'Twilio','Eway'
    );

	public function employee_customer(){
		$this->layout = 'employee';
		$data = $this->Customer->find('all',array('conditions' => array('is_delete' => 0)));
		$this->set('customer',$data);
	}

	public function employee_addcustomer(){
		$this->layout = 'employee';
		if(!empty($this->request->data)){
			if($this->Customer->save($this->request->data)){
				$this->Session->setFlash("Customer SuccessFully Added");
				$this->redirect(array('action' => 'customer'));
			}
		}
	}

	public function employee_editcustomer($id = null){
		$this->layout = 'employee';

		$customer = $this->Customer->find('first',array('conditions' => array('id' => $id)));
		$this->set('customer',$customer);	
		if(!empty($this->request->data)){
			 $this->Customer->create();
			  $this->Customer->id=$id;
			if($this->Customer->save($this->request->data)){
				$this->Session->setFlash("Customer SuccessFully Edited");
				$this->redirect(array('action' => 'customer'));
			}
		}
		$this->request->data = $customer;
        $this->set('editcustomer',$customer);


	}

	public function employee_deletecustomer($id = null) {
        $this->Customer->id = $id;
        if (!$this->Customer->exists()) {
            throw new NotFoundException(__('Invalid Category'));
        }
        $status = $this->Customer->activedelete($id);
       	$this->Session->setFlash("Customer SuccessFully Deleted");
       	$this->redirect(array('action' => 'customer'));
    }
    public function employee_payment(){
    	$this->loadModel('Payment');
		$this->layout = 'employee';				
		$data=$this->Customer->find('list');
		$this->set('customer',$data);
		 $payment_data=$this->request->data;
		 if(!empty($payment_data)){
		     
			      $transactionId = $this->Eway->payment($payment_data);
			      $transactionId = $this->Eway->payment($payment_data);
				  if($transactionId!=0){
				  	$transaction_data=array();
				  	$transaction_data['transaction_id']=$transactionId;
				  	$transaction_data['customer_name']=$payment_data['CustomerCredit']['customer_id'];
				  	$transaction_data['driver_id']=$this->Auth->user('id');
				  	$transaction_data['amount']=$payment_data['CustomerCredit']['amount'];
				  	if($this->Payment->save($transaction_data)){
				  			$this->Session->setFlash("Transaction Occured SuccessFully!");	
				  	}
				  }
				  else{
				  	$this->Session->setFlash("Error Occured In Transaction!");	
				  }    

			      
		}	      
			  
			  /*else{
			  	$this->Session->setFlash("Sorry Customer ALready Exist!");
			  }	*/      
		
	}

	 public function employee_sendsms() {
	      $this->layout = 'employee';
	      $this->loadModel('Message');
	      $data=$this->request->data;
	      if(!empty($data)){
		      $contact_no=$data['Customer']['mobile'];
		      $customer_name=$data['Customer']['name'];
		      $is_customer=$this->Customer->find('all', array('conditions'=>array('Customer.mobile'=>$contact_no)));		
		      if(!$is_customer){     
		      	  $data['Customer']['is_delete']=0;
		      	  $this->Customer->save($data);
		      	  $messageData=$this->Message->find('all', array('conditions'=>array('Message.type'=>'FIRST')));
		      	  $message=$messageData[0]['Message']['message'];
		      
		      	  $sms = $this->Twilio->sendmessage($message, $contact_no);
			      
			      $this->Session->setFlash("SMS Sent SuccessFully!");
			  }
			  else{
			  	$this->Session->setFlash("Sorry Customer ALready Exist!");
			  }
		      }
		      
    	  
    }

   

}
?>