<?php
      require_once 'common/database.php';
      if(!isset($_SESSION['logged_in'])){
          header('location: index.php');
      }

      $id_paket = $_GET['id'];
      $id_akun  = $_GET['id_akun'];
       $email = getEmail($id_akun);
       $subject = "Pemesanan Paket";
       $message = "Terima kasih sudah memesan tiket dari kami. Have a nice holiday!!";

      $kuota = GetKuota($id_paket);
      $tiket_terpakai = jumlahTiketTerpakai($id_paket);
      $data = cekTiket($id_paket,$id_akun);
    if(mail($email,$subject,$message)){
      if($kuota == $tiket_terpakai){
        echo "<script>alert('Maaf Paket Sudah Habis!'); 
        window.location = 'detailpaket1.php?id=$id_paket'; </script>";
      }
      else{
          if($data < 1){
              $buat_tiket = CreateTiket($id_paket,$id_akun);
              if($_SESSION['role'] == 2){
                  if($buat_tiket > 0){
                    echo "<script>alert('Tiket berhasil dibuat'); 
                    window.location = 'detailpaket1.php?id=$id_paket'; </script>";
                  }
                  else{
                    echo "<script>alert('Tiket gagal dibuat'); 
                    window.location = 'detailpaket1.php?id=$id_paket'; </script>";
                  }
              }
          }
          else{
            echo "<script>alert('Tiket sudah pernah dibuat'); 
            window.location = 'detailpaket1.php?id=$id_paket'; </script>";
          }
      }
    }
      else{
        echo "<script>alert('Email tidak terkirim ke email anda');
               window.location = 'detailpaket1.php?id=$id_paket'</script>";
      }

?>