<?php

$conn = mysqli_connect("localhost","root","","villahayati_db2");

function registrasi($data){
    global $conn;

    $username = strtolower(stripslashes($data["username"]));
    if(strlen($username) < 4 ){
        echo "<script>
                alert('username at least 6 characters');
            </script>";
        return false;
    }
    $password = mysqli_real_escape_string($conn, $data["pass"]);
    if(strlen($password) < 8 ){
        echo "<script>
                alert('password at least 8 characters');
            </script>";
        return false;
    }
    $cpass = mysqli_real_escape_string($conn, $data["c-pass"]);

    //cek ketersediaan username
    $exist = mysqli_query($conn, "SELECT username FROM user WHERE username = '$username'");

    if(mysqli_fetch_assoc($exist)){
        echo "<script>
                alert('username already exist');
            </script>";
        return false;
    }


    if($password !== $cpass){
        echo "<script>
                alert('password incorrect')
            </script>";
        return false;
    }

    //enkripsi password
    $password = password_hash($password, PASSWORD_DEFAULT);

    //tambahkan user ke database
    $insert_user = mysqli_query($conn, "INSERT INTO user (username, password) VALUES ('$username', '$password')");

    if(!$insert_user){
        echo "<script>
                alert('registration failed');
            </script>";
        return false;
    }
    function getMessages(){
        global $conn;
    
        $query = "SELECT * FROM messages";
        $result = mysqli_query($conn, $query);
    
        return mysqli_fetch_all($result, MYSQLI_ASSOC);
    }
    
    function addMessage($user_id, $username, $message){
        global $conn;
    
        $query = "INSERT INTO messages (user_id, username, message) VALUES ('$user_id', '$username', '$message')";
        mysqli_query($conn, $query);
    }
    // Mendapatkan nilai id terakhir yang tersimpan di tabel
    $lastId = mysqli_insert_id($conn);

    // Membuat unique_id dengan format U00001, U00002, U00003, dst.
    $uniqueId = 'U' . str_pad($lastId, 5, '0', STR_PAD_LEFT);

    // Update kolom unique_id pada baris yang baru saja ditambahkan
    $update_unique_id = mysqli_query($conn, "UPDATE user SET unique_id = '$uniqueId' WHERE id = $lastId");

    if(!$update_unique_id){
        echo "<script>
                alert('registration failed');
            </script>";
        return false;
    }

    return mysqli_affected_rows($conn);
}

function query($query){
    global $conn;

    $result = mysqli_query($conn, $query);
    $rows = [];
    while( $row = mysqli_fetch_assoc($result)){
        $rows[] = $row;
    }

    return $rows;
}


function tambah($data){
    global $conn;

    $nama = htmlspecialchars($data["nama"]);
    $fasilitas1 = htmlspecialchars($data["fasilitas_1"]);
    $fasilitas2 = htmlspecialchars($data["fasilitas_2"]);
    $fasilitas3 = htmlspecialchars($data["fasilitas_3"]);
    $fasilitas4 = htmlspecialchars($data["fasilitas_4"]);
    $tempat = htmlspecialchars($data["tempat"]);
    $deskripsi = htmlspecialchars($data["deskripsi"]);
    $harga = htmlspecialchars($data["harga"]);
    
    //cek apakah ada foto yang di upload
    $foto = upload();
    $foto2 = upload2();
    $foto3 = upload3();
    $foto4 = upload4();
    if( !$foto ){
        return false;
    }
    if( !$foto2 ){
        return false;
    }
    if( !$foto3 ){
        return false;
    }
    if( !$foto4 ){
        return false;
    }

    $query = "INSERT into tb_villa
                VALUES
                ('','$nama','$fasilitas1','$fasilitas2','$fasilitas3','$fasilitas4','$tempat','$deskripsi','$harga','$foto','$foto2','$foto3','$foto4')";

    mysqli_query($conn, $query);

    return mysqli_affected_rows($conn);
}

function hapus($id){
    global $conn;
    mysqli_query($conn, "DELETE FROM tb_villa WHERE id = $id");
    return mysqli_affected_rows($conn); 
}


function update($data, $id){
    global $conn;
    
    // Mendapatkan dan membersihkan data input untuk tabel tb_villa
    $nama = htmlspecialchars($data["nama"]);
    $fasilitas1 = htmlspecialchars($data["fasilitas_1"]);
    $fasilitas2 = htmlspecialchars($data["fasilitas_2"]);
    $fasilitas3 = htmlspecialchars($data["fasilitas_3"]);
    $fasilitas4 = htmlspecialchars($data["fasilitas_4"]);
    $tempat = htmlspecialchars($data["tempat"]);
    $deskripsi = htmlspecialchars($data["deskripsi"]);
    $harga = htmlspecialchars($data["harga"]);
    $fotoLama = $data["fotoLama"];
    $fotoLama2 = $data["fotoLama2"];
    $fotoLama3 = $data["fotoLama3"];
    $fotoLama4 = $data["fotoLama4"];

    // Mendapatkan dan membersihkan data input untuk tabel tb_bank
    $bankName1 = htmlspecialchars($data["bank_name1"]);
    $accountNumber1 = htmlspecialchars($data["account_number1"]);
    $bankName2 = htmlspecialchars($data["bank_name2"]);
    $accountNumber2 = htmlspecialchars($data["account_number2"]);
    $bankName3 = htmlspecialchars($data["bank_name3"]);
    $accountNumber3 = htmlspecialchars($data["account_number3"]);

    // Mengupdate tabel tb_villa
    $queryVilla = "UPDATE tb_villa SET
        nama = '$nama', 
        fasilitas_1 = '$fasilitas1',
        fasilitas_2 = '$fasilitas2',
        fasilitas_3 = '$fasilitas3',
        fasilitas_4 = '$fasilitas4',
        tempat = '$tempat',
        deskripsi = '$deskripsi',
        harga = '$harga',
        foto = '$fotoLama',
        foto2 = '$fotoLama2',
        foto3 = '$fotoLama3',
        foto4 = '$fotoLama4'  
        WHERE id = $id";

    mysqli_query($conn, $queryVilla);

    // Mengupdate tabel tb_bank
    $queryBank = "UPDATE tb_bank SET
        nama_bank1 = '$bankName1',
        nmr_rekening1 = '$accountNumber1',
        nama_bank2 = '$bankName2',
        nmr_rekening2 = '$accountNumber2',
        nama_bank3 = '$bankName3',
        nmr_rekening3 = '$accountNumber3'";

    mysqli_query($conn, $queryBank);

    return mysqli_affected_rows($conn);
}


function upload(){
    
    $fileName = $_FILES["foto"]["name"];
    $fileSize = $_FILES["foto"]["size"];
    $tmpFile = $_FILES["foto"]["tmp_name"];
    $error = $_FILES["foto"]["error"];

    //cek apabila inputan kosong
    if( $error === 4 ){
        echo "
        <script>
            alert('please input picture');
        </script>        
        ";
        return false;
    }
    //cek apakah file yang dikirim adalah foto
    //menentukan extensi apa saja yang dapat di upload
    $extensionValidation = ['jpg','jpeg','png','jfif'];
    
    //memisahkan extensi dengan nama file
    $extension = explode('.',$fileName); // ==> ['namafoto','extensi']

    //mengambil index terakhir dari variabel extension dan mengubah extensi menjadi huruf kecil semua
    $extension = strtolower(end($extension));

    //pengecekan file yang diupload dengan extensi yang dibolehkan
    if( !in_array($extension , $extensionValidation)){
        echo "
        <script>
            alert('this is not picture, please input picture');
        </script>        
        ";
        return false;
    }

    //cek maximal ukuran file
    if( $fileSize > 2000000 ){
        echo "
        <script>
            alert('ur file size is to large');
        </script>        
        ";
        return false;
    }

    //membuat nama file baru
    $newFileName = uniqid();
    $newFileName .= '.';
    $newFileName .= $extension;

    //Penempatan file yang telah diupload
    move_uploaded_file($tmpFile, 'img/' . $newFileName);

    return $newFileName;
}

function upload2(){
    
    $fileName2 = $_FILES["foto2"]["name"];
    $fileSize2 = $_FILES["foto2"]["size"];
    $tmpFile2 = $_FILES["foto2"]['tmp_name'];
    $error2 = $_FILES["foto2"]["error"];

    //cek apabila inputan kosong
    if( $error2 === 4 ){
        echo "
        <script>
            alert('please input picture');
        </script>        
        ";
        return false;
    }

    //cek apakah file yang dikirim adalah foto
    //menentukan extensi apa saja yang dapat di upload
    $extensionValidation = ['jpg','jpeg','png','jfif'];

    //memisahkan extensi dengan nama file
    $extension2 = explode('.',$fileName2); // ==> ['namafoto','extensi']

    //mengambil index terakhir dari variabel extension dan mengubah extensi menjadi huruf kecil semua
    $extension2 = strtolower(end($extension2));

    //pengecekan file yang diupload dengan extensi yang dibolehkan
    if( !in_array($extension2 , $extensionValidation)){
        echo "
        <script>
            alert('this is not picture2, please input picture');
        </script>        
        ";
        return false;
    }

    //cek maximal ukuran file
    if( $fileSize2 > 9000000000 ){
        echo "
        <script>
            alert('ur file size is to large');
        </script>        
        ";
        return false;
    }

    //membuat nama file baru
    $newFileName2 = uniqid();
    $newFileName2 .= '.';
    $newFileName2 .= $extension2;

    //Penempatan file yang telah diupload
    move_uploaded_file($tmpFile2, 'img/' . $newFileName2);

    return $newFileName2;
}

function upload3(){
    
    $fileName3 = $_FILES["foto3"]["name"];
    $fileSize3 = $_FILES["foto3"]["size"];
    $tmpFile3 = $_FILES["foto3"]['tmp_name'];
    $error3 = $_FILES["foto3"]["error"];

    //cek apabila inputan kosong
    if( $error3 === 4 ){
        echo "
        <script>
            alert('please input picture');
        </script>        
        ";
        return false;
    }

    //cek apakah file yang dikirim adalah foto
    //menentukan extensi apa saja yang dapat di upload
    $extensionValidation = ['jpg','jpeg','png','jfif'];

    //memisahkan extensi dengan nama file
    $extension3 = explode('.',$fileName3); // ==> ['namafoto','extensi']

    //mengambil index terakhir dari variabel extension dan mengubah extensi menjadi huruf kecil semua
    $extension3 = strtolower(end($extension3));

    //pengecekan file yang diupload dengan extensi yang dibolehkan
    if( !in_array($extension3 , $extensionValidation)){
        echo "
        <script>
            alert('this is not picture3, please input picture');
        </script>        
        ";
        return false;
    }

    //cek maximal ukuran file
    if( $fileSize3 > 2000000 ){
        echo "
        <script>
            alert('ur file size is to large');
        </script>        
        ";
        return false;
    }

    //membuat nama file baru
    $newFileName3 = uniqid();
    $newFileName3 .= '.';
    $newFileName3 .= $extension3;

    //Penempatan file yang telah diupload
    move_uploaded_file($tmpFile3, 'img/' . $newFileName3);

    return $newFileName3;
}

function upload4(){
    
    $fileName4 = $_FILES["foto4"]["name"];
    $fileSize4 = $_FILES["foto4"]["size"];
    $tmpFile4 = $_FILES["foto4"]['tmp_name'];
    $error4 = $_FILES["foto4"]["error"];

    //cek apabila inputan kosong
    if( $error4 === 4 ){
        echo "
        <script>
            alert('please input picture');
        </script>        
        ";
        return false;
    }

    //cek apakah file yang dikirim adalah foto
    //menentukan extensi apa saja yang dapat di upload
    $extensionValidation = ['jpg','jpeg','png','jfif'];

    //memisahkan extensi dengan nama file
    $extension4 = explode('.',$fileName4); // ==> ['namafoto','extensi']

    //mengambil index terakhir dari variabel extension dan mengubah extensi menjadi huruf kecil semua
    $extension4 = strtolower(end($extension4));

    //pengecekan file yang diupload dengan extensi yang dibolehkan
    if( !in_array($extension4 , $extensionValidation)){
        echo "
        <script>
            alert('this is not picture4, please input picture');
        </script>        
        ";
        return false;
    }

    //cek maximal ukuran file
    if( $fileSize4 > 2000000 ){
        echo "
        <script>
            alert('ur file size is to large');
        </script>        
        ";
        return false;
    }

    //membuat nama file baru
    $newFileName4 = uniqid();
    $newFileName4 .= '.';
    $newFileName4 .= $extension4;

    //Penempatan file yang telah diupload
    move_uploaded_file($tmpFile4, 'img/' . $newFileName4);

    return $newFileName4;
}

function search($keyword){
    $query = "SELECT * FROM tb_villa WHERE 
            nama LIKE '%$keyword%' OR
            tempat LIKE '%$keyword%' OR
            rating LIKE '%$keyword%' OR
            harga LIKE '%$keyword%'";

    return query($query);
}

//data booking yang akan tetap ada
function bookLogData($data){
    global $conn;
    $villa_id = $data["villa_id"];
    $user_id = $data["user_id"];
    $nama = $data["nama"];
    $email = htmlspecialchars($data["email"]);
    $nomor = htmlspecialchars($data["nomor"]);
    $tanggalPesan = $data["checkin"];
    $tanggalPulang = $data["checkout"];

    //insert data yang telah diinputkan pada booking page
    $query = "INSERT INTO tb_booklog 
                VALUES
            ('','$nama','$email','$nomor','$tanggalPesan','$tanggalPulang','$villa_id','$user_id','verifikasi')";

    mysqli_query($conn, $query);

    // Blokir rentang tanggal pada tabel tb_villa_booking_date
    $tanggalBooking = $tanggalPesan;
    while($tanggalBooking <= $tanggalPulang){
    if ($tanggalBooking != $tanggalPulang) {
        $query = "INSERT INTO tb_villa_booking_date 
                  VALUES
                  ('','$villa_id','$tanggalBooking', '$user_id')";

        mysqli_query($conn, $query);
    }
    $tanggalBooking = date('Y-m-d', strtotime($tanggalBooking .' +1 day'));
}

return mysqli_affected_rows($conn);

}






//data booking bersifat sementara, akan terhapus setelah melakukan payment
function bookData($data){
    global $conn;
    $villa_id = $data["villa_id"];
    $user_id = $data["user_id"];
    $nama = $data["nama"];
    $email = htmlspecialchars($data["email"]);
    $nomor = htmlspecialchars($data["nomor"]);
    $tanggalPesan = $data["tanggal-pesan"];
    $tanggalPulang = $data["tanggal-pulang"];

    //insert data yang telah diinputkan pada booking page
    $query = "INSERT into tb_booking
                VALUES
            ('','$nama','$email','$nomor','$tanggalPesan','$tanggalPulang','$villa_id','$user_id')";

    mysqli_query($conn, $query);

    return mysqli_affected_rows($conn);
}


function delete_bookLog($id){
    global $conn;
    
    // Delete records from tb_booklog table
    mysqli_query($conn, "DELETE FROM tb_booklog WHERE user_id = $id");

    // Delete records from tb_villa_booking_date table
    mysqli_query($conn, "DELETE FROM tb_villa_booking_date WHERE user_id = $id");

    // Return the number of affected rows
    return mysqli_affected_rows($conn); 
}


function move_to_bookLog($data) {
    global $conn;

    // Get all the bookings of the user
    $bookings = query("SELECT * FROM tb_booking WHERE user_id = $user_id");

    // Loop through each booking
    foreach ($bookings as $booking) {
        
        $villa_id = $booking["villa_id"];
        $user_id = $booking["user_id"];
        $nama = $booking["nama"];
        $email = htmlspecialchars($booking["email"]);
        $nomor = htmlspecialchars($booking["nomor"]);
        $tanggalPesan = $booking["tanggal-pesan"];
        $tanggalPulang = $booking["tanggal-pulang"];

        // Insert the booking into the booklog table
        $query = "INSERT INTO tb_booklog
                  VALUES
                  ('', $user_id, $villa_id, '$check_in', '$check_out', $guest_count, $total_price)";
        mysqli_query($conn, $query);
    }

    // Delete all the bookings of the user
    mysqli_query($conn, "DELETE FROM tb_booking WHERE user_id = $user_id");
}



function amountPay($userId){
    global $conn;

    $query = "SELECT datediff(tanggal_pulang, tanggal_pesan)*harga as total FROM tb_booking INNER JOIN tb_villa 
                ON tb_booking.villa_id = tb_villa.id WHERE id = $userId";

    $objPay = mysqli_query($conn, $query);

    return $objPay;
}

function payments($data, $pay, $userId, $villa_id){
    global $conn;

    $nama = htmlspecialchars($data["nama_pengirim"]);
    $bank = htmlspecialchars($data["nama_bank"]);
    $bukti = uploadBukti();

    //cek apakah ada butki bayar
    if( !$bukti ){
        return false;
    }

    $query = "INSERT INTO tb_paymentapproval
                VALUES
            ('','$nama','$bank','$bukti','$pay','$userId','$villa_id')";

    mysqli_query($conn,$query);


    return mysqli_affected_rows($conn);
}

function uploadBukti(){
    
    $fileName = $_FILES["bukti_bayar"]["name"];
    $fileSize = $_FILES["bukti_bayar"]["size"];
    $tmpFile = $_FILES["bukti_bayar"]["tmp_name"];
    $error = $_FILES["bukti_bayar"]["error"];

    //cek apabila inputan kosong
    if( $error === 4 ){
        echo "
        <script>
            alert('please input picture');
        </script>        
        ";
        return false;
    }
    //cek apakah file yang dikirim adalah foto
    //menentukan extensi apa saja yang dapat di upload
    $extensionValidation = ['jpg','jpeg','png','jfif'];
    
    //memisahkan extensi dengan nama file
    $extension = explode('.',$fileName); // ==> ['namafoto','extensi']

    //mengambil index terakhir dari variabel extension dan mengubah extensi menjadi huruf kecil semua
    $extension = strtolower(end($extension));

    //pengecekan file yang diupload dengan extensi yang dibolehkan
    if( !in_array($extension , $extensionValidation)){
        echo "
        <script>
            alert('this is not picture, please input picture');
        </script>        
        ";
        return false;
    }

    //cek maximal ukuran file
    if( $fileSize > 2000000 ){
        echo "
        <script>
            alert('ur file size is to large');
        </script>        
        ";
        return false;
    }

    //membuat nama file baru
    $newFileName = uniqid();
    $newFileName .= '.';
    $newFileName .= $extension;

    //Penempatan file yang telah diupload
    move_uploaded_file($tmpFile, '../buktiBayar/' . $newFileName);

    return $newFileName;
}


?>