<?php
session_start();

$pageSequence = $_SESSION['sequence'];
$toDiv = "contentZone" . $pageSequence;
$resultZoneDiv = "resultZone" . $pageSequence;
$tableZone = "#tableZone" . $pageSequence;
$startDateName = "startDate".$pageSequence ;
$endDateName = "endDate".$pageSequence ;
$startHourName = "startHour".$pageSequence ;
$endHourName = "endHour".$pageSequence;

$_SESSION['sequence']++;


$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir'] . "veriflog.php");
include($_SESSION['basedir'] . "core.php");

$rPage = $_REQUEST['rPage'];
$rDiv = $_REQUEST['rDiv'];
if (isset($_REQUEST['opId'])) {
    $opId = $_REQUEST['opId'];
}


$channelName = "";
$description = "";
$calendarType = 0;
$calendardName = "";
$fullTimeIndicator = "oui";
$itemId = 0;


$monday = 'checked="checked"';
$tuesday = 'checked="checked"';
$wednesday = 'checked="checked"';
$thursday = 'checked="checked"';
$friday = 'checked="checked"';
$saturday = 'checked="checked"';
$sunday = 'checked="checked"';

$startDate = date('Y-m-d');
$endDate = date('Y-m-d');
$startHour = "00:00:00";
$endHour = "23:59:59";

$buttonLabel = "Ajouter";

if ($opId > 0) {
    $itemId = $_REQUEST['itemId'];
    $res = $dba->SCCGetCalendarDtlsById($itemId);

    $calendardName = $res[0]['calendar_name_v'];
    $description = $res[0]['calendar_desc_v'];
    $fullTimeIndicator = $res[0]['fulltime_indicator_v'];
    $startDate = $res[0]['start_date_d'];
    $endDate = $res[0]['end_date_d'];
    $startHour = $res[0]['start_time_d'];
    $endHour = $res[0]['end_time_d'];
    $calendarType = $res[0]['type_id_n'];
    $daylist = $res[0]['days_list_v'];
    $buttonLabel = "Modifier";
    
    
$monday = '';
$tuesday = '';
$wednesday = '';
$thursday = '';
$friday = '';
$saturday = '';
$sunday = '';
    
    
    
    
    $daysArr = explode(",", $daylist);
    
    for($p = 0 ; $p < count($daysArr); $p++){
        
        switch ($daysArr[$p]){
            
            case 0 : $sunday = 'checked="checked"';
                  break;
            case 1 : $monday = 'checked="checked"';
                  break;
            case 2 : $tuesday = 'checked="checked"';
                  break;
            case 3 :  $wednesday = 'checked="checked"';
                  break;
            case 4 :  $thursday = 'checked="checked"';
                  break;
            case 5 :  $friday = 'checked="checked"';
                  break;
            case 6 :  $saturday = 'checked="checked"';
                  break;
            
            
        }
        
        
        
    }
    
    
}
?>


<style type="text/css">

    /* Style the form */
    #regForm {
        background-color: #ffffff;
        margin: 5px auto;
        padding: 40px;
        width: 80%;
        min-width: 300px;
    }

    /* Style the input fields */
    input {
        padding: 10px;
        width: 50%;
        font-size: 17px;
        font-family: Raleway;
        border: 1px solid #aaaaaa;
    }

    textarea {
        padding: 10px;
        width: 50%;
        font-size: 17px;
        font-family: Raleway;
        border: 1px solid #aaaaaa;
    }

    /* Mark input boxes that gets an error on validation: */
    input.invalid {
        background-color: #ffdddd;
    }

    /* Hide all steps by default: */
    .tab {
        display: none;
    }

    /* Make circles that indicate the steps of the form: */
    .step {
        height: 15px;
        width: 15px;
        margin: 0 2px;
        background-color: #bbbbbb;
        border: none;
        border-radius: 50%;
        display: inline-block;
        opacity: 0.5;
    }

    /* Mark the active step: */
    .step.active {
        opacity: 1;
    }

    /* Mark the steps that are finished and valid: */
    .step.finish {
        background-color: #04AA6D;
    }

    .moduleId323 { background-color:#E9F2FE }


</style>
<section class="content" >
    <div class="row">
        <div class="col-sm-12">
            <div class="panel panel-bd lobidrag">
                <div class="panel-heading">
                    <div class="btn-group" id="buttonexport">
                        <a href="#">
                            <h4>Ajouter/Modifier un calendrier</h4>
                        </a>
                    </div>
                </div>
                <div class="panel-body">

                    <form action="<?php echo $currentBaseDir; ?>/exec_add_edit_calendar.php"  method="post" onsubmit="return false;">
                        <div class="col-sm-6" >
                            <div class="form-group">
                                <label>Identifiant</label>
                                <input type="text" class="form-control" id="calendardName" required name="calendardName" value="<?php echo $calendardName; ?>">

                                <input type="hidden" name="opId"  id="opId" value="<?php echo $opId; ?>">
                                <input type="hidden" name="itemId"  id="itemId" value="<?php echo $itemId; ?>">






                            </div>

                            <div class="form-group">
                                <label>Description</label>
                                <textarea id="description" name="description" class="form-control" ><?php echo $description; ?></textarea>
                            </div>

                            <div class="form-row">

                                <div class="form-group col-md-3">
                                    <label>Lundi</label>
                                    <input type="checkbox" <?php echo $monday; ?>   name="daysList[]" value="1"  />

                                </div>

                                <div class="form-group col-md-3">
                                    <label>Mardi</label>
                                    <input type="checkbox" <?php echo $tuesday; ?>   name="daysList[]" value="2"  />


                                </div>

                                <div class="form-group col-md-3">
                                    <label>Mercredi</label>
                                    <input type="checkbox" <?php echo $wednesday; ?>  name="daysList[]" value="3"  />


                                </div>
                                
                                 <div class="form-group col-md-3">
                                   <label>Jeudi</label>
                                <input type="checkbox" <?php echo $thursday; ?>  name="daysList[]" value="4"  />


                                </div>





                            </div>
                          
                            <div class="form-row">

                                <div class="form-group col-md-4">
                                    <label>Vendredi</label>
                                <input type="checkbox" <?php echo $friday; ?>   name="daysList[]" value="5"  />
                                </div>

                                <div class="form-group col-md-4">
                                   <label>Samedi</label>
                                <input type="checkbox"  <?php echo $saturday; ?>   name="daysList[]" value="6"  />


                                </div>

                                <div class="form-group col-md-4">
                                     <label>Dimanche</label>
                                <input type="checkbox" <?php echo $sunday; ?>   name="daysList[]" value="0"  />


                                </div>
                                
                              



                            </div>
                            
                            
                            <div class="form-group">
                                <label>Type de calendrier</label>
                                <select name="calendarType" id="calendarType" class="form-control">
                                    <option value="0" <?php if ($calendarType == 0) { ?>  selected="selected" <?php } ?>>Normal</option>
                                    <option value="1" <?php if ($calendarType == 1) { ?>  selected="selected" <?php } ?>>Exceptionnel</option>

                                </select>
                            </div>

                            <div class="form-group">
                                <label>Temps plein</label>
                                <select name="fullTimeIndicator" id="fullTimeIndicator" class="form-control">
                                    <option value="Oui" <?php if ($fullTimeIndicator == "Oui") { ?>  selected="selected" <?php } ?> >Oui</option>
                                    <option value="Non" <?php if ($fullTimeIndicator == "Non") { ?>  selected="selected" <?php } ?>>Non</option>

                                </select>
                            </div>

                         
                         


                            <div class="reset-button">
                                <button   type="submit" class="btn addition"><?php echo $buttonLabel; ?></button>  <button type="button" class="btn close-btn" >Fermer</button>
                            </div>

                        </div>

                        <div class="col-sm-6" >

                            

                            <div class="form-group">
                                <label>Date de début</label>
                                <div class="form-group">
                                    
                                 <div class='input-group date' id='<?php echo $startDateName ; ?>'>
                                        <input name="startDate" type='text' class="form-control" id="startDate"  value="<?php echo $startDate; ?>" size="30"/>
                                        <span class="input-group-addon">
                                            <span class="glyphicon glyphicon-calendar"></span>
                                        </span>
                                    </div>
                                    
                                    
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Heure de début</label>
                                <div class="form-group">
                                    <div class='input-group date' id='<?php echo $startHourName; ?>'>
                                        <input name="startHour" type='text' class="form-control" id="startHour"  value="<?php echo $startHour; ?>" size="30"/>
                                        <span class="input-group-addon">
                                            <span class="glyphicon glyphicon-time"></span>
                                        </span>
                                    </div>
                                </div>
                            </div>



                            <div class="form-group">
                                <label>Date de fin</label>
                                <div class="form-group">
                                    <div class='input-group date' id='<?php echo $endDateName ; ?>'>
                                        <input name="endDate" type='text' class="form-control" id="endDate"  value="<?php echo $endDate; ?>" size="30"/>
                                        <span class="input-group-addon">
                                            <span class="glyphicon glyphicon-calendar"></span>
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Heure de fin</label>
                                <div class="form-group">
                                    <div class='input-group date' id='<?php echo $endHourName; ?>''>
                                        <input name="endHour" type='text' class="form-control" id="endHour"  value="<?php echo $endHour; ?>" size="30"/>
                                        <span class="input-group-addon">
                                            <span class="glyphicon glyphicon-time"></span>
                                        </span>
                                    </div>
                                </div>
                            </div>




                        </div>

                    </form>


                </div>
            </div>
        </div>
    </div>


</section>





<script type="text/javascript">

    $(".close-btn").on('click', function () {
        // create the backdrop and wait for next modal to be triggered
        // $('body').modalmanager('loading');
        $('#loader').loading({
            message: "Chargement...",
            theme: 'dark'

        });

        $("#content-wrapper").load("<?php echo $rPage; ?>");
        $('#loader').loading('stop');

    });


    $(document).ready(function () {


        $("#roleId").change(function () {

            $("#userId").load("<?php echo $currentBaseDir; ?>/get_available_agents.php?id=" + $(this).val());

        });



        $('form').on("submit", function (event) {

            var toDiv = $("form").attr('toDiv');
            var rpage = $("form").attr('returnPage');

            $.post($(this).attr("action"), $(this).serialize(), function (data) {

                var obj = jQuery.parseJSON(data);

                if (obj.errorCode == 0) {

                    //alert( obj.errorMessage );

                    //    $.alert( obj.errorMessage );

                    $.confirm({
                        icon: 'glyphicon glyphicon-ok',
                        title: 'RESULTAT',
                        content: obj.errorMessage,
                        type: 'green',
                        typeAnimated: true,
                        buttons: {
                            confirm: {
                                text: 'OK',
                                action: function () {
                                    $("#content-wrapper").load("<?php echo $rPage; ?>");
                                }

                            }


                        }

                    });
                    //  alert(data);


                } else {


                    $.alert({
                        icon: 'glyphicon glyphicon-warning-sign',
                        title: 'RESULTAT',
                        content: obj.errorMessage,
                        type: 'red',
                        typeAnimated: true
                    });

                    // $.alert( obj.errorMessage );
                }

            });
            // return false; // prevent normal submit


            event.preventDefault();

            /*
             var postData = $(this).serializeArray();
             var formURL = $(this).attr("action");
             
             $.ajax({
             url: formURL,
             type: "POST",
             data: postData,
             success: function(data, textStatus, jqXHR) {
             
             alert("Processing the form...");
             
             },
             error: function(jqXHR, status, error) {
             console.log(status + ": " + error);
             }
             });
             event.preventDefault(); */
        });



  $(function () {
                            $('#<?php echo $startDateName; ?>').datepicker({format: 'yyyy-mm-dd', autoclose:true, language:'fr'});
                            
                            
                            
                        });

                    
                    
                        $(function () {
                            $('#<?php echo $endDateName; ?>').datepicker({format: 'yyyy-mm-dd', autoclose:true, language:'fr'});
                        });
                        
                        
              /*          
              $(function () {
                            $('#<?php echo $startHourName; ?>').datetimepicker({format:'hh:mm:ss', pickDate: false });
                            
                            
                            
                        });

                    
                    
                        $(function () {
                            $('#<?php echo $endHourName; ?>').datetimepicker({format:'hh:mm:ss', pickDate: false });
                        });
                        */


    });






</script>

<link rel="stylesheet" href="css/jquery-confirm.min.css">
<script src="js/jquery-confirm.min.js"></script>
<script type="text/javascript" src="js/svam.js" ></script>

