<?php
require '../../data.php';
$pasa = mysqli_real_escape_string($data, $_GET['pasaran']);
?>
<h3 class="text-center title-paito text-uppercase fw-bold text-warning">Paito Togel Warna <?php echo $pasa; ?></h3>
<div class="sticky-top">
    <div class="bg-dark paito-picker p-2 rounded-bottom" style="width: 17rem;">
        <p class="mb-0">Pilih Warna:</p>
      <div id="colormenu">
          <fieldset id="color-selector" class="d-flex">
            <div class="box-color aqua selected" data-color="aqua"></div>
            <div class="box-color gold" data-color="gold"></div>
            <div class="box-color lgreen" data-color="lightgreen"></div>
            <div class="box-color dpink" data-color="deeppink"></div>
            <div class="box-color violet" data-color="violet"></div>
            <div class="box-color turto" data-color="turquoise"></div>
            <div class="box-color orange" data-color="orange"></div>
            <div><button type="button" onclick="resetSelectForm()" class="btn-hapuspaito">Hapus</button></div>
          </field>
      </div>
    </div>
</div>

<table id="table-paito" class="table-paito w-100 d-block d-md-table">
<thead>
<tr class="head text-center">
    <th colspan="4">Senin</th><th>D</th><th colspan="4">Selasa</th><th>D</th><th colspan="4">Rabu</th><th>D</th><th colspan="4">Kamis</th><th>D</th><th colspan="4">Jumat</th><th>D</th><th colspan="4">Sabtu</th><th>D</th><th colspan="4">Minggu</th><th>D</th>
  </tr>
</thead>

<tbody>

  <tr>
<?php

   $spa = "SELECT * FROM (SELECT * FROM result_lotto ORDER BY res_jam DESC) tmp WHERE tmp.res_pasaran = '$pasa' ORDER BY tmp.res_jam ASC";
   $qpa = mysqli_query($data, $spa);

   if(mysqli_num_rows($qpa) > 0) {
    $z = 0;
        while($fpa = mysqli_fetch_assoc($qpa)) {
            $angka = $fpa['res_angka'];
            $jm = $fpa['res_jam'];

            $regex = '/(?<!\ )[a-zA-Z0-9]/';
            $num1 = preg_replace($regex, '<td class="paito-num p-num">\\0</td>'.'' , $angka);

            echo $num1;

            $seperate = str_split($angka);

                $kepala = $seperate[2];

                $ekor = $seperate[3];

            if($kepala == 'x' && $ekor == 'x') {
                echo '<td class="paito-num addi-paito">-</td>';
                
            } else if($kepala + $ekor > 9) {
                $kalkulasi = $kepala + $ekor;
                    $sep = str_split($kalkulasi);
                    $hpaito = $sep[0] + $sep[1];
                echo '<td class="paito-num addi-paito">'.$hpaito.'</td>';
                
            }else {
                $count = $kepala + $ekor;
                echo '<td class="paito-num addi-paito">'.$count.'</td>';
            }
            
			$z++;

            if($z % 7 == 0) {
                echo "</tr><tr>";
                
            }
        }
   }

?>

<tr>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num addi-paito">.</td>

    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num addi-paito">.</td>

    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num addi-paito">.</td>

    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num addi-paito">.</td>

    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num addi-paito">.</td>

    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num addi-paito">.</td>

    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num"></td>
    <td class="paito-num addi-paito">.</td>
</tr>
    
  </tbody>
</table>

<script>
	// $("#table-paito > tbody > tr").hide().slice(40).show();
    var mouseDownState, eraseState, curColor;
	
	// Drawing functionality
	$("#table-paito").delegate("td", "mousedown", function() {
		mouseDownState = true;
		$el = $(this);
	    if (eraseState) {
	    	$el.removeAttr("style");
	    } else {
	    	$el.css("background", curColor);
	    }
	}).delegate("td", "mouseenter", function() {
		if (mouseDownState) {
			$el = $(this);
		    if (eraseState) {
		    	$el.removeAttr("style");
		    } else {
		    
		    	// DRAWING ACTION
		    	$el.css("background", curColor);
		    }
		}
	});
	$("html").bind("mouseup", function() {
		mouseDownState = false;
	});
	
	// Erasing functionality through OPTION key
	$(document).keydown(function(event) {
		if (event.keyCode == 18) {
			eraseState = true;
			$(".selected").addClass("previous");
			$(".box-color").removeClass("selected");
			$(".eraser").addClass("selected");
			
		}
	}).keyup(function(event) {
		if (event.keyCode == 18) {
			eraseState = false;
			$(".box-color").removeClass("selected");
			$(".previous").addClass("selected").removeClass("previous");
			$("." + curColor).addClass("selected");
		}
	});
	
	// Color selection swatches
	$("#color-selector").delegate(".box-color", "click", function() {
		
		$el = $(this);
		var pulledVal = $el.attr("data-color");
		
		if (pulledVal == 'eraser') {
			eraseState = true;
		} else {
			eraseState = false;
			curColor = pulledVal;
		}
		
		$(".box-color").removeClass("selected");
		$(this).addClass("selected");
	});
	
	// Tracing Functionality
	$("#tracing-image-form").submit(function() {
		
			var url = $("#fileLocation").val();
						
			$("<div />", {

				id: "tracing-image"
			
			}).appendTo("#table-wrap");
			
			$("#table-paito").css("opacity", 0.5);
			$("#toggle-tracing-mode").show(); 
			$("#tracing-image-form").remove();
			tracingMode = true;	
						
			return false;
	
		});
	
	$("#toggle-tracing-mode").click(function() {
	
		if (tracingMode) {
			$("#tracing-image").css("visibility", "hidden");
			$(this).html("Toggle Tracing Mode On");
			$("#table-paito").css("opacity", 1);
			tracingMode = false;
		} else {
			$("#tracing-image").css("visibility", "visible");
			$(this).html("Toggle Tracing Mode Off");
			$("#table-paito").css("opacity", 0.5);
			tracingMode = true;
		}
	
	});
	
	$('.box-color input').box-colorPicker({
		onSubmit: function(hsb, hex, rgb, el) {
		
			var $swatch = $(el).parent();
			var newColor = "#" + hex;
			
			$(".box-color").removeClass("selected");
			$("." + $swatch.attr("data-color")).css("background", newColor).addClass("selected");
			$swatch.attr("data-color", newColor);
			curColor = newColor;
			    		    		
		},
		onBeforeShow: function () {
			$(this).box-colorPickerSetColor(this.value);
		}
	});
    
	$("#get-html-button").click(function() {
		$("#the-html").val("<table style='width: 100%; border-collapse: collapse;'>" + $("#table-paito").html() + "</table>");
	});	

    function resetSelectForm() {
        $('.paito-num').removeAttr("style");
        $('.paito-num').removeClass('a1').removeClass('c1').removeClass('k1').removeClass('e1');
      }

</script>