<?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 Branch extends Admin_Core_Controller {

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

    public function view_all_branch()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_atp'] = $this->user->get_all_atp_for_admin();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/atp/view_all_atp');
        $this->load->view('admin/include/footer');
    }

    function add_new_branch()
    {
        $this->load->model('course_model', 'course');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        // $data['all_programm'] = $this->course->get_all_course_programm();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/atp/add_new_atp');
        $this->load->view('admin/include/footer');
    }

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

    function edit_branch($id)
    {
        $this->load->model('course_model', 'course');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['atp'] = $this->user->get_user_by_id($id);
        // $data['all_programm'] = $this->course->get_all_course_programm();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/atp/edit_atp');
        $this->load->view('admin/include/footer');
    }

    function update_branch($id)
    {
        if ($this->user->update_atp($id)) 
        {
            $this->session->set_userdata('success', "Branch Successfully Updated");
            redirect(base_url().'admin/branch/view_all_branch');
        } else {
            $this->session->set_userdata('error', " Branch Update Failed");
            redirect($this->agent->referrer());
        }
    }

    function change_status($id, $status)
    {
        if ($this->user->change_status($id, $status)) 
        {
            $this->session->set_userdata('success', "Branch Status Successfully Updated");
            redirect(base_url().'admin/branch/view_all_branch');
        } else {
            $this->session->set_userdata('error', " Branch Status Update Failed");
            redirect($this->agent->referrer());
        }
    }

    function add_manual_money()
    {
        if ($this->user->add_manual_money()) 
        {
            $this->session->set_userdata('success', "Money Successfully Send");
            redirect(base_url().'admin/branch/view_all_atp');
        } else {
            $this->session->set_userdata('error', "Money Sending Failed");
            redirect($this->agent->referrer());
        }
    }

    public function pay_renewal_fee()
    {
        $recharge_id = date('Y').rand(1000,1000000);
        $amount_recharge = user()->user_rew_fee;
        $this->session->set_userdata('renewal_id', $recharge_id);
        $this->session->set_userdata('amount_renewal', $amount_recharge);
        $payment = $this->setting_model->get_all_app_setting();
        //echo '<pre>';
        //print_r($payment);
        $app_setting = $this->setting_model->get_all_app_setting(); 
        $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'          => $amount_recharge * 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/pay_renewal_fee', 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;
    }

    function pay_atp_renewal_fee()
    {
        $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) 
        {
            $cus_id = user()->user_id; 
            $ren_date = date("Y-m-d", strtotime(date("Y-m-d", strtotime(user()->user_rew_date)) . " + 365 day"));
            $this->db->where('user_id', $cus_id);
            $this->db->update('users', ['user_rew_date' => $ren_date]);
            
            $this->session->set_userdata('success', 'Payment Successfully Complete. Renewal date Now Updated');
            redirect(base_url().'admin/admin/dashboard');
        }
        else 
        {
            $this->session->set_userdata('error', 'Transaction Faild');
            redirect(base_url().'admin/admin/dashboard');
        }
    }

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

    public function approve_kyc($id)
    {
        if ($this->user->approve_kyc($id)) 
        {
            $this->session->set_userdata('success', "KYC Successfully Approved");
            redirect(base_url().'admin/branch/view_all_branch');
        } else {
            $this->session->set_userdata('error', "Money Approved Failed");
            redirect($this->agent->referrer());
        }
    }

    public function reject_kyc($id)
    {
        if ($this->user->reject_kyc($id)) 
        {
            $this->session->set_userdata('success', "KYC Successfully Reject");
            redirect(base_url().'admin/atp/view_all_atp');
        } else {
            $this->session->set_userdata('error', "Money Rejected Failed");
            redirect($this->agent->referrer());
        }
    }

    public function update_video_status()
    {
        $user_id = user_id();

        $this->db->where('user_id', $user_id);
        echo $this->db->update('users', ['user_is_video_complete' => 1]);
    }

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

    

}
