<?php
// Recoded by Gh0Code
// Hargailah orang lain jika Anda ingin dihargai
session_start();
require("../mainconfig.php");
$msg_type = "nothing";

if (isset($_SESSION['user'])) {
	$sess_username = $_SESSION['user']['username'];
	$check_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$sess_username'");
	$data_user = mysqli_fetch_assoc($check_user);
	if (mysqli_num_rows($check_user) == 0) {
		header("Location: ".$cfg_baseurl."logout.php");
	} else if ($data_user['status'] == "Suspended") {
		header("Location: ".$cfg_baseurl."logout.php");
	} else if ($data_user['level'] != "Developers") {
		header("Location: ".$cfg_baseurl);
	} else {
		if (isset($_POST['delete'])) {
			$post_id = $_POST['provider'];
			$checkdb_provider = mysqli_query($db, "SELECT * FROM provider WHERE code = '$post_id'");
			if (mysqli_num_rows($checkdb_provider) == 0) {
				$msg_type = "error";
				$msg_content = "<b>Gagal:</b> Provider tidak ditemukan.";
			} else {
				$delete_provider = mysqli_query($db, "DELETE FROM provider WHERE code = '$post_id'");
				if ($delete_provider == TRUE) {
					$msg_type = "success";
					$msg_content = "<b>Berhasil:</b> Provider deleted.";
				}
			}
		}

	include("../lib/header.php");
?>
                        </div>
      </div>
    </div>
    <!-- Page content -->
    <div class="container-fluid mt--6">
      <div class="card mb-4">
        <!-- HTML5 inputs -->
            <div class="card">
              <!-- Card header -->
              <div class="card-header">
                <h3 class="mb-0">Daftar Provider</h3>
              </div>
              <!-- Card body -->
              <div class="card-body">
										<?php 
											if ($msg_type == "success") {
											?>
											<div class="alert alert-success dark" role="alert">
                      <p><?php echo $msg_content; ?></p>
                    </div>
											<?php
											} else if ($msg_type == "error") {
											?>
											<div class="alert alert-secondary dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                        </div>
											<?php
											}
											?>
										<div class="col-md-6">
											<a href="<?php echo $cfg_baseurl; ?>admin/provider/add.php" class="btn btn-info m-b-20"><i class="fa fa-plus"></i> Tambah</a>
										</div>
										<div class="col-md-6">
										</div>
										<div class="clearfix"></div>
										<br />
										<div class="table-responsive">
											<table class="table table-striped table-bordered table-hover m-0">
												<thead>
													<tr>
														<th>#</th>
														<th>Provider</th>
														<th>Api Key</th>
														<th>Link</th>
														<th>Aksi</th>
													</tr>
												</thead>
												<tbody>
												<?php
// start paging config
$query_list = "SELECT * FROM provider ORDER BY id DESC"; // edit
$records_per_page = 10; // edit

$starting_position = 0;
if(isset($_GET["page_no"])) {
	$starting_position = ($_GET["page_no"]-1) * $records_per_page;
}
$new_query = $query_list." LIMIT $starting_position, $records_per_page";
$new_query = mysqli_query($db, $new_query);
// end paging config
												$no = 1;
												while ($data_show = mysqli_fetch_assoc($new_query)) {
												?>
													<tr>
														<td><?php echo $no; ?></td>
														<td><?php echo $data_show['code']; ?></td>
														<td><?php echo $data_show['api_key']; ?></td>
														<td><?php echo $data_show['link']; ?></td>
														<td align="center">
														<a href="<?php echo $cfg_baseurl; ?>admin/provider/edit.php?provider=<?php echo $data_show['code']; ?>" class="btn btn-xs btn-warning"><i class="fa fa-edit"></i></a>
														<a href="<?php echo $cfg_baseurl; ?>admin/provider/delete.php?provider=<?php echo $data_show['code']; ?>" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></a>
														</td>
													</tr>
												<?php
												$no++;
												}
												?>
												</tbody>
											</table>
										</div>
                                </div>
                            </div>
                            <!-- end col -->
                        </div>
                        <!-- end row -->
<?php
	include("../lib/footer.php");
	}
} else {
	header("Location: ".$cfg_baseurl);
}
?>