<?php
session_start();
require_once('../config/config.php');
require_once('../class/functions.php');
dbconnect();

if (!is_admin()) {
    redirect(LOGIN);
}

$page = "Notification";
$geo = ip2location($_SERVER['REMOTE_ADDR']);
$country = $geo->country;
$city = $geo->city;
visit_log($_SERVER['REMOTE_ADDR'], $country, $city, $_SERVER['HTTP_USER_AGENT'], getBrowser($_SERVER['HTTP_USER_AGENT']), getOS($_SERVER['HTTP_USER_AGENT']), $page, $_SESSION['admin_login']);


if (isset($_POST['addNft']) && $_POST['addNft'] == 'addNft')
{
if ($_POST['alrt_rcvr'] == ""){ store_msg("Select notification receiver", "danger"); redirect(NOTIFICATION);}
elseif ($_POST['status'] == ""){ store_msg("Select notification status", "danger"); redirect(NOTIFICATION);}
elseif ($_POST['alrt_cnt'] == ""){ store_msg("Notification message cannot be empty", "danger"); redirect(NOTIFICATION);}
else{
$msg_cnt = clean_data($_POST['alrt_cnt']);
$msg_subject = truncate($msg_cnt,35,"&hellip;");
$status = clean_data($_POST['status']);
$msg_rcvr = clean_data($_POST['alrt_rcvr']);

$stmt = $pdo->prepare('INSERT INTO '.ALERT_TABLE_NAME.' (alrt_subject, alrt_cnt, alrt_rcvr, rcvr_name, alrt_date, alrt_status) VALUES (:subject, :cnt, :rcvr, :name, NOW(), :status)');
$stmt->execute(array(':subject' => $msg_subject, ':cnt' => $msg_cnt, ':rcvr' => $msg_rcvr, ':name' => $msg_rcvr, ':status' => $status));
if ($pdo->lastInsertId()){
log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $msg_rcvr, 'Notification Added Successfully');
store_msg("Notification Added Successfully", "success"); redirect(NOTIFICATION);
}else{
    store_msg("Notification Error", "danger"); redirect(NOTIFICATION);
}
}
}


if (isset($_POST['delNft']) && $_POST['delNft'])
{
$ntfid = clean_data($_POST['delNft']);
if (delete_alert($ntfid)) {
    log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $ntfid, 'Notification Successfully Deleted');
    store_msg("Notification Successfully Deleted", "success"); redirect(NOTIFICATION);
}else{
    store_msg("Delete Notification Error", "danger"); redirect(NOTIFICATION);
}
}

require_once('head.php');
?>

<body>
  <div class="app" id="app">

<!-- ############ LAYOUT START-->
  <!-- aside -->
  <?php require_once('aside.php'); ?>
  <!-- / aside -->



  <!-- content -->
  <div id="content" class="app-content box-shadow-z3" role="main">

    <?php require_once('header.php'); ?>

    <?php require_once('footer.php'); ?>

    <div ui-view class="app-body" id="view">
    <!-- ############ PAGE START-->

    <div class="item">
        <div class="item-bg">
          <img src="../users/user.png" class="blur opacity-3">
        </div>
        <div class="p-a-md">
          <div class="row m-t">
            <div class="col-sm-10">
              <a href class="pull-left m-r-md">
                <span class="avatar w-96">
                  <img src="../users/user.png">
                  <i class="on b-white"></i>
                </span>
              </a>
              <div class="clear m-b">
                <p class="text-md profile-status">Welcome</p>
                <h3 class="m-a-0 m-b-xs">Administrator</h3>
                <p class="text-muted">
                    <?php
                    $lastlogin = alast_login($_SESSION['admin_login']);
                    foreach($lastlogin as $last){
                        if ($last['last_ts'] == '0000-00-00 00:00:00') {
                            echo '';
                        }else{
                            $lastdate = date('l jS F Y', strtotime($last['last_ts']));
                            $lastip = $last['last_ip'];
                            echo'<span class="m-r">Last login: <strong>'.$lastdate.'</strong></span>';
                            echo' From: <small><strong><i class="fa fa-map-marker m-r-xs"></i>'.$lastip.'</strong> </small><br>';
                            echo' <a href="http://api.hostip.info/get_html.php?ip='.$lastip.'" target="_blank"><img src="http://api.hostip.info/flag.php?ip='.$lastip.'" alt="Country" width="39" height="24"></a> ';
                        }
                    } ?>
                </p>
              </div>
            </div>
          </div>
        </div>
    </div>

    <?php if (!empty($_SESSION['msg_text'])) {
    $msg = create_alert($_SESSION['msg_text'], $_SESSION['msg_type']);
    echo $msg;
    clear_msg(); }
    ?>


    <div class="padding">
      <div class="row">
        <div class="col-sm-12 col-md-12">
          <div class="box-header m-b">
              <h2 class="m-a-0 m-b-sm text-lg">Notification</h2>
              <button class="btn rounded btn-outline b-info text-info p-x-md pull-right" data-toggle="modal" data-target="#addNft">New Notification</button>
              <small>Notification messages to display on account holders page</small>
          </div>
          <div class="m-b" id="accordion">
            <?php
			$sql = "SELECT * FROM `".ALERT_TABLE_NAME."` ORDER BY alrt_id DESC";
			$stmt = $pdo->query($sql);
            if ($stmt->rowCount() > 0) {
			while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
		    ?>
            <div class="panel box no-border m-b-xs">
              <div class="box-header p-y-sm">
                <span class="pull-left label text-sm <?php if($row['alrt_status'] == '1'){echo 'success';}else{echo 'danger';}?>"><?php if($row['alrt_status'] == '1'){echo 'Published';}else{echo 'Unpublished';}?></span>
                <span class="pull-right label text-sm warn"><?php echo strtoupper($row['alrt_rcvr']); ?></span>
                <a data-toggle="collapse" data-parent="#accordion" data-target="#c_<?php echo $row['alrt_id']; ?>">
                  <strong><?php echo $row['alrt_subject']; ?></strong>
                </a>
              </div>
              <div id="c_<?php echo $row['alrt_id']; ?>" class="collapse">
                <div class="box-header">
                  <span class="pull-left label text-sm success">Sent to: <?php echo $row['alrt_rcvr']; ?></span>
                  <span class="pull-right label text-sm info"><?php echo date('jS F Y', strtotime($row['alrt_date'])); ?></span>
                </div>
                <div class="box-body">
                  <p class="text-md">
                  <?php echo $row['alrt_cnt']; ?>
                  </p>

    			  	<div class="box-footer m-b">
    			  	<form id="Dlnft" method="post" action="" onsubmit="return confirm('Are you sure you want to delete this notification?');" ui-jp="parsley">
                        <button type="submit" name="delNft" value="<?php echo $row['alrt_id']; ?>" class="btn btn-sm rounded btn-outline b-danger text-danger pull-right">Delete</button>
                    </form>
    			  	</div>
                </div>
              </div>
            </div>
            <?php
            }
            }else{
                echo '<div class="text-muted">No Notification added yet</div>';
            } ?>
          </div>
        </div>
      </div>
    </div>



    <!-- Add New Notification modal -->
    <div id="addNft" class="modal" data-backdrop="true">
      <div class="modal-dialog">
      <form id="AddNft" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
        <div class="modal-content">
          <div class="modal-header">
          	<h5 class="modal-title">Add New Notification</h5>
          </div>
          <div class="modal-body p-lg">
           <div class="row row-sm">
              <div class="col-sm-12">
                <div class="md-form-group">
                  <select name="alrt_rcvr" class="md-input" data-parsley-error-message="Select Receiver" required>
                    <option value="">Select Receiver</option>
                    <option value="all">All Account Holders</option>
                    <?php
    				$users = get_users();
                    if (empty($users)) {
                    echo '<option value="">No User exist</option>';
                    }else{
    				foreach($users as $user)
    				{
    				echo '
                    <option value="'.$user['user_login'].'">'.$user['user_login'].' ('.$user['user_names'].')</option>
    				';
    				}
                    }
    			    ?>
                  </select>
                  <label>Notification Receiver</label>
                </div>
              </div>
            </div>
            <div class="row row-sm">
              <div class="col-sm-12">
                <div class="md-form-group">
                  <select name="status" class="md-input" data-parsley-error-message="Select Status" required>
                    <option value="">Select Status</option>
                    <option value="1">Published</option>
                    <option value="0">Unpublished</option>
                  </select>
                  <label>Notification Status</label>
                </div>
              </div>
            </div>
            <div class="row row-sm">
              <div class="col-sm-12">
                <div class="md-form-group float-label">
                  <textarea name="alrt_cnt" class="md-input" rows="3" data-parsley-error-message="SMS message is required" required></textarea>
                  <label>Notification Message</label>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="submit" name="addNft" value="addNft" class="btn rounded btn-outline b-success text-success p-x-md">Add Notification</button>
            <button type="button" class="btn rounded btn-outline b-danger text-danger p-x-md" data-dismiss="modal">Cancel</button>
          </div>
        </div><!-- /.modal-content -->
      </form>
      </div>
    </div>
    <!-- / .modal -->




    <!-- ############ PAGE END-->
    </div>



    </div>
<!-- ############ LAYOUT END-->

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="../libs/jquery/jquery/dist/jquery.js"></script>

<!-- Bootstrap -->
  <script src="../libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="../libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
<!-- core -->
  <script src="../libs/jquery/underscore/underscore-min.js"></script>
  <script src="../libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
  <script src="../libs/jquery/PACE/pace.min.js"></script>

  <script src="../scripts/config.lazyload2.js"></script>

  <script src="../scripts/palette.js"></script>
  <script src="../scripts/ui-load.js"></script>
  <script src="../scripts/ui-jp.js"></script>
  <script src="../scripts/ui-include.js"></script>
  <script src="../scripts/ui-device.js"></script>
  <script src="../scripts/ui-form.js"></script>
  <script src="../scripts/ui-nav.js"></script>
  <script src="../scripts/ui-screenfull2.js"></script>
  <script src="../scripts/ui-scroll-to.js"></script>
  <script src="../scripts/ui-toggle-class.js"></script>

  <script src="../scripts/app.js"></script>

  <!-- ajax -->
  <script src="../libs/jquery/jquery-pjax/jquery.pjax.js"></script>
  <script src="../scripts/ajax.js"></script>
<!-- endbuild -->
</body>
</html>
