<?php
$pgMod="security";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);



if(isset($_POST['update_chapter'])){
    
    $resulr1=$_POST['subjectid'];

    foreach($resulr1 as $x => $x_value) {
	  
		$saveAry=array("subject"=>$_POST["subject"][$x]);
		$_POST['subjectid'][$x];

        $flgUp=$db->updateAry("subject_new", $saveAry,"where id='".$_POST['subjectid'][$x]."' and session='".$_SESSION["admin"]["session"]."' and type='chapter' ");
		

    }
		redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>"add_subject","action"=>"edit","id"=>$_GET['id'])));
}

if($_POST)
{
    // print_r($_POST);
    // print_r($_FILES);
    //     if(isset($_FILES["image"]["name"]) && !empty($_FILES["image"]["name"])){
    //     echo "image selected";
    //     }
    // die();
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['name']) || trim($_POST['name'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter name.");
	}
	
	
    if(isset($_FILES["image"]["name"]) && !empty($_FILES["image"]["name"])){
		$lfilename = basename($_FILES["image"]["name"]);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/security/".$lnewfile))
    		{
    		    $_POST['image'] = $lnewfile;
    		}
	}
	
	
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$flgIn=$db->insertAry("security",$_POST);
		redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("security",$_POST,"where id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
}

elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
    $getoldimage = $db->getVal("select image from security where id='".$_GET['id']."' ");
    unlink("uploads/security/$getoldimage");
	$res=$db->delete("security","where id='".$_GET['id']."' ");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
}


if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
?>
<div class="alert alert-success">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-thumbs-up-alt"></i> <b></b><?php echo $_SESSION['msg'] ?></i>
</div>
<?php
unset($_SESSION["msg"]);
}

if(is_array($alert_err) && count($alert_err)>0)
{
?>
<div class="alert alert-error">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-warning-sign"></i> <b></b> <?php echo $alert_err[0] ?></i>
</div>
<?php

}


if($pgAct=="edit" && $_GET["id"]!="")
{
	$aryForm=$db->getRow("SELECT * FROM security WHERE id='".$_GET['id']."'");
}

if($pgAct=="add" || ($pgAct=="edit" && $_GET["id"]!="")){
?>
<input type="hidden" name="src" value="Notice" />
<input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
<div class="row-fluid">
            <div class="span12">
            <div class="top-bar">
<h3><i class="icon-cog"></i>Security</h3>
<div>
</div>
</div>
<div class="well no-padding"  id="printDiv2">
<div class="controls controls-row">

<div style="padding:20px;">
  <form class="login-form" method="post" novalidate  enctype="multipart/form-data">

    <div class="control-group">
    <label class="control-label">Name</label>
      <div  class="controls">
          <input type="text" name="name" value="<?php echo $aryForm["name"]; ?>" style="width:100%"/>
      </div>
    </div>

    <div class="control-group">
    <label class="control-label">Mobile</label>
      <div  class="controls">
          <input type="text" name="mobile" value="<?php echo $aryForm["mobile"]; ?>" style="width:100%"/>
      </div>
    </div>
    
    <div class="control-group">
    <label class="control-label">Address</label>
      <div  class="controls">
          <input type="text" name="address" value="<?php echo $aryForm["address"]; ?>" style="width:100%"/>
      </div>
    </div>

    <div class="control-group">
    <label class="control-label">Photo</label>
      <div  class="controls">
          
          <?php if($pgAct=="edit" && $_GET["id"]!=""){
            ?>
            <img src="../uploads/security/<?php echo $aryForm["image"]; ?>" style="height:100px;" />
            <?php
          }
          ?><br>
          <input type="file" name="image" />
      </div>
    </div>

    <br>
      
        <div class="form-actions">
			<button type="submit" class="btn btn-primary"> Save</button>
			<button type="button" class="btn"> Cancel</button>
		</div>
    </form>
    
</div>

        </div>
        <br>
</div>
<!-- / Forms: Content -->
</div>
    </div>

<?php
}

else {
    
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <button style="float:right; margin-top:5px" onClick="window.location='./?src=Notice&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">

    <div id="filterdata"> 
	<?php
	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM security order by visit_time DESC"); 
	$i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table  id="example" class="table table-striped table-bordered"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Name</th>
                    <th>Mobile</th>
                    <th>Address</th>
                    <th>Image</th>
                    <th>Date</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["name"]; ?></td>
                    <td><?php echo $clist["mobile"]; ?></td>
                    <td><?php echo $clist["address"]; ?></td>
                    <td><img src="../uploads/security/<?php echo $clist["image"]; ?>" style="height:100px;" /></td>
                    <td><?php echo $clist["visit_time"]; ?></td>
                    <td>
                    <?php $aryPgAct['action']="edit"; ?>
                        <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>
                    <?php $aryPgAct['action']="delete"; ?>
                         <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>  
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                	<th></th>
                    <th colspan="3"></th>
                </tr>
            </tfoot>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div>
	</div></div></div>
	<?php

}
?>
    
    <!--</form>-->


<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
}

@media screen {
   #myPrntbtn1 {
        display: none;
    }
}
</style>

<script type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"bAutoWidth": false
		});
	});
	
	</script>
	
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>




<script>
	
function delete_subjectimage(id){
  var r = confirm("Are you sure want to delete?");
  if (r == true) {
    $.post( "Notice/ajax.php", { type: "delete_subjectimage" , id: id } , function( data ) {
        location.reload();

	});
  }

}



</script>

<style>
a {
    color: blue;
    text-decoration: none;
}
</style>
