<?php
defined('BASEPATH') or exit('No direct script access allowed');
function set_trx($matches) {
    return 'Trx #<a href="'.site_url('admin/transaksi/view/'.$matches[1]).'">'.$matches[1].'</a>';
}
?>
<!DOCTYPE html>
<html>  
  <head>
    <title>SMS &amp; Jabber</title>
    <?php include_once(VIEWPATH. 'includes/head.php'); ?>
    <style type="text/css">nav{float: right;margin-bottom: 15px;}</style>
  </head>
  <body>
    <?php include_once(VIEWPATH. 'admin/_navbar.php'); ?>
    <div class="container">
      <ul class="breadcrumb">
        <li><a href="<?php echo site_url();?>"><i class="fa fa-home"></i></a></li>
        <li><a href="<?php echo site_url('admin');?>">Admin Panel</a></li>
        <li class="active"><span>SMS &amp; Jabber</span></li>
      </ul>
      <div class="row">
        <div class="col-sm-4 col-md-3">
          <div class="row">
            <div class="col-xs-6"><a class="btn btn-primary btn-block" style="margin-bottom: 15px;" data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kirim');?>"><i class="fa fa-pencil"></i> Menulis</a></div>
            <div class="col-xs-6"><a class="btn btn-warning btn-block" style="margin-bottom: 15px;" data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/broadcast');?>"><i class="fa fa-bullhorn"></i> Broadcast</a></div>
          </div>
          <?php
          $params = array(array(
            'title' => 'SMS &amp; Jabber',
            'icon' => 'fa fa-envelope',
            'links' => array(
                array('Kotak Masuk', site_url('admin/sms/kotak-masuk'), 'fa fa-inbox'),
                array('Kotak Keluar', site_url('admin/sms/kotak-keluar'), 'fa fa-paper-plane', true),
            )
          ));
          echo sidebar_menu($params);
          ?>
        </div>
        <div class="col-sm-8 col-md-9">
          <?php if (!$total):?>
          <div class="alert alert-warning">Tidak ada SMS keluar.</div>
          <?php else:?>
          <?php echo form_open('#', 'id="form-confirm"');?>
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3><a class="pull-right" data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kosongkan-kotak-keluar');?>"><i class="fa fa-trash" data-toggle="tooltip" data-placement="left" title="Hapus semua pesan"></i></a><i class="fa fa-paper-plane"></i> Kotak Keluar</h3>
            </div>
            <div class="table-responsive">
              <table class="table table-hover">
                <thead>
                  <tr>
                    <th class="text-center" style="width: 40px;"><input type="checkbox" id="select-all"/></th>
                    <th style="width: 160px; white-space: nowrap;">No. Tujuan</th>
                    <th>Pesan</th>
                    <th style="width: 120px; white-space: nowrap;">Tgl Dibuat</th>
                    <th style="width: 120px; white-space: nowrap;">Tgl Status</th>
                    <th class="text-center" style="width: 80px;">Tindakan</th>                  
                  </tr>
                </thead>
                <tbody>
                  <?php foreach ($results as $sms):?>
                  <tr id="sms-<?php echo $sms->out_id;?>">
                    <td class="text-center"><input type="checkbox" class="checkbox1" name="sms[]" value="<?php echo $sms->out_id;?>"/></td>
                    <td style="width: 160px; white-space: nowrap;"><a data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kirim/?phone='.urlencode($sms->out_to).'&amp;message='.urlencode($sms->out_message));?>"><?php echo html_escape($sms->out_to);?></a></td>
                    <td><?php echo nl2br(preg_replace_callback("/Trx #([0-9]{1,12})/i", "set_trx", $sms->out_message));?></td>
                    <td style="width: 120px; white-space: nowrap;"><?php echo format_tanggal($sms->out_submit_date);?></td>
                    <td style="width: 120px; white-space: nowrap;"><?php if ($sms->out_status == 'success'):?><span class="text-success"><i class="fa fa-check" data-toggle="tooltip" data-title="Disampaikan"></i></span><?php elseif ($sms->out_status == 'sent'):?><i class="fa fa-check" data-toggle="tooltip" data-title="Terkirim"></i><?php elseif ($sms->out_status == 'failed'):?><i class="fa fa-times" data-toggle="tooltip" data-title="Error: <?php echo $sms->out_error;?>"></i><?php endif;?>
                    <?php echo $sms->out_send_date ? format_tanggal($sms->out_send_date) : '';?></td>
                    <td class="text-center" style="width: 80px; white-space: nowrap;">
                      <a class="btn btn-default btn-xs" href="<?php echo site_url('admin/sms/percakapan?phone='.urlencode($sms->out_to));?>" title="Lihat percakapan"><i class="fa fa-comments-o"></i></a>
                      &nbsp;
                      <a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/hapus-kotak-keluar/'.$sms->out_id);?>" title="Hapus"><i class="fa fa-remove"></i></a>
                    </td>
                  </tr>
                  <?php endforeach;?>
                </tbody>              
              </table>
            </div>
          </div>
          <div class="row" style="margin-bottom: 5px;">
            <div class="col-xs-12 col-sm-4 col-md-3">
              <button class="btn btn-danger btn-block btn-sm" type="submit" style="margin-bottom: 15px;"><i class="fa fa-trash"></i> Hapus yang ditandai</button>
            </div>
            <div class="col-xs-12 col-sm-8 col-md-9">
              <?php echo pagination(site_url('admin/sms/kotak-keluar'), $start, $total, $this->system->set['list_per_page'], '/%d');?>
            </div>
          </div>
          <?php echo form_close();?>
          <?php endif;?>
        </div>
      </div>
    </div>
    <?php include_once(VIEWPATH.'includes/foot.php'); ?>
    <script type="text/javascript">
    $('#select-all').click(function(event){if(this.checked){$('.checkbox1').each(function(){this.checked=true;});}else{$('.checkbox1').each(function(){this.checked=false;});}});
    $(document).on("submit","#form-confirm", function() {
      var formData = $(this).serialize();
      if (formData.indexOf('sms') > 0) {
        $("#myModal").modal("show");
        $.ajax({
          type: "POST",
          url: "<?php echo site_url('admin/sms/hapus-kotak-keluar');?>",
          data: formData,
          success: function (data) {
            var csrf = $(data).find('[name="<?php echo $this->security->get_csrf_token_name();?>"]').val();
            $('#form-confirm [name="<?php echo $this->security->get_csrf_token_name();?>"]').val(csrf);
            $("#myModal .modal-content").html(data);
          },
          error: function() {
            alert("Terjadi kesalahan!");
          }
        });
      }
      return false;
    });
    </script>
  </body>
</html>