<?php
session_start();

$pageSequence = $_SESSION['sequence'];
$toDiv = "contentZone" . $pageSequence;
$resultZoneDiv = "resultZone" . $pageSequence;
$tableZone = "#tableZone" . $pageSequence;
$_SESSION['sequence']++;
$accordion = "accordion" . $pageSequence;

$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 = "";
$outgoingUrl = "";
$requestTimeOut = 30;
$itemId = 0;
$buttonLabel = "Ajouter";
$formId = $_REQUEST['formId'];

$sccModuleres = $dba->getSCCModules(0);
//$callreasonres = $dba->getSCCCallReasonDetails(0);

$callreasonres = $dba->getSCCCallReasonByFormId($formId, 0);
$cmpList = $dba->getSCCCCompanyList(0);

$priority = $dba->getSCCPriorities(0);

$refreshEnable = 1;

/*
  if(count($customerDtls) == 1 ){
  // Customer found renaming data;
  $firstName = $customerDtls[0]['first_name_v'];
  $lastName = $customerDtls[0]['last_name_v'];
  $company = $customerDtls[0]['location_v']."[".$customerDtls[0]['company_id_n']."]";
  $email = $customerDtls[0]['email_id_v'];
  $profession = $customerDtls[0]['email_id_v'];

  }



  $firstName = $res['first_name_v'];
  $lastName = $res['last_name_v'];
  $company = $res['location_v'];
  $email = $res['email_id_v'];
  $profession = $res['email_id_v'];
 * 
 */

$firstName = "";
$lastName = "";
$company = "";
$email = "";
$profession = "";
?>


<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
    }
    /* CSS LOADER */
    .loader_skf {
        visibility: hidden;
        border: 2px solid;
        border-color: transparent black;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        margin: 8px ;
        display: inline-block;
        position: relative;
        box-sizing: border-box;
        animation: rotation_skf 2s linear infinite;
    }
    .loader_skf::after {
        content: '';  
        box-sizing: border-box;
        position: absolute;
        left: 50px;
        top: 50%;
        border: 24px solid;
        border-color: transparent rgba(255, 255, 255, 0.15);
        border-radius: 50%;
        transform: translate(-50%, -50%);
    }

    @keyframes rotation_skf {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
    } 

</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>Créer un ticket de réclamation</h4>
                        </a>
                    </div>
                </div>
                <div class="panel-body">

                    <form action="<?php echo $currentBaseDir; ?>/exec_add_reclamation_ticket.php" method="post" enctype="multipart/form-data"  id="campaignFrm"  toDiv="<?php echo $toDiv; ?>" returnPage="<?php echo $rPage; ?>" onsubmit="return false;">

                        
                        
                        <div class="form-row">
                             <div class="form-group col-md-12">
                                <label for="company">Matricule mutualiste</label> 
                                <input type="text" onfocus="resetError()" onblur="getElement()" class="form-control" id="company" name="company" value="<?php echo $company; ?>">
                                <span id="erreurMsg" style="color:red;visibility:hidden;margin:3px;">Format du matricule saisi invalide. Le format attendu est 123456A1234.</span>
                                <span class='loader_skf' id="loader_skf"></span>
                                
                                
                            </div>
                            
                        </div>
                        
                        

                        <div class="form-row">
                            
                            
                            
                        <div class="form-group col-md-4">
                                <label for="firstName">Nom</label>
                                <input type="text" class="form-control" id="firstName" name="firstName" value="<?php echo $firstName; ?>"  readonly>
                            </div>
                        
                            <div class="form-group col-md-4">
                                <label for="lastName">Mutualiste</label>
                                <input type="text" class="form-control" id="lastName" name="lastName" value="<?php echo $lastName; ?>"   readonly>
                            </div>
                            
                             <div class="form-group col-md-4">
                                <label for="employer">Organisme employeur</label>
                                <input type="text" class="form-control" id="employer" name="employer"  value=""  readonly>
                            </div>
                             
                          
                        </div>
                        
                         <div class="form-row">
                            
                             <div class="form-group col-md-4">
                                <label for="destContactName">Nom du bénéficiaire</label>
                                <input type="text" class="form-control" id="destContactName" name="destContactName"  value=""  >
                            </div>

                            <div class="form-group col-md-4">
                                <label for="destMatricule">Matricule du bénéficiaire</label>
                                <input type="text" class="form-control" id="destMatricule" name="destMatricule"  value="">  
                            </div>
                             
                             <div class="form-group col-md-4">
                                <label for="formId" >Contact N°1 :</label>
                                <input type="tel" class="form-control" id="callerId" name="callerId" value=""   > 
                                <input type="hidden" id="formId" name="formId" value="<?php echo $formId; ?>"/>
                            </div>
                                                       


                        </div>
                        
                        
                        <div class="form-row">   
                             <div class="form-group col-md-4">
                                <label for="mobileNumber">Contact N°2 </label>
                                <input type="tel" class="form-control" id="mobileNumber" name="mobileNumber"  value=""  >
                        </div>

                            <div class="form-group col-md-4">
                                <label for="emailId">Email</label>
                                <input type="email" class="form-control" id="emailId" name="emailId"  value="">  <input type="hidden"  id="actionType" name="actionType"  value="2">
                            </div>
                            
                            
                        </div>
                        



                        <div class="form-row">

                            <div class="form-group col-md-12">
                                <label for="callModule">Objets</label>
                                <select id="callModule" name="callModule" class="form-control" >

                                    <?php for ($i = 0; $i < count($sccModuleres); $i++) { ?>
                                        <option value="<?php echo $sccModuleres[$i]['motif_id_n']; ?>" <?php if ($res['reason_category_n'] == $sccModuleres[$i]['motif_id_n']) { ?> selected="selected" <?php } ?> ><?php echo $sccModuleres[$i]['motif_name_v']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>

                        </div>

                        <div class="form-row">

                            <div class="form-group col-md-12">
                                <label for="submodule">Détails</label>
                                <select id="submodule" name="submodule" class="form-control">
                                    <option value="-1" > -- Aucun --</option>
                                    <?php
                                    if ($res['char_field_3'] != "" && $res['char_field_3'] != "-1") {

                                        $smRes = $dba->getSubMobuleById($res['char_field_3']);
                                        ?>
                                        <option value="<?php echo $smRes[0]['motif_id_n']; ?>" selected="selected" > <?php echo $smRes[0]['motif_name_v']; ?></option>

                                    <?php } ?>

                                </select>
                            </div>

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

                            <div class="form-group col-md-12">
                                <label for="callReasonType">Motif d'appel</label>
                                <select id="callReasonType" name="callReasonType" class="form-control" >
                                    <option value="-1" selected>- Veuillez selectionner une option -</option>
                                    <?php for ($i = 0; $i < count($callreasonres); $i++) { ?>
                                        <option value="<?php echo $callreasonres[$i]['motif_id_n']; ?>" <?php if ($res['reason_n'] == $callreasonres[$i]['motif_id_n']) { ?> selected="selected" <?php } ?> ><?php echo $callreasonres[$i]['motif_name_v']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>

                        </div>
                        
                
                        



                        <div class="form-row">

                            <div class="form-group col-md-12">
                                <div id="workDelay"></div>
                            </div>

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

                            <div class="form-group col-md-12">
                                <label for="callprority">Priorité</label>
                                <select id="callprority" name="callprority" class="form-control">
                                    <option value="-1" selected>- Veuillez selectionner une option -</option>
                                    <?php for ($i = 0; $i < count($priority); $i++) { ?>
                                        <option value="<?php echo $priority[$i]['priority_id_n']; ?>" <?php if ($res['char_field_4'] == $priority[$i]['priority_id_n']) { ?> selected="selected" <?php } ?> ><?php echo $priority[$i]['short_desc_v']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>

                        </div> -->

                        <div class="form-row">

                            <div class="form-group col-md-12">
                                <label for="callType">Canal d'échange</label>
                                <select id="callType" name="callType" class="form-control">
                                    <option value="Appel" <?php if ($res['char_field_5'] == "Appel") { ?> selected="selected" <?php } ?>  >Appel</option>
                                    <option value="SMS" <?php if ($res['char_field_5'] == "SMS") { ?> selected="selected" <?php } ?>  >SMS</option>
                                    <option value="Mail" <?php if ($res['char_field_5'] == "Mail") { ?> selected="selected" <?php } ?>  >Mail</option>
                                    <option value="WhatsApp" <?php if ($res['char_field_5'] == "WhatsApp") { ?> selected="selected" <?php } ?>  >WhatsApp</option>
                                    <option value="Facebook" <?php if ($res['char_field_5'] == "Facebook") { ?> selected="selected" <?php } ?>  >Facebook</option>
                                    <option value="Skype" <?php if ($res['char_field_5'] == "Skype") { ?> selected="selected" <?php } ?>  >Skype</option>
                                    <option value="Twitter" <?php if ($res['char_field_5'] == "Twitter") { ?> selected="selected" <?php } ?>  >Twitter</option>
                                    <option value="Présentiel" <?php if ($res['char_field_5'] == "Présentiel") { ?> selected="selected" <?php } ?>  >Présentiel</option>
                                    <option value="Visite" <?php if ($res['char_field_5'] == "Visite") { ?> selected="selected" <?php } ?>  >Visite</option>

                                </select>
                            </div>

                        </div>

                        <div class="form-row">

                            <div class="form-group col-md-12">
                                <label for="csrDetails">Description de la requête client</label>
                                <textarea id="csrDetails" name="csrDetails" class="form-control" cols="5" rows="3"></textarea>
                            </div>

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

                            <div class="form-group  col-md-12">

                                <label for="csrAnalysisCause">Analyse des causes</label>
                                <textarea id="csrAnalysisCause" name="csrAnalysisCause" class="form-control" cols="5" rows="3"></textarea>
                            </div>

                        </div>


 <div class="form-row">

                            <div class="form-group  col-md-12">

                                <label for="csrAnalysisComment">Analyse de la plainte ou réclamation</label>
                                <textarea id="csrAnalysisComment" name="csrAnalysisComment" class="form-control" cols="5" rows="3"></textarea>
                            </div>

                        </div>

 <div class="form-row">

                            <div class="form-group  col-md-12">

                                <label for="csrDecisionComment">Décision</label>
                                <textarea id="csrDecisionComment" name="csrDecisionComment" class="form-control" cols="5" rows="3"></textarea>
                            </div>

                        </div>

                        <div class="form-row">

                            <div class="form-group  col-md-12">

                                <label for="listDeFichier" class="form-label">Selectionnez les fichiers</label>
                                <input class="form-control" type="file" id="listDeFichier" name="listDeFichier[]" multiple />
                            </div>

                        </div>

                        <div class="form-group  col-md-12">



                            <button type="submit" class="btn btn-primary" id="save" >Enregistrer</button> 
                            <button type="button" class="btn close-btn" data-dismiss="modal">Fermer</button>




                        </div>




                    </form>


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


</section>





<script type="text/javascript" src="js/jquery.loading.min.js"></script>
<div id="loader" ></div>

<script>
    // DECLARATION DU MESSAGE ERREUR
    var idErreurMsg = document.getElementById('erreurMsg');

    // POUR RECACHER LE MESSAGE ERREUR QUAND L'UTILISATEUR VA SAISIER ENTRER DANS LE INPUT
    function resetError(){
        idErreurMsg.style.visibility = "hidden";
    }
    // FONCTION POUR RECUPERER LES INFOS A PARTIR DU MATRICULE
    
    function getElement(){
        var classLOADER = document.getElementById('loader_skf')
        var idFirtName = document.getElementById('firstName')
        var idLastName = document.getElementById('lastName')
        var idCallerId = document.getElementById('callerId')
        var idCallerId2 = document.getElementById('mobileNumber')
        var idOrganisme =document.getElementById('employer')
        var idCompany = document.getElementById('company').value
        
        // condition pour s'assurer que le champs n'est pas vide
        if(idCompany !== null && idCompany !== ""){
            
        //les configurations pour API    
        var myHeaders = new Headers();
        myHeaders.append("AUTH_TOKEN", "929c39cc4915d2dada2472ffe8feddccb7defa9b2eaff05aaade5360c3f4d02b");
        myHeaders.append("FORMAT_RESPONSE", "JSON");
        classLOADER.style.visibility='visible';
        var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'
        };
        fetch("https://apps.mugef-ci.org/ords/partner/svam/getInfos?SEARCH_MATRICULE="+idCompany, requestOptions)
        .then(response => response.json())
        .then(result => {
            if(result?.status === "KO"){
                idErreurMsg.style.visibility='visible';
                return
            }
            let Recup = result?.mutualiste; // Récupération de la valeur de la variable mutualiste depuis l'API
            let mots = Recup.split(' '); 
            let premierMot = mots[0]; 

            idFirtName.value = premierMot;
            idLastName.value = result?.mutualiste;
            idCallerId.value = result?.tel1;
            idCallerId2.value = result?.tel2;
            idOrganisme.value = result?.orgemp;
            //console.log(result);
            })
        .catch(error => console.log('error', error))
        .finally( 
            res => classLOADER.style.visibility='hidden'
        )
        }
        //fin de API
       
    }
    </script>

<script>
                        $(function () {
                            $("#<?php echo $accordion; ?>").accordion();
                        });
</script>

<script type="text/javascript">

    var $modal = $('#myModal');
    var $myLargeModal = $('#myLargeModal');


    $('button[type=submit]').click(function (event) {
        event.preventDefault();

        // retrieve form element
        var form = this.form;
        // prepare data
        var data = new FormData(form);
        // get url
        var url = form.action;

        // send request
        $.ajax({
            type: 'POST',
            url: url,
            data: data,
            dataType: 'json',
            processData: false,
            contentType: false
        }).done(function (obj) {
            if (obj.errorCode === 0) {

                $('#loader').hide();
                $.confirm({

                    icon: 'glyphicon glyphicon-ok',
                    title: 'RESULTAT',
                    content: obj.errorMessage,
                    type: 'green',
                    typeAnimated: true,
                    buttons: {
                        confirm: {
                            text: 'OK',
                            btnClass: 'btn-blue',
                            action: function () {

                                $("#content-wrapper").load("<?php echo $_SERVER['PHP_SELF']; ?>?rPage=<?php echo $rPage; ?>&item=<?php echo $res['item_id_n']; ?>");


                                                            }

                                                        }}
                                                });
                                                //  alert(data);


                                            } else {

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




                                    $(".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');

                                    });


</script>

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


<script type="text/javascript" src="js/jquery.loading.min.js"></script>



<script type="text/javascript">






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

                                        });
                                        var dataURL = $(this).attr('data-href');
                                        var modalTitle = $(this).attr('title');
                                        $('.modal-title').html(modalTitle);
                                        $('#loader').loading('stop');
                                        $('.modal-body').load(dataURL, function () {
                                            $modal.modal({
                                                backdrop: 'static',
                                                keyboard: false
                                            });
                                        });


                                    });




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

                                        });
                                        var dataURL = $(this).attr('data-href');
                                        var modalTitle = $(this).attr('title');
                                        $('.modal-title').html(modalTitle);
                                        $('#loader').loading('stop');
                                        $('.modal-body').load(dataURL, function () {
                                            $modal.modal({
                                                backdrop: 'static',
                                                keyboard: false
                                            });
                                        });


                                    });



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

                                        });
                                        var dataURL = $(this).attr('data-href');
                                        var modalTitle = $(this).attr('title');
                                        $('.modal-title').html(modalTitle);
                                        $('#loader').loading('stop');
                                        $('.modal-body').load(dataURL, function () {
                                            $modal.modal({
                                                backdrop: 'static',
                                                keyboard: false
                                            });
                                        });


                                    });









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

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

                                    });


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

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

                                    });



</script>


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