<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once "vendor/autoload.php";
use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;

class Store extends Admin_Core_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model', 'user');
    }

    public function wallet()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_package'] = $this->setting_model->get_all_package_for_atp();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/wallet');
        $this->load->view('admin/include/footer');
    }

    function add_atp()
    {
        if ($this->user->add_atp()) 
        {
            $this->session->set_userdata('success', "ATP Successfully Added");
            redirect(base_url().'admin/atp/view_all_atp');
        } else {
            $this->session->set_userdata('error', " ATP Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function add_recharge_amount()
    {
        $recharge_id = date('Y').rand(1000,1000000);
        $app_setting = $this->setting_model->get_all_app_setting(); 
        // if(user()->user_atp_is_demo == 0)
        // {
        //     $amount_recharge = $this->input->post('amount');
        // }
        // else
        // {
        //     $amount_recharge = $app_setting->user_welcome_recharge;
        // }
        $amount_recharge = $this->input->post('amount');

        $this->session->set_userdata('rechage_id', $recharge_id);
        $this->session->set_userdata('amount_recharge', $amount_recharge);
        $payment = $this->setting_model->get_all_app_setting();
        //echo '<pre>';
        //print_r($payment);
        
        $api = new Api($payment->rozar_key_id, $payment->rozar_key_secret);
        $_SESSION['payable_amount'] = $this->input->post('amount');
        $razorpayOrder = $api->order->create(array(
            'receipt'         => $recharge_id,
            'amount'          => $this->input->post('amount') * 100, // 2000 rupees in paise
            'currency'        => 'INR',
            'payment_capture' => 1 // auto capture
        ));
        $amount = $razorpayOrder['amount'];
        $razorpayOrderId = $razorpayOrder['id'];
        $_SESSION['razorpay_order_id'] = $razorpayOrderId;
        $_SESSION['recharge_id'] = $recharge_id;
        $_SESSION['amount'] = $amount;
        $raz_pay_data = $this->rozr_pay_prepareData($amount,$razorpayOrderId, $recharge_id);
        $this->load->view('payment/rezorpay_wallet_recharge', array('data' => $raz_pay_data));
    }

    

    public function rozr_pay_prepareData($amount,$razorpayOrderId, $order_id)
    {
        $app_setting = $this->setting_model->get_all_app_setting(); 
        $data = array(
            "key" => $app_setting->rozar_key_id,
            "amount" => $amount,
            "name" => $order_id,
            "description" => $app_setting->app_name,
            "image" => base_url().$app_setting->app_sticky_logo,
            "prefill" => array(
                "name"  => user()->user_name,
                "email"  => user()->user_email,
                "contact" => user()->user_mobile,
            ),
            "notes"  => array(
                "address"  => "Hello World",
                "merchant_order_id" => rand(),
            ),
            "theme"  => array(
                "color"  => "#F37254"
            ),
            "order_id" => $razorpayOrderId,
        );
        return $data;
    }

    public function rozr_pay_verify()
    {

        $payment = $this->setting_model->get_all_app_setting();
        $order_id = $this->session->userdata('rechage_id');
        $amount_recharge = $_SESSION['amount'] / 100;
        $success = true;
        $error = "payment_failed";
        if (empty($_POST['razorpay_payment_id']) === false) {
            $api = new Api($payment->rozar_key_id, $payment->rozar_key_secret);
        try {
                $attributes = array(
                    'razorpay_order_id' => $_SESSION['razorpay_order_id'],
                    'razorpay_payment_id' => $_POST['razorpay_payment_id'],
                    'razorpay_signature' => $_POST['razorpay_signature']
                );
                $api->utility->verifyPaymentSignature($attributes);
            } catch(SignatureVerificationError $e) {
                $success = false;
                $error = 'Razorpay_Error : ' . $e->getMessage();
            }
        }
        if ($success === true) 
        {
            $app_setting = $this->setting_model->get_all_app_setting(); 

            $cus_id = user_id(); 
            $ord_id = $_SESSION['recharge_id'];


            $GST_Amount = ($amount_recharge * $app_setting->gst_percantage ) / 100;
            $ttl_amount = round($amount_recharge - $GST_Amount);

            // if(user()->user_atp_is_demo == 0)
            // {
            //     $balance = user()->user_wallet_balance + $ttl_amount;
            // }
            // else
            // {
            //     $balance = $ttl_amount;
            // }
            
            $balance = atp_details()->user_wallet_balance + $ttl_amount;

            $arrayName = array(
                'user_wallet_balance' => $balance,
                'user_atp_is_demo' => 0
            );
            $this->db->where('user_id', $cus_id);
            $this->db->update('users', $arrayName);

            wallet_recharge_sms(atp_details()->user_mobile, atp_details()->user_name, $amount_recharge, atp_details()->user_business_name, atp_details()->user_taluka, $balance);

             $data = array(
                'user_id' => $cus_id, 
                'payment_id' => $attributes['razorpay_payment_id'],
                'payment_status' => 'Credit',
                'payment_request_id' => $attributes['razorpay_order_id'], 
                'rechage_id' => $order_id, 
                'rechage_amount' => $amount_recharge, 
                'date' => date("Y-m-d h:i:s"),
                'payment_gateway' => 'Rozar Pay',
                'comment' => 'Wallet Recharge',
                'credit_amount' => $ttl_amount
            );
            $this->db->insert('wallet_recharge_details', $data);

            


            $this->session->set_userdata('success', 'Recharge Successfully Done');
            redirect(base_url().'admin/store/wallet');
        }
        else 
        {
            $this->session->set_userdata('error', 'Transaction Faild');
            redirect(base_url().'admin/store/wallet');
        }
    }

    function certificate_and_marksheet()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_order'] = $this->setting_model->get_all_order_by_atp_id();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/certificate_and_marksheet');
        $this->load->view('admin/include/footer');
    }

    function active_package($rand_id)
    {
        $user_id = user_id();

        $this->db->limit(1);
        $this->db->order_by('id', 'desc');
        $this->db->where('user_id', $user_id);
        $this->db->where('comment', "Wallet Recharge");
        $recharge_data = $this->db->get('wallet_recharge_details')->row();

        $result = (75/100) * $recharge_data->credit_amount;
        $main_per = $recharge_data->credit_amount - $result;

        if(atp_details()->user_wallet_balance > $result)
        {
            $this->session->set_userdata('error', 'Payment Faild. Please Use 75% Your Previous Wallet Balance.');
            redirect(base_url().'admin/store/wallet');
        }
        else
        {
            $recharge_id = date('Y').rand(1000,1000000);

            $package        = $this->setting_model->get_package_by_rand_code($rand_id);
            $app_setting    = $this->setting_model->get_all_app_setting(); 

            $tax_amt = ($package->package_price * $app_setting->gst_percantage) / 100;
            $main_pack_amt = $package->package_price + $tax_amt;
            //echo '<pre>';
            //print_r($payment);
            
            $_SESSION['payable_amount'] = $main_pack_amt;
            $_SESSION['pack_recharge_id'] = $recharge_id;
            $_SESSION['pack_id'] = $package->package_id;

            $api = new Api($app_setting->rozar_key_id, $app_setting->rozar_key_secret);
            $razorpayOrder = $api->order->create(array(
                'receipt'         => $recharge_id,
                'amount'          => $main_pack_amt * 100, // 2000 rupees in paise
                'currency'        => 'INR',
                'payment_capture' => 1 // auto capture
            ));
            $amount = $razorpayOrder['amount'];
            $razorpayOrderId = $razorpayOrder['id'];
            $_SESSION['razorpay_order_id'] = $razorpayOrderId;
            $_SESSION['pack_recharge_amount'] = $amount;
            $raz_pay_data = $this->rozr_pay_prepareData_for_pack_recharge($amount,$razorpayOrderId, $recharge_id);
            $this->load->view('payment/rezorpay_pack_recharge', array('data' => $raz_pay_data));
        }
    }

    public function rozr_pack_recharge_pay_verify()
    {
        $app_setting = $this->setting_model->get_all_app_setting();
        $order_id = $this->session->userdata('pack_recharge_id');
        $amount_recharge = $_SESSION['pack_recharge_amount'] / 100;
        $success = true;
        $error = "payment_failed";
        if (empty($_POST['razorpay_payment_id']) === false) 
        {
            $api = new Api($app_setting->rozar_key_id, $app_setting->rozar_key_secret);
        try {
                $attributes = array(
                    'razorpay_order_id' => $_SESSION['razorpay_order_id'],
                    'razorpay_payment_id' => $_POST['razorpay_payment_id'],
                    'razorpay_signature' => $_POST['razorpay_signature']
                );
                $api->utility->verifyPaymentSignature($attributes);
            } catch(SignatureVerificationError $e) {
                $success = false;
                echo $error = 'Razorpay_Error : ' . $e->getMessage();
            }
        }
        if ($success === true) 
        {
            $package = $this->setting_model->get_package_by_id($_SESSION['pack_id']);

            $cus_id     = user_id(); 
            $ord_id     = $_SESSION['pack_recharge_id'];
                        

            $arrayName = array(
                'user_wallet_balance'       => atp_details()->user_wallet_balance + $package->package_balance_amount,
                'user_atp_current_package'  => $package->package_id,
                'is_demo_package'           => 0
            );
            $this->db->where('user_id', $cus_id);
            $this->db->update('users', $arrayName);

            // wallet_recharge_sms(atp_details()->user_mobile, atp_details()->user_name, $amount_recharge, atp_details()->user_business_name, atp_details()->user_taluka, $arrayName['user_wallet_balance']);

             $data = array(
                'user_id'                   => $cus_id, 
                'payment_id'                => $attributes['razorpay_payment_id'],
                'payment_status'            => 'Credit',
                'payment_request_id'        => $attributes['razorpay_order_id'], 
                'rechage_id'                => $order_id, 
                'recharge_package_amount'   => $package->package_price,
                // 'recharge_gst'              => $app_setting->gst_percantage,
                'rechage_amount'            => $amount_recharge, 
                'date'                      => date("Y-m-d h:i:s"),
                'payment_gateway'           => 'Rozar Pay',
                'comment'                   => 'Wallet Recharge',
                'credit_amount'             => $package->package_balance_amount
            );
            $this->db->insert('wallet_recharge_details', $data);

            
            $this->session->set_userdata('success', 'Recharge Successfully Done');
            redirect(base_url().'admin/store/wallet');
        }
        else 
        {
            $this->session->set_userdata('error', 'Transaction Faild');
            redirect(base_url().'admin/store/wallet');
        }
    }

    public function rozr_pay_prepareData_for_pack_recharge($amount,$razorpayOrderId, $order_id)
    {
        $app_setting = $this->setting_model->get_all_app_setting(); 
        $data = array(
            "key" => $app_setting->rozar_key_id,
            "amount" => $amount,
            "name" => $order_id,
            "description" => $app_setting->app_name,
            "image" => base_url().$app_setting->app_sticky_logo,
            "prefill" => array(
                "name"  => atp_details()->user_name,
                "email"  => atp_details()->user_email,
                "contact" => atp_details()->user_mobile,
            ),
            "notes"  => array(
                "address"  => "Hello World",
                "merchant_order_id" => rand(),
            ),
            "theme"  => array(
                "color"  => "#F37254"
            ),
            "order_id" => $razorpayOrderId,
        );
        return $data;
    }


    //Affilate Start

    public function affilate()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_package'] = $this->setting_model->get_all_package_for_atp();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/affilate');
        $this->load->view('admin/include/footer');
    }

    //Affilate End

    public function about_us()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['about_us'] = $this->setting_model->get_about_us_by_atp_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/about_us');
        $this->load->view('admin/include/footer');
    }

    public function update_about_us()
    {
        if ($this->setting_model->update_about_us(user_id())) 
        {
            $this->session->set_userdata('success', "About Us Successfully Updated");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " About Us Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    public function all_portfolio()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_portfolio'] = $this->setting_model->get_all_portfolio_by_atp_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/all_portfolio');
        $this->load->view('admin/include/footer');
    }

    public function add_portfolio()
    {
        $check_count = $this->setting_model->get_all_portfolio_by_atp_id(user_id());
        if(count($check_count) > 5)
        {
            $this->session->set_userdata('error', "Portfolio Limit Over");
            redirect($this->agent->referrer());
        }
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/add_portfolio');
        $this->load->view('admin/include/footer');
    }

    public function portfolio_add()
    {
        if ($this->setting_model->add_new_portfolio()) 
        {
            $this->session->set_userdata('success', "Portfolio Successfully Added");
            redirect(base_url().'admin/store/all_portfolio');
        } else {
            $this->session->set_userdata('error', "Portfolio Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function edit_portfolio($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['portfolio'] = $this->setting_model->get_portfolio_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/edit_portfolio');
        $this->load->view('admin/include/footer');
    }

    public function update_portfolio($id)
    {
        if ($this->setting_model->update_portfolio($id)) 
        {
            $this->session->set_userdata('success', "Portfolio Successfully Updated");
            redirect(base_url().'admin/store/all_portfolio');
        } else {
            $this->session->set_userdata('error', "Portfolio Update Failed");
            redirect($this->agent->referrer());
        }
    }
    
    public function delete_portfolio($id)
    {
        if ($this->setting_model->delete_portfolio($id)) 
        {
            $this->session->set_userdata('success', "Portfolio Successfully Deleted");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Portfolio Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    public function all_faq()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_faq'] = $this->setting_model->get_all_faq_by_atp_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/all_faq');
        $this->load->view('admin/include/footer');
    }

    public function add_faq()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/add_faq');
        $this->load->view('admin/include/footer');
    }

    public function faq_add()
    {
        if ($this->setting_model->add_new_faq()) 
        {
            $this->session->set_userdata('success', "FAQ Successfully Added");
            redirect(base_url().'admin/store/all_faq');
        } else {
            $this->session->set_userdata('error', "FAQ Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function edit_faq($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['faq'] = $this->setting_model->get_faq_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/edit_faq');
        $this->load->view('admin/include/footer');
    }

    public function update_faq($id)
    {
        if ($this->setting_model->update_faq($id)) 
        {
            $this->session->set_userdata('success', "Faq Successfully Updated");
            redirect(base_url().'admin/store/all_faq');
        } else {
            $this->session->set_userdata('error', "Faq Update Failed");
            redirect($this->agent->referrer());
        }
    }

    public function delete_faq($id)
    {
        if ($this->setting_model->delete_faq($id)) 
        {
            $this->session->set_userdata('success', "FAQ Successfully Deleted");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "FAQ Delete Failed");
            redirect($this->agent->referrer());
        }
    }

    public function all_gallery()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_gallery'] = $this->setting_model->get_all_gallery_by_atp_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/all_gallery');
        $this->load->view('admin/include/footer');
    }

    public function add_gallery()
    {
        $check_count = $this->setting_model->get_all_gallery_by_atp_id(user_id());
        if(count($check_count) > 11)
        {
            $this->session->set_userdata('error', "Gallery Limit Over");
            redirect($this->agent->referrer());
        }
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/add_gallery');
        $this->load->view('admin/include/footer');
    }

    public function gallery_add()
    {
        if ($this->setting_model->add_new_gallery_image()) 
        {
            $this->session->set_userdata('success', "Image Successfully Added");
            redirect(base_url().'admin/store/all_gallery');
        } else {
            $this->session->set_userdata('error', "Image Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function edit_gallery_image($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['image'] = $this->setting_model->get_gallery_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/edit_gallery');
        $this->load->view('admin/include/footer');
    }

    public function gallery_update($id)
    {
        if ($this->setting_model->update_gallery_image($id)) 
        {
            $this->session->set_userdata('success', "Image Successfully Updated");
            redirect(base_url().'admin/store/all_gallery');
        } else {
            $this->session->set_userdata('error', "Image Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    public function delete_gallery($id)
    {
        if ($this->setting_model->delete_gallery_image($id)) 
        {
            $this->session->set_userdata('success', "Image Successfully Deleted");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Image Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    public function all_course()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_course'] = $this->course_model->get_all_course_for_admission_by_atp_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/all_courses');
        $this->load->view('admin/include/footer');
    }

    public function add_new_course()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/add_new_course');
        $this->load->view('admin/include/footer');
    }

    public function add_course()
    {
        if ($course_id = $this->setting_model->add_course()) 
        {
            $this->session->set_userdata('success', "Course Successfully Added");
            redirect(base_url().'admin/store/edit_course/'.$course_id);
        } else {
            $this->session->set_userdata('error', "Course Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function edit_course($id)
    {
        $this->load->model('course_model', 'course');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['course'] = $this->course_model->get_course_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/edit_course');
        $this->load->view('admin/include/footer');
    }

    public function update_course($id)
    {
        if ($this->setting_model->update_course($id)) 
        {
            $this->session->set_userdata('success', "Course Successfully Updated");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Course Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    public function course_delete($id)
    {
        if ($this->setting_model->course_delete($id)) 
        {
            $this->session->set_userdata('success', "Course Successfully Deleted");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Course Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    public function all_course_inquiry()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_inq'] = $this->setting_model->get_all_flagship_inquiry_by_atp_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/all_course_inquiry');
        $this->load->view('admin/include/footer');
    }

    public function website()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_inq'] = $this->setting_model->get_all_flagship_inquiry_by_atp_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/store/website');
        $this->load->view('admin/include/footer');
    }

    public function update_website_data()
    {
        if ($this->setting_model->update_website_data()) 
        {
            $this->session->set_userdata('success', "Data Successfully Updated");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Data Updated Failed");
            redirect($this->agent->referrer());
        }
    }

}
