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

class Admin_Controller extends MY_Controller {

    function __construct() {
        parent::__construct();
        $ip_info = ip_info("Visitor", "Country");
        if( $ip_info == 'Palestine' || $ip_info == 'Israel' ){
            if(isset($this->session->userdata('userData')->password) && $this->session->userdata('userLevel') <= 2) {
    			$dbPassword = $this->general_model->getTableWhere('users', 'password', $this->session->userdata('userData')->password);
                if(!isset($dbPassword[0]) || $dbPassword[0]->userId != $this->session->userdata('userData')->userId || $dbPassword[0]->userLevel > 2) {
                    redirect('login', 'refresh');
                }
                $this->db->cache_on();
                
                $customerData = $this->general_model->getRowById('customers', 'customerId', getAdminId());
                $this->data['customerData'] = $customerData;
                $userId = getUserId();
                $main_permessions = $this->db->query("SELECT * FROM `users_permissions_admins_main` WHERE `for_agent` = 0 ORDER BY `parentType` ASC")->result();
                $permissions = array();
                foreach( $main_permessions as $mPer ){
                    $user_permessions = $this->db->query("SELECT permission FROM `users_permissions_admins` WHERE `userId` = '$userId' AND `type` = '$mPer->type' AND `text` = '$mPer->text' ")->row();
                    if(!empty($user_permessions)){
                        $per = $user_permessions->permission;
                    }else{
                        $per = 0;
                    }
                    if( $mPer->parentType != '' ){
                        if(!is_array($permissions[$mPer->type][$mPer->parentType])){
                            $permissions[$mPer->type][$mPer->parentType] = array();
                            $current_text = $mPer->parentType;
                            $parent_user_permessions = $this->db->query("SELECT permission FROM `users_permissions_admins` WHERE `userId` = '$userId' AND `type` = '$mPer->type' AND `text` = '$current_text' ")->row();
                            if(!empty($parent_user_permessions)){
                                $new_per = $parent_user_permessions->permission;
                            }else{
                                $new_per = 0;
                            }
                            $permissions[$mPer->type][$mPer->parentType]['main'] = $new_per;
                        }
                        $permissions[$mPer->type][$mPer->parentType][$mPer->text] = $per;
                    }else{
                        $permissions[$mPer->type][$mPer->text] = $per;
                    }
                }
                $accounts = reindex_by_key($this->db->query("SELECT * FROM `accounts` WHERE `is_deleted` = 0 ")->result(), 'id');

                $this->template
                    ->set('template', 'adminMain')
    		        ->set('title', 'Cube Billing')
                    ->set('MainAccounts', $accounts)
                    ->set('permissions', $permissions);
    		} else {
                redirect('login/logout', 'refresh');		  
    		}
        }else{
            redirect('login/logout', 'refresh');
        }
	}
    
}