<?php





$enable_hide_numbers = $_settings->info('enable_hide_numbers');
$enable_cpf = $_settings->info('enable_cpf');

if ($enable_cpf == 1) {
	$search_type = 'search_orders_by_cpf';
}
else {
	$search_type = 'search_orders_by_phone';
}

echo '<div class="container app-main">' . "\r\n" . '   <div class="mb-3">' . "\r\n" . '      <div class="row justify-content-between w-100 align-items-center">' . "\r\n" . '         <div class="col">' . "\r\n" . '            <div class="app-title">' . "\r\n" . '               <h1>🛒 Meus títulos</h1>' . "\r\n\r\n" . '            </div>' . "\r\n" . '         </div>' . "\r\n" . '         <div class="col-auto text-end"><button type="button" data-bs-toggle="modal" data-bs-target="#modal-buscar" class="btn btn-primary btn-sm"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="MuiBox-root css-q0bs1u iconify iconify--ic" sx="[object Object]" width="1.5em" height="1.5em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path fill="currentColor" d="M15.5 14h-.79l-.28-.27A6.47 6.47 0 0 0 16 9.5A6.5 6.5 0 1 0 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5S14 7.01 14 9.5S11.99 14 9.5 14"></path></svg> Buscar</button></div>' . "\r\n" . '      </div>' . "\r\n" . '   </div>' . "\r\n" . '   <form id="modal-buscar" class="modal fade" aria-hidden="true" style="display: none;">' . "\r\n" . '      <div class="modal-dialog modal-dialog-centered modal-sm modal-fullscreen-sm-down">
' . "\r\n" . '         <div class="modal-content">' . "\r\n" . '            <div class="modal-header">' . "\r\n" . '               <h5 class="modal-title"><svg style="margin-right: 6px;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="MuiBox-root css-ci8oll iconify iconify--material-symbols" sx="[object Object]" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path fill="currentColor" d="M7 15q-1.25 0-2.125-.875T4 12t.875-2.125T7 9t2.125.875T10 12t-.875 2.125T7 15m0 3q1.925 0 3.475-1.1T12.65 14H13l1.3 1.3q.15.15.325.212t.375.063t.375-.062t.325-.213L17 14l1.75 1.375q.15.125.338.187t.387.038t.363-.112t.287-.238l2.25-2.575q.125-.15.188-.325t.062-.375t-.075-.362t-.2-.288L21.325 10.3q-.15-.15-.337-.225T20.6 10h-7.95q-.6-1.7-2.113-2.85T7 6Q4.5 6 2.75 7.75T1 12t1.75 4.25T7 18"></path></svg>Login</h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fechar"></button>
      ' . "\r\n" . '            </div>' . "\r\n" . '            <div class="modal-body">' . "\r\n" . '            ';

if ($enable_cpf != 1) {
    echo '  <div class="form-group mb-3"><hr>' . "\r\n" . 
         '    <input onkeyup="formatarTEL(this);" maxlength="15" name="phone" required class="form-control" value="" placeholder="TELEFONE*">' . "\r\n" . 
         '  </div>' . "\r\n";
    $textoBotao = 'Informe seu TELEFONE para continuar';
} else {
    echo '  <div class="form-group mb-3"><hr>' . "\r\n" . 
         '    <input name="cpf" class="form-control" id="cpf" value="" maxlength="14" minlength="14" placeholder="CPF*" oninput="formatarCPF(this.value)" required>' . "\r\n" . 
         '  </div>' . "\r\n";
    $textoBotao = 'Informe seu CPF para continuar';
}

echo '  <div>
  <button type="button" class="w-100 d-flex align-items-center shadow-sm" style="background-color: #8a5700; color: #ffffff; border: none; padding: 10px 14px; border-radius: 10px; font-weight: 500; font-size: 14px;">
    <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="#ffd700" viewBox="0 0 24 24" class="me-2">
      <path d="M12 2a10 10 0 1 0 .001 20.001A10 10 0 0 0 12 2zm0 15a1.25 1.25 0 1 1 0-2.5 1.25 1.25 0 0 1 0 2.5zm1-4.75a1 1 0 0 1-2 0V7a1 1 0 0 1 2 0v5.25z"/>
    </svg>
    ' . $textoBotao . '
  </button>
</div>


<br><div><button type="submit" class="btn btn-primary w-100">Continuar</button></div>' . "\r\n" . '               </div>' . "\r\n" . '            </div>' . "\r\n" . '         </div>' . "\r\n" . '      </form>' . "\r\n" . '      <div class="alert alert-warning"><i class="bi bi-check2-circle"></i> Clique em buscar para localizar suas compras</div>' . "\r\n" . '      <div>' . "\r\n" . '         ';
$i = 1;
$phone = (isset($_SESSION['phone']) ? $_SESSION['phone'] : '');
$cpf = (isset($_SESSION['cpf']) ? $_SESSION['cpf'] : '123');

if ($enable_cpf != 1) {
	$phone = $conn->real_escape_string($phone);
	$phoneQuery = $conn->query("\r\n" . '             SELECT id' . "\r\n" . '             FROM customer_list' . "\r\n" . '             WHERE phone = \'' . $phone . '\'' . "\r\n" . '             ');
}
else {
	$cpf = $conn->real_escape_string($cpf);
	$phoneQuery = $conn->query("\r\n" . '            SELECT id' . "\r\n" . '            FROM customer_list' . "\r\n" . '            WHERE cpf = \'' . $cpf . '\'' . "\r\n" . '            ');
}
if ($phoneQuery && 0 < $phoneQuery->num_rows) {
	$customerId = $phoneQuery->fetch_assoc()['id'];
	$orders = $conn->query("\r\n" . '           SELECT o.*, p.image_path, p.qty_numbers, o.product_id, p.type_of_draw' . "\r\n" . '           FROM `order_list` o' . "\r\n" . '           INNER JOIN `product_list` p ON o.product_id = p.id' . "\r\n" . '           WHERE o.customer_id = \'' . $customerId . '\'' . "\r\n" . '           ORDER BY ABS(UNIX_TIMESTAMP(o.date_created)) DESC' . "\r\n" . '           ');
	if ($orders && 0 < $orders->num_rows) {
		while ($orderRow = $orders->fetch_assoc()) {
			echo '             ';
			$class = '';
			$border = '';
			$btn = '';
			$status = $orderRow['status'];

			if ($orderRow['status'] == '1') {
				$class = 'bg-warning';
				$border = 'border-warning';
				$btn = 'btn-warning';
			}

			if ($orderRow['status'] == '2') {
				$class = 'bg-success';
				$border = 'border-success';
				$btn = 'btn-success';
			}

			if ($orderRow['status'] == '3') {
				$class = 'bg-danger';
				$border = 'border-danger';
				$btn = 'btn-danger';
			}

			echo '            <div class="card app-card mb-2 pointer border-bottom border-2 ';
			echo $border;
			echo '">' . "\r\n" . '               <div class="card-body">' . "\r\n\r\n" . '                <div class="row align-items-center row-gutter-sm">' . "\r\n" . '                 <div class="col-auto">' . "\r\n" . '                  <div class="position-relative rounded-pill overflow-hidden box-shadow-08" style="width: 56px; height: 56px;">' . "\r\n" . '                   <div style="display: block; overflow: hidden; position: absolute; inset: 0px; box-sizing: border-box; margin: 0px;">' . "\r\n" . '                    <img src="';
			echo validate_image($orderRow['image_path']);
			echo '" decoding="async" data-nimg="fill" style="position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%;">' . "\r\n" . '                    <noscript></noscript>' . "\r\n" . '                 </div>' . "\r\n" . '              </div>' . "\r\n" . '           </div>' . "\r\n" . '           <div class="col ps-2">' . "\r\n" . '           <div class="compra-title font-weight-500">';
			echo $orderRow['product_name'];
			echo '</div>' . "\r\n" . '           <small class="compra-data font-xss opacity-50 text-uppercase"><i class="bi bi-calendar4-week"></i> ';
			echo date('d-m-Y H:i', strtotime($orderRow['date_created']));
			echo '</small>' . "\r\n" . '            <div class="compra-cotas font-xs mt-2">' . "\r\n" . '               ';
			$nCollection = explode(',', $orderRow['order_numbers']);
			$qty_nums = count($nCollection);
			$type_of_draw = $orderRow['type_of_draw'];

			if (1 < $type_of_draw) {
				echo drope_format_luck_numbers_dashboard($orderRow['order_numbers'], $orderRow['qty_numbers'], $class, $opt = true, $type_of_draw);
			}
			else if (($type_of_draw == 1) && ($status == 1) || $status == 3 && $enable_hide_numbers == 1) {
				echo 'As cotas serão geradas após o pagamento.';
			}
			else {
				echo '<div class="drope-tab">' . "\r\n" . '                  <input id="drope-tab-' . $orderRow['id'] . '" type="checkbox">' . "\r\n" . '                  <label for="drope-tab-' . $orderRow['id'] . '">Ver números</label>' . "\r\n" . '                  <div class="drope-content">' . drope_format_luck_numbers_dashboard($orderRow['order_numbers'], $orderRow['quantity'], $class = false, $opt = true, $type_of_draw) . '</div>' . "\r\n" . '                 </div>';
			}

			unset($_SESSION['phone']);
			unset($_SESSION['cpf']);
			echo '   ' . "\r\n" . '            ' . "\r\n" . '            ' . "\r\n" . '         </div>' . "\r\n" . '      </div>' . "\r\n" . '     <div class="col-12 pt-2">' . "\r\n" . '        <a href="/compra/';
			echo $orderRow['order_token'];
			echo '">' . "\r\n" . '           <span class="btn ';
			echo $btn;
			echo ' btn-sm p-1 px-2 w-100 font-xss">';

			if ($status == '1') {
				echo 'Efetuar pagamento';
			}

			if ($status == '2') {
				echo 'Visualizar compra';
			}

			if ($status == '3') {
				echo 'Compra cancelada';
			}

			echo '</span>' . "\r\n" . '        </a>' . "\r\n" . '     </div>' . "\r\n" . '  </div>' . "\r\n\r\n\r\n\r\n" . '</div>' . "\r\n" . '</div>' . "\r\n";
		}
	}
}

echo '</div>' . "\r\n" . '</div>' . "\r\n" . '<script>' . "\r\n" . '   $(window).on(\'load\', function() {' . "\r\n" . '      var tipo = "';
echo $status;
echo '";' . "\r\n" . '      if (!tipo){' . "\r\n" . '         $(\'#modal-buscar\').modal(\'show\');' . "\r\n" . '      }' . "\r\n" . '   });' . "\r\n" . '   $(document).ready(function(){' . "\r\n" . '   var tipo = "';
echo $search_type;
echo '";' . "\r\n" . '    $(\'#modal-buscar\').submit(function(e){' . "\r\n" . '     e.preventDefault()' . "\r\n" . '     $.ajax({' . "\r\n" . '      url:_base_url_+"class/Main.php?action=" + tipo,' . "\r\n" . '      method:\'POST\',' . "\r\n" . '      type:\'POST\',' . "\r\n" . '      data:new FormData($(this)[0]),' . "\r\n" . '      dataType:\'json\',' . "\r\n" . '      cache:false,' . "\r\n" . '      processData:false,' . "\r\n" . '      contentType: false,' . "\r\n" . '      error:err=>{' . "\r\n" . '       console.log(err)' . "\r\n" . '       alert(\'An error occurred\')' . "\r\n" . '    },' . "\r\n" . '    success:function(resp){' . "\r\n" . '       if(resp.status == \'success\'){' . "\r\n" . '         location.href = (resp.redirect)' . "\r\n" . '      }else{' . "\r\n" . '        alert(\'Nenhum registro de compra foi encontrado\')' . "\r\n" . '        console.log(resp)' . "\r\n" . '     }' . "\r\n" . '  }' . "\r\n" . '})' . "\r\n" . '  })' . "\r\n" . ' })' . "\r\n" . '</script>';

?>