<div class="container-fluid">
    <div>
        <h3>Announcements</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Tools</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Announcements</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-4 col-md-6">
            <div class="card">
                <div class="card-body">
                  <h6 class="card-title">Manage Announcements</h6>
                    <div class="announcement-settings-alert"></div>
                    <form class="announcement-settings" method="POST">
                      <label class="form-label">Create New Announcement (HTML Allowed)</label>
                    	<div class="form-group form-float">
                            <div class="form-line">
                                <textarea name="announcement_text" id="announcement_text" class="form-control" cols="30" rows="10"></textarea>
                            </div>
                        </div>
                        <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                        <button type="submit" class="btn btn-primary m-t-15 waves-effect">Create</button>
                    </form>
                </div>
            </div>
        </div>
		<div class="col-lg-4 col-md-6">
            <div class="card">
                <div class="card-body">
					<h6 class="card-title">Active Announcements</h6>
					<div class="active-announcements announcements">
			               <?php
                     $active_announcement_list = $db->where('active','1')->orderBy('id', 'DESC')->get(T_ANNOUNCEMENTS);
                     if (!empty($active_announcement_list)) {
                       $table                    = T_ANNOUNCEMENT_VIEWS;

                      foreach ($active_announcement_list as $announcement) {                
                          $announcement->time   = Time_Elapsed_String($announcement->time);
                          $announcement->views  = $db->where('announcement_id',$announcement->id)->getValue($table,"count(*)");

                          echo  LoadAdminPage("manage-announcements/active",array(
                              'ANN_ID'    => $announcement->id,
                              'ANN_VIEWS' => $announcement->views,
                              'ANN_TEXT'  => htmlspecialchars_decode($announcement->text),
                              'ANN_TIME'  => $announcement->time,
                          ));
                      }
                     }
                     else{
                      echo '<h5 class="no-active-announcements"><small>There are no active announcements.</small></h5>';
                    }
			                  ?>
			            </div>
				</div>
			</div>
		</div>
		<div class="col-lg-4 col-md-6">
            <div class="card">
                <div class="card-body">
					<h6 class="card-title">Inactive Announcements</h6>
					<div class="inactive-announcements announcements">
			               <?php 
                        $inactive_announcement_list = $db->where('active','0')->orderBy('id', 'DESC')->get(T_ANNOUNCEMENTS);
                        if (!empty($inactive_announcement_list)) {
                          foreach ($inactive_announcement_list as $announcement) {
                              $table                    = T_ANNOUNCEMENT_VIEWS;
                              $announcement->time     = Time_Elapsed_String($announcement->time);
                              $announcement->views    = $db->where('announcement_id',$announcement->id)->getValue($table,"count(*)");

                              echo  LoadAdminPage("manage-announcements/inactive",array(
                                  'ANN_ID'    => $announcement->id,
                                  'ANN_VIEWS' => $announcement->views,
                                  'ANN_TEXT'  => htmlspecialchars_decode($announcement->text),
                                  'ANN_TIME'  => $announcement->time,
                              ));
                          }
                        }
                        else{
                          echo '<h5 class="no-inactive-announcements"><small>There are no inactive announcements.</small></h5>';
                        }
			                  ?>
			            </div>
				</div>
			</div>
		</div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>
$(function() {
    var form_announcement_settings = $('form.announcement-settings');
    form_announcement_settings.ajaxForm({
        url: '{{CONFIG ajax_url}}/ap/add_announcement',
        beforeSend: function() {
            form_announcement_settings.find('.waves-effect').text('Please wait..');
        },
        beforeSubmit : function(arr, $form, options){
          arr.splice(0, 1);
          tinymce.get("announcement_text").setContent(tinymce.activeEditor.getContent());
          document.getElementById("announcement_text").value=tinymce.activeEditor.getContent();
          arr.push({name:'announcement_text', value:btoa(unescape(encodeURIComponent($('#announcement_text').val())))})
        },
        success: function(data) {
        	if (data.status == 200) {
	            $('.no-active-announcements').hide(100);
	            $('.active-announcements').prepend(data.text);
	            form_announcement_settings.find('.waves-effect').text('Create');
                location.reload();
	        }
        }
    });
    tinymce.init({
      selector: '#announcement_text',
      height: 270,
      entity_encoding : "raw",
      paste_data_images: true,
      image_advtab: true,
      toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
      toolbar2: "print preview media | forecolor backcolor",
      plugins: [
          "advlist autolink lists link image charmap print preview hr anchor pagebreak",
          "searchreplace wordcount visualblocks visualchars code fullscreen",
          "insertdatetime media nonbreaking save table contextmenu directionality",
          "template paste textcolor colorpicker textpattern"
        ],
  });
});

function Wo_DeleteAnnouncement(id) {
   var announcement_container = $('.announcements').find('.announcement-' + id);
   $.post('{{CONFIG ajax_url}}/ap/delete-announcement', {
      id: id
   }, function (data) {
      if (data.status == 200) {
         announcement_container.slideUp(200, function () {
            $(this).remove();
            location.reload();
         });
      }
   });
}

function Wo_DisableAnnouncement(id) {
   var announcement_container = $('.active-announcements').find('.announcement-' + id);
   var inactiveannouncement_container = $('.inactive-announcements');
   $.post('{{CONFIG ajax_url}}/ap/disable-announcement', {
      id: id
   }, function (data) {
      if (data.status == 200) {
         announcement_container.slideUp(200, function () {
            $(this).remove();
            location.reload();
         });
      }
   });
}

function Wo_ActivateAnnouncement(id) {
   var announcement_container = $('.inactive-announcements').find('.announcement-' + id);
   var activeannouncement_container = $('.active-announcements');
   $.post('{{CONFIG ajax_url}}/ap/enable-announcement', {
      id: id
   }, function (data) {
      if (data.status == 200) {
         announcement_container.slideUp(200, function () {
            $(this).remove();
            location.reload();
         });
      }
   });
}
</script>