<?php
session_start();
ob_start();

$domain = parse_url($_SERVER['SERVER_NAME']);
$upload = "https://" . $domain['path'] .'/lp1/upload/';

$list = array(
    1 => 'Section 1',
    2 => 'Section 2',
    
    99 => 'Java',
);


function upload($i) {
	$allowedExts = array("gif", "jpeg", "jpg", "png");
	$fileimg = $_FILES['image'. $i];
	
	$temp = explode(".", $fileimg["name"]);
	$extension = end($temp);
	$name = '';
	
	if (( ($fileimg["type"] == "image/gif") || ($fileimg["type"] == "image/jpeg") || ($fileimg["type"] == "image/jpg") || ($fileimg["type"] == "image/pjpeg") || ($fileimg["type"] == "image/x-png") || ($fileimg["type"] == "image/png")) && in_array($extension, $allowedExts))
	{
	    if ($fileimg["error"] == 0)
	    {
	        $filename = $fileimg["name"];
	        if (file_exists("upload/" . $filename)) {
	      		$filename = str_replace('.'. $extension, '_'. rand(0, 99) .'.'. $extension, $filename);
	        }
	        
            move_uploaded_file($fileimg["tmp_name"], "upload/" . $filename);
			$name = $filename;
	    }
	}
	
	return $name;
}


$content = file_get_contents('upload/price_home.json');
$row = json_decode($content);

$contenttimer = file_get_contents('upload/timer_home.json');
$timer = json_decode($contenttimer);


if(isset($_GET['delete'])  &&  $_SESSION['login'])
{
	$json = array();
	
	foreach($list as $i => $_list)
	{
		if($_GET['delete'] == $i){
			$json[$i] = array(
				'title' => '',
				'text' => '',
				'link' => '',
				'image' => '',
				'field1' => '',
				'field2' => '',
			);
		}
		else{
			$json[$i] = array(
				'title' => $row->{$i}->title,
				'text' => $row->{$i}->text,
				'link' => $row->{$i}->link,
				'image' => $row->{$i}->image,
				'field1' => $row->{$i}->field1,
				'field2' => $row->{$i}->field2,
			);
		}
	}
	
	$result = json_encode($json);
	
	$file = fopen("upload/price_home.json", "w");
	fwrite($file, $result);
	$ok = fclose($file);
	if($ok){
		header('location: params_home.php?'. rand(1000, 9999));
		exit;
	}
}

$getimage = '';

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if($_POST['task'] == 'getimage')
	{
		$getimage = upload(99999);
	}
	else if($_POST['task'] == 'delimage')
	{
		@unlink('upload/'. $_POST['filename']);
	}
	else if($_POST['task'] == 'edit')
	{
		// timer
		$filetimer = fopen("upload/timer_home.json", "w");
		fwrite($filetimer, json_encode($_POST['timer']));
		fclose($filetimer);
		
		
		// price
		$json = array();
		
		foreach($list as $i => $_list)
		{
			$image = upload($i);
			
			$json[$i] = array(
				'title' => $_POST['title'. $i],
				'text' => $_POST['text'. $i],
				'link' => $_POST['link'. $i],
				'image' => $image ? $image : $_POST['image'. $i],
				'field1' => $_POST['field1'. $i],
				'field2' => $_POST['field2'. $i],
			);
		}
		
		$result = json_encode($json);
		
		$file = fopen("upload/price_home.json", "w");
		fwrite($file, $result);
		$ok = fclose($file);
		if($ok){
			header('location: params_home.php?'. rand(1000, 9999));
			exit;
		}
	}
	else{
		if($_POST['username'] == 'admin'  &&  $_POST['password'] == '123456Rb'){
			$_SESSION['login'] = 1;
		}
		header('location: params_home.php?'. rand(1000, 9999));
		exit;
	}
}


include('params_include.php');
?>
<section class="main">
	<div class="container">
			
		<?php if(empty($_SESSION['login'])){ ?>
			<form class="form-horizontal" method="post" enctype="multipart/form-data">
				<input type="hidden" name="task" value="login">
				<div class="row">
			        <div class="col-md-offset-1 col-md-10">
			            <div class="panel panel-default">
			                <div class="panel-heading">
				                Login
			                </div>
			                <div class="panel-body">
								<div class="form-group">
									<label class="col-md-3 control-label">Username:</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="username">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">Password:</label>
									<div class="col-md-8">
										<input type="password" class="form-control" name="password">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">&nbsp;</label>
									<div class="col-md-8">
										<button type="submit" class="btn btn-primary">Login</button>
									</div>
								</div>
			                </div>
			            </div>
			        </div>
				</div>
			</form>
			
		<?php } else { ?>
			<form class="form-horizontal" method="post" enctype="multipart/form-data">
				<input type="hidden" name="task" value="getimage">
				<div class="row">
			        <div class="col-md-offset-1 col-md-10">
			            <div class="panel panel-default">
			                <div class="panel-heading">
				                Get Image Link
			                </div>
			                <div class="panel-body">
								<div class="form-group">
									<label class="col-md-3 control-label">Image:</label>
									<div class="col-md-8">
										<input type="file" class="form-control" name="image99999">
										<?=$getimage ? '<br>'. $upload . $getimage .'<br>' : ''?>
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">&nbsp;</label>
									<div class="col-md-8">
										<button type="submit" class="btn btn-primary">Submit</button>
									</div>
								</div>
			                </div>
			            </div>
			        </div>
				</div>
			</form>
			
			<form class="form-horizontal" method="post" enctype="multipart/form-data">
				<input type="hidden" name="task" value="delimage">
				<div class="row">
			        <div class="col-md-offset-1 col-md-10">
			            <div class="panel panel-default">
			                <div class="panel-heading">
				                Delete Image
			                </div>
			                <div class="panel-body">
								<div class="form-group">
									<label class="col-md-3 control-label">File name:</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="filename">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">&nbsp;</label>
									<div class="col-md-8">
										<button type="submit" class="btn btn-primary">Submit</button>
									</div>
								</div>
			                </div>
			            </div>
			        </div>
				</div>
			</form>
			
			<form class="form-horizontal" method="post" enctype="multipart/form-data">
				<input type="hidden" name="task" value="edit">
				<div class="row">
			        <div class="col-md-offset-1 col-md-10">
			            <div class="panel panel-default">
			                <div class="panel-heading">
				                Timer
			                </div>
			                <div class="panel-body">
								<div class="form-group">
									<label class="col-md-3 control-label">Iphone:</label>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[iphone_min]" placeholder="Minute" value="<?=$timer->iphone_min?>">
									</div>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[iphone_sec]" placeholder="Second" value="<?=$timer->iphone_sec?>">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">Samsung:</label>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[samsung_min]" placeholder="Minute" value="<?=$timer->samsung_min?>">
									</div>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[samsung_sec]" placeholder="Second" value="<?=$timer->samsung_sec?>">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">Tablet:</label>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[tablet_min]" placeholder="Minute" value="<?=$timer->tablet_min?>">
									</div>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[tablet_sec]" placeholder="Second" value="<?=$timer->tablet_sec?>">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">Desktop:</label>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[desktop_min]" placeholder="Minute" value="<?=$timer->desktop_min?>">
									</div>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[desktop_sec]" placeholder="Second" value="<?=$timer->desktop_sec?>">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">Popup:</label>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[popup_min]" placeholder="Minute" value="<?=$timer->popup_min?>">
									</div>
									<div class="col-md-4">
										<input type="text" class="form-control" name="timer[popup_sec]" placeholder="Second" value="<?=$timer->popup_sec?>">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">&nbsp;</label>
									<div class="col-md-8">
										<button type="submit" class="btn btn-primary">Update</button>
									</div>
								</div>
			                </div>
			            </div>
			        </div>
				</div>
				
		        <?php foreach($list as $i => $_list){  ?>
					<div class="row">
				        <div class="col-md-offset-1 col-md-10">
				            <div class="panel panel-default">
				                <div class="panel-heading">
					                <?=$_list?>
				                </div>
				                <div class="panel-body">
									<div class="form-group">
										<div class="col-md-12">
											<textarea name="title<?=$i?>" class="form-control"><?=$row->{$i}->title?></textarea>
										</div>
									</div>
									<div class="form-group">
										<div class="col-md-12">
											<textarea name="text<?=$i?>" id="editor<?=$i?>" class="form-control"><?=$row->{$i}->text?></textarea>
											
											<?php if(!in_array($i, array(99))){ ?>
											<script>
	  										    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
	  											CKEDITOR.config.allowedContent = true;
	  											CKEDITOR.replace('editor<?=$i?>');
	  										</script>
											<?php } ?>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Link</label>
										<div class="col-md-8">
											<input type="text" class="form-control" name="link<?=$i?>" value="<?=$row->{$i}->link?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Image:</label>
										<div class="col-md-8">
											<input type="file" class="form-control" name="image<?=$i?>">
											<input type="hidden" name="image<?=$i?>" value="<?=$row->{$i}->image?>">
											<?php if($row->{$i}->image){ ?>
											<div style="width: 200px">
												<a href="upload/<?=$row->{$i}->image?>" target="_blank">
													<img src="upload/<?=$row->{$i}->image?>" class="img-responsive">
												</a>
											</div>
											<?php } ?>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Field 1:</label>
										<div class="col-md-8">
											<input type="text" class="form-control" name="field1<?=$i?>" value="<?=$row->{$i}->field1?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Field 2:</label>
										<div class="col-md-8">
											<input type="text" class="form-control" name="field2<?=$i?>" value="<?=$row->{$i}->field2?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">&nbsp;</label>
										<div class="col-md-8">
											<button type="submit" class="btn btn-primary">Update</button>
											<a href="params_home.php?delete=<?=$i?>" class="btn btn-danger">Delete</a>
										</div>
									</div>
				                </div>
				            </div>
				        </div>
					</div>
		        <?php } ?>
			</form>
        <?php } ?>
        
        <div class="targetLayer"></div>
	</div>
</section>
</body>
</html>