<?php
if (Wo_IsLogged() === false) {
    header("Location: " . Wo_SeoLink('index.php?tab1=welcome'));
    exit();
}
if (!empty($_GET['user'])) {
    $user_id = Wo_Secure($_GET['user']);
    $user    = Wo_UserData($user_id);
    if (empty($user['user_id'])) {
        unset($user);
    }
}
?>
<div class="page-margin">
   <div class="row">
      <div class="col-lg-9">
         <ul class="list-group text-sender-container">
            <li class="list-group-item text-muted" contenteditable="false">
               <?php echo $wo['lang']['messages']; ?> 
               <span id="user-name" class="hidden"></span> 
               <span class=" <?php echo Wo_RightToLeft('pull-right');?> loading-icon">
               <i class="fa fa-comments progress-icon" data-icon="comments"></i>
               </span>
            </li>
            <li class="messages-load-more-messages view-more-wrapper hidden"></li>
            <div class="messages-container">
               <div class="no-messages">
                  <?php echo $wo['lang']['choose_one_of_your_friends']; ?> 
                  <h1><i class="fa fa-pencil-square-o"></i></h1>
                  <?php echo $wo['lang']['and_say_hello']; ?>
               </div>
            </div>
            <div class="text-right charsLeft-message">
               <span id="charsLeft"></span>
            </div>
            <form method="post" class="sendMessages" enctype="multipart/form-data">
               <div class="input-group">
                  <textarea name="textSendMessage" class="form-control custom-controls" id="sendMessage" onkeyup="Wo_SubmitForm(event);" onfocus="Wo_SubmitForm(event);" placeholder="<?php echo $wo['lang']['write_something'];?>" cols="10" rows="4" class="form-control" disabled></textarea>
                  <span class="input-group-btn">
                  <span class="btn-main-color btn-main btn-file MS-File">
                  <?php if($wo['config']['fileSharing'] == 1) { ?>
                  <i class="fa fa-file-text-o  progress-icon" data-icon="file-text-o "></i>
                  <input type="file" id="sendMessasgeFile" name="sendMessageFile" onchange="Wo_ShareFile();" disabled />
                  <?php } else { ?>
                  <i class="fa fa-picture-o progress-icon" data-icon="picture-o"></i>
                  <input type="file" id="sendMessasgeFile" name="sendMessageFile" onchange="Wo_ShareFile();" accept="image/x-png, image/gif, image/jpeg" disabled />
                  <?php } ?>
                  </span>
                  </span>
               </div>
               <input type="hidden" id="user-id" name="user_id" value="0" />
            </form>
         </ul>
      </div>
      <div class="clean"></div>
      <div class="col-lg-3">
         <form action="#" method="post" class="messages-search-users-form">
            <div class="form-group inner-addon  <?php echo Wo_RightToLeft('left-addon');?> messages-search-icon">
               <i class="glyphicon fa fa-search progress-icon" data-icon="search"></i>
               <input type="text" name="query" id="query" onkeyup="Wo_GetMessagesUsers(this.value);" class="form-control" placeholder="<?php echo $wo['lang']['search'];?>" autocomplete="off">
            </div>
            <div class="messages-users-list">
                <?php
                foreach(Wo_GetMessagesUsers($wo['user']['user_id']) as $wo['recipient']) {
                  echo Wo_LoadPage('messages/messages-recipients-list');
                }
                ?> 
            </div>
         </form>
      </div>
   </div>
</div>
<script type="text/javascript">
$(function () {
  <?php if (!empty($user['user_id'])) { ?>
  Wo_GetUserMessages(<?php echo $user['user_id'] ?>, "<?php echo $user['name']?>");
  <?php } ?>
  $('#sendMessage').limit("<?php echo $wo['config']['maxCharacters']?>", '#charsLeft');
  setInterval(function () {
    Wo_getNewMessages();
  }, 7500);
  <?php if ($wo['config']['message_seen'] == 1) { ?>
  setInterval(function () {
    Wo_getMessageSeen()
  }, 6000);
  <?php } ?>
  setInterval(function () {
    Wo_UpdateUsers()
  }, 10000);
  $('form.sendMessages').ajaxForm({
    url: Wo_Ajax_Requests_File() + '?f=messages&s=send_message',
    beforeSend: function () {
      $('.sendMessage').attr('disabled', true);
      Wo_progressIconLoader($('form.sendMessages').find('.btn-file'));
    },
    success: function (data) {
      if(data.status == 200) {
        if($('.messages-container').length == 0) {
          $(".messages-container").html(data.html);
        } else {
          $(".no-messages").hide();
          $(".messages-container").append(data.html);
        }
        $('form.sendMessages').clearForm();
        $('#sendMessage').val('').attr('disabled', false).keyup().focus();
        $(".messages-container").animate({
          scrollTop: $('.messages-container')[0].scrollHeight
        }, 500);
      }
      Wo_progressIconLoader($('form.sendMessages').find('.btn-file'));
    }
  });
});

function Wo_GetMessagesUsers(query) {
  searchForm = $('.messages-search-users-form');
  Wo_progressIconLoader(searchForm.find('.messages-search-icon'));
  $.get(Wo_Ajax_Requests_File(), {
    f: 'search',
    s: 'recipients',
    query: query
  }, function (data) {
    if(data.status == 200) {
      if(data.html.length == 0) {
        $('.messages-users-list').html('<span class="center-text"><?php $wo["lang"]["no_result"];?></span>');

      } else {
        $('.messages-users-list').html(data.html);
      }
      scrollToTop();
    }
    Wo_progressIconLoader(searchForm.find('.messages-search-icon'));
  });
}

function Wo_UpdateUsers() {
  if ($('.messages-search-users-form #query').val().length > 0) {
       return false;
  }
  $.get(Wo_Ajax_Requests_File(), {
    f: 'messages',
    s: 'update_recipients'
  }, function (data) {
    if(data.status == 200) {
      $('.messages-users-list').html(data.html);
    } 
  });
}

function Wo_GetUserMessages(user_id, user_name) {
  view_more_wrapper = $('.view-more-wrapper');
  Wo_progressIconLoader($('.text-sender-container').find('.loading-icon'));
  $('#user-name').text(' / ' + user_name).removeClass('hidden');
  $('#user-id').attr('value', user_id);
  $('#messages-recipient-' + user_id).find('.new-message-alert').fadeOut(200);
  $.get(Wo_Ajax_Requests_File(), {
    f: 'messages',
    s: 'get_user_messages',
    user_id: user_id
  }, function (data) {
    if(data.status == 200) {
      if(data.can_replay == true) {
        $('#sendMessage').val('').attr('disabled', false);
        $('#sendMessasgeFile').attr('disabled', false);
      } else {
        $('#sendMessage').val('<?php $wo["lang"]["sorry_cant_reply"];?>').attr('disabled', true);
        $('#sendMessasgeFile').attr('disabled', true);
      }
      $('#charsLeft').text("<?php echo $wo['config']['maxCharacters']?>");
      if(data.html.length == 0) {
        view_more_wrapper.hide();
        $('.messages-container').html('<div class="no-messages"><?php echo $wo["lang"]["no_more_message_to_show"];?> <h1><i class="fa fa-pencil-square-o"></i></h1> </div>');
      } else {
        $('.messages-container').html(data.html);
        view_more_wrapper.html('<a href="javascript:void(0);"><i class="fa fa-arrow-up progress-icon" data-icon="arrow-up"></i> ' + data.view_more_text + '</a>').show();
        view_more_wrapper.attr('onclick', 'Wo_getOldMessages(' + user_id + ');').removeClass('hidden');
        $(".messages-container").animate({
          scrollTop: $('.messages-container')[0].scrollHeight
        }, 200);
      }
      <?php if ($wo['config']['message_seen'] == 1) { ?>
         Wo_getMessageSeen();
      <?php } ?>
      Wo_progressIconLoader($('.text-sender-container').find('.loading-icon'));
    }
  });
}

function Wo_getNewMessages() {
  user_id = $('#user-id').val();
  message_id = $('.messages-container').find('.messages-wrapper:last').attr('data-message-id');
  if(user_id == 0) {
    return false;
  }
  $.get(Wo_Ajax_Requests_File(), {
    f: 'messages',
    s: 'get_new_messages',
    user_id: user_id,
    message_id: message_id
  }, function (data) {
    if(data.status == 200) {
      $(".messages-container").append(data.html);
      $(".messages-container").animate({
        scrollTop: $('.messages-container')[0].scrollHeight
      }, 200);
      if(data.sender == user_id) {
        document.getElementById('message-sound').play();
        if(!$('.sendMessage').is(':focus')) {
          document.title = 'New Message ' + document_title;
        }
      }
    }
  });
}

function Wo_getMessageSeen() {
  var last_id = $('.messages-text:last').attr('data-message-id');
  if(!$('.messages-text:last').find('.message-seen').is(':empty')) {
    return false;
  }
  $.get(Wo_Ajax_Requests_File(), {
    f: 'messages',
    s: 'get_last_message_seen_status',
    last_id: last_id
  }, function (data) {
    if(data.status == 200) {
      $('.messages-text:last').find('.message-seen').hide().html('<i class="fa fa-check"></i> <?php echo $wo["lang"]["seen"];?> (<span class="ajax-time" title="' + data.time + '">' + data.seen + '</span>)').fadeIn(300);
      $(".messages-container").animate({
        scrollTop: $('.messages-container')[0].scrollHeight
      }, 200);
    }
  });
}

function Wo_SubmitForm(e) {
  document.title = document_title;
  $('.charsLeft-message').fadeIn(200);
  if(e.keyCode == 13 && e.shiftKey == 0) {
    e.preventDefault();
    $('form.sendMessages').submit();
  }
}

function Wo_getOldMessages(user_id) {
  view_more_wrapper = $('.view-more-wrapper');
  before_message_id = $('.messages-text:first').attr('data-message-id');
  Wo_progressIconLoader(view_more_wrapper);
  $.get(Wo_Ajax_Requests_File(), {
    f: 'messages',
    s: 'load_previous_messages',
    user_id: user_id,
    before_message_id: before_message_id
  }, function (data) {
    if(data.status == 200) {
      if(data.html.length == 0) {
        view_more_wrapper.text('<?php echo $wo["lang"]["no_more_message_to_show"];?>');
        view_more_wrapper.delay(1000).slideUp(200);
      } else {
        $('.messages-container').prepend(data.html);
      }
    } else {
      view_more_wrapper.hide();
    }
    Wo_progressIconLoader(view_more_wrapper);
  });
}

function Wo_ShareFile() {
  document.title = document_title;
  $("#sendMessage").focus();
  $("form.sendMessages").submit();
}


function Wo_DeleteMessage(message_id) {
  Wo_progressIconLoader($('#messageId_' + message_id).find('.deleteMessage'));
  $.get(Wo_Ajax_Requests_File(), {
    f:'messages',
    s:'delete_message',
    message_id: message_id
  }, function (data) {
    if(data.status == 200) {
      $('#messageId_' + message_id).slideUp(200, function () {
        $(this).remove();
      });
    }
  });
}

function Wo_ResizeVideoPlayer(message_id) {
  var message_container = $('#messageId_' + message_id);
  message_container.find('.message-media').toggleClass('full-size');
}
</script>