<?php
session_start();
$pageSequence = $_SESSION['sequence'];
$toDiv = "contentZone" . $pageSequence;
$resultZoneDiv = "resultZone" . $pageSequence;
$tableZone = "#tableZone" . $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['userId'])) {
    $userId = $_REQUEST['userId'];
}


if (isset($_REQUEST['opId'])) {
    $opId = $_REQUEST['opId'];
}
else {
    
    $opId = 0;
}

if (isset($_REQUEST['itemId'])) {
    $itemId = $_REQUEST['itemId'];
}

$buttonLabel = "Ajouter";


$res = $dba->getSystemRoles($_SESSION['ownerId'], false, 0, 0);

$profileRes = $dba->getSCCProfileList(0) ;

$userId = "";
$profileId = "";
$callerId = "";
$username= "";


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

    $userId = $res[0]['user_id_n'];
    $profileId = $res[0]['profile_id_n'];
    $callerId = $res[0]['caller_id_v'];
    $username = $res[0]['user_name_v']; 
    
   // echo $groupId;
   
    $buttonLabel = "Modifier";
}




?>


<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 ou modifier  un utilisateur</h4>
                        </a>
                    </div>
                </div>
                <div class="panel-body">

                    <form id="regForm" action="<?php echo $currentBaseDir; ?>/exec_add_edit_sccuser.php"  method="post" onsubmit="return false;">
                        <?php if($opId!=1) { ?>
                              <div class="form-group">
                                 <label>Rôles</label>
                                 <select name="roleId" id="roleId" class="form-control">
                                     <option value="-1">-- Aucun --</option>
                                       <?php
                                 

                                    for ($i = 0; $i < count($res); $i++) {
                                        ?>
                                       <option value="<?php echo $res[$i]['role_id_n']; ?>"><?php echo $res[$i]['role_name_v']; ?></option>

                                    <?php }  ?>
                                 </select>
                              </div>
                       
                              <div class="form-group">
                                 <label>Utilisateur</label>
                                 <select name="userId" id="userId" class="form-control">
                                    <option value="-1">-- Aucun --</option>
                                 </select>
                                 
                              </div>
                        
                        
                        
                         <?php } ?>
                        
                         <input type="hidden" name="uid" id="uid"  size="50" value="<?php echo $userId; ?>"   />
                         <input type="hidden" name="opId" id="opId"  size="50" value="<?php echo $opId; ?>"   />

                         
                          <?php if($opId == 1) { ?>
                            <div class="form-group">
                                <label style="font-size: 16px">Utilisateur : <strong style="color:RED"><?php echo $username; ?></strong></label>
                         
                            </div>
                         <?php } ?>
                         
                         <div class="form-group">
                                 <label>Profil</label>
                                 <select name="profileId" id="profileId" class="form-control">
                                     <option value="-1">-- Aucun --</option>
                                       <?php
                                 

                                    for ($i = 0; $i < count($profileRes); $i++) {
                                        ?>
                                     <option value="<?php echo $profileRes[$i]['profile_id_n']; ?>"  <?php if($profileId == $profileRes[$i]['profile_id_n']) { ?>  selected="selected" <?php } ?>    ><?php echo $profileRes[$i]['profile_name_v']; ?></option>

                                    <?php }  ?>
                                 </select>
                              </div>
                        
                        
                        
                        
                              <div class="form-group">
                                  <label for="callerId">Numéro d'appel</label>
                                  <input type="text" name="callerId" id="callerId"  size="50" value="<?php echo $callerId ?>"  placeholder="Numéro d'appel" class="form-control"   />

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

                        </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(); */
        });



    });



</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>