<?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 = '';
$working_info = array();
if($func->get('working_id')) {
    $path = '?working_id='.$func->get('working_id');
    $working_info = $db->query("SELECT * FROM " . DBPREFIX . "working_plan WHERE working_id='" . $func->get('working_id') . "'");
    $working_info = $working_info->num_rows?$working_info->row:array();
}
if($_SERVER['REQUEST_METHOD']=='POST') {
    $query = array();
    foreach ($func->post() as $key=>$item) {
        if($key=='day' || $key=='time'){
            $item = serialize($item);
        }
        $query[] = $key."='".$db->escape($item)."'";
    }


    if($func->get('working_id')){
        $sql = "UPDATE " . DBPREFIX . "working_plan SET " . implode(',', $query)." WHERE working_id='".$func->get('working_id')."'";
        $db->query($sql);
        $working_id = $func->get('working_id');
        $db->query("DELETE FROM ".DBPREFIX."working_days WHERE working_id='".$working_id."'");
        $db->query("DELETE FROM ".DBPREFIX."working_times WHERE working_id='".$working_id."'");
    }else {
        $db->query("DELETE FROM ".DBPREFIX."working_plan WHERE user_id='".$func->post('user_id')."'");
        $sql = "INSERT INTO " . DBPREFIX . "working_plan SET " . implode(',', $query);
        $db->query($sql);
        $working_id = $db->getLastId();
    }
    //echo '<pre>';print_r($sql);echo '</pre>';die();
    if($func->post('day')){
        foreach ($func->post('day') as $item) {
            $db->query("INSERT INTO ".DBPREFIX."working_days SET working_id='".$working_id."',`day`='".$item."'");
        }
    }

    if($func->post('time')){
        foreach ($func->post('time') as $item) {
            $db->query("INSERT INTO ".DBPREFIX."working_times SET working_id='".$working_id."',`time`='".$item."'");
        }
    }
    $location->redirect('admin/work_plan');
}

$users = $db->query("SELECT * FROM ".DBPREFIX."users ");
//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>Çalışma Planı</strong> Ekle/Düzenle</h2>
                </div>
                <!-- END Meta Data Title -->

                <!-- Meta Data Content -->
                <form action="<?php echo SITE_ADMIN; ?>new_working.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">Personel Adı</label>
                        <div class="col-md-9">
                            <select class="form-control" name="user_id">
                                <?php if($users->num_rows){ ?>
                                <?php foreach($users->rows as $user){ ?>
                                <option value="<?php echo $user['user_id']; ?>" <?php echo $working_info?($working_info['user_id']==$user['user_id']?'selected="selected"':''):''; ?>><?php echo $user['name']; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">Günler</label>
                        <div class="col-md-9">
                            <select class="form-control" name="day[]" multiple>
                                <option value="Monday" <?php if($working_info){ $gunler = unserialize($working_info['day']); echo in_array('Monday',$gunler)?'selected="selected"':''; } ?>>Pazartesi</option>
                                <option value="Tuesday" <?php if($working_info){ $gunler = unserialize($working_info['day']); echo in_array('Tuesday',$gunler)?'selected="selected"':''; } ?>>Salı</option>
                                <option value="Wednesday" <?php if($working_info){ $gunler = unserialize($working_info['day']); echo in_array('Wednesday',$gunler)?'selected="selected"':''; } ?>>Çarşamba</option>
                                <option value="Thursday" <?php if($working_info){ $gunler = unserialize($working_info['day']); echo in_array('Thursday',$gunler)?'selected="selected"':''; } ?>>Perşembe</option>
                                <option value="Friday" <?php if($working_info){ $gunler = unserialize($working_info['day']); echo in_array('Friday',$gunler)?'selected="selected"':''; } ?>>Cuma</option>
                                <option value="Saturday" <?php if($working_info){ $gunler = unserialize($working_info['day']); echo in_array('Saturday',$gunler)?'selected="selected"':''; } ?>>Cumartesi</option>
                                <option value="Sunday" <?php if($working_info){ $gunler = unserialize($working_info['day']); echo in_array('Sunday',$gunler)?'selected="selected"':''; } ?>>Pazar</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">Saatler</label>
                        <div class="col-md-9">
                            <select class="form-control" name="time[]" multiple >
                                <?php for($i=1;$i<25;$i++){ ?>
                                    <option value="<?php echo $i; ?>" <?php if($working_info){ $gunler = unserialize($working_info['time']); echo in_array($i,$gunler)?'selected="selected"':''; } ?>><?php echo $i.':00'; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-telephone">Durum</label>
                        <div class="col-md-9">
                            <select class="form-control" name="status">
                                <option value="1" <?php echo $working_info?($working_info['status']?'selected="selected"':''):''; ?>>Aktif</option>
                                <option value="0" <?php echo $working_info?(!$working_info['status']?'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> Save</button>
                            <a href="<?php echo SITE_ADMIN; ?>working_plan.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'; ?>
