<?php
	namespace App;

	use Illuminate\Http\Request;
	use \App\User;
	use Illuminate\Support\Facades\DB;

	class Helper {
		public static function getUserAktif() {
			if(auth()->check()){
				$allow = ['admin', 'member', 'reseller', 'priority'];
				$level = auth()->user()->level;
				if(!in_array($level, $allow)){
					return abort(404);
				}
				$user = User::where('expired_date', '>=', date('Y-m-d'))->count();
				return $user;
			} else {
				return abort(404);
			}
		}
		public static function panelName() {
			if(auth()->check()){
				$allow = ['admin', 'member', 'reseller', 'priority'];
				$level = auth()->user()->level;
				if(!in_array($level, $allow)){
					return abort(404);
				}
				$user = User::where('username', auth()->user()->register_by)->first();
				if($user !== NULL){
					if ($user->level == 'reseller') {
					    $con = mysqli_connect('localhost', 'instagr9_panel', 'T;y^54SN@!Sa', 'instagr9_panel');
				
        				$query = mysqli_query($con, "SELECT * FROM users_panel WHERE register_by='".auth()->user()->register_by."' ");
        				
        				$panels = mysqli_fetch_assoc($query);
        				return $panels['panelname'];
						
					}else{
					    return 'IGPanel';
					}
					   // return 'IGPanel';
						
				// 	}
				// 	 else {
				// 		$text = '';
				// 		switch ($user->username) {
				// 			case 'alv':
				// 				$text = ALV;
				// 				break;
				// 			case 'aydapanel':
				// 				$text = AYDA;
				// 				break;
				// 			case 'ifato':
				// 				$text = IFATO;
				// 				break;
				// 			case 'dian':
				// 				$text = DIAN;
				// 				break;
				// 			default:
				// 				$text = IGPanel;
				// 				break;
				// 		}
				// 		return $text;
				// 	}
				
				
				}
			} else {
				return abort(404);
			}
		}
		
		public static function barColor() {
			$co = rand(1,5);
			switch ($co) {
			    case 1:
			        return '#556eca';
			        break;
			    case 2:
			        return '#f7636f';
			        break;
			    case 3:
			        return '#02a1d5';
			        break;
			    case 4:    
			        return '#fc943e';
			        break;
			    case 5:    
			        return '#47ba70';
			        break;
			    default:
			        return '#556eca';
			        break;
			     
			}
			
		}

		public static function isUserRegisteredByAdmin() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->level == 'admin' || $user->level == 'media') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

		public static function isUserRegisteredByAlv() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'alv') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

		public static function isUserRegisteredByAyda() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'aydapanel') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

		public static function isUserRegisteredByIfato() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'ifato') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}
		public static function isUserRegisteredByDian() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'dian') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

    public static function subtractSaldo($id, $price){
			if(auth()->check()){
				Helper::checkLevel(['admin','reseller','member', 'priority']);
				DB::table('users')->where('id', '=', $id)->decrement('saldo', $price);
			} else {
				return false;
			}
    }

    public static function addSaldo($id, $price){
			if(auth()->check()){
				Helper::checkLevel(['admin','reseller','member', 'priority']);
				DB::table('users')->where('id', '=', $id)->increment('saldo', $price);
			} else {
				return false;
			}
    }

		public static function saveUserBalanceHistory($id, $price, $type, $note, $status){
		    $uniqueid_prefix = $id.rand(0,9).rand(0,9);
		    $uniqueid = uniqid($uniqueid_prefix, FALSE);
        DB::table('users_balance')->insert([
            'id_user' => $id,
            'code' => $uniqueid,
            'value' => $price,
            'type' => $type,
            'note' => $note,
            'status' => $status,
            'updated_at' => date('Y-m-d H:i:s'),
            'created_at' => date('Y-m-d H:i:s')
        ]);
    }

		public static function checkLevel($allow){
	      $level = auth()->user()->level;
	      if(!in_array($level, $allow)){
	          return abort(505);
	      }
		}
	}
