<?php
require '../../data.php';

  $findday = "SELECT * FROM prediksi_togel WHERE DATE(tanggal_prediksi) = '$todaydate'";
  $qfindday = mysqli_query($data, $findday);

  function bbfsNum($number, $length, $permutation = '', $used = array(), &$result = array()) {
    if (strlen($permutation) === $length) {
        $result[] = $permutation;
        return;
    }
  
    for ($i = 0; $i < strlen($number); $i++) {
        if (!in_array($i, $used)) {
            $used[] = $i;
            bbfsNum($number, $length, $permutation . $number[$i], $used, $result);
            array_pop($used);
        }
    }
  
  }

    if(mysqli_num_rows($qfindday) == 0) {
        $spas = "SELECT * FROM pasaran_web";
        $qpas = mysqli_query($data, $spas);

        while($fpas = mysqli_fetch_assoc($qpas)) {
          $angkamain = rand(12345, 99999);
          $ashio = rand(0, 11);
          $pasaranprediksi = $fpas['nama_pasaranweb'];

                $iprediksi = "INSERT INTO prediksi_togel(pasaran_prediksi, angka_main, tanggal_prediksi) VALUES('$pasaranprediksi', '$angkamain', '$today')";
                $qprediksi = mysqli_query($data, $iprediksi);
            
              if($qprediksi) {

                  $shio = ['Harimau', 'Kerbau', 'Tikus', 'Babi', 'Anjing', 'Ayam', 'Monyet', 'Kambing', 'Kuda', 'Ular', 'Naga', 'Kelinci',];
                  $colokb = substr($angkamain, 1, 1) . " / " . substr($angkamain, 4, 1);
                  $colokdua = substr($angkamain, 2, 2) . " / " . substr($angkamain, 1, 2);
                  $colokshio = $shio[$ashio];

                    $angkabaru = strval($angkamain);

                    $result2d = [];
                    $result3d = [];
                    $result4d = [];

                    bbfsNum($angkabaru, 2, '', array(), $result2d);
                    bbfsNum($angkabaru, 3, '', array(), $result3d);
                    bbfsNum($angkabaru, 4, '', array(), $result4d);

                    $tigad = array_unique($result3d);
                    $empatd = array_unique($result4d);
                    $duad = array_unique($result2d);

                    $tigaan = '';
                    $empataan = '';
                    $duaan = '';

                    foreach ($tigad as $permutation) {
                      $tigaan .= $permutation . "*";
                    }

                    foreach ($empatd as $permutation) {
                      $empataan .= $permutation . "*";
                    }

                    foreach ($duad as $permutation) {
                      $duaan .= $permutation . "*";
                    }

                 $upredik = "UPDATE prediksi_togel SET duad = '$duaan', tigad = '$tigaan', empatd = '$empataan', colok_bebas = '$colokb', colok_duad = '$colokdua', colok_shio = '$colokshio' WHERE pasaran_prediksi = '$pasaranprediksi' AND DATE(tanggal_prediksi) = '$todaydate' ";
                 $qpredik = mysqli_query($data, $upredik);

                  $todd = date("l");

                    switch($todd) {
                          case 'Tuesday':
                              $delpre = "DELETE FROM prediksi_togel WHERE pasaran_prediksi = 'singapore' AND DATE(tanggal_prediksi) = '$todaydate'";
                              mysqli_query($data, $delpre);

                              $delpre1 = "DELETE FROM prediksi_togel WHERE pasaran_prediksi = 'magnum4d' AND DATE(tanggal_prediksi) = '$todaydate'";
                              mysqli_query($data, $delpre1);
                          break;

                          case 'Friday':
                            $delpre = "DELETE FROM prediksi_togel WHERE pasaran_prediksi = 'singapore' AND DATE(tanggal_prediksi) = '$todaydate'";
                            mysqli_query($data, $delpre);

                            $delpre1 = "DELETE FROM prediksi_togel WHERE pasaran_prediksi = 'magnum4d' AND DATE(tanggal_prediksi) = '$todaydate'";
                            mysqli_query($data, $delpre1);
                        break;

                        case 'Sunday':
                            $delpre = "DELETE FROM prediksi_togel WHERE pasaran_prediksi = 'pcso' AND DATE(tanggal_prediksi) = '$todaydate'";
                            mysqli_query($data, $delpre);
                        break;

                        case 'Monday':
                            $delpre = "DELETE FROM prediksi_togel WHERE pasaran_prediksi = 'magnum4d' AND DATE(tanggal_prediksi) = '$todaydate'";
                            mysqli_query($data, $delpre);
                        break;

                        case 'Thursday':
                            $delpre = "DELETE FROM prediksi_togel WHERE pasaran_prediksi = 'magnum4d' AND DATE(tanggal_prediksi) = '$todaydate'";
                            mysqli_query($data, $delpre);
                        break;
                    }

              }


        }

    }

?>