<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//db call
$db = new SQLite3('./api/.db.db');

//table name
$table_name = "apks";

//current file var
$base_file = basename($_SERVER["SCRIPT_NAME"]);

//create if not
$db->exec("CREATE TABLE IF NOT EXISTS {$table_name}(id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL,name TEXT,url TEXT,imge TEXT,desc TEXT,vers TEXT,date TEXT)");

//table call
$res = $db->query("SELECT * FROM {$table_name}");

//update call
@$resU = $db->query("SELECT * FROM {$table_name} WHERE id='{$_GET['update']}'");
@$rowU=$resU->fetchArray();
if(isset($_POST['submitU'])){
	$db->exec("UPDATE {$table_name} SET name='{$_POST['name']}',url='{$_POST['url']}',imge='{$_POST['imge']}',desc='{$_POST['desc']}',vers='{$_POST['vers']}', date='{$_POST['date']}' WHERE id='{$_GET['update']}'");
	$db->close();
	header("Location: {$base_file}");
}

//submit new
if (isset($_POST['submit'])){
	$db->exec("INSERT INTO {$table_name}(name,url,imge,desc,vers,date) VALUES('{$_POST['name']}','{$_POST['url']}','{$_POST['imge']}','{$_POST['desc']}','{$_POST['vers']}','{$_POST['date']}')");
	header("Location: {$base_file}");
	}

//delete row
if(isset($_GET['delete'])){
	$db->exec("DELETE FROM {$table_name} WHERE id=".$_GET['delete']);
	header("Location: ". basename($_SERVER["SCRIPT_NAME"])."?status=1");
}

include ('includes/header.php');

//delete modal
?>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h2>Confirm</h2>
            </div>
            <div class="modal-body">
                Do you really want to delete?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>
<?php
if (isset($_GET['create'])){

//create form
?>
        <div class="col-md-8 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-commenting"></i> APKs</h2>
                        </center>
                    </div>
                    <div class="card-body">
                        <div class="col-12">
                            <h3>Create New APK</h3>
                        </div>
                            <form method="post">
                                <div class="form-group">
                                    <label class="form-label " for="name">App Name</label>
                                    <input class="form-control" id="description" name="name" placeholder="App Name" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="url">App URL</label>
                                    <input class="form-control" id="description" name="url" placeholder="App URL" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="imge">App Image</label>
                                    <input class="form-control" id="description" name="imge" placeholder="App Image" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="base64">App Description</label>
                                    <textarea rows="6" class="form-control" id="description" name="desc" placeholder="App Description" type="text"/></textarea>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="vers">App Version</label>
                                    <input class="form-control" id="description" name="vers" placeholder="App Version" type="text"/>
                                </div>
								<input type="hidden" name="date"  value="<?=date("Y-m-d")?>">
                                <div class="form-group">
                                    <center>
                                        <button class="btn btn-info " name="submit" type="submit">
                                            <i class="icon icon-check"></i> Submit
                                        </button>
                                    </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>

<?php 
}else if (isset($_GET['update'])){ 

//update form
?>
        <div class="col-md-8 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-bullhorn"></i> APKs</h2>
                        </center>
                    </div>
                    
                    <div class="card-body">
                        <div class="col-12">
                            <h3>Edit APK</h3>
                        </div>
                            <form method="post">
                                <div class="form-group">
                                    <label class="form-label " for="name">App Name</label>
                                    <input class="form-control" id="description" name="name" placeholder="App Name" value="<?=$rowU['name'] ?>" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="url">App URL</label>
                                    <input class="form-control" id="description" name="url" placeholder="App URL" value="<?=$rowU['url'] ?>" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="imge">App Image</label>
                                    <input class="form-control" id="description" name="imge" placeholder="App Image" value="<?=$rowU['imge'] ?>" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="desc">App Description</label>
                                    <textarea rows="6" class="form-control" id="description" name="desc" placeholder="App Description" type="text"/><?=$rowU['desc']?></textarea>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="vers">App Version</label>
                                    <input class="form-control" id="description" name="vers" placeholder="App Version" value="<?=$rowU['vers'] ?>" type="text"/>
                                </div>
								<input type="hidden" name="date"  value="<?=date("Y-m-d")?>">
                                <div class="form-group">
                                    <center>
                                        <button class="btn btn-info " name="submitU" type="submit">
                                            <i class="icon icon-check"></i> Submit
                                        </button>
                                    </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>
<?php
 }else{
//main table/form
	 ?>
        <div class="col-md-12 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-commenting"></i> APK Entries</h2>
                        </center>
                    </div>

                    <div class="card-body">
                        <div class="col-12">
                        	<center>
	        					<a id="button" href="./<?php echo $base_file ?>?create" class="btn btn-info">Create New APK</a>
	        				</center>
    					</div>

    					<hr>
						<div class="table-responsive">
							<table id="Dtable" class="table" style="width:100% background-color:#FFFFE0;">
							<thead style="color:white!important">
								<tr>
								<th>APK Name</th>
								<th>APK URL</th>
								<th>App Image</th>
								<th>App Version</th>
								<th>Description</th>
								<th>Edit&nbsp&nbsp&nbspDelete</th>
								</tr>
							</thead>
							<tbody>
							<?php while ($row = $res->fetchArray()) {?>
							<tr style="background-color:#2b2b2b;color:#ffffff;">
								<td><?=$row['name'] ?></td>
								<td><?=$row['url'] ?></td>
								<td><?=$row['imge'] ?></td>
								<td><?=$row['vers'] ?></td>
								<td>Click edit to see -></td>
								<td>
								<a class="btn btn-info btn-ok" href="./<?php echo $base_file ?>?update=<?=$row['id'] ?>"><i class="fa fa-pencil-square-o"></i></a>
								&nbsp&nbsp&nbsp
								<a class="btn btn-danger btn-ok" href="#" data-href="./<?php echo $base_file ?>?delete=<?=$row['id'] ?>" data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash-o"></i></a>
								</td>
							</tr>
							<?php }?>
							</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
<?php }?>

<?php include ('includes/footer.php');?>

</body>
</html>