<?php

date_default_timezone_set("Asia/Bangkok");
function get_dataa($url) {
  // $ch = curl_init();
  // curl_setopt($ch, CURLOPT_URL, $url);
  // curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36");
  // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  // curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  // // curl_setopt($ch, CURLOPT_COOKIE, '__cfduid=d0bdc625d1c4b0786f8da66410eae7bd21580287293; ruayz=1nrkl89l3tc76tbm8sqep3c083auijar'); 
  // $response = curl_exec($ch);
  // curl_close($ch);


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://www.ruay.org/member/result/all',
  // CURLOPT_URL => 'https://n8n.at-mybot.me/webhook/ruay-api',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Cookie: _ga=GA1.2.1813037619.1757401665; _gid=GA1.2.880462370.1757401665; _tt_enable_cookie=1; _ttp=01K4PMTK747DBKJHEV2EA02PHW_.tt.1; csrf_cookie=e62c60d33694e131aaa3879381fa761d; ruayz=23di3i8tps2h1ohbopkj6ssn3shg38q5; alertnews14052025=disable; _gat_gtag_UA_157160543_1=1; ttcsid=1757401664743::jn9d--IC_PyodoBbDnoO.1.1757402839508; ttcsid_CE29VOBC77UFHEHJIH7G=1757401664742::W6RqALkQUsOY26jDwytj.1.1757402839755'
  ),
));

$response = curl_exec($curl);

curl_close($curl);


  return $response;
}

function remove_empty($array) {
  return array_filter($array, '_remove_empty_internal');
}

function _remove_empty_internal($value) {
  return !empty($value) || $value === 0;
}

function Get_ruay($loop,$variable){
      $loop_time            = file_get_contents('system/loop_time_ruay.json');
      $loop_time_json       = json_decode($loop_time, true);

      $start      =  strpos($variable,'จับยี่กี VIP - รอบที่ '.$loop);
      $start_cut  =  substr($variable,$start);

      $stop       =  strpos($start_cut,'<div class="carousel-item ">');
      $sub        =  substr($start_cut,0,$stop); 
	  $result           = [];
      //Remore HTML
      $FilterHTML = strip_tags(trim($sub));
      $data       = explode("\n",$FilterHTML);
      $trimmedArray = array_map('trim', $data);
      $filterData = [];
      foreach ($trimmedArray as $key => $value) {
        if(trim($value) != ''){
          array_push($filterData,$value);
        }
      }
      $result['loop'] = trim($filterData[0]);
      $result['top'] = trim($filterData[2]);
      $result['button'] = trim($filterData[4]);
      $result['time'] = date('Y-m-d H:i:s');
      $result['status'] = true;
      $jsonloop["lottery_loop_name"]       = $loop_time_json[$loop];
      $jsonloop["lottery_loop_time"]       = $loop;
      $jsonloop["lottery_loop_3unit"]      = trim($result['top']);
      $jsonloop["lottery_loop_2unit"]      = trim($result['button']);
      $jsonloop["lottery_loop_3unit_vip"]  = 'xxx';
      $jsonloop["lottery_loop_2unit_vip"]  = 'xx';
      $jsonloop["lottery_loop_3unit_spc"]  = 'xxx';
      $jsonloop["lottery_loop_2unit_spc"]  = 'xx';
	return $jsonloop;
}    


function Get_loop($loop) {
   # code...
  $loopid = $loop;
  $start = 1;
  $end = 1;
  if($loopid >= 88):
    $end = $loopid;
    $start = $loopid - 87;
  endif;
    $end = $loopid;
  return ['start'=>$start , 'end' => $end];
}

function Build_Json($loop) {
    $url = 'https://www.ruay.org/login';
    // $url='https://n8n.at-mybot.me/webhook/ruay-api';
    $variable =  get_dataa($url);
    // print_r($variable); 
    $start      =  strpos($variable,'<div class="card border-secondary text-center mb-2">');
    $start_cut  =  substr($variable,$start);
    $stop       = strpos($start_cut,'<!-- end carousel-inner -->');
    $sub        = substr($start_cut,0,$stop); 
    $item                 = Get_loop($loop);
    $jsonloop             = [];
    $loop_time            = file_get_contents('system/loop_time_ruay.json');
    $loop_time_json       = json_decode($loop_time, true);
    for($i = $item['start'];$i<= $item['end'] ;$i++){
    	$result               = Get_ruay(($i),$sub);// ต่อตรงนี้
    	array_push($jsonloop,$result);
    }
    return ['jsonloop'          => $jsonloop];
}

$loop = null;
$Itemloop = $_GET['loop'];
if(isset($Itemloop) and $Itemloop != ''){
  $res = Build_Json($Itemloop);
}
echo json_encode($res['jsonloop']);


