<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;
use DB;
use \App\Rules\Count;

class CobaController extends Controller
{
    public function cekdata(){
        $postdata = 'api=2019gantidiaivanbangsatbocahtolol&action=add&link=https://www.instagram.com/p/BuAdhR5gBtA/&type=112&quantity=200';
        $ch = curl_init('https://followers-gratis.com/api.php');
            
        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)');
        $results = curl_exec($ch);
        if (curl_errno($ch)) {
            $results = curl_error($ch);
        }
                
        curl_close($ch);
        
        $order = json_encode($results);
        $ambil= json_decode($results,true);
        dd($ambil);
        
    }
    public function index(){
    
        return view('server.darurat');
        
    }
    
    public function doar(){
//         // Get the current URL without the query string...
// echo url()->current();

// // Get the current URL including the query string...
// echo url()->full();

// // Get the full URL for the previous request...
// echo url()->previous();
$postdata = 'api=2019gantidiaivanbangsatbocahtolol&action=add&link=https://www.instagram.com/p/BuAdhR5gBtA/&type=112&quantity=1';
        $ch = curl_init('https://followers-gratis.com/api.php');
            
        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)');
        $results = curl_exec($ch);
        if (curl_errno($ch)) {
            $results = curl_error($ch);
        }
                
        curl_close($ch);
        
        $order = json_encode($results);
        $ambil= json_decode($results,true);
        dd($ambil);
    }
    
    public function subdarurat(){
        $api_link = "https://justanotherpanel.com/api/v2";
        $order_postdata = "key=53155f07429d66115dd199a5fda0769c&action=add&service=450&link=".request()->url."&quantity=".request()->jumlah;
        
        $cekhisto = \App\History::where('submitted_by', auth()->user()->username)->where('type','Server Like Darurat')->whereDate('created_at', '=', Carbon::today()->toDateString())->first();
        
        if($cekhisto != null){
            if($cekhisto->total_submit + request()->jumlah > 1000){
                $pesan = "Maaf, Jumlah yang anda masukkan melebihin limit hari ini !";
            }else if($cekhisto->total_submit + request()->jumlah ==  1000){
                \App\Delay::where('user_id', auth()->user()->id)->update(['like_12' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
                $pesan = "Silahkan Gunakan Server Like Darurat Besok !";
                
            }else{
                $this->jap($api_link, $order_postdata);
                $cekhisto->total_submit = $cekhisto->total_submit + request()->jumlah;
                $cekhisto->save();
                
                $pesan = "Sukses Submit Like !";
            }
            
        }else{
            $this->jap($api_link, $order_postdata);
            
            $history = new \App\History;
            $history->submitted_by = auth()->user()->username;
            $history->target = request()->url;
            $history->type = 'Server Like Darurat';
            $history->total_submit = request()->jumlah;
            
            $history->ip = $_SERVER['REMOTE_ADDR'];
            $history->save();
            $pesan = "Sukses Submit Like !";
        }
        
        session()->flash('status', $pesan);
        return redirect()->back();
    }
    
    public function jap($api_link, $order_postdata){
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api_link);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $order_postdata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $chresult = curl_exec($ch);
        curl_close($ch);
        $order_data = json_decode($chresult, true);
    }
    
    public function dor(){
        // $data['username'] = \Auth::User()->username;
        // $data['subserver'] = \App\History::where('submitted_by', \Auth::User()->username)
        //                                     ->whereDate('created_at', '=', date('Y-m-d'))->count();
        // $a = \App\History::where('submitted_by', \Auth::User()->username)
        //                                     ->where('type', 'Like Server 1')
        //                                     ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
                                            
        // $b = \App\History::where('submitted_by', \Auth::User()->username)
        //                                     ->where('type', 'Like Server 2')
        //                                     ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
        
        // $data['sublike'] = $a+$b;
        
        // echo $data['subserver'] . '<br>';
        // echo $data['sublike'];
        
        // MEDIA LIKE
        
// $url = 'https://www.instagram.com/p/Bq4qTc5HtFV/?utm_source=ig_share_sheet&igshid=1pz2755dly99y'; // ubah dengan url photo ig lu

// $c=curl_init('https://api.instagram.com/oembed/?url='.trim($url));
// curl_setopt($c,CURLOPT_RETURNTRANSFER,1); 
// curl_setopt($c,CURLOPT_SSL_VERIFYPEER,0);
// curl_setopt($c,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0");
// $x=curl_exec($c);
// $mama=curl_getinfo($c);
// curl_close($c);
// preg_match('#provider_url#',$x);
// $obj = json_decode($x,true);
// $media = $obj['media_id'];
// $authid = $obj['author_id'];

// $cc=curl_init($url);
// curl_setopt($cc,CURLOPT_RETURNTRANSFER,1);
// curl_setopt($cc,CURLOPT_SSL_VERIFYPEER,0);
// curl_setopt($cc,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0");
// $x1=curl_exec($cc);
// $mama1=curl_getinfo($cc);
// curl_close($cc);
// preg_match('/\"edge_media_preview_like\"\:\s?\{\"count\"\:\s?([0-9]+)/',$x1,$l);
// echo $l[1];
// echo 'y';

        // $postdata = 'api=2019gantidiaivanbangsatbocahtolol&action=add&link=https://www.instagram.com/p/BuAdhR5gBtA/&type=120&quantity=200';
        // $result = $this->ubai($postdata);
        // $order = json_encode($result);
        // $ambil= json_decode($result,true);
        $id = 2;
        $user_id = auth()->user()->id;
        $cek = DB::table('subserver')->where('user_id', $user_id)->where('date', date('Y-m-d'))->get();
        $data = DB::table('subserver')->where('user_id', $user_id)->where('date', date('Y-m-d'))->first();
        if(count($cek) == 0){
          if($id == 1){
            $ins = DB::table('subserver')
              ->insert([
                'user_id' => $user_id,
                'server1' => 1,
                'server2' => 0,
                'date' => date('Y-m-d')
              ]);
          }else{
            $ins = DB::table('subserver')
              ->insert([
                'user_id' => $user_id,
                'server1' => 0,
                'server2' => 1,
                'date' => date('Y-m-d')
              ]);
          }
        }else{
          if($id == 1){
            $ins = DB::table('subserver')
              ->where('user_id', $user_id)
              ->where('date', date('Y-m-d'))
              ->update([
                'server1' => $data->server1 + 1,
              ]);
          }else{
            $ins = DB::table('subserver')
              ->where('user_id', $user_id)
              ->where('date', date('Y-m-d'))
              ->update([
                'server2' => $data->server2 + 1,
              ]);
          }
        }


    }
    
    public function ubai($postdata){
        
        
        $ch = curl_init('https://followers-gratis.com/api.php');
            
        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)');
        $results = curl_exec($ch);
        if (curl_errno($ch)) {
            $results = curl_error($ch);
        }
                
        curl_close($ch);
        
        
        
       
    }
    
    public function limit(){
        $dbx = DB::table('users')
                ->join('users_history', 'users.username', '=', 'users_history.submitted_by')
                ->where('status', 'aktif')->whereDate('expired_date', '>=', date('Y-m-d'))
                ->where('submitted_by', 'fardaalan')
                ->first();
        dd($dbx);
        return view('limit');
    }
    
}