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

class PurchaseController extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('GuestModel');
		$this->load->model('SiteModel');
		$this->load->model('PackageModel');
		$this->load->model('AuthModel');
		$this->load->model('NotificationModel');
		$this->load->library('Paypal_lib');
	}

	public function index()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
			
        $site_row = $this->SiteModel->get_row($userdata['site_id']);
		$where = " where package_tb.id > 0 and package_tb.is_display = 0 and package_tb.isDeleted = 0 ";

		$data['title'] = 'Purchase Pass | MAKEABL';
		$data['userdata'] = $userdata;
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);
		$data['purchase_list'] = $this->PackageModel->get_purchase_rows($where."and site_tb.id = '".$userdata["site_id"]."' group by package_tb.id order by package_tb.id desc");
		$data['notification_list'] = $this->NotificationModel->get_rows(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' order by id desc");
		$data['new_notification_count'] = $this->NotificationModel->get_count(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' and isShow = 0");
        $data['site_logo'] = 'http://d1z6a3vjnfowyr.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];
        
		$this->load->view('template/header', $data);
		$this->load->view('template/sidebar', $data);
		$this->load->view('digital-pass', $data);
		$this->load->view('template/footer', $data);
	}
	
	public function purchase(){
	    $userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
		
		$guest_id = $this->input->get("guest_id", TRUE);
		$package_id = $this->input->get("package_id", TRUE);
		$from_date = $this->input->get('from_date', TRUE);
		$validity = $this->input->get('validity', TRUE);
		$price = $this->input->get('price', TRUE);
		
		// Set variables for paypal form
		$returnURL = base_url().'packagepaypal/success?guest_id='.$guest_id.'&package_id='.$package_id.'&from_date='.$from_date.'&validity='.$validity;
		$cancelURL = base_url().'packagepaypal/cancel?guest_id='.$guest_id.'&package_id='.$package_id.'&from_date='.$from_date.'&validity='.$validity;
		$notifyURL = base_url().'packagepaypal/ipn?guest_id='.$guest_id.'&package_id='.$package_id.'&from_date='.$from_date.'&validity='.$validity.'&user_email='.$userdata["email"];
 
		// Add fields to paypal form
		$this->paypal_lib->add_field('return', $returnURL);
		$this->paypal_lib->add_field('cancel_return', $cancelURL);
		$this->paypal_lib->add_field('notify_url', $notifyURL);
		$this->paypal_lib->add_field('custom', $userdata['id']);
		$this->paypal_lib->add_field('item_number', $package_id);
		$this->paypal_lib->add_field('amount', $price);

		// Render paypal form
		$this->paypal_lib->paypal_auto_form();
	}
}
