<?php
/*
  | Source Code Aplikasi Rental Mobil PHP & MySQL
  | 
  | @package   : rental_mobil
  | @file	   : info.php 
  | @author    : fauzan1892 / Fauzan Falah
  | @copyright : Copyright (c) 2017-2021 Codekop.com (https://www.codekop.com)
  | @blog      : https://www.codekop.com/products/source-code-aplikasi-rental-mobil-php-mysql-7.html 
  | 
  | 
  | 
  | 
 */
    require '../koneksi/koneksi.php';
    $title_web = 'Info Website';
    include 'header.php';
    if(empty($_SESSION['USER']))
    {
        session_start();
    }

    // Ensure logo column exists
    $koneksi->query("ALTER TABLE infoweb ADD COLUMN IF NOT EXISTS logo VARCHAR(255) NULL AFTER email");

    if(!empty($_POST['nama_rental']))
    {
        $data[] =  htmlspecialchars($_POST["nama_rental"]);
        $data[] =  htmlspecialchars($_POST["telp"]);
        $data[] =  htmlspecialchars($_POST["alamat"]);
        $data[] =  htmlspecialchars($_POST["email"]);

        $row_old = $koneksi->prepare("SELECT logo FROM infoweb WHERE id = 1");
        $row_old->execute();
        $old_data = $row_old->fetch(PDO::FETCH_OBJ);

        if(!empty($_FILES['logo']['name'])){
            $allowedImageType = array("image/gif", "image/JPG", "image/jpeg", "image/pjpeg", "image/png", "image/x-png", 'image/webp');
            $filepath = $_FILES['logo']['tmp_name'];
            $fileinfo = finfo_open(FILEINFO_MIME_TYPE);
            $filetype = finfo_file($fileinfo, $filepath);
            
            if(!in_array($filetype, array("image/png", "image/jpeg", "image/gif", "image/jpg", "image/webp"))) {
                echo '<script>alert("Format file tidak didukung");history.go(-1)</script>';
                exit();
            }

            $dir = '../assets/image/';
            $tmp_name = $_FILES['logo']['tmp_name'];
            $temp = explode(".", $_FILES["logo"]["name"]);
            $newfilename = 'logo_' . round(microtime(true)) . '.' . end($temp);
            $target_path = $dir . $newfilename;

            if (move_uploaded_file($tmp_name, $target_path)) {
                if ($old_data->logo && file_exists('../assets/image/'.$old_data->logo)) {
                    unlink('../assets/image/'.$old_data->logo);
                }
                $data[] = $newfilename;
            } else {
                $data[] = $old_data->logo;
            }
        } else {
            $data[] = $old_data->logo;
        }

        $data[] =  htmlspecialchars($_POST["no_rek"]);
        $data[] =  1;
        $sql = "UPDATE infoweb SET nama_rental = ?, telp = ?, alamat = ?, email = ?, logo = ?, no_rek = ?  WHERE id = ? ";
        $row = $koneksi->prepare($sql);
        $row->execute($data);
        echo '<script>alert("Update Data Info Website Berhasil !");window.location="info.php"</script>';
        exit;
    }
?>

<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-primary text-white">
            <h5 class="card-title">Info Website</h5>
        </div>
        <div class="card-body">
            <form action="" method="post" enctype="multipart/form-data">
                <?php
                    $sql = "SELECT * FROM infoweb WHERE id = 1";
                    $row = $koneksi->prepare($sql);
                    $row->execute();
                    $edit = $row->fetch(PDO::FETCH_OBJ);
                ?>
                <div class="form-group">
                    <label for="">Nama Rental</label>
                    <input type="text" class="form-control" value="<?= $edit->nama_rental;?>" name="nama_rental" id="nama_rental" placeholder=""/>
                </div>
                <div class="form-group">
                    <label for="">Logo Website</label>
                    <input type="file" class="form-control" name="logo" id="logo" accept="image/*"/>
                    <?php if(!empty($edit->logo)){?>
                        <img src="../assets/image/<?= $edit->logo;?>" class="img-fluid mt-2" style="width:100px;">
                    <?php }?>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label for="">Email</label>
                            <input type="text" class="form-control" value="<?= $edit->email;?>" name="email" id="email" placeholder=""/>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label for="">Telepon</label>
                            <input type="text" class="form-control" value="<?= $edit->telp;?>" name="telp" id="telp" placeholder=""/>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="">Alamat</label>
                    <textarea class="form-control" name="alamat" id="alamat" rows="3" placeholder=""><?= $edit->alamat;?></textarea>
                </div>
                <div class="form-group">
                    <label for="">Informasi No. Rekening</label>
                    <textarea class="form-control" name="no_rek" id="no_rek" rows="3" placeholder=""><?= $edit->no_rek;?></textarea>
                </div>
                <hr>
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-save"></i> Simpan Perubahan
                </button>
            </form>
        </div>
    </div>
</div>

<?php include 'footer.php';?>
