<?php 

function messaging_settings_fields() {
	$sms_sender = ($option = get_option('sms_sender')) ? $option : substr(get_option('company_name'), 0, 11);
	$sms_username = ($option = get_option('sms_username')) ? $option : '';
	$sms_password = ($option = get_option('sms_password')) ? $option : '';
	label('SMS Settings');
	if(get_option('send_sms')) {
    	input(['label' => 'Send SMS', 'id' => 'send_sms', 'value' => 1, 'type' => 'checkbox', 'checked' => 'checked']);
    } else {
    	input(['label' => 'Send SMS', 'id' => 'send_sms', 'value' => 1, 'type' => 'checkbox']);
    }
	input(['label' => 'Username', 'id' => 'sms_username', 'value' => $sms_username]);
	input(['label' => 'Password', 'id' => 'sms_password', 'value' => $sms_password, 'type' => 'password']);
	input(['label' => 'Default Sender', 'id' => 'sms_sender', 'value' => $sms_sender,  'maxlength'=> "11"]);
	if(get_option('send_notification_sms')) {
    	input(['label' => 'Send Notification SMS', 'id' => 'send_notification_sms', 'value' => 1, 'type' => 'checkbox', 'checked' => 'checked']);
    } else {
    	input(['label' => 'Send Notification SMS', 'id' => 'send_notification_sms', 'value' => 1, 'type' => 'checkbox']);
    }
    if(get_option('include_chatting_api')) {
    	input(['label' => 'Include Chatting API', 'id' => 'include_chatting_api', 'value' => 1, 'type' => 'checkbox', 'checked' => 'checked']);
    } else {
    	input(['label' => 'Include Chatting API', 'id' => 'include_chatting_api', 'value' => 1, 'type' => 'checkbox']);
    }
    checkBox(['label' => 'Automatically Close Support Tickets <small>When Admin Responds</small>', 'id' => 'auto_close_support_tickets'], get_option('auto_close_support_tickets'));
}

function save_messaging_settings($args) {
	// $send_sms = ($args['send_sms'] == 1) ? 1 : 0;
	update_option('send_sms', $args['send_sms'] ?? '');
	// $send_notification_sms = ($args['send_notification_sms'] == 1) ? 1 : 0;
	update_option('send_notification_sms', $args['send_notification_sms'] ?? '');
	update_option('sms_username', $args['sms_username']);
	update_option('sms_password', $args['sms_password']);
	update_option('sms_sender', $args['sms_sender']);
	// $include_chatting_api = ($args['include_chatting_api'] == 1) ? 1 : 0;
	update_option('include_chatting_api', $args['include_chatting_api'] ?? '');
	// $auto_close_support_tickets = ($args['auto_close_support_tickets'] == 1) ? 1 : 0;
	update_option("auto_close_support_tickets", $args['auto_close_support_tickets'] ?? '');
}

function support_ticket_after_new_comment($obj) {
	$post = new post;
	$ticket_meta = $post->get_post_meta($obj["obj"]);
	$support = new support(["software_engineer", "super_admin_tech", "super_admin", "admin", "admin_tech"]);
	$ticket_meta['last_convo'] = time();
	if(current_user_can("respond_to_others_support_tickets")) {
		if(get_option("auto_close_support_tickets")) {
			$ticket_meta['status'] = "inactive";
		} else {
			$ticket_meta['status'] = "active";
		}
		$post->update_post_meta($ticket_meta["id"], $ticket_meta);
		$info = $ticket_meta['subject'].": Admin has responded to your support ticket number #".$ticket_meta['ticket_number'];
		inform_user($ticket_meta['user_id'], $info, "SUPPORT TICKET #".$ticket_meta['ticket_number'], get_option("home_url")."view-ticket?id=".$ticket_meta['id'], SMS, EMAIL, NOTIFICATION);
	} else {
		$info = $ticket_meta['subject'].": ".get_fullname($ticket_meta['user_id'])." has responded to your support ticket number #".$ticket_meta['ticket_number'];
		inform_admins($info, "SUPPORT TICKET #".$ticket_meta['ticket_number'], get_option("home_url")."view-ticket?id=".$ticket_meta['id'], "admin_tech", "admin", "super_admin_tech", "software_engineer");
		$ticket_meta['status'] = "active";
		$post->update_post_meta($ticket_meta["id"], $ticket_meta);
	}
	
}

function add_sms_list_to_form($args = array()) {
	$args = array_merge([
		'submit-btn' => 'submit',
		'exclude' => ['software_engineer', 'super_admin']
	], $args);
	?>
	<div id="display-receivers" data-exclude='<?php echo json_encode($args["exclude"]) ?>' data-btn="#<?php echo $args['submit-btn']; ?>"></div>
	<?php
	global $post_meta;
	add_page_javascript('contents/plugins/messaging/js/get-receipients.js');
}

function msg_announcement($user_meta) {
  return;
	$announcements = messaging_get_announcements($user_meta);
	if(_count($announcements) && is_array($announcements)) :
    ?>
  <style type="text/css">
    /* Widget: TODO LIST */
    .todo-list {
      margin: 0;
      padding: 0;
      list-style: none;
      overflow: auto;
    }
    .todo-list > li {
      border-radius: 2px;
      padding: 10px;
      background: #f4f4f4;
      margin-bottom: 2px;
      border-left: 2px solid #e6e7e8;
      color: #444;
    }
    .todo-list > li:last-of-type {
      margin-bottom: 0;
    }
    .todo-list > li > input[type='checkbox'] {
      margin: 0 10px 0 5px;
    }
    .todo-list > li .text {
      display: inline-block;
      margin-left: 5px;
      font-weight: 600;
    }
    .todo-list > li .label {
      margin-left: 10px;
      font-size: 9px;
    }
    .todo-list > li .tools {
      display: none;
      float: right;
      color: #dd4b39;
    }
    .todo-list > li .tools > .fa,
    .todo-list > li .tools > .glyphicon,
    .todo-list > li .tools > .ion {
      margin-right: 5px;
      cursor: pointer;
    }
    .todo-list > li:hover .tools {
      display: inline-block;
    }
    .todo-list > li.done {
      color: #999;
    }
    .todo-list > li.done .text {
      text-decoration: line-through;
      font-weight: 500;
    }
    .todo-list > li.done .label {
      background: #d2d6de !important;
    }
    .todo-list .danger {
      border-left-color: #dd4b39;
    }
    .todo-list .warning {
      border-left-color: #f39c12;
    }
    .todo-list .info {
      border-left-color: #00c0ef;
    }
    .todo-list .success {
      border-left-color: #00a65a;
    }
    .todo-list .primary {
      border-left-color: #3c8dbc;
    }
    .todo-list .handle {
      display: inline-block;
      cursor: move;
      margin: 0 5px;
    }
  </style>
    <div class="row mb-4">
      	<div class="col-md-12">
      		<div class="box box-primary">
            <div class="box-header ui-sortable-handle" style="cursor: move;">
              <!-- <i class="ion ion-clipboard"></i> -->

              <h3 class="box-title">Announcements</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <!-- See dist/js/pages/dashboard.js to activate the todoList plugin -->
              <ul class="todo-list ui-sortable">
              	<?php foreach ($announcements as $key => $value): ?>
                <li>
                  <!-- drag handle -->
                  <!-- <span class="handle ui-sortable-handle"> -->
                        <!-- <i class="fa fa-ellipsis-v"></i> -->
                        <!-- <i class="fa fa-ellipsis-v"></i> -->
                      <!-- </span> -->
                  <!-- todo text -->
                  <h4><?php echo $value['caption'] ?></h4>
                  <span class="text"><?php echo $value['content'] ?></span>
                  <!-- Emphasis label -->
                  <small class="label label-danger"><i class="fa fa-clock-o"></i> <?php echo date(_DATE_FORMAT_, $value['posted_on']) ?></small>
                  <!-- General tools such as edit or delete-->
                  <?php if (current_user_can("create_announcement")): ?>
                  <div class="tools">
                    <a href="edit-announcement" target="_blank"></a><i class="fa fa-edit"></i>
                  </div>
                  <?php endif ?>
                </li>
                <?php endforeach ?>
              </ul>
            </div>
            <!-- /.box-body -->
          </div>
      	</div>
    </div>
    <?php 
    endif;
}

function msg_scrolling_announcement() {
  global $app;
  if(isset($_GET['media'])) return;
  $announcements = messaging_get_announcements($app->user_meta);
  if(_count($announcements) && is_array($announcements)) :
    ?>
  <style type="text/css">
    marquee.announcement {
      background: #f9f9f9;
    }
    marquee.announcement .announcement-container span {
      display: inline-block;
    }
    marquee.announcement .accouncement-heading {
      font-weight: bold;
    }
    marquee.announcement .accouncement-heading::after {
      content: "";
      height: 5px;
      border-top: 5px solid transparent;
      border-bottom: 5px solid transparent;
      border-left: 5px solid #e11616;
      display: inline-block;
      margin-left: 5px;
      margin-right: 2px;
    }
    marquee.announcement .announcement-body::after {
      content: " | ";
      font-size: xx-large;
      line-height: 1px;
      color: #eeeeee;
    }
  </style>
    <marquee width="100%" direction="left" class="announcement" scrollamount="2">
      <?php foreach ($announcements as $meta): ?>
        <span class="announcement-container">
          <span class="accouncement-heading"><?php echo $meta['caption'] ?></span>
          <span class="announcement-body"><?php echo strip_tags($meta['content']) ?></span>
        </span>
      <?php endforeach ?>
    </marquee>
    <?php 
  endif;
}