<?php

namespace App\Controllers;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

class QrBarcode extends BaseController
{

    public function index()
    {
        echo 'PEDA MATENG - PEMPROV JAWA TENGAH';
    }


    function qr()
    {
        $data = array();
        echo view('QrBarcode/qr', $data);

    }
 

    function cekqr(){
        
        $res = trim($this->request->uri->getSegment(3)); // Output: admin
        
        $q = "select a.*,b.nama as tujuan,c.nik,c.nama,d.nobus,d.nama as namabus,d.nopol from tbl_mudik a left join mst_wilayah b on a.wilayah_id=b.wilayah_id left join tbl_warga c on a.warga_id=c.warga_id left join tbl_armada d on a.armada_id=d.armada_id where id=(select t.id from tbl_mudik t left join tbl_warga u on t.warga_id=u.warga_id where t.etiket=:et: and u.status='4' and u.statusdata='1')";

        $query = $this->db->query($q,['et'=>$res]); 
        $row = $query->getRow();

        if(!$row) $data['hasil'] = '<img src="assets/images/pemprovsmr/qr_no.png"><br/>e-Tiket :<b>'.$res.' </b>tidak DIKENAL';
        else {
            if($row->sdh_absen == 0)
            {
            $data['hasil'] = '<img src="assets/images/pemprovsmr/qr_ok.png"><br/>Nama : '.strtoupper($row->nama).'<br/>NIK : '.$row->nik.'<br/>NO BUS : '.$row->nobus;
            $this->db->query("update tbl_mudik set sdh_absen =:sdh_absen: where etiket=:etiket:",['etiket'=>$res,'sdh_absen'=>1]);
            }
            else { 
            $data['hasil'] = '<img src="assets/images/pemprovsmr/qr_no.png"><br/>Nama : '.strtoupper($row->nama).'<br/>NIK : '.$row->nik.'<br/>NO BUS : '.$row->nobus.'<br/><span style="color:red"><b>SUDAH TERPAKAI</b></span>';
            }
            
        }

//        $data['hasil'] = "HASILNYALAH : ".$res;
        echo json_encode($data);
    }
    
   public function uriuri()
    {
        echo "<h3>URL Segment From Controller</h3>";

        echo $this->request->uri; // Output: http://localhost:8080/admin/my-route
        echo "<br/>";
//        echo $this->request->uri->getSegments(); // Output: Returns all segment into array format
        echo "<br/>";
        echo $this->request->uri->getSegment(1); // Output: admin
        echo "<br/>";
        echo $this->request->uri->getScheme(); // Output: http
        echo "<br/>";
        echo $this->request->uri->getHost(); // Output: localhost
        echo "<br/>";
        echo $this->request->uri->getPort(); // Output: 8080
        echo "<br/>";
        echo $this->request->uri->getPath(); // Output: admin/my-route
        echo "<br/>";

        // IF url = http://localhost:8080/admin/my-route?name=sanjay&email=sanjay@gmail.com

//        echo $uri->getQuery(); // Output: name=sanjay&email=sanjay%40gmail.com
    }    
    

}