<?php
	session_start();
	include("library/conn.php");
	
	$role_rs = array();
	$get_role_mode = mysqli_query($conn, "SELECT * FROM user_role_privileges WHERE userID = '". $_SESSION['userIdd'] ."' AND roleName = 'bulk_sms'") or die (mysqli_error($conn));
	if (mysqli_num_rows($get_role_mode) > 0) {
		$role_rs = mysqli_fetch_array($get_role_mode);
	} else {
		$role_rs = ['-1', '-1', '-1', '-1', '-1', '-1'];
	}
	
?>
<!DOCTYPE html>
<html lang="en">
	<head>
        <?php include("library/head.php");?>
        <title>Tenant Bulk SMS| <?php echo $_SESSION['systemName']; ?></title>
	</head>
	<body class="app sidebar-mini">

		<!-- Navbar-->
		<?php include("library/header.php");?>
		<!-- Sidebar menu-->
		<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
		<?php include("library/sidebar.php");?>
  
		<main class="app-content">
			<div class="app-title">
				<div>
					<h4><i class="fa fa-envelope fa-lg"></i> TENANT BULK SMS</h4>
					<p>Tenant Bulk Short Messages Page</p>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="tenant-bulk-sms">Tenant Bulk SMS</a></li>
				</ul>
			</div>
      
			<?php include("models/admin_config.php");?>
			<?php include("models/system_config.php");?>

			<div class="row">
				<div class="col-md-12">
                    <div class="tile">
                        <div class="tile-body">
                            <form action="#" method="POST" id="searchTenantBulkMessagesAdminForm">
                                <div class="row">
                                    <div class="form-group col-md-4">
                                        <label class="control-label">Tenant Information</label>
                                        <select class="form-control" style="width: 100%;" name="cmbTenantIddSearchTenantBulkMessagesAdmin" id="cmbTenantIddSearchTenantBulkMessagesAdmin">
                                            
                                        </select>
                                    </div>
                                    <div class="form-group col-md-2">
                                        <label class="control-label">Message Subject</label>
                                        <select class="form-control" style="width: 100%;" name="cmbMessageSubjectSearchTenantBulkMessagesAdmin" id="cmbMessageSubjectSearchTenantBulkMessagesAdmin">
                                            <option value="">All (General)</option>
                                            <?php
                                                $getMessageSubjects = mysqli_query($conn, "SELECT DISTINCT messageSubject FROM bulk_sms WHERE receiverID LIKE 'TN%' ORDER BY messageSubject ASC") or die(mysqli_error($conn));
                                                if (mysqli_num_rows($getMessageSubjects) > 0) {
                                                    while ($sms_subject_rs = mysqli_fetch_array($getMessageSubjects)) {
                                                        ?><option value="<?php echo $sms_subject_rs[0]; ?>"><?php echo $sms_subject_rs[0]; ?></option><?php
                                                    }
                                                }
                                            ?>
                                        </select>
                                    </div>
                                    <div class="form-group col-md-2">
                                        <label class="control-label">Date</label>
                                        <select class="form-control" name="cmbCustomerDateSearchTenantBulkMessagesAdmin" id="cmbCustomerDateSearchTenantBulkMessagesAdmin" style="width: 100%;">
                                            <option value="0">All Date</option>
                                            <option value="1">Today</option>
                                            <option value="2">Yesterday</option>
                                            <option value="3">Last 3 Days</option>
                                            <option value="4">This Week</option>
                                            <option value="5">Last 10 Days</option>
                                            <option value="6">This Month</option>
                                            <option value="7">This Year</option>
                                            <option value="8">Custom Date</option>
                                        </select>
                                    </div>
                                    <div class="form-group col-md-2">
                                        <label class="control-label">From Date</label>
                                        <input class="form-control" type="date" name="txtFromDateSearchTenantBulkMessagesAdmin" id="txtFromDateSearchTenantBulkMessagesAdmin" />

                                        </input>
                                    </div>
                                    <div class="form-group col-md-2">
                                        <label class="control-label">To Date</label>
                                        <input class="form-control" type="date" name="txtToDateSearchTenantBulkMessagesAdmin" id="txtToDateSearchTenantBulkMessagesAdmin" />

                                        </input>
                                    </div>
                                    <div class="form-group col-md-12 text-center mt-2">
                                        <button type="submit" class="btn btn-primary col-md-2" name="btnSearchTenantBulkMessagesAdmin" id="btnSearchTenantBulkMessagesAdmin"><i class="fa fa-search" style="font-size: 18px; margin-left: 5px; margin-bottom: 3px;"></i> Search</button>
                                    </div>
                                </div>	
                            </form>
                        </div>
                    </div>
                </div>

                <div class="col-md-12 pt-3">
                    <div class="tile">
                        <div class="tile-body">
                            <div class="row">
								<div class="form-group col-md-12">
									<h5 class="pb-2">
										<span><i class="fa fa-list-ul"></i> Tenant Bulk Messages List</span>
										<span>
											<?php
												if ($_SESSION['userType'] == 0) {
													?><button type="button" class="btn btn-success pull-right" data-toggle="modal" data-target="#sendTenantBulkMessagesModal"><i class="fa fa-send"></i> Send New Message</button><?php
												} else {
													if ($role_rs[3] == '1') {
														?><button type="button" class="btn btn-success pull-right" data-toggle="modal" data-target="#sendTenantBulkMessagesModal"><i class="fa fa-send"></i> Send New Message</button><?php								
													}
												}
											?>
										</span>
									</h5>
									<hr />
								</div>
							</div>
                            <div class="table-responsive" id="tenantShorMessagesSMSAdminSection">
                                <table class="table table-bordered table-hover table-sm" id="tenantBulkMessagesTable">
                                    <thead>
                                        <tr class="text-white bg-info">
                                            <th class="text-center">No</th>
                                            <th class="text-left">Tenant Name</th>
                                            <th class="text-left">Message Subject</th>
                                            <th class="text-left">Message Body</th>
                                            <th class="text-center">Status</th>
                                            <th class="text-center">Sent Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $getTenants = mysqli_query($conn, "SELECT bulk_sms.smsID, tenants.tenantName, bulk_sms.messageSubject, bulk_sms.messageBody, bulk_sms.messageStatus, bulk_sms.sentDate
                                            FROM bulk_sms
                                            JOIN tenants ON bulk_sms.receiverID = tenants.tenantID
                                            WHERE bulk_sms.receiverID LIKE 'TN%' ORDER BY bulk_sms.smsID DESC") or die(mysqli_error($conn));
                                            $x = 1;
                                            while ($request_rs = mysqli_fetch_array($getTenants)) { ?>
                                                <tr>
                                                    <td class="text-center" width="5%"><?php echo $x; ?></td>
                                                    <td class="text-left" width="20%"><?php echo $request_rs[1]; ?></td>
                                                    <td class="text-left" width="15%"><?php echo $request_rs[2]; ?></td>
                                                    <td class="text-justify"><a href="#" class="btnOpenTenantShortMessageContentDetailsModalAdmin" data-id="<?php echo $request_rs[0]; ?>" data-toggle="modal" data-target="#tenantShortMessageContentDetailsAdminModal"><?php if (strlen($request_rs[3]) >= 90) { echo substr($request_rs[3], 0, 90). " <strong>Read More...</strong>"; } else { echo $request_rs[3]; } ?></a></td>
                                                    <td class="text-center" width="7%">
                                                        <?php if ($request_rs[4] == 1) { ?><span class="badge text-success p-2"><i class="fa fa-check"></i> Sent</span><?php } ?>
                                                    </td>
                                                    <td class="text-center" width="12%"><?php echo substr($request_rs[5], 0, 16); ?></td>
                                                </tr><?php
                                                $x++;
                                            }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
			</div>
		</main>
  
		<!-- Essential javascripts for application to work-->
		<?php include("library/footer.php");?>  
		<?php include("library/scripts.php");?>

		<script type="text/javascript">
			$('#tenantBulkMessagesTable').DataTable({"pageLength": 100, "order": []})
			$("#cmbTenantIddSearchTenantBulkMessagesAdmin, #cmbMessageSubjectSearchTenantBulkMessagesAdmin, #cmbCustomerDateSearchTenantBulkMessagesAdmin").select2({ tags: false })
			$("#cmbTenantNameSendTenantBulkShortMessage").select2({ tags: false, dropdownParent: $("#sendTenantBulkMessagesModal") })
		</script>

	</body>
</html>
