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

class Student_details extends CI_Controller {
	
	public function __construct(){
        parent::__construct();
        	$this->load->model('mod_home');
       }
	   
	  public function index($param=FALSE){
		$title='Basic Information';
		$param= substr_replace(substr($param, 5), "", -2);
		if($param!=''){
			$batch_details = $this->mod_home->get_particalur_batch($param);
			if(count($batch_details) == 0){
				redirect(base_url());
			}
			if($this->input->post('submit')){
				$this->form_validation->set_rules('mobile', 'Mobile Number', 'trim|required');
				$this->form_validation->set_rules('name', 'Name', 'trim|required');
				$this->form_validation->set_rules('email', 'Email', 'trim|required');
				if($this->form_validation->run() ==FALSE){
				$err = validation_errors();
				}else{
					$trans_no = $this->generateTransactionId(10, "SAPW_");
					 $user_details = array(
						  'student_name' => $this->input->post('name'),
						  'email_address' => $this->input->post('email'),
						  'mobile' => $this->input->post('mobile'),
						  'course_id' => $batch_details['course_id'],
						  'batch_id' => $batch_details['id'],
						  'transaction_no' => $trans_no,
						  'amount' => $batch_details['batch_fee'],
						  'status' => 0
					 );
				$status = $this->mod_home->add_new_user($user_details);	
				 if($status!=0){
					$fid  = "FPSA1111";
					$fkey = "kJHurFBd";
					$email    = $this->input->post('email');
					$userid   = $status;
					$name     = $this->input->post('name');
					$mobile   = $this->input->post('mobile');
					$amount   = $batch_details['batch_fee'];
					$erp      = $batch_details['batch_name'];
					$trans_no = $trans_no;
					$hash     = hash('sha512', $fid . "||" . $userid . "||" . $email . "||" . $name . "||" . $mobile . "||" . $amount . "|||||" . $fkey);
					?>	
					<form method="post" name="form1" id="form1" action="https://www.feepal.org/erp-exam-module-android">			
						<input type="hidden" name="FID" id="FID" value="<?= $fid; ?>" />	
						<input type="hidden" name="EMAIL" id="EMAIL" value="<?= $email; ?>" />
						<input type="hidden" name="USER_ID" id="USER_ID" value="<?= $userid; ?>" />	
						<input type="hidden" name="FULLNAME" id="FULLNAME" value="<?= $name; ?>" />
						<input type="hidden" name="PHONENO" id="PHONENO" value="<?= $mobile; ?>" />
						<input type="hidden" name="AMOUNT" id="AMOUNT" value="<?= $amount; ?>" />	
						<input type="hidden" name="ERP" id="ERP" value="<?= $erp; ?>" />	
						<input type="hidden" name="TRANSACTION_NO" id="TRANSACTION_NO" value="<?= $trans_no ?>" />
						<input type="hidden" name="CHECKSUM" id="CHECKSUM" value="<?= $hash; ?>" />		
						<script>document.form1.submit();</script>
					</form>			
				<?php  
				}else{
					 $this->session->set_flashdata('danger', 'Try again Some thing Wrong');
				}}
			}
			$data = array('main_content' =>'page/student_details','title' =>$title);
			$this->load->view('template', $data);
		}else{
			redirect(base_url());
		}
	 }
	 
	 
	 public function return_payment(){
		$data['title']        = "PAYMENT RETURN";
        $data['main_content'] = 'pages/thanksyoupage';
        $trans_no             = $this->input->post('TRANSACTION_NO');
        $useridd              = $this->input->post('USER_ID');
        $status               = $this->input->post('STATUS');
        $txnamount            = $this->input->post('TXNAMOUNT');
        $data_records = $this->mod_home->get_transaction($useridd);
		$amount = number_format($data_records['amount'], 2);
		$fid  = "FPSA1111";
		$fkey = "kJHurFBd";
		$str1 = $this->input->post('ORDERID')."||".$fid."||".$data_records['id']."||".$amount."||".$this->input->post('STATUS')."||".$data_records['transaction_no']."|||||".$fkey;
	    $hash = hash('sha512', $str1);
	    if(strcasecmp($hash, $this->input->post('CHECKSUM')) == 0) {
			 if ($status == "TXN_SUCCESS") {
                if (sizeof($data_records) > 0) {
                    $status = $this->mod_home->set_transaction_status($data_records['id']);
                    if ($status) {
                        $this->session->set_flashdata('success', 'Your Transaction Successfully done');
						$data['status'] = 1;
                    } else {
                        $this->session->set_flashdata('danger', 'Transaction Failed Try Again');
						$data['status'] = 0;
                    }
                }
            } else {
                $this->session->set_flashdata('danger', 'Transaction Failed Try Again');
				$data['status'] = 0;
            }
        }else{
			$this->session->set_flashdata('danger', 'Transaction Failed Try Again');
			$data['status'] = 0;
		}
		 $this->load->view('template', $data);
    }
	 
	 
		 
    function generateTransactionId($l=5, $pre){
   		$transId = $this->generateRandomString($l);
		$checker = $this->mod_home->checkTransactionIdExist($pre.$transId);
   		if(count($checker)!=0){
   			$this->generateTransactionId($l, $pre);
		} else {
			return $pre.$transId;
		}
	}
		 
	function generateRandomString($l=5){
		$str = '1WM3JE1HR2GR5RG3DD6RS4DH54JD1WM3JE1HR2GR5RG3DD6RS4DH54JD6KS7LG8VH2S87KT9TT0EY9WU8XI9KO7CO4AP0J6LL5JQ4GS4DD3SF2THE4JRT1TY6EE0YD4UW01WM3JE1HR2GR5RG3DD6RS4DH54JD6KS7LG8VH2S87KT9TT0EY9WU8XI9KO7CO4AP0J6LL5JQ4GS4DD3SF2THE4JRT1TY6EE0YD4UW0OO6KS7LG8VH2S87KT9TT0EY9WU8XI9KO71WM3JE1HR2GR5RG3DD6RS4DH54JD6KS7LG8VH2S87KT9TT0EY9WU8XI9KO7CO4AP0J6LL5JQ4GS4DD3SF2THE4JRT1TY6EE0YD4UW0OCO4AP0J6LL51WM3JE1HR2GR5RG3DD6RS4DH54JD6KS7LG8VH2S87KT9TT0EY9WU8XI9KO7CO4AP0J6LL5JQ4GS4DD3SF2THE4JRT1TY6EE0YD4UW0OJQ4GS4DD3SF2THE4JRT1TY6EE0YD4UWO';
    	return substr(str_shuffle($str), 0, $l);
	}	 
		 
}
