<?php
session_start();
// Panggil koneksi database.php untuk koneksi database
require_once "../../config/database.php";

// fungsi untuk pengecekan status login user 
// jika user belum login, alihkan ke halaman login dan tampilkan pesan = 1
if (empty($_SESSION['username']) && empty($_SESSION['password'])){
    echo "<meta http-equiv='refresh' content='0; url=index.php?alert=1'>";
}
// jika user sudah login, maka jalankan perintah untuk insert, update, dan delete
else {
    if ($_GET['act']=='tambahIbadah') {
        if (isset($_POST['submit'])) {
            // ambil data hasil submit dari form 
            $lokasi   		= mysqli_real_escape_string($mysqli, trim($_POST['lokasi']));
            
            $tanggal  	    = mysqli_real_escape_string($mysqli, trim($_POST['tanggal']));
            $lk  	    	= mysqli_real_escape_string($mysqli, trim($_POST['lk']));
            $pr		  	    = mysqli_real_escape_string($mysqli, trim($_POST['pr']));
            //$kolekte  	    = mysqli_real_escape_string($mysqli, trim($_POST['kolekte']));
			//$perpuluhan	    = mysqli_real_escape_string($mysqli, trim($_POST['perpuluhan']));
			//$syukur  	    = mysqli_real_escape_string($mysqli, trim($_POST['syukur']));
            $ket  	   		= mysqli_real_escape_string($mysqli, trim($_POST['ket']));
			$tglpelayan     = mysqli_real_escape_string($mysqli, trim($_POST['tglpelayan']));
			$pic  	  	    = mysqli_real_escape_string($mysqli, trim($_POST['pic']));
			$alamat  	    = mysqli_real_escape_string($mysqli, trim($_POST['alamat']));
            $created_user   = $_SESSION['id_user'];

            // perintah query untuk menyimpan data ke tabel obat
            $query = mysqli_query($mysqli, "INSERT INTO ibadah(lokasi,tanggal,lk,pr,ket,pic,alamat,tglpelayan,created_user,updated_user) 
                                            VALUES('$lokasi','$tanggal','$lk','$pr','$ket','$pic','$alamat','$tglpelayan','$created_user','$created_user')")
                                            or die('Ada kesalahan pada query insert : '.mysqli_error($mysqli));    

            // cek query
            if ($query) {
                // jika berhasil tampilkan pesan berhasil simpan data
                header("location: ../../main.php?module=ibadah&alert=1");
            }   
        }   
    }
    
    elseif($_GET['act']=='approve'){
        
        $id = $_GET['id'];
        $updated_user = $_SESSION['id_user']; 

        $query = mysqli_query($mysqli, "UPDATE ibadah set status = 'A' , approve_user = ' $updated_user'   where id = $id")
        or die('Ada kesalahan pada query update : '.$id.mysqli_error($mysqli));

        // cek query
        if ($query) {
        // jika berhasil tampilkan pesan berhasil update data
        header("location: ../../main.php?module=ibadah&alert=4");
}         
    }
    elseif ($_GET['act']=='editIbadah') {
        if (isset($_POST['submit'])) {

           $id  	    = mysqli_real_escape_string($mysqli, trim($_POST['id']));
           $lokasi   		= mysqli_real_escape_string($mysqli, trim($_POST['lokasi']));
            
            $tanggal  	    = mysqli_real_escape_string($mysqli, trim($_POST['tanggal']));
            $lk  	    	= mysqli_real_escape_string($mysqli, trim($_POST['lk']));
            $pr		  	    = mysqli_real_escape_string($mysqli, trim($_POST['pr']));
            //$kolekte  	    = mysqli_real_escape_string($mysqli, trim($_POST['kolekte']));
			//$perpuluhan	    = mysqli_real_escape_string($mysqli, trim($_POST['perpuluhan']));
			//$syukur  	    = mysqli_real_escape_string($mysqli, trim($_POST['syukur']));
			$tglpelayan     = mysqli_real_escape_string($mysqli, trim($_POST['tglpelayan']));
			$pic  	  	    = mysqli_real_escape_string($mysqli, trim($_POST['pic']));
			$alamat  	    = mysqli_real_escape_string($mysqli, trim($_POST['alamat']));
			$ket  	        = mysqli_real_escape_string($mysqli, trim($_POST['ket']));

           $updated_user = $_SESSION['id_user']; 
			
			// perintah query untuk mengubah data pada tabel obat
            $query = mysqli_query($mysqli, "UPDATE ibadah SET  tanggal = '$tanggal',
                                                                    lk    = '$lk',
                                                                    pr    = '$pr',
                                                                    kolekte    = '$kolekte',
																	tglpelayan = '$tglpelayan',
                                                                    pic   	   = '$pic',
																	alamat     = '$alamat',
																	ket   	   = '$ket',
                                                                   updated_user    = '$updated_user'
                                                                                                                                      
                                                                    WHERE id       =  $id ")
                                            or die('Ada kesalahan pada query update : '.$id.mysqli_error($mysqli));
			
            
            // cek query
            if ($query) {
                // jika berhasil tampilkan pesan berhasil update data
                header("location: ../../main.php?module=ibadah&alert=2");
            }         
            
        }
    }
	elseif ($_GET['act']=='addkolekte') {
        if (isset($_POST['submit'])) {

			$id  	    = mysqli_real_escape_string($mysqli, trim($_GET['id']));
            $total 	    = mysqli_real_escape_string($mysqli, trim($_POST['total']));

			$created_user = $_SESSION['id_user']; 

			$jenis = 1; //jenis 1 : kolekte
            // perintah query untuk mengubah data pada tabel obat
            $query = mysqli_query($mysqli, "INSERT INTO dana(jenis,ibadah,nama,total,created_user,updated_user) 
                                            VALUES('$jenis','$id','','$total','$created_user','$created_user')")
                                            or die('Ada kesalahan pada query update : '.$id.mysqli_error($mysqli));
            
            
            // cek query
            if ($query) {
                // jika berhasil tampilkan pesan berhasil update data
                header("location: ../../main.php?module=form_ibadah&form=edit&id=$id");
            }         
            
        }
    }
	elseif ($_GET['act']=='addperpuluhan') {
        if (isset($_POST['submit'])) {

			$id  	    = mysqli_real_escape_string($mysqli, trim($_GET['id']));
            $nama  	    = mysqli_real_escape_string($mysqli, trim($_POST['nama']));
            $total 	    = mysqli_real_escape_string($mysqli, trim($_POST['total']));

			$created_user = $_SESSION['id_user']; 

			$jenis = 2; //jenis 2 : perpuluhan
            // perintah query untuk mengubah data pada tabel obat
            $query = mysqli_query($mysqli, "INSERT INTO dana(jenis,ibadah,nama,total,created_user,updated_user) 
                                            VALUES('$jenis','$id','$nama','$total','$created_user','$created_user')")
                                            or die('Ada kesalahan pada query update : '.$id.mysqli_error($mysqli));
            
            
            // cek query
            if ($query) {
                // jika berhasil tampilkan pesan berhasil update data
                header("location: ../../main.php?module=form_ibadah&form=edit&id=$id");
            }         
            
        }
    }
	elseif ($_GET['act']=='addsyukur') {
        if (isset($_POST['submit'])) {

			$id  	    = mysqli_real_escape_string($mysqli, trim($_GET['id']));
            $nama  	    = mysqli_real_escape_string($mysqli, trim($_POST['nama']));
            $total 	    = mysqli_real_escape_string($mysqli, trim($_POST['total']));
			$tujuan	    = mysqli_real_escape_string($mysqli, trim($_POST['tujuan']));
			
			$created_user = $_SESSION['id_user']; 

			$jenis = 3; //jenis 3 : tanda meriah ukur
            // perintah query untuk mengubah data pada tabel obat
            $query = mysqli_query($mysqli, "INSERT INTO dana(jenis,ibadah,nama,total,tujuan,created_user,updated_user) 
                                            VALUES('$jenis','$id','$nama','$total','$tujuan','$created_user','$created_user')")
                                            or die('Ada kesalahan pada query update : '.$id.mysqli_error($mysqli));
            
            
            // cek query
            if ($query) {
                // jika berhasil tampilkan pesan berhasil update data
                header("location: ../../main.php?module=form_ibadah&form=edit&id=$id");
            }         
            
        }
    }
	
	elseif ($_GET['act']=='deldana') {


			$id  	    = mysqli_real_escape_string($mysqli, trim($_GET['id']));
            $danaid  	= mysqli_real_escape_string($mysqli, trim($_GET['danaid']));


			$updated_user = $_SESSION['id_user']; 

            $query = mysqli_query($mysqli, "Update dana set status = 'N' where id = $danaid")
                                            or die('Ada kesalahan pada query update : '.$id.mysqli_error($mysqli));
            
            
            // cek query
            if ($query) {
                // jika berhasil tampilkan pesan berhasil update data
                header("location: ../../main.php?module=form_ibadah&form=edit&id=$id");
            }         
            
    }

   

    elseif ($_GET['act']=='deleteIbadah') {
        if(isset($_POST['delete'])){
            $id = mysqli_real_escape_string($mysqli, trim($_POST['id']));
            
            $query = "UPDATE ibadah SET status = 'N' WHERE id = '$id'";
            $execQuery = mysqli_query($mysqli, $query);
            if($execQuery) {
                //jika berhasil tampilkan pesan berhasil simpan data
                header("location: ../../main.php?module=ibadah&alert=3");
            } else {
                header("location: ../../main.php?module=ibadah&alert=2");
            }
        }
    }
    
    
}       
?>