<?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->redirect('admin/login');

$path = '';
$customer_info = array();
if($func->get('user_id')) {
    $path = '?user_id='.$func->get('user_id');
    $customer_info = $db->query("SELECT * FROM " . DBPREFIX . "users WHERE user_id='" . $func->get('user_id') . "'");
    $customer_info = $customer_info->num_rows?$customer_info->row:array();
}
if($_SERVER['REQUEST_METHOD']=='POST') {
    $query = array();
    foreach ($func->post() as $key=>$item) {
        if($key!='supports')
            $query[] = $key."='".$db->escape($item)."'";
    }
    if($func->get('user_id')){
        $sql = "UPDATE " . DBPREFIX . "users SET " . implode(',', $query)." WHERE user_id='".$func->get('user_id')."'";
        $db->query($sql);
        $db->query("DELETE FROM ".DBPREFIX."user_supports WHERE user_id='".$func->get('user_id')."'");
        $user_id = $func->get('user_id');
    }else {
        $query[] = "date_added='" . date('Y-m-d H:i:s') . "'";
        $sql = "INSERT INTO " . DBPREFIX . "users SET " . implode(',', $query);
        $db->query($sql);
        $user_id = $db->getLastId();

    }

    if($func->post('supports')){
        foreach ($func->post('supports') as $item) {
            $db->query("INSERT INTO " . DBPREFIX . "user_supports SET user_id='".$user_id."',support_id='".$item."'");
        }
    }

    $location->redirect('admin/users');
}

$supports = $db->query("SELECT * FROM " . DBPREFIX . "supports WHERE status='1'");
//echo '<pre>';print_r($_SERVER);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>Müşteri</strong> Ekle/Düzenle</h2>
                </div>
                <!-- END Meta Data Title -->

                <!-- Meta Data Content -->
                <form action="<?php echo SITE_ADMIN; ?>new_user.php<?php echo $path; ?>" method="post" class="form-horizontal form-bordered">
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">İsim/Soyisim</label>
                        <div class="col-md-9">
                            <input type="text" id="product-meta-title" name="name" value="<?php echo $customer_info?$customer_info['name']:''; ?>" class="form-control" placeholder="İsim/Soyisim..">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-telephone">Telefon</label>
                        <div class="col-md-9">
                            <input type="text" id="product-telephone" name="telephone" value="<?php echo $customer_info?$customer_info['telephone']:''; ?>" class="form-control" placeholder="Telefon Numarası">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-point">Görevler</label>
                        <div class="col-md-9">
                            <select name="supports[]" multiple class="form-control">
                                <?php if($supports->num_rows){ ?>
                                    <?php foreach($supports->rows as $support){ ?>
                                    <option value="<?php echo $support['support_id']; ?>"><?php echo $support['name']; ?></option>
                                    <?php  } ?>
                                <?php  } ?>
                            </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; ?>users.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>$(function(){ EcomProducts.init(); });</script>

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