<?php
session_start();

require_once('functions.php');
require_once('dbconnection.php');
require_once('includes/init.php');

$password = $_SESSION['password'];
$admin_data = get_admin($conn, $password);
if ($password != $admin_data['password']) {
	header('location: login.php');
}
if ( !isset($_SESSION['password']) ) {
	header('location: login.php');
}
if ( empty( $admin_data ) ) header('location: login.php');

$pageTitle = 'Set App Color';

$data_file = "app-data.json";
$success = false ;

if ( isset($_POST['submit']) ) {
	$data = json_decode(file_get_contents($data_file), true) ?? array();
	$data = array_merge($data, $_POST);
	file_put_contents($data_file, json_encode($data, JSON_PRETTY_PRINT));
	snack ("success", "Success");
	$success = true ;
}

$app_data = json_decode(file_get_contents($data_file), true);

foreach( $app_data as $key => $val ) {
	$$key = $val;
}

?>
<!DOCTYPE html>
<html>
	<head>
		<title>App Settings</title>
		<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=false, shrink-to-fit=no">
		<link rel="stylesheet" href="css/style.css">
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	</head>
	<body class="bg-gray-200">
		<?php include("header.php") ?>
	<main>
	<div class="fr-container fr-lg pb-0">
	    <section>
				<div class="mx-1 col-sm">
				         <?php if ($success) :?>
				    <div class="alert alert-success">
                       <strong>Success!</strong> changes Saved.
                         </div>
                    <?php endif ?>
					<form class="py-3" action="" autocomplete="off" method="POST">
						<section class="row">
						<div class="form-group col-sm">
							<label>Main Color</label>
							<input name="primary_color" value="<?= $primary_color ?? ''?>" type="color" class="form-control" required>
						</div>
						
						<div class="form-group col-sm">
							<label>Gradient Color</label>
							<input name="gradient_color_end" value="<?= $gradient_color_end ?? ''?>" type="color" class="form-control" required>
						</div>
						
							</section>
						
					<button name="submit" type="submit" class="fab bg-primary material-icons border-0">check</button>
				</form>
				
				</div>