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

class PaymentGatewayController 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("session");

        $this->load->helper('url');
        ini_set('display_errors', 1);

        $this->load->model('SiteDataModel');
        // $this->SiteDataModel->insert_row();
    }


    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://d38x82mkkml5o1.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('my_stripe');
        $this->load->view('template/footer', $data);
    }

    public function cartPayment()
    {
        $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://d38x82mkkml5o1.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('cart_stripe');
        $this->load->view('template/footer', $data);
    }

    public function makePayment()
    {
        require_once('application/libraries/stripe-php/init.php');
        \Stripe\Stripe::setApiKey($this->config->item('stripe_secret'));

        \Stripe\Charge::create(
            [
                "amount" => 100 * 100,
                "currency" => "inr",
                "source" => $this->input->post('stripeToken'),
                "description" => "HKOW Photo.",
            ]
        );

        $this->session->set_flashdata('success', 'Payment is successful.');
        redirect('/payment-gateway', 'refresh');
    }
}