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

class FlexiloadCtrl extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();

		isset($_SESSION['sec_rf_admin']) ? $_SESSION['sec_rf_admin'] == true ? null : redirect('/') : redirect('/');
	}

	public function index()
	{
	}

	public function load_fund_req()
	{
		$q = $this->db->query('SELECT * FROM `flexiload_fund_req` WHERE status=0 ORDER BY `created_at` DESC');
		$data['fund_reqsts'] = $q->result();


		$q1 = $this->db->query('SELECT * FROM `flexiload_fund_req` WHERE status=1 OR status=2 ORDER BY `created_at` DESC');
		$data['fund_reqsts_his'] = $q1->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/fund_request', $data); //4_dynamic

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

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

	public function fund_accepted()
	{
		$this->load->library('sms_send');

		$this->load->model('common_m');

		$package_amount = $this->input->post('package_amount');

		$flexiIncome = $this->input->post('income');

		$msg = "You have to successfully added flexiload blance " . $package_amount . "Tk from your flexi income " . $flexiIncome . "Tk please check your Mali-Mart Account Thanks. https://mali-mart.com/";

		$user_id = $this->input->post('user_id');

		$id = $this->input->post('id');

		$exist_balance = $this->common_m->getRow('flexiload_balance', 'blance', 'user_id', $user_id);

		$existflexiBlnc = $this->common_m->getRow('total_balance', 'flexiincome', 'user_id', $user_id);

		$user = $this->common_m->getRow('users_login', 'mobile_num', 'user_id', $user_id);

		$data['success'] = false;

		$this->db->trans_start();

		if ($exist_balance !== false) {
			$new_blance = $exist_balance->blance + $package_amount;

			$this->common_m->updateData('flexiload_balance', 'user_id', $user_id, array('blance' => $new_blance));
		} else {
			$this->common_m->insert('flexiload_balance', array('user_id' => $user_id, 'blance' => $package_amount));
		}

		if ($existflexiBlnc !== false) {
			$this->common_m->updateData('total_balance', 'user_id', $user_id, array('flexiIncome' => $existflexiBlnc->flexiincome + $flexiIncome));
		}

		$this->common_m->updateData('flexiload_fund_req', 'id', $id, array('status' => 1));

		$this->db->trans_complete();

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
		} else {
			$this->db->trans_commit();
			$this->sms_send->send_sms($user->mobile_num, $msg);
			$data['success'] = true;
			echo json_encode($data);
		}
	}

	public function fund_rejected()
	{
		$this->load->model('common_m');

		$id = $this->input->post('id');

		$result = $this->common_m->updateData('flexiload_fund_req', 'id', $id, array('status' => 2));

		if ($result !== false)
			echo json_encode(array('success' => true));
		else
			echo json_encode(array('success' => false));
	}
}
