<?php include('header.php');?>
<?php include('sidebar.php');?>
 
<?php
$clientid=$_REQUEST['clientid'];
$maintitle="Buildings";
$page="branches.php";
$table="tbl_branch";
$search=$_REQUEST['search'];
$field_id="branchid";
$mode=$_REQUEST['mode'];
$conmode=$_REQUEST['conmode'];
$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");
$image=$helper->image_upload($_FILES['frm_filename'],"buildingcontract");
if($_REQUEST['mode']=="add"){
$message=$helper->insert($table,$values,$required,$dataexist);
    if($image!=""){
		$value=array("filename"=>$image);
		$bid=array("branchid"=>$message['id']);
		$value=array_merge($value,$bid);
		$helper->insert("tbl_building_contract",$value,array(),array());
	}
      /*  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);
    if($image!=""){
		$value=array("filename"=>$image);
		$bid=array("branchid"=>$_REQUEST['contentid']);
		$value=array_merge($value,$bid);
		$helper->insert("tbl_building_contract",$value,array(),array());
	}
}
}
?>

<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;?>?clientid=<?php echo $clientid;?>"><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"){
	 
		if($conmode=="condelete"){
			 $message=$helper->delete("tbl_building_contract", array("contractid"=>$_REQUEST['conid']));
		}
		 
$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">Client Name</label>

                  <div class="col-sm-6">
                  <?php
                  $clients= $helper->select("tbl_client","*",array("clientid"=>$clientid));
				  ?>
                  <select name="clientid" class="form-control" id="clientid">
                      <option value="<?php echo $clients[0]['clientid'];?>"><?php echo $clients[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-6"> 
                  <input name="branchname" type="text" value="<?php echo $contentdetails[0]['branchname'];?>" class="form-control pull-right" id="branchname">
                 
                </div>
                <!-- /.input group -->
              </div>
                   
               <div class="form-group">
                   
                  <label for="inputEmail3" class="col-sm-3 control-label">Contact Person</label>

                  <div class="col-sm-8">
                    <input type="text" value="<?php echo $contentdetails[0]['firstname'];?>" class="form-control" id="firstname" name="firstname" placeholder="Full Name" required="required">
                  </div>
                </div>
                
                
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_EMAIL;?></label>

                  <div class="col-sm-8">
                    <input type="email" class="form-control" value="<?php echo $contentdetails[0]['email'];?>"  id="email" name="email" placeholder="Email Address" required="required">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_PHONE;?></label>

                  <div class="col-sm-8">
                    <input type="text" class="form-control" value="<?php echo $contentdetails[0]['phone'];?>" id="phone" name="phone" placeholder="Phone" required="required">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_MOBILE;?></label>

                  <div class="col-sm-8">
                    <input type="text" class="form-control" id="mobile" value="<?php echo $contentdetails[0]['mobile'];?>" name="mobile" placeholder="Mobile" required="required">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_ADDRESS;?></label>

                  <div class="col-sm-8">
                    <input type="text" class="form-control" value="<?php echo $contentdetails[0]['address'];?>" id="address" name="address" placeholder="Address">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_CITY;?></label>

                  <div class="col-sm-8">
                    <input type="text" name="city" class="form-control" id="city" value="<?php echo $contentdetails[0]['city'];?>"  placeholder="City">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_COUNTRY;?></label>

                  <div class="col-sm-8">
                  <?php
                  $countries= $helper->select("tbl_country","countryid,country",array("countryid"=>234),array());					   
				  ?>
                  <select name="country" class="form-control" id="country">
                  <?php
				  foreach($countries as $country){
				  ?>
                  <option <?php if($country['countryid']==$contentdetails[0]['country']){ echo "selected";}?>  value="<?php echo $country['countryid'];?>"><?php echo $country['country'];?></option>
                  <?php
				  }
				  ?>
                  </select>
                   </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_ZIPCODE;?></label>

                  <div class="col-sm-8">
                    <input type="text" class="form-control" id="zipcode" value="<?php echo $contentdetails[0]['zipcode'];?>" name="zipcode" placeholder="Zipcode">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Upload Contract</label>
                  <div class="col-sm-8">
                    <input type="file" class="" name="frm_filename">
                  </div>
                </div>
             <?php
			 $contracts= $helper->select("tbl_building_contract","*",array("branchid"=>$_REQUEST['contentid']),array("contractid"=>"desc"));
            if(count($contracts>0)){
             ?>
            
                <div class="form-group">
                <div class="col-sm-3"></div>
                <div class="col-sm-8">
                <?php
				 foreach($contracts as $contract) {
			    ?>
                   <div class="col-sm-3">
                      <a target="_self" target="_blank" href="<?php echo $site_url;?>images/buildingcontract/download.php?file=<?php echo $contract['filename'];?>"><img src="images/download.png"/></a><br/>
                      <a href="<?php echo $site_url;?><?php echo $page;?>?mode=edit&conmode=condelete&conid=<?php echo $contract['contractid'];?>&clientid=<?php echo $_REQUEST['clientid'];?>&contentid=<?php echo $_REQUEST['contentid'];?>&start=<?php echo $_REQUEST['start'];?>">Delete</a><br/>
                      Uploaded on: <?php 
					  echo date('d-m-Y', strtotime($contract['str_date']));
					  ?>
                  </div>
                  <?php
				 }
				 ?>
                 </div>
                </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-right"/>
                 
              </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>
                 <?php
                  $client= $helper->select("tbl_client","companyname",array("clientid"=>$clientid)); 
		         ?>
              <h3 class="box-title">Buildings of <?php echo $client[0]['companyname'];?> </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="clientid" type="hidden" value="<?php echo $clientid;?>"/>
                <button class="btn btn-danger btn-flat searchbtn" type="submit"><i class="glyphicon  glyphicon-search"></i>&nbsp;Search</button>
                </form>
                </div>
                <a href="<?php echo $page;?>?mode=add&clientid=<?php echo $clientid;?>"><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			
			if($search!=""){
			$listtypes=$helper->branchsearch($table,"*",array("branchname"=>$search),$start,$limit,$clientid);
			}
			else {
			$listtypes=$helper->selectlimit($table,"*",array("clientid"=>$clientid),$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>Building</th>
                  <th>Client</th>
                   <th>Email Address</th>
                   <th>Phone</th>
                  <th><?php echo AL_STATUS;?></th>
                  <th>Emergency Calls</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;
				  }
				 
                  $valexist=$helper->datapresent("tbl_projects","branchid",$type[$field_id]);  
				  ?>
                <tr>
                  <td><?php echo $i;?></td>
                  
                    <td><?php echo $type['branchname'];?></td>
                  <td><?php echo $client[0]['companyname'];?></td>
                   <?php
                  $membername= $helper->select("tbl_members","firstname,middlename,lastname",array("memberid"=>$type['memberid']));
		   ?>
                 
                  <td><?php echo $type['email'];?></td>
                   <td><?php echo $type['phone'];?></td>
                  <td>	
                    <a class="fancybox fancybox.iframe" href="<?php echo $site_url;?>add-projects.php?mode=add&branchid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>"><span class="label label-success">Add Task</span></a>  
                    <a class="fancybox fancybox.iframe" href="<?php echo $site_url;?>add-projects.php?branchid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>"><span class="label label-success">All Tasks</span></a>  
                 
                  </td>
                  <td><a class="fancybox fancybox.iframe" href="<?php echo $site_url;?>emergency.php?branchid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>"><span class="label label-success">Emergency Calls</span></a></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;?>" onclick="return getConfirmation()"><span class="label label-danger"><i class="fa fa-times"></i> <?php echo AL_DELETE;?></span></a>
                   <?php
				  }
				  ?>
               <a class="fancybox fancybox.iframe" href="<?php echo $site_url;?>view-branch.php?mode=add&branchid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>"><img src="images/view.png"/></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>
    <!-- /.content -->
  </div>
<?php include('footer.php');  ?>