<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class LikezController extends Controller
{
    
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if(auth()->check()){

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

                $level = auth()->user()->level;
                if(!in_array($level, $allow)){
                    abort(505);
                }
            }
            else{
                return redirect('/login');
            }
            return $next($request);
        });
    }
    
    public function index()
    {
        return view('server.likeall');
    }
    
    public function data(){
        if(request()->jumlah > 250){
            \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
            return back();
        }
        
        $nowz = date('Y-m-d');
        $a = DB::table('users_history')->where('submitted_by', auth()->user()->username)->where('type', 'Like Server 2')->whereDate('created_at', $nowz)->count();
        $b = DB::table('users_history')->where('submitted_by', auth()->user()->username)->where('type', 'Like Server 3')->whereDate('created_at', $nowz)->count();
        $c = DB::table('users_history')->where('submitted_by', auth()->user()->username)->where('type', 'Like Server 4')->whereDate('created_at', $nowz)->count();
        $d = DB::table('users_history')->where('submitted_by', auth()->user()->username)->where('type', 'Like Server 5')->whereDate('created_at', $nowz)->count();
        
        $totalz = $a + $b + $c + $d;
        
        if($totalz > 50){
            return back();
        }
        
        $username = "ivanz";
    	$password = "kntolz32";
    	$links = request()->url;
    	
        
        // $this->proses("http://167.99.202.150/kuy/kntlzeaivan.php","urlxx={$links}&jumlahlahx={$jums}",$username,$password);
        
        // $this->proses("http://167.99.202.150/kuy/ivankntlz69.php","urlxx={$links}&jumlahlahx={$jums}",$username,$password);
        
            
            $key = 'hgfghfghfgf657656756sadhghsadg';
            $jml = request()->jumlah;
        
        $this->proses2('https://103.65.236.87/api2.php', 'api='.$key.'&action=add&link='.$links.'&type=2&quantity='.$jml);
        $this->proses2('https://146.71.85.78/api2.php', 'api='.$key.'&action=add&link='.$links.'&type=2&quantity='.$jml);
        $this->proses2('https://103.65.236.168/api2.php', 'api='.$key.'&action=add&link='.$links.'&type=2&quantity='.$jml);
        $this->proses2('https://146.71.85.79/api2.php', 'api='.$key.'&action=add&link='.$links.'&type=4&quantity='.$jml);
        
        // if($jml <= 60){
        //     $delay = 6;
        // }else if($jml <= 125){
        //     $delay = 12;
        // }else{
        //     $delay = 20;
        // }
        $delay = 30;
        $level = auth()->user()->level;
        if($level == 'admin'){
            $delay = 1;
        }
        
        // \App\Delay::where('user_id', auth()->user()->id)->update(['like_4' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 10 . ' minute'))]);
        \App\Delay::where('user_id', auth()->user()->id)->update(['like_1' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        \App\Delay::where('user_id', auth()->user()->id)->update(['like_6' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        // \App\Delay::where('user_id', auth()->user()->id)->update(['like_5' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        \App\Delay::where('user_id', auth()->user()->id)->update(['like_7' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        \App\Delay::where('user_id', auth()->user()->id)->update(['like_8' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        \App\Delay::where('user_id', auth()->user()->id)->update(['like_9' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        
        for($i = 2; $i < 6; $i++){
            
            // if($i == 1){
            //     $tot = 1500;
            // }else if($i == 2){
            //     $tot = 600;
            // }else if($i == 3){
            //     $tot = 600;
            // }else if($i == 4){
            //     $tot = 1500;
            // }else if($i == 5){
            //     $tot = 1000;
            // }else{
            //     $tot = 600;
            // }
            // if($i == 1){
            //     $tot = 1500;
            // }else($i == 2 || $i == 3 || $i == 4 || $i == 5){
            //     $tot = 600;
            // }
            
            $history = new \App\History;
            $history->submitted_by = auth()->user()->username;
            $history->target = request()->url;
            $history->type = 'Like Server ' . $i;
            $history->total_submit = $jml;
            
            $history->ip = $_SERVER['REMOTE_ADDR'];
            $history->save();
        }
        return back();
    }
    
    function proses($url,$params,$username,$password){
            
    	 $ch = curl_init();
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$username&password=$password");
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_COOKIEJAR, "-");
         curl_setopt($ch, CURLOPT_COOKIEFILE, "-");
         $result = curl_exec($ch);
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
         $result = curl_exec($ch);
         curl_close($ch);
    }
    
    function proses2($api, $postdata){
            
    	$ch = curl_init($api);
    		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    		
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            $result = curl_error($ch);
        }
				
        curl_close($ch);
            
    }
    
    public function autolike(){
        return view('server.autolike');
    }
    
    public function subauto(){
        $username = request()->username;
        $jumlah = request()->jumlah;
        $api = 'http://45.55.59.66/curl/autolekz.php';
        $postdata = 'username='.$username.'&jumlahnya='.$jumlah;
        
        $result = $this->proses2($api, $postdata);
        dd($result);
    }
    
}