<?php
	ob_start();
session_start();
if($_SESSION['admin']=="")
{
	header("location: login.php");
}
$madeby = $_SESSION['admin'];
$pageTitle = "تعديل الخلفية";
include("../var.php");
include("header.php");
	$msg="";
$con = mysqli_connect($DBHOST,$DBUSER,$DBPASS,$DBNAME) or die("Some error occurred during connection " . mysqli_error($con));
mysqli_query($con, "SET CHARACTER SET 'utf8'");
$posted = "";
if(isset($_POST['submit'])){ $posted = $_POST['submit']; }
if($posted !== "")
   {
   //IMG UPLOAD
	$file_name = "";
	if(isset($_FILES['img']['name'])){ $file_name = $_FILES['img']['name']; }
   if($file_name != "")
	{
   $tmp_name = $_FILES['img']['tmp_name'];
   $ext = pathinfo($file_name, PATHINFO_EXTENSION);
   $file_name = "".time().".".$ext."";
   $path= "../img/".$file_name."";
	
	if (move_uploaded_file($tmp_name, $path))
	{
	}
		$img = $file_name;
		$img_sql = "`session` = '".$img."'";
	}
	else
	{
	$img = $_POST['imgn']; 
	$img_sql = "";   
	}
   //IMG UPLOAD
	$password = substr(hash('sha512',rand()),0,20);
	mysqli_query($con, "UPDATE `admin` SET ".$img_sql.", `pass` = '$password' WHERE  `user`='background'");
	$msg="تم تعديل الخلفية بنجاح";
   }

$song = @mysqli_fetch_array(mysqli_query($con, "select * from admin where `user`='background' limit 0,1"));
$logo = $song['session'];

$del = "";
if(isset($_GET['del'])){ $del = $_GET['del']; }
if ($del == 1)
{
	$password = substr(hash('sha512',rand()),0,20);
	mysqli_query($con, "UPDATE `admin` SET `session` = '', `pass` = '$password' WHERE  `user`='background'");
	$logo = "";
}
?>
<table width="100%" border="0" cellspacing="3" cellpadding="3">
	<tr>
	  <td valign="top">
	  <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC">
        <tr> 
            <td  valign="top" bgcolor="#FFFFFF">
			 <table width="800" border="1" style="border-collapse:collapse" cellspacing="2" cellpadding="6" align="center">
              <form method="post" action="background.php" enctype="multipart/form-data">
                <tr>
                  <td colspan="2" bgcolor="f2f2f2" align="center"><strong>تعديل الخلفية</strong></td>
                </tr>
				<? if($msg!="") {?>
				 <tr>
                  <td colspan="2"  align="center"><font color="#FF0000"><?=$msg?></font></td>
                </tr>
				<? }?>
                <tr>
                  <td height="26">الخلفية الحالية:</td>
                  <td>
				  <?
				  if($logo == "")
				  {
				  echo 'لم يتم اضافة خلفية بعد<br>';
				  $bgset = 0;
				  }else{
				  echo '<img src="../img/'.$logo.'" height="300" /><br>';
				  $bgset = 1;
				  }
				  ?>
				  <input name="imgn" type="hidden" value="<?=$logo?>"/>
				  <input name="img" type="file" id="img" size="50" />
				  <br>
				  <? if ($bgset == 1) { ?>
				  <a href="background.php?del=1" onclick="return confirm('هل انت متاكد من حذف هذه الخلفية؟');" style="text-decoration: none; color: red;">حذف الخلفية</a>
				  <?}?>
				  </td>
                </tr>
				<tr>
                <input type='hidden' name='id' value='<?=$id?>' size='20'></tr>
				<tr>
				  <td colspan="2" align="center" bgcolor="#CCCCCC"><input type="submit" name="submit" value="تحديث الخلفية"></td>
				</tr>
              </form>
            </table>
        </tr>
      </table>
	  </td>
</tr>
</table>
<?	include("footer.php");?>
