<?php session_start();
$pageSequence = $_SESSION['sequence'];
$toDiv = "contentZone" . $pageSequence;
$resultZoneDiv = "resultZone" . $pageSequence;
$_SESSION['sequence']++;
//echo  dirname(__FILE__);  
//echo $_SERVER['DOCUMENT_ROOT'] . "   ".dirname(__FILE__)."  ".str_replace($_SERVER['DOCUMENT_ROOT'],"",dirname(__FILE__)) . " SELF :" .$_SERVER['PHP_SELF']  ;
$toDiv = $_REQUEST['toDiv'];
$rpage = $_REQUEST['rpage'];
$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir']."core.php");

$res = $dba->getSCCTicketDetailById($_REQUEST['item']) ;
$callreasonres = $dba->getSCCCallReasonDetails(0);
$customerTypeRes = $dba->getSCCCustomerType( 0 );
$cmpList = $dba->getSCCCCompanyList(0);

?>

<form action="<?php echo $currentBaseDir; ?>/exec_edit_ticket.php" method="post" enctype="multipart/form-data"  id="campaignFrm"  toDiv="<?php echo $toDiv; ?>" returnPage="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return false;">
<div class="form-row">
    <div class="form-group col-md-6">
      <label >N° Ticket : </label>
      <h4 class="svam-ticket-nb" ><?php echo $res['caller_ticket_id']; ?> <input type="hidden" name="csrId" id="csrId" value="<?php echo $res['caller_ticket_id']; ?>" /><input type="hidden" name="operationId" id="operationId" value="1" /></h4>
    </div>
    <div class="form-group col-md-6">
      <label >N° Appelant :</label>
      <h3><?php echo $res['from_callerid_v']; ?> <input type="hidden" name="callerId" id="callerId" value="<?php echo $res['from_callerid_v']; ?>" /><input type="hidden" name="callId" id="callId" value="<?php echo $_REQUEST['item']; ?>" /></h3>
    </div>
  </div>
  
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="firstName">Nom</label>
      <input type="text" class="form-control" id="firstName" name="firstName" value="<?php echo $res['first_name_v']; ?>">
    </div>
    <div class="form-group col-md-6">
      <label for="lastName">Pr&eacute;noms</label>
      <input type="text" class="form-control" id="lastName" name="lastName" value="<?php echo $res['last_name_v']; ?>">
    </div>
  </div>
  <div class="form-row">   
    <div class="form-group col-md-4">
      <label for="company">Soci&eacute;t&eacute;</label> 
      <p><input type="text" class="form-control" id="company" name="company" value="<?php echo $res['company_name_v']; ?>"></p>
      <!--
       <select id="company" name="company" class="form-control">
        <option value="-1" selected>- Veuillez selectionner une option -</option>
        <?php for ($i=0; $i<count($cmpList); $i++){ ?>
        <option value="<?php echo $cmpList[$i]['company_id_n']; ?>" <?php if( $res['company_name_v'] == $cmpList[$i]['company_id_n']){ ?> selected="selected" <?php } ?> ><?php echo $cmpList[$i]['company_name_v']; ?></option>
        <?php }?>
      </select>
      -->
    </div>
    <div class="form-group col-md-4">
      <label for="mobileNumber">Contact</label>
      <input type="tel" class="form-control" id="mobileNumber" name="mobileNumber"  value="<?php echo $res['contact_v']; ?>">
    </div>
    
     <div class="form-group col-md-4">
      <label for="emailId">Email</label>
      <input type="email" class="form-control" id="emailId" name="emailId"  value="<?php echo $res['email_id_v']; ?>">  <input type="hidden"  id="actionType" name="actionType"  value="2">
    </div>
  </div>
  
  <div class="form-row">
  
  <div class="form-group col-md-12">
      <label for="customerType">Type</label>
      <select id="customerType" name="customerType" class="form-control">
        <option value="-1" selected>- Veuillez selectionner une option -</option>
        <?php for ($i=0; $i<count($customerTypeRes); $i++){ ?>
        <option value="<?php echo $customerTypeRes[$i]['type_id_n']; ?>" <?php if( $res['type_id_n'] == $customerTypeRes[$i]['type_id_n']){ ?> selected="selected" <?php } ?> ><?php echo $customerTypeRes[$i]['type_v']; ?></option>
        <?php }?>
      </select>
    </div>
  
  </div>
  
  <div class="form-row">
  
  <div class="form-group col-md-12">
      <label for="callReasonType">Liste des motifs 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">
      <label for="callType">Type de requête</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="IM" <?php if( $res['char_field_5'] == "IM" ){ ?> selected="selected" <?php } ?>  >Instant Messaging</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"><?php echo $res['ticket_reason_desc_v']; ?></textarea>
    </div>
  
  </div>
  <div class="form-row">
  
  <div class="form-group  col-md-12">
     <?php
	  $dbres = $dba->getTicketCommentsByTicketId($res['caller_ticket_id']) ;
	 ?>
      <label for="csrComment">Commentaire du conseiller client</label>
      <textarea id="csrComment" name="csrComment" class="form-control" cols="5" rows="3"><?php echo $dbres[0]['comment_v']; ?></textarea>
    </div>
  
  </div>
  
  
  
  
 
  <button type="submit" class="btn btn-primary" id="save">Enregistrer</button> <button type="button" class="btn btn-primary" id="closeTicket">Clôturer</button> <button type="button" class="btn close-btn" data-dismiss="modal">Fermer</button>
</form>
                


<script type="text/javascript">

$('#closeTicket').on('click', function(e){
	document.getElementById('actionType').value = 3;
   $(this).parents('form').submit() ;
});

$('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 );
			 
			 $.alert({
				 icon: 'glyphicon glyphicon-ok',
				 title: 'RESULTAT',
                 content: obj.errorMessage,
                 type: 'green',
                 typeAnimated: true

				});
			    //  alert(data);
            $("#" + toDiv).load(rpage);
            $('#loader').hide();
			   
		   }
		   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(); */
    });

$(".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'
		  
		});
		
		   $("#<?php echo $toDiv; ?>").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">
$(function() {
    
    //autocomplete
    $("#company").autocomplete({
        source: "<?php echo $currentBaseDir; ?>/companysearch.php"	
    });                

});
</script>

