<?php include('header.php');?>
<?php include('sidebar.php');?>
 
<?php
$memberid=$_REQUEST['userid'];
$maintitle=AL_DOCUMENTS;
$page="userdocuments.php";
$table="tbl_documents";
$field_id="documentid";
$mode=$_REQUEST['mode'];
$content_id=$_REQUEST['contentid'];
if(isset($_REQUEST['start'])){
$start=$_REQUEST['start']-1;
}
else {
$start=0;
}
?>
<?php
 
if(isset($_REQUEST['submit'])=="submit"){ 
$dataexist=array();
//$required = array("issuedate","expiredate","memberid");
$values = $_POST;
unset($values['submit']);
unset($values['mode']);

$userid=array("user_id"=>$user_id);
$values=array_merge($values,$userid);
$images=$helper->multiple_image_upload($_FILES['frm_filename_images'],"gallery");

if($_REQUEST['mode']=="add"){
$message=$helper->insert($table,$values,$required,$dataexist);
        foreach($images as $image){
                   $helper->insert_multiple_images($image,$message['id'],"tbl_gallery_images","galleryid");
           } 
}
if($_REQUEST['mode']=="edit"){
$message=$helper->update($table, $values,array($field_id=>$_REQUEST['contentid']),$required);
 foreach($images as $image){
	   $helper->insert_multiple_images($image,$_REQUEST['contentid'],"tbl_gallery_images","galleryid");
   }
}
}
?>

<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;?>?userid=<?php echo $memberid;?>"><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']));

        if(isset($_REQUEST['mode1'])){
			 $message=$helper->delete("tbl_gallery_images", array("imageid"=>$_REQUEST['imgid']));
		}
}
?>

     <div class="col-md-12">
          <!-- Horizontal Form -->
          <div class="box box-info">
            <div class="box-header with-border">
              <h3 class="box-title"><?php echo AL_ADD;?> <?php echo $maintitle;?></h3>
            </div>
            
            
            <div class="nav-tabs-custom">
           
             <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
             
                 <?php
			if($message!=""){
			?>
            <button class="btn btn-block btn-success btn-flat" type="button"><?php echo $message['message'];?></button>
			<?php	
			}
			?>
            <!-- /.box-header -->
            <!-- form start -->
           
            <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"><?php echo AL_DOCUMENTTYPES;?></label>

                  <div class="col-sm-6">
                  <?php
                  $documenttypes= $helper->select(" tbl_document_type","documenttypeid,title",array());				   
				  ?>
                  <select name="documenttypeid" class="form-control" id="documenttypeid">
                      <option value=""><?php echo AL_SELECTDOCUMENTTYPES;?></option>
                  <?php
				  foreach($documenttypes as $doctype){
				  ?>
                  <option <?php if($doctype['documenttypeid']==$contentdetails[0]['documenttypeid']){ echo "selected";}?> value="<?php echo $doctype['documenttypeid'];?>"><?php echo $doctype['title'];?></option>
                  <?php
				  }
				  ?>
                  </select>
                   </div>
                </div>
                
                   
                 <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_MEMBERS;?> </label>

                  <div class="col-sm-6">
                  <?php
                   $memberdetails= $helper->select("tbl_members","*",array("memberid"=>$memberid));					     			   
				  ?>
                  <input type="hidden" name="branchid" value="<?php echo $memberdetails[0]['branchid'];?>"/>
                  <select name="memberid" class="form-control" id="memberid"> 
                       <?php
			 foreach($memberdetails as $content){
                        ?>
                         <option value="<?php echo $memberdetails[0]['memberid'];?>"><?php echo $memberdetails[0]['firstname']." ".$memberdetails[0]['middlename']." ". $memberdetails[0]['lastname'];?></option>
                        <?php
			 }
			 ?>
                      
                  </select>
                   </div>
                </div>
                   <div class="form-group">
                <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_TITLE;?></label>
				<div class="col-sm-6"> 
                  <input name="title" type="text" value="<?php echo $contentdetails[0]['title'];?>" class="form-control pull-right" id="title">
                 
                </div>
                <!-- /.input group -->
              </div>
                
                <div class="form-group">
                <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_ISSUEDATE;?></label>
				<div class="col-sm-6">
                <div class="input-group date">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <input name="issuedate" type="text" value="<?php echo $contentdetails[0]['issuedate'];?>" class="form-control pull-right" id="datepicker">
                </div>
                </div>
                <!-- /.input group -->
              </div>
                  <div class="form-group">
                <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_EXPIRYDATE;?></label>
				<div class="col-sm-6">
                <div class="input-group date">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <input name="expiredate" type="text" value="<?php echo $contentdetails[0]['expiredate'];?>" class="form-control pull-right" id="datepicker2">
                </div>
                </div>
                <!-- /.input group -->
              </div>
            <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_PLACEOFISSUE;?></label>

                  <div class="col-sm-6">
                    <input type="text" name="placeofissue" value="<?php echo $contentdetails[0]['placeofissue'];?>" class="form-control" id="title" placeholder="Place of Issue" >
                  </div>
                </div>
                 <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_ISSUEDBY;?></label>

                  <div class="col-sm-6">
                    <input type="text" name="issuedby" class="form-control" value="<?php echo $contentdetails[0]['issuedby'];?>" id="issuedby" placeholder="issued by" >
                  </div>
                </div> 
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_IMAGE;?></label>
                  <div class="col-sm-8">
                   <input type="file" class="" name="frm_filename_images[]" multiple="multiple">
                  </div>
                </div>
                <?php
				 $allimages= $helper->select("tbl_gallery_images","*",array("galleryid"=>$_REQUEST['contentid']));
				 if(count($allimages)>0){
				?>
               <div class="form-group">
                    
                 <?php
				 foreach($allimages as $image){
				 ?> 
                  <div class="col-sm-3">
                      
                   <img src="<?php echo $site_url;?>thumb.php?src=<?php echo $site_url;?>images/gallery/<?php echo $image['image'];?>&w=160&h=160"/><br/>
                   <a href="<?php echo $site_url;?><?php echo $page;?>?mode=edit&mode1=imgdelete&imgid=<?php echo $image['imageid'];?>&contentid=<?php echo $_REQUEST['contentid'];?>" onclick="return getConfirmation()"><span class="label label-danger"><i class="fa fa-times"></i> <?php echo AL_DELETE;?></span></a>
                   <a href="<?php echo $site_url;?>images/gallery/download.php?file=<?php echo $image['image'];?>"><span class="label label-primary"> <?php echo AL_DOWNLOAD;?></span></a>
                   <br/>
                  </div>
                 <?php
				 }
				 ?>
                </div>
                <?php
				}
				?>
                
                
             
              
             <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-left"/>
                 
              </div>
              <!-- /.box-footer -->
             
            </div>
            <!-- /.tab-content -->
            </form>
          </div>
            
          </div>
         
        </div>
        </div>
        <?php
	    }
		else {
	    ?>
        <?php
		if($mode=="delete"){
			 $message=$helper->delete($table, array($field_id=>$_REQUEST['contentid']));
		}
		?>
          <?php
		if($mode=="approve"){
			$helper->update($table, array("status"=>1),array($field_id=>$_REQUEST['contentid']), array());
		}
		?>
         <?php
		if($mode=="pending"){
			$helper->update($table, array("status"=>2),array($field_id=>$_REQUEST['contentid']), array());
		}
		?>
        <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&userid=<?php echo $memberid;?>"><button class="btn btn-danger btn-flat"><i class="glyphicon  glyphicon-plus"></i>&nbsp;<?php echo AL_ADDNEW;?> <?php echo $maintitle;?></button></a>
                   
                </div>
              </div>
            </div>
            <?php
			$listtypes=$helper->selectlimit($table,"*",array("memberid"=>$memberid),$start,$limit);			 
			?>
            <!-- /.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 class="table table-hover">
                <tr>
                  <th><?php echo AL_SERIALNO;?></th>
                  <th><?php echo AL_DOCUMENTS;?></th>
                  <th><?php echo AL_NAME;?></th>
                   <th><?php echo AL_EXPIRYDATE;?></th>
                   <th><?php echo AL_ISSUEDATE;?></th>
                  <th><?php echo AL_STATUS;?></th>
                  <th><?php echo AL_ACTION;?></th>
                </tr>
                <?php
				$i=($start*$limit)+1;
				foreach($listtypes as $type){
				?>
                 <?php
				  if($_REQUEST['start']>0){
					  $limitpage=$_REQUEST['start'];
				  }
				  else {
					  $limitpage=1;
				  }
				 
                    
				  ?>
                <tr>
                  <td><?php echo $i;?></td>
                  <?php
                  $doctype= $helper->select("tbl_document_type","title",array("documenttypeid"=>$type['documenttypeid']));
		 ?>
                   <?php
				  
                  $expirydate  =date_create($type['expiredate']);
                  $current_date =date_create(date("Y-m-d"));
                //$date_to_compare = date("Y-m-d",time()+86400); //1 day later
                  $diff=date_diff($current_date,$expirydate);
                  $expirestatus=$diff->format("%a");
				  
                  if ($expirydate<$current_date) {
                   $expires= "Expired";
				   $expirecheck=-1;
                  }
                  else {
                       $expires= $expirestatus." days to expire";
					   $expirecheck=1;
                  }
                    ?>
                  <td><b><?php echo $doctype[0]['title'];?></b><br/><?php echo $type['title'];?></td>
                   <?php
                  $membername= $helper->select("tbl_members","firstname,middlename,lastname",array("memberid"=>$type['memberid']));
		   ?>
                  <td><?php echo $membername[0]['firstname']." ".$membername[0]['middlename']." ".$membername[0]['lastname'];?></td>
                  <td><?php echo $type['expiredate'];?></td>
                   <td><?php echo $type['issuedate'];?></td>
                  <td>	  
                 <?php
				 if($expirecheck>0){
				  if($expirestatus>45){
				 ?>	  
                 <a href="#"><span class="label label-success"><?php echo $expires ; ?></span></a>
                 <?php
				  }
				   if($expirestatus<=45 & $expirestatus>=30) {
				 ?>
                 <a href="#"><span class="label label-primary"><?php echo $expires ; ?></span></a>
                 <?php
				  }
				 
                   if($expirestatus<30 & $expirestatus>=15) {
				 ?>
                 <a href="#"><span class="label label-info"><?php echo $expires ; ?></span></a>
                 <?php
				  }
				 if($expirestatus<15 & $expirestatus>=0) {
				 ?>
                 <a href="#"><span class="label label-warning"><?php echo $expires ; ?></span></a>
                 <?php
				  }
				 }
				  if($expirecheck<0){
				 ?>
                 <a href="#"><span class="label label-danger"><?php echo $expires ; ?></span></a>
                 <?php
				  }
				  ?>
                  </td>
                  <td>
                  <a href="<?php echo $site_url;?><?php echo $page;?>?userid=<?php echo $_REQUEST['userid'];?>&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($valexist==0 && $valexist==0){
				  ?>
                   <a href="<?php echo $site_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
				$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>
<?php include('footer.php');  ?>