<?php
include('header.php'); 
if (!empty($_GET['case'])) {
$case = make_safe($_GET['case']);	
} else {
$case = '';	
}
switch ($case) {
case 'edit';
$id = intval(make_safe($_GET['id']));
if (isset($_POST['submit'])) {
$title = make_safe($_POST['title']);
if (isset($_POST['privacy'])) {
$privacy = intval($_POST['privacy']);
} else {
$privacy = 0;
} 
if (empty($title)) {
$message = notification('warning','Insert List Title Please');	
} else {
$update = $mysqli->query("UPDATE lists SET title='$title',privacy='$privacy' WHERE id='$id'");	
if ($update) {
$message = notification('success','List is Edited Successfully.');
} else {
$message = notification('danger','Error Happened.');	
}
}
}
$list = $general->lists($id);
?>
			<div class="page-header page-heading">
				<h1>Edit List
				<a href="lists.php" class="btn btn-default  pull-right"><span class="fa fa-arrow-right"></span></a>
				</h1>
			</div>
			<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="title">Title <span>*</span></label>
			<input type="text" class="form-control" name="title" id="title" value="<?php echo $list['title']; ?>" />
		  </div>
		  <div class="form-group">
			<input type="checkbox" name="privacy" id="privacy" value="1" <?php if (isset($list['privacy']) AND $list['privacy'] == 1) {echo 'CHECKED';} ?> /> Private
		  </div>
		  <?php 
		  $lists_media = $general->lists_media($id);
		  if ($lists_media != 0) {
			  echo '<div id="sort_media_list">
			  <ul>';
		  foreach ($lists_media AS $media) {
			?>
				<li class="row category_li delete_me_<?php echo $media['lmid']; ?>" id="records_<?php echo $media['lmid']; ?>" title="Drag To Re-Order">
					<div class="col-xs-9"><?php echo $media['title']; ?></div>
					<div class="col-xs-3 text-right"><a href="javascript:void();" id="<?php echo $media['lmid']; ?>" class="remove-from-list btn btn-xs btn-danger"><i class="fa fa-remove"></i></a></div>
				</li>
			<?php			
		  }
		  echo '</ul></div>';
		  }
		  ?>
		  <button type="submit" name="submit" class="btn btn-primary margin-top">Save</button>
		</form>
<?php
break;
case 'delete';
$id = abs(intval(make_safe(xss_clean($_GET['id']))));
if (isset($_POST['delete'])) {
$delete_items = $mysqli->query("DELETE FROM lists_media WHERE list_id='$id'");
$delete = $mysqli->query("DELETE FROM lists WHERE id='$id'");
if ($delete) {
$message = notification('success','Video Deleted Successfully.');
$done = true;
} else {
$message = notification('danger','Error Happened.');
}
}
$lists = $general->lists($id);
?>
			<div class="page-header page-heading">
				<h1>Delete List
				<a href="lists.php" class="btn btn-default pull-right"><span class="fa fa-arrow-right"></span></a>
				</h1>
			</div>
			<?php if (isset($message)) {echo $message;} ?>
		  <form role="form" method="POST" action="">
		  <?php if (empty($done)) { ?>
			<div class="alert alert-warning">Are you sure that you want to delete List : <b><?php echo htmlspecialchars_decode($lists['title'],ENT_QUOTES); ?></b>. If you Choose to Delete you Can't Undo this Action Later.</div>
		  <?php } ?>
		  <?php if ($done) { ?>
		  <a href="lists.php" class="btn btn-default">Back To Lists</a>
		  <?php } else { ?>
		  <button type="submit" name="delete" class="btn btn-danger">Delete</button>
		  <?php } ?>
		</form>
<?php
break;
case 'search';
$q = make_safe(xss_clean($_GET['q']));
if (isset($_POST['delete']) AND isset($_POST['id'])) {
	$ids = $_POST['id'];
	$count= count($ids);
	for($i=0;$i<$count;$i++){
	$delete_items = $mysqli->query("DELETE FROM lists_media WHERE list_id='$del_id'");
	$delete = $mysqli->query("DELETE FROM lists WHERE id='$del_id'");
	if ($delete) {
	$message = notification('success','The Selected Lists Were Deleted Permanently.');
	} else {
	$message = notification('error','Error Happened');
	}
	}
}
?>
<div class="page-header page-heading">
	<h1 class="row"><div class="col-md-6"><i class="fa fa-list"></i> Search In Lists</div>
	<div class="col-md-6">
	<div class="pull-right search-form">
	<form method="GET" action="lists.php">
		<div class="input-group">
		  <input type="hidden" name="case" value="search" />
		  <input type="text" name="q" class="form-control" placeholder="Search" value="<?php echo $q; ?>" />
		  <span class="input-group-addon"><button class="btn-link"><span class="fa fa-search"></span></button></span>
		</div>
	</form>
	</div>
	</div>
	</h1>
</div>
<?php
if (isset($message)) {echo $message;}
$page = 1;
$size = 20;
if (isset($_GET['page'])){ $page = (int) $_GET['page']; }
$sqls = "SELECT * FROM lists WHERE title LIKE '%$q%'";
$query = $mysqli->query($sqls);
$total_records = $query->num_rows;
if ($total_records == 0) {
echo notification('warning','There Are No Results for Search Query.');
} else {
$pagination = new Pagination();
$pagination->setLink("lists.php?case=search&q=$q&page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
$get = "SELECT * FROM lists WHERE title LIKE '%$q%' ORDER BY id DESC ".$pagination->getLimitSql();
$q = $mysqli->query($get);
?>
<form role="form" method="POST" action="">
<table class="table">
<thead>
<th width="15"><input type="checkbox" class="parentCheckBox" /></th>
<th>Title</th>
<th>User</th>
<th>Media Items</th>
<th>Created at</th>
<th>Privacy</th>
<th></th>
</thead>
<tbody>
<?php 
while ($row = $q->fetch_assoc()) {
?>
<tr>
<td><input type="checkbox" name="id[]" class="childCheckBox" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo get_user($row['user_id']); ?></td>
<td><?php echo get_list_media_items($row['id']); ?></td>
<td><?php echo date('Y-n-j h:i a',$row['create_datetime']); ?></td>
<td><?php if ($row['privacy'] == 0) {echo 'Public';} else {echo 'Private';} ?></td>
<td align="right">
<a class="btn btn-danger btn-xs" href="lists.php?case=delete&id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" title="Delete"><span class="fa fa-close"></span></a>
</td>
</tr>
<?php
}
?>
<tbody>
</table>
<div class="lists-actions">
<div class="row">
<div class="col-sm-2 col-md-3"><button type="submit" name="delete" class="btn btn-danger"><span class="fa fa-trash"></span> Delete</button></div>
<div class="col-sm-10 col-md-9"><?php echo $pagination->create_links(); ?></div>
</div>
</div>
</form>
<?php
}		
break;

default;
if (isset($_POST['delete']) AND isset($_POST['id'])) {
	$ids = $_POST['id'];
	$count= count($ids);
	for($i=0;$i<$count;$i++){
	$delete_items = $mysqli->query("DELETE FROM lists_media WHERE list_id='$del_id'");
	$delete = $mysqli->query("DELETE FROM lists WHERE id='$del_id'");
	if ($delete) {
	$message = notification('success','The Selected Lists Were Deleted Permanently.');
	} else {
	$message = notification('error','Error Happened');
	}
	}
}
?>
<div class="page-header page-heading">
	<h1 class="row"><div class="col-md-6"><i class="fa fa-list"></i> Lists</div>
	<div class="col-md-6">
	<div class="pull-right search-form">
	<form method="GET" action="lists.php">
		<div class="input-group">
		  <input type="hidden" name="case" value="search" />
		  <input type="text" name="q" class="form-control" placeholder="Search">
		  <span class="input-group-addon"><button class="btn-link"><span class="fa fa-search"></span></button></span>
		</div>
	</form>
	</div>
	</div>
	</h1>
</div>
<?php
if (isset($message)) {echo $message;}
$page = 1;
$size = 20;
if (isset($_GET['page'])){ $page = (int) $_GET['page']; }
$sqls = "SELECT * FROM lists ORDER BY id DESC";
$query = $mysqli->query($sqls);
$total_records = $query->num_rows;
if ($total_records == 0) {
echo notification('warning','There Are No Lists for Now.');
} else {
$pagination = new Pagination();
$pagination->setLink("?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
$get = "SELECT * FROM lists ORDER BY id DESC ".$pagination->getLimitSql();
$q = $mysqli->query($get);
?>
<form role="form" method="POST" action="">
<table class="table">
<thead>
<th width="15"><input type="checkbox" class="parentCheckBox" /></th>
<th>Title</th>
<th>User</th>
<th>Media Items</th>
<th>Created at</th>
<th>Privacy</th>
<th></th>
</thead>
<tbody>
<?php 
while ($row = $q->fetch_assoc()) {
?>
<tr>
<td><input type="checkbox" name="id[]" class="childCheckBox" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo get_user($row['user_id']); ?></td>
<td><?php echo get_list_media_items($row['id']); ?></td>
<td><?php echo date('Y-n-j h:i a',$row['create_datetime']); ?></td>
<td><?php if ($row['privacy'] == 0) {echo 'Public';} else {echo 'Private';} ?></td>
<td align="right">
<a class="btn btn-default btn-xs" href="lists.php?case=edit&id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" title="Edit"><span class="fa fa-edit"></span></a>
<a class="btn btn-danger btn-xs" href="lists.php?case=delete&id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" title="Delete"><span class="fa fa-close"></span></a>
</td>
</tr>
<?php
}
?>
<tbody>
</table>
<div class="lists-actions">
<div class="row">
<div class="col-sm-2 col-md-3"><button type="submit" name="delete" class="btn btn-danger"><span class="fa fa-trash"></span> Delete</button></div>
<div class="col-sm-10 col-md-9"><?php echo $pagination->create_links(); ?></div>
</div>
</div>
</form>
<?php
} 
} 
include('footer.php');
?>