<?php
session_start();

    global $db;
    $db = mysqli_connect('localhost','root','','proyek_psw');
    if(!$db){
        die("Database Connect Problem");
    }

    function ExecuteQuery($query){
        global $db;
        $result = mysqli_query($db,$query);

        return $result;
    }
    
    function query($query){
        global $db;
        $result = mysqli_query($db,$query);
        $rows = [];
        while ($row = mysqli_fetch_assoc($result)) {
            $rows[] = $row;
        }
        return $rows;
    }

    function LoginAccount($username,$password){
        $query = "SELECT * FROM akun WHERE username='$username' AND password='$password'";

        $user = ExecuteQuery($query);

        return $user;
    }

    function redirect($_location)
	{
	    header('Location: ' .$_location);
    }

    function CekAkun($username){
        $query = "SELECT username FROM akun WHERE username ='$username'";

        $result = ExecuteQuery($query);

        return mysqli_num_rows($result);   
    }

    function AddAkun($nama,$username,$pass,$email,$role){
        global $db;
        $query = "INSERT INTO akun(nama,username,password,email,role)
        VALUES (?,?,?,?,?)";

        $result = $db->prepare($query);
        $result->bind_param('ssssd',$nama,$username,$pass,$email,$role);
        $result->execute();
        $result->close();

        return mysqli_affected_rows($db);
    }

    function getAllPaket(){
        $query = "SELECT * FROM paket";

        $result = ExecuteQuery($query);
        $data = [];

        while ($paket = mysqli_fetch_assoc($result)) {
            $data[] = $paket;
        }

        return $data;
    }

    function CreatePaket($nama,$deskripsi,$tanggal,$gambar,$harga,$kuota){
        global $db;
        $query = "INSERT INTO paket(nama_paket,deskripsi,tanggal,gambar,harga,kuota) VALUES(?,?,?,?,?,?)";

        $result = $db->prepare($query);
        $result->bind_param('ssssii',$nama,$deskripsi,$tanggal,$gambar,$harga,$kuota);
        $result->execute();
        $result->close(); 

        return mysqli_affected_rows($db);
    }

    function DeletePaket($id_paket){
        $query = "DELETE FROM paket WHERE id_paket=$id_paket";

        $result = ExecuteQuery($query);

        return $result;
    }

    function EditPaket($id,$nama,$deskripsi,$tanggal,$gambar,$harga,$kuota){
        global $db;

        $query = "UPDATE paket SET nama_paket='$nama',deskripsi='$deskripsi',
                  tanggal='$tanggal',gambar='$gambar',harga='$harga',kuota= '$kuota'
                  WHERE id_paket='$id'";

        mysqli_query($db,$query);

        return mysqli_affected_rows($db);
    }

    function getPaket(){
        $query = "SELECT * FROM paket";

        $result = ExecuteQuery($query);

        return $result;
    }

    

    function getOnePaket($id){
        $query = "SELECT * FROM paket WHERE id_paket ='$id'";

        $result = ExecuteQuery($query);

        return $result;
    }

    function getSatuPaket($id){
        $query = "SELECT * FROM paket WHERE id_paket = '$id'";

        $result = ExecuteQuery($query);

        $data = mysqli_fetch_assoc($result);

        return $data;

    }

    function getEmail($id){
        $query = "SELECT email from akun where id_akun = '$id'";

        $result = ExecuteQuery($query);

        $data = mysqli_fetch_assoc($result);

        return $data['email'];
    }

    function GetKuota($id_paket){
        $query ="SELECT kuota FROM paket WHERE id_paket='$id_paket'";

        $result = ExecuteQuery($query);

        $data = mysqli_fetch_assoc($result);

        return $data['kuota'];
    }

    function jumlahTiketTerpakai($id_paket){
        $query = "SELECT COUNT(*) AS jlh_tiket FROM tiket WHERE kode_paket='$id_paket'";

        $result = ExecuteQuery($query);
       
        $data = mysqli_fetch_assoc($result); 
        
        return $data['jlh_tiket'];
    }

    function cekTiket($id_paket,$id_akun){
        global $db;

        $query = "SELECT COUNT(id_akun) AS id_akun FROM tiket WHERE id_akun='$id_akun' AND kode_paket='$id_paket'";

        $result = ExecuteQuery($query);

        $data = mysqli_fetch_assoc($result);

        return $data['id_akun'];
    }

    function CreateTiket($id_paket,$id_akun){
        global $db;
        $no_kursi = count_kursi($id_paket);
        $total_kursi = mysqli_fetch_assoc($no_kursi);
        $total_kursi = $total_kursi['total_kursi']+1;
        $query = "INSERT INTO tiket VALUES('','$id_akun','$id_paket','$total_kursi')";

        $result = ExecuteQuery($query);

        return mysqli_affected_rows($db);

    }

    function count_kursi($id_paket){
        $query = "SELECT COUNT(kode_paket) AS total_kursi FROM tiket WHERE kode_paket ='$id_paket'";

        $result = ExecuteQuery($query);

        return $result;
    }

    function GetAllTiketUser($id_akun){
        $query = "SELECT * FROM paket INNER JOIN tiket ON paket.id_paket
                 = tiket.kode_paket WHERE id_akun = '$id_akun'";

        $result = ExecuteQuery($query);
        $AllTiket = [];
        
        while ($data = mysqli_fetch_assoc($result)) {
            $AllTiket[] = $data;
        }

        return $AllTiket;
    }

    function caripaket($keyword){
        $query = "SELECT * FROM paket WHERE nama_paket LIKE '%$keyword%'";

        return query($query);
    }

    function DeleteTiket($id_tiket){
        $query = "DELETE FROM tiket WHERE id_tiket = '$id_tiket'";

        $result = ExecuteQuery($query);
        
        return $result;
    }
?>