<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');


class FlexiloadCtrl extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('Common_m');
		$this->load->helper(array('form', 'url'));
	}

	private function pin_check($pin_number)
	{
		$user_id = $_SESSION['user_id'];

		$query = $this->db->query("SELECT activation_pin from users_detail WHERE id=$user_id AND activation_pin='$pin_number'");

		return count($query->result()) > 0 ? true : false;
	}

	public function buy_package()
	{
		chk_login();
		$data['packages'] = $this->Common_m->select_with_where('flexiload_package', '*', 'status=1');

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('flexiload/buy_package.php', $data); //4_dynamic

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}

	public function singleLoadForm()
	{
		chk_login();

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('flexiload/single_flexiload.php'); //4_dynamic

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}

	public function flexiload_store()
	{

		$data['amount'] = $this->input->post('amount');

		if ($this->pin_check($this->input->post('pin_number')) === false) {
			$this->session->set_flashdata(array("type" => "danger", "api_response" => "your pin code is incorrect"));
			echo $this->singleLoadForm();
			return false;
		}

		$user_id = $_SESSION['user_id'];

		$load_blance = blance_check();

		if ($load_blance < $data['amount']) {

			$this->session->set_flashdata(array("type" => "danger", "api_response" => "flexiload blance is Insufficient!!"));

			echo $this->singleLoadForm();
			return false;
		}

		$this->load->library('loadapi');

		$data['phone_number'] = $this->input->post('phone_number');

		$data['remarks'] = $this->input->post('remarks');

		$data['operator'] = $this->input->post('operator');

		$data['number_type'] = $this->input->post('number_type');

		$api_info = $this->loadapi->load_api($data['phone_number'], $data['number_type'], $data['operator'], $data['amount']);

		
		$response = json_decode($api_info);
		
		
	print_r(json_decode($api_info));


		if ($response->pin == "445900") {
			$data['transaction_id'] = $response->load_id;
			$data['user_id'] = $user_id;
			$data['status'] = 1;

			$this->db->trans_start();

			$this->db->set($data);
			$this->db->insert('load_transaction');

			$this->db->set('blance', $load_blance - $data['amount']);
			$this->db->where('user_id', $user_id);
			$this->db->update('flexiload_balance');

			$this->db->trans_complete();

			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();

				$this->session->set_flashdata(array("type" => "danger", "api_response" => "something went wrong / server error"));
				redirect('FlexiloadCtrl/singleLoadForm');
			} else {
				$this->db->trans_commit();

				$this->session->set_flashdata(array("type" => "success", "api_response" => $response->msg));
				redirect('FlexiloadCtrl/singleLoadForm');
			}
		} else {
			$this->session->set_flashdata(array("type" => "danger", "api_response" => $response->msg));
			echo $this->singleLoadForm();
			return false;
		}
	}

	public function blance()
	{
		chk_login();
		$user_id = $_SESSION['user_id'];


		$data['sender_amount'] = $this->db->query("SELECT sum(amount) as total FROM load_blance_transfer where sender_id=$user_id")->row();
		$data['receiver_amount'] = $this->db->query("SELECT sum(amount) as total FROM load_blance_transfer where receiver_id=$user_id")->row();
		$data['total_transaction'] = $this->db->query("SELECT sum(amount) as total_transaction FROM load_transaction where user_id=$user_id")->row();
		$q = $this->db->query("SELECT sum(package_amount) as total_load FROM flexiload_fund_req as fr  where fr.status=1 AND user_id=$user_id");
		$data['total_package_amount'] = $q->row();

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('flexiload/blance.php', $data); //4_dynamic

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}
	public function blance_transfer()
	{
		chk_login();
		$user_id = $_SESSION['user_id'];

		$q = $this->db->query("select u.user_id,u.mobile_num from users_detail INNER JOIN users_login as u ON users_detail.id=u.user_id where users_detail.active_status=1 AND u.user_id!=$user_id");
		$data['users'] = $q->result();

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('flexiload/blance_transfer.php', $data); //4_dynamic

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}

	public function blance_transfer_store()
	{

		$this->load->library('form_validation');
		$this->load->library('sms_send');

		$data = $this->input->post();

		if ($this->pin_check($data['pin_code']) === false) {

			$this->session->set_flashdata(array("type" => "danger", "message" => "your pin code is incorrect !!"));
			echo $this->blance_transfer();
			return false;
		}

		if (blance_check() < $data['amount']) {

			$this->session->set_flashdata(array("type" => "danger", "message" => "flexiload blance is Insufficient!!"));
			echo $this->blance_transfer();
			return false;
		}

		$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
		$this->form_validation->set_rules('receiver_id', 'Receiver Member', 'required');
		$this->form_validation->set_rules('amount', 'Transfer Amount', 'required');

		if ($this->form_validation->run() == false) {
			echo $this->blance_transfer();

			return false;
		} else {
			$exist_blance = $this->Common_m->getRow('flexiload_balance', 'blance', 'user_id', $data['receiver_id']);
			$receive_user = $this->Common_m->getRow('users_login', 'mobile_num', 'user_id', $data['receiver_id']);

			unset($data['pin_code']);
			unset($data['remaining_amount']);
			$data['sender_id'] = $_SESSION['user_id'];

			$this->db->trans_start();
			if ($exist_blance === false) {
				$this->Common_m->insert('flexiload_balance', array('user_id' => $data['receiver_id'], 'blance' => $data['amount']));
			} else {
				$new_blance = $exist_blance->blance + $data['amount'];
				$this->Common_m->updateData('flexiload_balance', 'user_id', $data['receiver_id'], array('blance' => $new_blance));
			}
			$this->Common_m->insert('load_blance_transfer', $data);
			$this->Common_m->updateData('flexiload_balance', 'user_id', $_SESSION['user_id'], array('blance' => blance_check() - $data['amount']));
			$this->db->trans_complete();

			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();
				$this->session->set_flashdata(array("type" => "danger", "message" => "Sometimes Server error please try again !!"));
			} else {
				$this->db->trans_commit();
				$msg = "Dear Merchant your blance added Successfully " . number_format($data['amount'], 2) . " Tk.please check your flexiload blance.  Thank You for Mali-Mart";
				$this->sms_send->send_sms($receive_user->mobile_num, $msg);
				$this->session->set_flashdata(array("type" => "success", "message" => "Blance Transfer Successfully done"));
			}
			redirect('FlexiloadCtrl/blance_transfer');
		}
	}

	public function flexiload_fund_req()
	{
		chk_login();

		$id = $this->input->get('id');
		if (isset($id)) {
			$data['package'] = $this->Common_m->select_with_where("flexiload_package", 'commission,id,package_amount,total_commission', "id='$id' and status=1");
			if ($data['package'] !== null) {
				$data['package'] = $data['package'][0];

				$this->load->view('templates/1_head.php');

				$this->load->view('templates/2_nav.php');

				$this->load->view('templates/3_sidebar_menu_left.php');

				$this->load->view('flexiload/add_fund', $data); //4_dynamic

				$this->load->view('templates/5_footer.php');

				$this->load->view('templates/6_script_end.php');
			} else {
				redirect('/VendorCtrl/products', 'refresh');
			}
		}
	}
	public function fund_req_store()
	{
		chk_login();

		$this->load->library('form_validation');

		foreach ($_POST as $key => $value) {
			$this->form_validation->set_rules($key, $key, 'required');
		}
		if (!empty($_FILES["bank_slip"]["name"])) {
			$tmp_name = $_FILES["bank_slip"]["tmp_name"];
			$name = $_FILES["bank_slip"]["name"];
			move_uploaded_file($tmp_name, 'uploads/payment_slip/' . $name);
			$_POST['bank_slip'] = $name;
		}

		$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');

		$data = array('success' => false, 'messages' => array());

		if ($this->form_validation->run() == true) {
			$data['success'] = true;
			$this->Common_m->insert('flexiload_fund_req', $this->input->post());
		} else {
			foreach ($_POST as $key => $value) {
				$data['messages'][$key] = form_error($key);
			}
		}
		echo json_encode($data);
	}

	public function payment_type()
	{
		$data['payment'] = $this->Common_m->select_all('flexiload_payment_info', '*');

		$data['payment'] = $data['payment'][0];

		echo json_encode($data['payment']);
	}
}
