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

class Admin extends Admin_Core_Controller {

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

	public function dashboard() {
		$data['title'] = 'Home';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$all_enq = $this->user->get_all_enq_by_atp_id_and_month();
		$all_admission = $this->user->get_all_admission_by_atp_id_and_month();
		$all_income = $this->user->get_all_income_by_atp_id_and_month();
		$all_expance = $this->user->get_all_expance_by_atp_id_and_month();

		$income = [];
		foreach ($all_income as $key => $income_d) 
		{
			if($income_d->fee_student_course_main_price)
			{
				$in = $income_d->fee_student_course_main_price;
			}
			else
			{
				$in = 0;
			}
			$income[] = $in;
		}

		$expance = [];
		foreach ($all_expance as $key => $expance_d) 
		{
			if($expance_d->expense_amount)
			{
				$ex = $expance_d->expense_amount;
			}
			else
			{
				$ex = 0;
			}
			$expance[] = $ex;
		}

		$data['all_enq'] = "'" . implode ( "', '", $all_enq ) . "'";
		$data['all_admission'] = "'" . implode ( "', '", $all_admission ) . "'";
		$data['all_income'] = "'" . implode ( "', '", $income ) . "'";
		$data['all_expance'] = "'" . implode ( "', '", $expance ) . "'";
		$this->load->view('admin/include/header', $data);
		$this->load->view('admin/main');
		$this->load->view('admin/include/footer');
	}

	function lead_management()
	{
		// if(check_user_permission('lead_management'))
		// {
			$data['title'] = 'Lead Management';
			$data['app_setting'] 	= $this->setting_model->get_all_app_setting();	
			$data['all_data'] 		= $this->setting_model->get_all_contact_details();	
			$this->load->view('admin/include/header', $data);
			$this->load->view('admin/lead_management');
			$this->load->view('admin/include/footer');
		// }
		// else
		// {
		// 	$this->session->set_userdata('error', "You Dont Permission to Access This Page ");
		// 	redirect(base_url().'admin/admin');
		// }
	}

	function change_lead_mgmt_status($status, $id)
	{
		if($id)
		{
			$this->db->where('lead_id', $id);
			$data = $this->db->update('lead_management', ['lead_status' => $status]);
			echo $data;
		}
	}


	public function check_mobile_no($mobile) {
		$this->db->select('user_id');
		$this->db->where('user_mobile', $mobile);
		$data = $this->db->get('users')->row();
		if ($data) {
			$arrayName = array(
				'msg' => '<span class="badge badge-danger">Mobile No Already Exits.</span>',
				'status' => 6,
			);
			echo json_encode($arrayName);
		} else {
			$arrayName = array(
				'msg' => '<span class="badge badge-success">Mobile No Available.</span>',
				'status' => 5,
			);
			echo json_encode($arrayName);
		}
	}

	public function check_email_id() {
		$email = $this->input->post('email');
		$this->db->select('user_id');
		$this->db->where('user_email', $email);
		$data = $this->db->get('users')->row();
		if ($data) {
			$arrayName = array(
				'msg' => '<span class="badge badge-danger">Email ID Already Exits.</span>',
				'status' => 6,
			);
			echo json_encode($arrayName);
		} else {
			$arrayName = array(
				'msg' => '<span class="badge badge-success">Email ID Available.</span>',
				'status' => 5,
			);
			echo json_encode($arrayName);
		}
	}

	public function get_aadress_detaile_by_pin_code($id) {
		if ($id) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, 'https://api.postalpincode.in/pincode/' . $id);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			$response = curl_exec($ch);
			$result = json_decode($response);
			curl_close($ch);

			// echo '<pre>';
			// print_r(json_decode($response));

			$area = '';
			foreach ($result[0]->PostOffice as $key => $value) {
				$area .= '<option value="' . $value->Name . '">' . $value->Name . '</option>';
			}

			$block = [];
			foreach ($result[0]->PostOffice as $key => $value_2) {
				$block[] = $value_2->Block;
			}

			$main_b_data = array_unique($block);

			$data_b = '';
			foreach ($main_b_data as $b_data) {
			    // Process each unique value
			    $data_b .= '<option value="' . $b_data . '">' . $b_data . '</option>';
			}

			$output = [
				'city' => $result[0]->PostOffice[0]->District,
				'state' => $state = $result[0]->PostOffice[0]->State,
				'area' => $area,
				'block' => $data_b,
			];

		}
		echo json_encode($output);
	}

	function login($id) {
		$this->load->model('auth_model');
		$pw = $this->auth_model->direct_login_by_id($id);
		if (!$pw) {
			$error = 'Password is wrong!';
			$this->session->set_userdata('error', $error);
			redirect(base_url() . 'admin');
		} else {
			$rand = rand();
			$user = array(
				'sess_id' => $pw['user_id'],
				'sess_mobile' => $pw['user_mobile'],
				'sess_email' => $pw['user_email'],
				'sess_role' => $pw['user_role'],
				'sess_status' => $pw['user_status'],
				'sess_logged_in' => true,
				'sess_token' => rand(),
				'sess_app_id' => $this->config->item('app_key'),
			);
			$this->auth_model->active_token($pw['user_id'], $user['sess_token']);
			$this->session->set_userdata($user);
			redirect(base_url() . 'admin/admin/dashboard');
		}
	}
	
}
