<?php
include('iframe-header.php');
?>
 <?php
$maintitle="Emergency Calls";
$page="emergency.php";
$table="tbl_emergency";
$search=$_REQUEST['search'];
$searchservice=$_REQUEST['service'];
$field_id="emergencyid";
$mode=$_REQUEST['mode'];
$content_id=$_REQUEST['contentid'];
$branchid=$_REQUEST['branchid'];

if(isset($_REQUEST['start'])){
$start=$_REQUEST['start']-1;
}
else {
$start=0;
}
?>
<?php
 
if(isset($_REQUEST['submit'])=="submit"){ 
$dataexist=array();
$required = array("title");
$values = $_POST;
unset($values['customurl']);
  $slug=str_replace(" ","-",strtolower($_POST['customurl']));
  if($slug==""){
	  $slug=$helper->escape_string(strtolower($_POST['title']));
  }
 /* $sluge=str_replace(" ","-",$slug); 
  $slugexist=$helper->slug_exist($sluge,$table,$content_id,$field_id);*/
unset($values['submit']);
unset($values['mode']);
unset($values['service']);
/*$customurl=array("customurl"=>$slugexist);
$values=array_merge($values,$customurl);*/
$userid=array("user_id"=>$user_id);
$values=array_merge($values,$userid);
$image=$helper->image_upload($_FILES['frm_filename'],"contract");
$services=$_POST['service'];
 
$photo=array("image"=>$image);
 
$services=$_POST['service'];
$images=$helper->multiple_image_upload($_FILES['frm_filename_images'],"gallery");
$images2=$helper->multiple_image_upload($_FILES['frm_filename_images2'],"gallery");
if($_REQUEST['mode']=="add"){
$message=$helper->insert($table,$values,$required,$dataexist);
}
if($_REQUEST['mode']=="edit"){
$message=$helper->update($table, $values,array("user_id"=>$user_id,$field_id=>$_REQUEST['contentid']),$required);
$helper->delete("tbl_emergency_services", array("emergencyid"=>$_REQUEST['contentid']));
           foreach($services as $service){			   
                   $helper->tbl_emergency_services($service,$content_id,"tbl_emergency_services");
           }
		   foreach($images as $image){
				 $helper->insert_multiple_images($image,$content_id,"tbl_before_emergency","emergencyid");
			 }
		   foreach($images2 as $image1){
			   $helper->insert_multiple_images($image1,$content_id,"tbl_after_emergency","emergencyid");
		   }
}

}
?>
   
<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>
            
<?php

if($mode=="edit"){
$contentdetails=$helper->select($table,"*",array($field_id=>$_REQUEST['contentid']));

         
}
?>
 <!-- Add / Edit / Delete --> 


     <div class="col-md-10">
          <!-- 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>
           
            <?php
			if($message!=""){
			?>
            <button class="btn btn-block btn-success btn-flat" type="button"><?php echo $message['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>
                <div class="form-group">
                    
                  <label for="inputEmail3" class="col-sm-3 control-label">Client Name</label>

                  <div class="col-sm-8">
                     <?php
				  $clientdetails= $helper->select("tbl_client","clientid,companyname",array("clientid"=>$contentdetails[0]['clientid']));	
				  ?>
                  <select name="clientid" class="form-control" id="clientid">
                   <option value="<?php echo $clientdetails[0]['clientid'];?>"><?php echo $clientdetails[0]['companyname'];?></option>
                  
                  </select>
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Building Name</label>
                  <div class="col-sm-8"> 
                  <?php
                  $building= $helper->select("tbl_branch","branchid,branchname",array("branchid"=>$contentdetails[0]['branchid']));				  				   
				  ?>                  
                  <select name="branchid" class="form-control" id="branchid">
                 <option value="<?php echo $building[0]['branchid'];?>"><?php echo $building[0]['branchname'];?></option>
                  </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-8">
                    <input type="text" required name="title" value="<?php echo $contentdetails[0]['title'];?>" class="form-control" id="title" placeholder="Title" >
                  </div>
                </div>
                <div class="form-group">
                    <input type="hidden" name="branchid" value="<?php echo $_REQUEST['branchid'];?>" />
                  <label for="inputEmail3" class="col-sm-3 control-label">Services</label>

                  <div class="col-sm-8">
                  <?php
                  $services= $helper->select("tbl_services","serviceid,title",array());				   
				  ?>
                  <select name="service[]" class="form-control" id="branchid" multiple>
                  <?php
				  foreach($services as $service){
				  $service_active=$helper->check_emg_types("tbl_emergency_services",'serviceid',$service['serviceid'],$content_id);	  
				  ?>
                  <option <?php if($service_active>0){?> selected="selected" <?php } ?> value="<?php echo $service['serviceid'];?>"><?php echo $service['title'];?></option>
                  <?php
				  }
				  ?>
                  </select>
                   </div>
                </div>
                
                
                <div class="form-group">
                  <label for="inputPassword3" class="col-sm-3 control-label">Work details</label>

                  <div class="col-sm-8">
                    <textarea class="form-control" name="workdetails" id="editor1" placeholder="Description"><?php echo $contentdetails[0]['workdetails'];?></textarea>
                  </div>
                </div>
               <div class="form-group">
                <label for="inputEmail3" class="col-sm-3 control-label">Date</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="startdate" type="text" value="<?php echo $contentdetails[0]['startdate'];?>" 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">Images Before Maintenance</label>

                  <div class="col-sm-8">
                    <input type="file" class="" name="frm_filename_images[]" multiple>
                  </div>
                </div>
                <?php
				 $allimages= $helper->select("tbl_before_emergency","*",array("emergencyid"=>$_REQUEST['contentid']));
				 if(count($allimages)>0){
				?>
               <div class="form-group">
                 <?php
				 foreach($allimages as $image){
				 ?> 
                  <div class="col-sm-2">
                  <a class="fancybox" href="<?php echo $site_url;?>images/gallery/<?php echo $image['image'];?>" data-fancybox-group="gallery"><img class="img-responsive" src="<?php echo $site_url;?>thumb.php?src=<?php echo $site_url;?>images/gallery/<?php echo $image['image'];?>&w=160&h=160"/></a>
                   <a href="<?php echo $site_url;?>admin/<?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> Delete</span></a><br/>
                  </div>
                 <?php
				 }
				 ?>
                </div>
                <?php
				}
				?>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Images After Maintenance</label>

                  <div class="col-sm-8">
                    <input type="file" class="" name="frm_filename_images2[]" multiple>
                  </div>
                </div>
                 <?php
				 $allimages= $helper->select("tbl_after_emergency","*",array("emergencyid"=>$_REQUEST['contentid']));
				 if(count($allimages)>0){
				?>
               <div class="form-group">
                 <?php
				 foreach($allimages as $image){
				 ?> 
                  <div class="col-sm-2">
                  <a class="fancybox" href="<?php echo $site_url;?>images/gallery/<?php echo $image['image'];?>" data-fancybox-group="gallery"><img class="img-responsive" src="<?php echo $site_url;?>thumb.php?src=<?php echo $site_url;?>images/gallery/<?php echo $image['image'];?>&w=160&h=160"/></a>
                   <a href="<?php echo $site_url;?>admin/<?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> Delete</span></a><br/>
                  </div>
                 <?php
				 }
				 }
				 ?>
              </div>
              <div class="form-group">
                <label for="inputEmail3" class="col-sm-3 control-label">Status</label>
				<div class="col-sm-6">
                <div class="input-group date">
                  <select name="status" class="form-control pull-right">
                  <option value="Scheduled" <?php if($contentdetails[0]['status']=="Scheduled"){ echo "selected"; } ?>>Scheduled</option>
         <option value="Under Processing" <?php if($contentdetails[0]['status']=="Under Processing"){ echo "selected"; } ?>>Under Processing</option>
                  <option value="Completed" <?php if($contentdetails[0]['status']=="Completed"){ echo "selected"; } ?>>Completed</option>
                  </select>
                   
                </div>
                </div>
                <!-- /.input group -->
              </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"){
			 $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">
                <div class="searchbox">
                <form action="" method="post">               
                <input type="text" name="search" class="searchtext" placeholder="Search"/>
                <input name="branchid" type="hidden" value="<?php echo $branchid;?>"/>
                <button class="btn btn-danger btn-flat searchbtn" type="submit"><i class="glyphicon  glyphicon-search"></i>&nbsp;Search</button>
                </form>
                </div>                
                   
                </div>
              </div>
            </div>
             <?php			
			if($search!="" || $searchservice!=""){
			$listtypes=$helper->projectsearch($table,"*",array("title"=>$search),$start,$limit,$branchid,$searchservice);
			}
			else {
			$listtypes=$helper->selectlimit($table,"*",array("branchid"=>$branchid),$start,$limit);			
			}
			?>
            <?php
					 
			?>
            <!-- /.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>Emergency</th>
                  <th>Building Name</th>
                   <th>Date</th>
                  
                  <th><?php echo AL_ACTION;?></th>
                  <th>Report</th>
                </tr>
                <?php
				$i=($start*$limit)+1;
				foreach($listtypes as $type){
				?>
                 <?php
				  if($_REQUEST['start']>0){
					  $limitpage=$_REQUEST['start'];
				  }
				  else {
					  $limitpage=1;
				  }
				 
                   $valexist=$helper->datapresent("tbl_schedule","projectid",$type[$field_id]);  
				  ?>
                <tr>
                  <td><?php echo $i;?></td>
                  <td><?php echo $type['title'];?></td>
                  <?php
                  $branch= $helper->select("tbl_branch","*",array("branchid"=>$type['branchid'])); 
		   ?>
                  <?php
                  $client= $helper->select("tbl_client","companyname",array("clientid"=>$clientid)); 
		         ?>
                   
                  <td><?php echo $branch[0]['branchname'];?></td>
                   <?php
                  $membername= $helper->select("tbl_members","firstname,middlename,lastname",array("memberid"=>$type['memberid']));
		   ?>
                  
                  <td><?php echo $type['startdate'];?></td>
                  
                  <td>
                  <a href="<?php echo $site_url;?><?php echo $page;?>?userid=<?php echo $_REQUEST['userid'];?>&mode=edit&clientid=<?php echo $type['clientid'];?>&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){
				  ?>
                   <a href="<?php echo $site_url;?><?php echo $page;?>?mode=delete&contentid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>&branchid=<?php echo $type['branchid'];?>" onclick="return getConfirmation()"><span class="label label-danger"><i class="fa fa-times"></i> <?php echo AL_DELETE;?></span></a>
                   <?php
				  }
				  ?>
                
                   </td>
                   <td>
                       <a target="_self" href="<?php echo $site_url;?>emergency-report.php?emergencyid=<?php echo $type['emergencyid'];?>"><span class="label label-success">Report</span></a>
                   </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>
   <?php
   include('iframe-footer.php');
   ?>