
 setInterval(function(){
    notification_guest();
    refresh_chat_guest_form();
 },1000);

 function refresh_chat_guest_form(){
    $('.chat_guest_history').each(function(){
      var email_guest = $(this).attr('email_guest');
      var admin_name = $(this).attr('admin_name');
      refresh_dialog_guest(email_guest,admin_name);
    });
}

function refresh_dialog_guest(email_guest,admin_name){
    $.ajax({
       url:"chat/guestbook/refresh_dialog_chat_guest.php",
       method:"POST",
       data:{email_guest:email_guest,
            admin_name:admin_name},
       success:function(data){
       $('#history').html(data);
       }
      })
  }

function notification_guest(){
    $.ajax({
        url: 'chat/guestbook/notification_guest.php',
        method : "GET",
     }).done(function(data){
     $('#guest').html(data);
     });
}  

function guest_data(){
 
    $.ajax({
        url: 'chat/guestbook/dataguest.php',
        method : "GET",
     }).done(function(data){
     $('.guest_data_chat').html(data);
     });

} 

function validasi_form(){

    var admin_name = $('select#fnid_admin option:selected').text();
    var username_guest = $('#username').val();
    var email_guest = $('#email').val();
    var chat = false;


    if($.trim(email_guest)  == ''){
        
         Swal.fire({ type: 'warning', title: 'Email Wajib Disi!' }); 
         chat = false;   
     }else if($.trim(username_guest) == ''){
         Swal.fire({ type: 'warning', title: 'Username Wajib Disi!' }); 
         chat = false;  
         
    }else{
         chat = true;  
    }

   if(chat){
     
     buat_chat(email_guest,username_guest,admin_name);
                                                                                                                                                                                   
    }

}

function buat_chat(email_guest,username_guest,admin_name){

    var modal_content = '<div id="dialog" class="user_dialog" title="Administrator : '+admin_name+'">';
        modal_content += '<div class="chat_guest_history" email_guest="'+email_guest+'" admin_name="'+admin_name+'" data-touserid="'+username_guest+'" id="history">';
        modal_content += muat_ulang_chat(email_guest,admin_name);
        modal_content += '</div>';
        modal_content += '<div class="form-group">';
        modal_content += '<textarea rows="2" placeholder="Write a message..." cols="46" admin_name="'+admin_name+'" email_guest="'+email_guest+'" username_guest="'+username_guest+'" name="'+admin_name+'" id="guest_chat_textbox" class="form-control txtarea"></textarea>';
        modal_content += '</div><div class="form-group" align="right">';
        modal_content += '<button type="button" email="'+email_guest+'" username="'+username_guest+'"  email="'+email_guest+'" name="send_chat" id="'+admin_name+'" class="btn btn-success sender" style="display:none;">Send</button></div></div>';
       $('#chatdiv').html(modal_content);

       $("#dialog").dialog({
        autoOpen:false,
        width:400,
        show: {
            effect: "drop",
            duration: 1000
          },
          hide: {
            effect: "drop",
            duration: 1000
          }
       });

       $("#dialog").dialog('open');

       autoscroll();

       $("#user_chat_guest").hide();
       $("#user_chat_guest").removeClass("in");
       $(".modal-backdrop").remove();

  } 
  
  //send chat enter
$(document).on('keydown', '.txtarea', function(){

  var username_guest = $(this).attr('username_guest'); 
  var chat_message = $('#guest_chat_textbox').val();
  var email_guest = $(this).attr('email_guest');
  var admin_name = $(this).attr('admin_name');

  if (event.keyCode == 13) {

     if( $.trim(chat_message) == ''){
      //   alert('Mohon Isi Pesan... ');
     }else{
        
      $.ajax({
          url: 'chat/guestbook/insert_chat_guest.php',
          method : "POST",
          data :{admin_name:admin_name, 
          chat_message:chat_message,
          email_guest:email_guest,
          username_guest:username_guest}
       }).done(function(data){
      
          //mengosongkan input setelah mengirim pesan
          $('#guest_chat_textbox').val('');
          //Hasil Chat akan di kirim ke chatstory 
          $('#history').html(data);

          autoscroll();

       });

    
           
        }
  }
  });


  function muat_ulang_chat(email_guest,admin_name){
    $.ajax({
       url:"chat/guestbook/muat_ulang_chat.php",
       method:"POST",
       data:{admin_name:admin_name,email_guest:email_guest},
       success:function(data){
       $('#history').html(data);
       }
      })
  }

  
function buka_tamu(username_guest,email_guest){

    $.ajax({
        url:"chat/guestbook/buka_tamu.php",
        method:"POST",
        data:{email_guest:email_guest},
        success:function(data){
         $('#chat_history_guest'+username_guest).html(data);
        }
       })

  }

  function update_status_chat_guest(email){
    $.ajax({
       url: 'chat/guestbook/update_status_chat_guest.php',
       method : "POST",
       data :{to_user_id:email}
    }).done(function(data){
    });
  } 

  function open_book(username_guest,email_guest){
  
    var modal_content = '<div id="user_dialog_'+username_guest+'" class="user_dialog" title="'+email_guest+'">';
    modal_content += '<div style="" class="chat_history_guest" data-touserid="'+username_guest+'"  email_guest="'+email_guest+'" id="chat_history_guest'+username_guest+'">';
    modal_content += buka_tamu(username_guest,email_guest);
    modal_content += '</div>';
    modal_content += '<div class="form-group">';
    modal_content += '<textarea rows="2" style="border-radius: 10px; border-color:#c7f5c0;  background-color: #f8f8f8; border-width: 3px;" cols="46" placeholder="Write a message..." email_guest="'+email_guest+'" username_guest="'+username_guest+'" name="chat_message_'+email_guest+'" id="chat_message_'+username_guest+'" class="form-control txtarea_toguest"></textarea>';
    modal_content += '</div><div class="form-group" align="right">';
    modal_content += '<button type="button" style="display:none;" name="send_chat" id="'+username_guest+'" email="'+email_guest+'" class="btn btn-success send_guest">Send</button></div></div>';  
    
    $('#chat_guest_detail').html(modal_content);

    $('#user_dialog_'+username_guest).dialog({
        autoOpen:false,
        width:400,
        show: {
            effect: "drop",
            duration: 1000
          },
          hide: {
            effect: "drop",
            duration: 1000
          }
       });
       
    $('#user_dialog_'+username_guest).dialog('open');

    autoscroll2(username_guest);

    update_status_chat_guest(email_guest);

}  

function autoscroll2(username_guest){

    window.setTimeout(function() {
    var elem = document.getElementById('chat_history_guest'+username_guest);
    elem.scrollTop = elem.scrollHeight;
    }, 1);
 
 }

function autoscroll(){

    window.setTimeout(function() {
    var elem = document.getElementById('history');
    elem.scrollTop = elem.scrollHeight;
    }, 1);    
 }

  // $(document).on('click', '.sender', function(){

  //   var to_user_id   = $(this).attr('id');
  //   var chat_message = $('#guest_chat').val(); 
  //   var email = $(this).attr('email');
  //   var username = $(this).attr('username');
  //   // //mengambil value/isi pesan di chatbox susuai user
    
  //   if(chat_message == ''){
  //   //  alert('Mohon Isi Pesan... ');
  //   }else{

  //       $.ajax({
  //           url: 'chat/guestbook/insert_chat_guest.php',
  //           method : "POST",
  //           data :{to_user_id:to_user_id, chat_message:chat_message, email:email, username:username}
  //        }).done(function(data){
             
  //           //autoscroll(to_user_id);
  //           //mengosongkan input setelah mengirim pesan
  //           $('#guest_chat').val('');
  //           //Hasil Chat akan di kirim ke chatstory 
  //           $('#history').html(data);

  //              autoscroll();

  //        });
  //    }
    
  // });

  
    