<?php include('header.php');?>
<?php include('sidebar.php');?>
<?php 
include('includes/class.usertype.php');
$usertype = new Usertype();
?>
<?php
$maintitle="Background Image";
$page="bgimage.php";
$table="background_image";
$field_id="image_id";
$mode=$_REQUEST['mode'];
$content_id=$_REQUEST['contentid'];
if(isset($_REQUEST['start'])){
$start=$_REQUEST['start']-1;
}
else {
$start=0;
}
?>
<?php
if(isset($_REQUEST['submit'])=="submit"){ 
$title=mysqli_real_escape_string($con,$_POST['title']);
unset($values['submit']);
unset($values['mode']);
$userid=array("user_id"=>$user_id);

if($_REQUEST['mode']=="add"){
	
$message=mysqli_query($con,"insert into background_image(image) values('')");
$sel_max=mysqli_query($con,"select max(image_id) as maxslider from background_image");
$res_max=mysqli_fetch_array($sel_max);
$mid=$res_max['maxslider'];

$extension = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);
$newname= "image-".$mid.".".$extension;
$image = $_FILES['image']['name'];
$target = "../images/background/".$newname;
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$message=mysqli_query($con,"update background_image set image='$newname' where image_id='$mid'");
$message = "Item Added Successfully";
}
}
	
if($_REQUEST['mode']=="edit"){
	
	//$message=mysqli_query($con,"update sliders set title='$title' where sliderid=$content_id");
	
	$oldimage=$_POST['oldimage'];
	$image = $_FILES['image']['name'];
	if($image!=''){
		$extension = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);
$newname= "image-".$content_id.".".$extension;
  	$target = "../images/background/".$newname;
 
  	$sql = "update background_image set image='$newname' where image_id=$content_id";
  	mysqli_query($con, $sql);
 
  	if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
  	$msg = "Image uploaded successfully";
  	}else{
  	$msg = "Failed to upload image";
  	}
	}
	$message = "Item Updated Successfully";
}

}
?>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1><?php echo $maintitle;?></h1>
      <ol class="breadcrumb">
        <li><a href="dashboard.php"><i class="fa fa-dashboard"></i> <?php echo AL_DASHBOARD;?></a></li>
        <li class="active"><?php echo $maintitle;?></li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
    <?php
	if($mode=="add" || $mode=="edit") { 
	?>
     <div class="row">
 <!-- Add / Edit / Delete --> 
        <div class="col-sm-12"> 
        <div class="box box-success">
         <div class="box-header">
              <i class="fa fa-building-o"></i>
              <h3 class="box-title"><?php echo $maintitle;?></h3>
              <div class="box-tools pull-right">
                <div class="btn-group">
                <a href="<?php echo $page;?>"><button class="btn btn-danger btn-flat">&nbsp; <?php echo AL_LISTALL;?> <?php echo $maintitle;?></button></a>
                   
                </div>
              </div>
            </div> 
            </div> 
            </div>

 <!-- Add / Edit / Delete --> 
<?php
if($mode=="edit"){
$contentdetails=$helper->select($table,"*",array($field_id=>$_REQUEST['contentid']),array());
}
?>
<div class="col-md-12">
           <div class="box box-info">
            <div class="box-header with-border">
              <h3 class="box-title"> <?php echo AL_ADD;?> <?php echo $maintitle;?></h3>
            </div>
             <?php
			if($message!=""){
			?>
            <button class="btn btn-block btn-success btn-flat" type="button"><?php echo $message;?></button>
			<?php	
			}
			?>
            <!-- /.box-header -->
            <!-- form start -->
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
            <input type="hidden" name="mode" value="<?php echo $_REQUEST['mode'];?>"/>
              <div class="box-body">
               <!-- <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Caption</label>
				  <div class="col-sm-8">
                    <input type="text" required="required" name="title" value="<?php echo $contentdetails[0]['title'];?>" class="form-control" id="title" placeholder="Title" >
                  </div>
                </div>-->
                
                             
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Background Image (2000x1200) </label>

                  <div class="col-sm-8">
                    <input type="file"  name="image"  class="form-control">
                <?php    if($mode=="edit"){ ?>
                   <input type="hidden" value="<?php echo $contentdetails[0]['image'];?>" name="oldimage">
                    <img src="../images/background/<?php echo $contentdetails[0]['image'];?>" style="    width: 20%;">
                    <?php } ?>
                  </div>
                </div>
                
              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <button type="reset" class="btn btn-default"><?php echo AL_CANCEL;?></button>
                <input type="submit" name="submit" value="<?php echo AL_SUBMIT;?>" class="btn btn-info pull-right"/>
                 
              </div>
              <!-- /.box-footer -->
            </form>
          </div>
         
        </div>
        </div>
        <?php
	    }
		else {
	    ?>
        <?php
		if($mode=="delete"){
			mysqli_query($con,"delete from sliders where sliderid=".$_REQUEST['contentid']);
		
		}
		?>
          <?php
		if($mode=="approve"){
			mysqli_query($con,"update tbl_slider set status='1' where propertytypeid=".$_REQUEST['contentid']);
		}
		?>
         <?php
		if($mode=="pending"){
			mysqli_query($con,"update tbl_slider set status='2' where propertytypeid=".$_REQUEST['contentid']);
		}
		?>
        <div class="row">
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header">
              <i class="fa fa-building-o"></i>
           <h3 class="box-title"><?php echo $maintitle;?></h3>

              <div class="box-tools pull-right" data-toggle="tooltip" title="Add <?php echo $maintitle;?>">
                <div class="btn-group">
                <a href="<?php echo $page;?>?mode=add"><button class="btn btn-danger btn-flat"><i class="glyphicon  glyphicon-plus"></i>&nbsp;<?php echo AL_ADD;?> <?php echo $maintitle;?></button></a>
                   
                </div>
              </div>
            </div>
            <?php
			$listtypes=mysqli_query($con,"select * from background_image");		 
			?>
            <!-- /.box-header -->
            <div class="box-body table-responsive no-padding">
            <?php
			if($message!=""){
			?>
            <button class="btn btn-block btn-danger btn-flat" type="button"><?php echo $message['message'];?></button>
			<?php	
			}
			?>
              <table width="1292" class="table table-hover">
                <tr>
                  <th width="113"><?php echo AL_SERIALNO;?></th>
                 
                  <th width="187">Background Image</th>
                  <th width="152"><?php echo AL_ACTION;?></th>
                </tr>
                <?php
				$i=($start*$limit)+1;
				while($type=mysqli_fetch_array($listtypes)){
				?>
                 <?php
				  if($_REQUEST['start']>0){
					  $limitpage=$_REQUEST['start'];
				  }
				  else {
					  $limitpage=1;
				  }
				 
                   //$valexist=$helper->datapresent($table,"parent_id",$type[$field_id]);
				   $valexist2=$helper->datapresent("tbl_members","typeid",$type[$field_id]);
				  ?>
                <tr>
                  <td><?php echo $i;?></td>
                 
                  <td>
                 <img src="../images/background/<?php echo $type['image'];?>" style="width: 20%;">
                  </td>
                  <td>
                  <a href="<?php //echo $admin_url;?><?php echo $page;?>?mode=edit&contentid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>"><span class="label label-primary"><i class="fa fa-edit"></i> <?php echo AL_EDIT;?></span></a>
                  <?php
				  if($valexist2==0){
				  ?>
                   <a href="<?php //echo $admin_url;?><?php echo $page;?>?mode=delete&contentid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>" onclick="return getConfirmation()"><span class="label label-danger"><i class="fa fa-times"></i> <?php echo AL_DELETE;?></span></a>
                   <?php
				  }
				  ?>
                  </td>
                </tr>
                 <?php 
               $subcategories=$usertype->subcategories($type[$field_id],1,$page,$admin_url,$limitpage);
                ?>
                <?php
				$i=$i+1;
				}
				?>
            </table>
              <?php
			   $counter_list= $helper->countofdata($table,array(),$limit,array("status"=>"ASC"));
			  include('pagination/pagination.php');
			  ?>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
      </div>
      <?php
		}
		?>

    </section>
    <!-- /.content -->
  </div>
  <script>
                        CKEDITOR.replace( 'editor1' );
                </script>
<?php include('footer.php');  ?>