<?php 
$module = @$_GET['module'];
$act    = @$_GET['act'];
$id     = (INT)@$_GET['id']; 
?>

<?php
switch ($act) {
	default: 
?>

<!-- default -->

<div class="container-xxl flex-grow-1 container-p-y">
   <div class="row">
    <div class="col-md-12">
        <!-- Basic Bootstrap Table -->
              <div class="card">
                
				<div class="card-header d-flex align-items-center justify-content-between">
                    <h5 class="mb-0">Games</h5>
                    <a href="index.php?module=<?php echo $module; ?>&act=add" class="text-secondary float-end" data-bs-toggle="tooltip"
                        data-bs-offset="0,4"
                        data-bs-placement="top"
                        data-bs-html="true"
                        title="<i class='bx bx-add-to-queue'></i> <span class='small'>Tambah data</span>"><i class="bx bx-list-plus"></i> <small>Tambah Data</small></a>
                </div>
                <div class="table-responsive text-nowrap px-4 pb-4">
                  <table id="example" class="table table-border table-striped">
        <thead>
            <tr>
                <th>No</th>
                <th>Image</th>
                <th>Judul</th>
                <th>Kategori</th>
                <th style="width:1px;">Aksi</th>
            </tr>
        </thead>
        <tbody>
		
		    <?php
		    $no = 0;
            $sql  = $pdo->query("SELECT * FROM zon_games ORDER BY id DESC");
            while($data = $sql->fetch(PDO::FETCH_ASSOC)){
                $no++;
            ?>
		
            <tr>
                <td style="width:1px;">
				    <?php echo $no; ?>
				</td>
                <td style="width:120px;">
				    <div class="avatar-">
					    <?php $image_path = (empty($data['game_image_url'])) ? "assets/img/backgrounds/default-bg.png" : "".$data['game_image_url'];// Example usage for: Ternary Operator ?>
                        <img src="<?php echo $image_path; ?>" width="100" alt="" class="rounded" />
                    </div>
				</td>
                <td><?php echo $data['game_name']; ?></td>
                <td><?php echo $data['game_category']; ?></td>
                <!--<td><?php //echo tanggal_indo(substr($data['posted'],0,10)); ?></td>-->
                <td><div class="dropdown">
                            <button type="button" class="btn p-0 dropdown-toggle hide-arrow" data-bs-toggle="dropdown">
                              <i class="bx bx-dots-vertical-rounded"></i>
                            </button>
                            <div class="dropdown-menu">
                              <a class="dropdown-item" href="index.php?module=<?php echo $module; ?>&act=edit&id=<?php echo @$data['id']; ?>" title="Edit"
                                ><i class="bx bx-edit-alt me-1"></i> Edit</a
                              >
                              <a class="dropdown-item" href="index.php?module=<?php echo $module; ?>&act=delete&id=<?php echo @$data['id']; ?>" title="Hapus"
                                ><i class="bx bx-trash me-1"></i> Hapus</a
                              >
                            </div>
                          </div></td>
            </tr>
            
			<?php } ?>
			
        </tbody>
        
    </table>
                </div>
              </div>
		</div>
	</div>		  
</div>

<!-- /default -->

<?php
    break;
    case "edit":
?>

<!-- edit -->

<div class="container-xxl flex-grow-1 container-p-y">         
    <div class="row">
	
                <!-- Basic Layout -->
				
				<?php
                $sql  = $pdo->query("SELECT * FROM zon_games WHERE id = '".$id."'");
                $data = $sql->fetch(PDO::FETCH_ASSOC);
                ?>
				
                <!-- Basic Layout -->
				<form id="form" method="POST" action="index.php?module=<?php echo $module; ?>&act=update&id=<?php echo $id; ?>" enctype="multipart/form-data">
                <div class="col-xxl">
                  <div class="card mb-4">
                    <div class="card-header d-flex align-items-center justify-content-between">
                      <h5 class="mb-0">Edit Data</h5>
                     
                    </div>
					<div class="card-body">
					    
                      <div class="d-flex align-items-start align-items-sm-center gap-4">
					    <?php $image_path = (empty($data['game_image_url'])) ? "assets/img/backgrounds/default-bg.png" : "".$data['game_image_url'];// Example usage for: Ternary Operator ?>
                        <img
                          src="<?php echo $image_path; ?>"
                          alt="user-avatar"
                          class="d-block rounded"
                          height=""
                          width="100"
                          id="uploadedAvatar"
                        />
                        
                      </div>
                    </div>
                    <hr class="my-0" />
                    <div class="card-body">
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Kategori Game</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="" name="game_category" value="<?php echo $data['game_category']; ?>" required />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Nama Gmae</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="judul" name="game_name" value="<?php echo $data['game_name']; ?>" required />
                                <input type="hidden" class="form-control" id="url" name="game_slug" value="<?php echo $data['game_slug']; ?>" />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Url Game</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="game_url" value="<?php echo $data['game_url']; ?>" required />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Url Image</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="game_image_url" value="<?php echo $data['game_image_url']; ?>" required />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-message">Deskripsi</label>
                            <div class="col-sm-10">
                                <textarea
                                   id="editor1"
                                   class="form-control"
                                   placeholder=""
                                   aria-label=""
                                   aria-describedby="basic-icon-default-message2"
                                   name="game_description"
                                ><?php echo $data['game_description']; ?></textarea>
                            </div>
                        </div>
                        <!--<div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-message">Instruksi</label>
                            <div class="col-sm-10">
                                <textarea
                                   id="editor2"
                                   class="form-control"
                                   placeholder=""
                                   aria-label=""
                                   aria-describedby="basic-icon-default-message2"
                                   name="game_instruction"
                                ><?php echo $data['game_instruction']; ?></textarea>
                            </div>
                        </div>-->
                        <div class="row justify-content-end">
                            <div class="col-sm-10 d-flex">
                                <input type="hidden" name="update">
                                <button type="submit" class="btn btn-primary me-2" name="update">
								     <span id="spin" class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="display:none;"></span>
                                    <span class="visually-hidden">Loading...</span>
								    Simpan
							    </button>
							    <button type="reset" class="btn btn-secondary" onclick="location.href='<?php echo BASE_URL; ?>/dashboard/index.php?module=<?php echo $module; ?>';">Kembali</button>
                            </div>
                        </div>
                    </div>
                  </div>
                </div>          
              </form>    
              
</div>
</div>

<!-- /edit -->

<?php
    break;
    case "add":
?>

<!-- add  -->

<div class="container-xxl flex-grow-1 container-p-y">         
    <div class="row">
	
        <!-- Basic Layout -->
		<form id="form" method="POST" action="index.php?module=<?php echo $module; ?>&act=create&id=<?php echo $id; ?>" enctype="multipart/form-data">
            <div class="col-xxl">
                 <div class="card mb-4">
                    <div class="card-header d-flex align-items-center justify-content-between">
                        <h5 class="mb-0">Tambah Data</h5>
                    </div>
					<div class="card-body">
					    
                        <div class="d-flex align-items-start align-items-sm-center gap-4">
					    <?php $image_path = (empty($data['game_image_url'])) ? "assets/img/backgrounds/default-bg.png" : "".$data['game_image_url'];// Example usage for: Ternary Operator ?>
                        <img
                          src="<?php echo $image_path; ?>"
                          alt="user-avatar"
                          class="d-block rounded"
                          height=""
                          width="100"
                          id="uploadedAvatar"
                        />
                        
                      </div>
                    </div>
                    <hr class="my-0" />
                    <div class="card-body">
                        <!--<div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Kategori</label>
                            <div class="col-sm-10">
                                <select name="game_category_id" class="form-select" required>
                                    <option value=""  selected disabled hidden >Pilih Kategori</option>
                                    <?php
                                    //$sql  = $pdo->query("SELECT * FROM tb_game_categories");
                                    //while($data = $sql->fetch(PDO::FETCH_ASSOC)){
                                    ?>
                                    
                                    <option value="<?php //echo $data['id']; ?>"><?php //echo $data['game_category_name']; ?></option>
                                    
                                    <?php //} ?>
                                </select>
                            </div>
                        </div>-->
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Kategori Game</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="" name="game_category" value="" required />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Nama Game</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="judul" name="game_name" value="" required />
                                <input type="hidden" class="form-control" id="url" name="game_slug" value="" />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Url Game</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="game_url" value="" required />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-name">Url Image</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="game_image_url" value="" required />
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-message">Deskripsi</label>
                            <div class="col-sm-10">
                                <textarea
                                   id="editor1"
                                   class="form-control"
                                   placeholder=""
                                   aria-label=""
                                   aria-describedby="basic-icon-default-message2"
                                   name="game_description"
                                ></textarea>
                            </div>
                        </div>
                        <!--
                        <div class="row mb-3">
                            <label class="col-sm-2 col-form-label" for="basic-default-message">Instruksi</label>
                            <div class="col-sm-10">
                                <textarea
                                   id="editor2"
                                   class="form-control"
                                   placeholder=""
                                   aria-label=""
                                   aria-describedby="basic-icon-default-message2"
                                   name="game_instruction"
                                ></textarea>
                            </div>
                        </div>
                        -->
                        <div class="row justify-content-end">
                            <div class="col-sm-10 d-flex">
                                <input type="hidden" name="add">
                                <button type="submit" class="btn btn-primary me-2" name="add">
								     <span id="spin" class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="display:none;"></span>
                                    <span class="visually-hidden">Loading...</span>
								    Simpan
							    </button>
							    <button type="reset" class="btn btn-secondary" onclick="location.href='<?php echo BASE_URL; ?>/dashboard/index.php?module=<?php echo $module; ?>';">Kembali</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>          
        </form>
    </div>
</div>

<!-- /add -->

<?php
    break;
    case "update":
?>

<!-- update  -->

<?php
$sql  = $pdo->query("SELECT * FROM zon_games WHERE id='".$id."'");
$data = $sql->fetch(PDO::FETCH_ASSOC);
?>
<?php
    $game_category     = htmlspecialchars($_POST['game_category'], ENT_QUOTES);
    $game_name         = htmlspecialchars($_POST['game_name'], ENT_QUOTES);
    $game_slug         = $_POST['game_slug'];
    $game_url          = $_POST['game_url'];
    $game_image_url    = $_POST['game_image_url'];
    $game_description  = addslashes($_POST['game_description']);
    //$game_instruction  = addslashes($_POST['game_instruction']);
    
    $sql = $pdo->query(
	"UPDATE zon_games SET
	    game_category     ='".$game_category."',
	    game_name         ='".$game_name."',
	    game_slug         ='".$game_slug."',
	    game_url          ='".$game_url."',
	    game_description  ='".$game_description."',
        game_image_url    ='".$game_image_url."'
	    WHERE 
	    id='".$id."'"
		);	
	
	if($sql){
	    header("Location: index.php?module=".$module."&act=edit&id=".$id);
	}
	else
	{
	    print_r($pdo->errorInfo());
	}
?>

<!-- /update -->

<?php
    break;
    case "create":
?>

<!-- create  -->

<?php
    $game_category     = htmlspecialchars($_POST['game_category'], ENT_QUOTES);
    $game_name         = htmlspecialchars($_POST['game_name'], ENT_QUOTES);
    $game_slug         = $_POST['game_slug'];
    $game_url          = $_POST['game_url'];
    $game_image_url    = $_POST['game_image_url'];
    $game_description  = addslashes($_POST['game_description']);
    //$game_instruction  = addslashes($_POST['game_instruction']);
    
    $pdo->query ("insert into zon_games
    (
        game_category,
        game_name,
        game_slug,
        game_url,
        game_description,
        game_image_url
        )
        values 
            (
            '".$game_category."',
            '".$game_name."',
            '".$game_slug."',
            '".$game_url."',
            '".$game_description."',
            '".$game_image_url."'
            )
             ");
		
	header("Location: index.php?module=".$module);
?>

<!-- /create -->

<?php
    break;
    case "delete":
?>

<!-- delete  -->


<?php
$s=$pdo->query("DELETE FROM zon_games WHERE id = '".$id."'")or die(print_r($pdo->errorInfo(), true));
header("Location: ".$_SERVER['HTTP_REFERER']);
?>

<!-- /delete -->

<?php	
    break;
}
?>


    <script type="text/javascript">
        $(document).ready(function() {
            $('#judul').keyup(function(e) {
                var judul = $('#judul').val();
                var url = $('#judul').val().toLowerCase();
                url     = url.replace(/[^a-z0-9]+/gi, '-');
                $('#url').val(url);
                $('#meta_title').val(judul);
            })
        });
    </script>