<?php include 'loader.php'; ?>
<?php include 'inc/config.php'; ?>
<?php include 'inc/template_start.php'; ?>
<?php include 'inc/page_head.php'; ?>
<?php
if(!$user->isLogged()) $location->aredirect('login');
//if(!$func->get('reservation_id')) $location->aredirect('orders');
$room_info = [];
$path = '';

if($_SERVER['REQUEST_METHOD']=='POST') {
    //$func->prnt($func->post(),true);
    //echo '<pre>';print_r($_FILES);echo '</pre>'; die();
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["logo"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    if($func->get('id')) {
        $params = $func->post();



        $sql = [];
        foreach ($params as $key=>$param) {
            if($key=='fiyat'){
                $param = str_replace(',','.',$param);
            }
            $sql[]= $key." = '".$param."'";
        }
        $sql = implode(',',$sql);

//echo '<pre>';print_r($_FILES);echo '</pre>'; die();
        $check = $_FILES["logo"]["size"];
        if($check > 100) {
            //echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
// Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
// Check file size
        if ($_FILES["logo"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
// Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png"  && $imageFileType != "svg" && $imageFileType != "jpeg"
            && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
        } else {
            //die('not error');
            if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {
                $sql.=" , logo='".$target_file."'";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
//die();
        $db->query("UPDATE " . DBPREFIX . "projeler SET ".$sql." WHERE id='".$func->get('id')."'");
        $success->set('Projeniz başarıyla güncellenmiştir.');
    }else{
        $params = $func->post();
        $sql = [];
        foreach ($params as $key=>$param) {
            if($key=='fiyat'){
                $param = str_replace(',','.',$param);
            }
            $sql[]= $key." = '".$param."'";
        }
        $sql = implode(',',$sql);

        $check = $_FILES["logo"]["size"];
        if($check > 100) {
            //echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
// Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
// Check file size
        if ($_FILES["logo"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
// Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png"  && $imageFileType != "svg" &&  $imageFileType != "jpeg"
            && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {
                $sql.=" , logo='".$target_file."'";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }

        $db->query("INSERT INTO " . DBPREFIX . "projeler SET ".$sql);
        $success->set('Projeniz başarıyla eklenmiştir.');
    }

   $location->aredirect('projects');
}

$projeler = $db->query("SELECT * FROM " . DBPREFIX . "projeler ");
$projeler = $projeler->num_rows?$projeler->rows:array();

if($func->get('id')) {
    $room_info = $db->query("SELECT * FROM " . DBPREFIX . "projeler WHERE id = '".$func->get('id')."'");
    $room_info = $room_info->row;
    $path = '?id='.$func->get('id');
    //echo '<pre>';print_r($room_info);echo '</pre>'; die();
}


?>
<!-- Page content -->
<div id="page-content">


    <div class="row">

        <div class="col-lg-12">
            <!-- Meta Data Block -->
            <div class="block">
                <!-- Meta Data Title -->
                <div class="block-title">
                    <h2><i class="fa fa-google"></i> <strong>Daire</strong> Ekle/Düzenle</h2>
                </div>
                <!-- END Meta Data Title -->

                <!-- Meta Data Content -->
                <form action="<?php echo SITE_ADMIN; ?>new_project.php<?php echo $path; ?>" method="post" class="form-horizontal form-bordered" enctype="multipart/form-data">

                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">Proje Adı</label>
                        <div class="col-md-9">
                            <input type="text" id="product-daire" name="adi" value="<?php echo $room_info?$room_info['adi']:''; ?>" class="form-control" placeholder="Proje Adı">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">Proje Kodu</label>
                        <div class="col-md-9">
                            <input type="text" id="proje_code" name="proje_code" value="<?php echo $room_info?$room_info['proje_code']:''; ?>" class="form-control" placeholder="Proje Kodu">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">Proje Logosu</label>
                        <div class="col-md-9">
                            <input type="file" id="product-daire" name="logo" value="<?php echo $room_info?$room_info['logo']:''; ?>" class="form-control" placeholder="Proje Adı">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">Durumu</label>
                        <div class="col-md-9">
                            <select type="text" id="durum" name="durum" class="form-control">
                                <option value="1" <?php echo isset($room_info['durum']) && $room_info['durum']=='1'?'selected="selected"':''; ?>>Aktif</option>
                                <option value="0" <?php echo isset($room_info['durum']) && $room_info['durum']=='0'?'selected="selected"':''; ?>>Pasif</option>
                            </select>
                        </div>
                    </div>


                    <div class="form-group form-actions">
                        <div class="col-md-9 col-md-offset-3">
                            <button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-floppy-o"></i> Kaydet</button>
                            <a href="<?php echo SITE_ADMIN; ?>projects.php" class="btn btn-sm btn-warning"><i class="fa fa-repeat"></i> İptal</a>
                        </div>
                    </div>
                </form>
                <!-- END Meta Data Content -->
            </div>
            <!-- END Meta Data Block -->

        </div>
    </div>


</div>









<?php include 'inc/page_footer.php'; ?>
<?php include 'inc/template_scripts.php'; ?>

<!-- Load and execute javascript code used only in this page -->
<script src="js/pages/ecomProducts.js"></script>
<script src="js/simple.money.format.js" type="text/javascript"></script>
<script>
    $(function(){ EcomProducts.init(); });
    $(document).ready(function () {
        $('input[name="fiyat"]').simpleMoneyFormat();
    });
</script>

<?php include 'inc/template_end.php'; ?>
