<?php include('header.php');?>
<?php include('sidebar.php');?>

<?php
$maintitle="Company";
$page="companies.php";
$table="tbl_company";
$field_id="companyid";
$mode=$_REQUEST['mode'];
$search=$_REQUEST['search'];
$content_id=$_REQUEST['content_id'];
if(isset($_REQUEST['start'])){
$start=$_REQUEST['start']-1;
}
else {
$start=0;
}
?>
<?php
 
if(isset($_REQUEST['submit'])=="submit"){ 
$dataexist=array();
$required = array();
$values = $_POST; 
unset($values['submit']);
unset($values['mode']); 
unset($values['contractingtype']);
unset($values['name']);
unset($values['position']);
unset($values['functionarea']);
unset($values['contactemail']);
unset($values['contactphone']);
$userid=array("user_id"=>$user_id);
$values=array_merge($values,$userid);
$image=$helper->image_upload($_FILES['frm_filename'],"company");
$photo=array("image"=>$image);
$contracts=$_POST['contractingtype'];

if($photo!=""){
$values=array_merge($values,$photo);
}
$name=$_REQUEST['name'];
$position=$_REQUEST['position'];
$functionarea=$_REQUEST['functionarea'];
$contactemail=$_REQUEST['contactemail'];
$contactphone=$_REQUEST['contactphone'];
if($_REQUEST['mode']=="add"){
$message=$helper->insert($table,$values,$required,$dataexist);
            foreach($contracts as $contract){			   
                   $helper->tbl_company_type($contract,$message['id'],"tbl_company_type");
           }
$helper->companygroupcontact($name,$position,$functionarea,$contactemail,$contactphone,$message['id'],"tbl_company_contacts","add");
}
if($_REQUEST['mode']=="edit"){
$message=$helper->update($table, $values,array("user_id"=>$user_id,$field_id=>$_REQUEST['contentid']),$required);
           if(count($contracts)>0){
            $helper->delete("tbl_company_type",array($field_id=>$_REQUEST['contentid']));
           foreach($contracts as $contract){			   
                   $helper->tbl_company_type($contract,$_REQUEST['contentid'],"tbl_company_type");
           }
		   }
$helper->companygroupcontact($name,$position,$functionarea,$contactemail,$contactphone,$_REQUEST['contentid'],"tbl_company_contacts","edit");
}

}
?>

<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("user_id"=>$user_id,$field_id=>$_REQUEST['contentid']));
}
?>

     <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>
            
            <?php
			if($message!=""){
			?>
            <button class="btn btn-block btn-success btn-flat" type="button"><?php echo $message['message'];?></button>
			<?php	
			}
			?>
            <!-- /.box-header -->
            <!-- form start -->
            <div class="nav-tabs-custom">
             <ul class="nav nav-tabs">
              <li class="active"><a href="#activity" data-toggle="tab">Group Details</a></li>
              <li><a href="#timeline" data-toggle="tab">Contacts</a></li>
            </ul>
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
            <div class="tab-content">
              <div class="active tab-pane" id="activity">
            <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">Contracting Type</label>

                  <div class="col-sm-8">
                  <?php
                  $contypes= $helper->select("tbl_contracting_types","contractingtypeid,title",array());				   
				  ?>
                  <select name="contractingtype[]" class="form-control" id="typeid" multiple>
                  <?php
				  foreach($contypes as $contype){
				  $service_active=$helper->check_types("tbl_company_type",'typeid',$contype['contractingtypeid'],$_REQUEST['contentid']);	  
				  ?>
                  <option <?php if($service_active>0){?> selected="selected" <?php } ?> value="<?php echo $contype['contractingtypeid'];?>">
				  <?php echo $contype['title'];?></option>
                  <?php
				  }
				  ?>
                  </select>
                   </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Company Name</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="companyname" value="<?php echo $contentdetails[0]['companyname'];?>" class="form-control" id="companyname" placeholder="Company Name" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Address</label>
                  <div class="col-sm-8">
                  <textarea class="form-control" name="address" id="address" placeholder="Address"><?php echo $contentdetails[0]['address'];?></textarea>
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">City</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="city" value="<?php echo $contentdetails[0]['city'];?>" class="form-control" id="city" placeholder="City" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">State</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="state" value="<?php echo $contentdetails[0]['state'];?>" class="form-control" id="state" placeholder="State" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Country</label>
                  <div class="col-sm-8">
                    <?php
                  $countries= $helper->select("tbl_country","countryid,country",array(),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">P.O Box</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="pobox" value="<?php echo $contentdetails[0]['pobox'];?>" class="form-control" id="pobox" placeholder="Pobox" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Email Address</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="email" value="<?php echo $contentdetails[0]['email'];?>" class="form-control" id="email" placeholder="Email Address" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Phone</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="phone" value="<?php echo $contentdetails[0]['phone'];?>" class="form-control" id="phone" placeholder="Phone" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Mobile</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="mobile" value="<?php echo $contentdetails[0]['mobile'];?>" class="form-control" id="mobile" placeholder="Mobile" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Fax</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="fax" value="<?php echo $contentdetails[0]['fax'];?>" class="form-control" id="fax" placeholder="FAX" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Website</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="website" value="<?php echo $contentdetails[0]['website'];?>" class="form-control" id="website" placeholder="Website" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Facebook</label>
                  <div class="col-sm-8">
                    <input type="text"  name="facebook" value="<?php echo $contentdetails[0]['facebook'];?>" class="form-control" id="facebook" placeholder="facebook" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Linkedin</label>
                  <div class="col-sm-8">
                    <input type="text"  name="linkedin" value="<?php echo $contentdetails[0]['linkedin'];?>" class="form-control" id="linkedin" placeholder="Linkedin" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Twitter</label>
                  <div class="col-sm-8">
                    <input type="text"  name="twitter" value="<?php echo $contentdetails[0]['twitter'];?>" class="form-control" id="twitter" placeholder="Twitter" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Instagram</label>
                  <div class="col-sm-8">
                    <input type="text"  name="instagram" value="<?php echo $contentdetails[0]['instagram'];?>" class="form-control" id="instagram" placeholder="Instagram" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Pinterest</label>
                  <div class="col-sm-8">
                    <input type="text" required="required" name="pinterest" value="<?php echo $contentdetails[0]['pinterest'];?>" class="form-control" id="twitter" placeholder="Pinterest" >
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputPassword3" class="col-sm-3 control-label"><?php echo AL_DESCRIPTION;?></label>

                  <div class="col-sm-8">
                    <textarea class="form-control" name="description" id="description" placeholder="Description"><?php echo $contentdetails[0]['description'];?></textarea>
                  </div>
                </div>
                <div class="form-group">
				 <?php
			if($contentdetails[0]['image']!=""){
			?>
              <img class="profile-user-img img-responsive img-circle" src="<?php echo $site_url;?>thumb.php?src=<?php echo $site_url;?>images/company/<?php echo $contentdetails[0]['image'];?>&w=160&h=160" alt="<?php echo $contentdetails[0]['firstname'];?>">
              <?php
			} else {
			?>
             <img class="profile-user-img img-responsive img-circle" src="<?php echo $site_url;?>thumb.php?src=<?php echo $site_url;?>images/no-image.jpg&w=160&h=160" alt="<?php echo $contentdetails[0]['firstname'];?>">
            <?php
			}
			?>
            </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">
                  </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>
              </div>
              <div class="tab-pane" id="timeline">
                                <div class="box-body">
                                     <div class="form-group">                  
                                          <div class="col-sm-12">
                                               <div class="table-responsive"> 
                                                <table class="table table-bordered" id="dynamic_fields"> 
                                                 <tr>  
                                                         <td><b>Name</b></td>    
                                                         <td><b>Position</b></td> 
                                                         <td><b>Function Areas</b></td>
                                                         <td><b>Email ID</b></td> 
                                                         <td><b>Phone</b></td>
                                                 </tr>
                                                 <?php
							 $k=300;
							$formfields = $helper->select("tbl_company_contacts","*",array("companyid"=>$contentdetails[0]['companyid']));
							 
							if(count($formfields)>0){
								foreach($formfields as $formfield){
							?>
                            <tr id="rowa<?php echo $k;?>">   
                           <td><input  type="text" name="name[]" value="<?php echo $formfield['name'];?>" placeholder="Student Name" class="form-control name_list" /></td>    
                           <td><input id="<?php echo $k;?>" type="text" name="position[]" value="<?php echo $formfield['position'];?>" placeholder="Position" class="form-control name_list emiratesid" /></td> 
                           <td><input type="text" name="functionarea[]" value="<?php echo $formfield['functionarea'];?>" placeholder="Function Afrea" class="form-control name_list" /></td>
                           <td><input type="text" name="contactemail[]" value="<?php echo $formfield['contactemail'];?>" placeholder="Email Id" class="form-control name_list" /></td> 
                           <td><input type="text" name="contactphone[]" value="<?php echo $formfield['contactphone'];?>" placeholder="Phone" class="form-control name_list" /></td>
                           <td><button type="button" name="remove" id="<?php echo $k;?>" class="btn btn-danger btn_removes">X</button></td>  
                                    </tr>
                            <?php
							 $k=$k+1;
							}
							  }
							  ?>
                                                  <tr>  
                                                 <td><input  type="text" name="name[]" placeholder="Name" class="form-control name_list" /></td>    
                                                 <td><input type="text" name="position[]" placeholder="Position" class="form-control name_list emiratesid" id="0" /></td> 
                                                 <td><input type="text" name="functionarea[]" placeholder="Function Area" class="form-control name_list" /></td>
                                                 <td><input type="text" name="contactemail[]" placeholder="Email Id" class="form-control name_list" /></td> 
                                                 <td><input type="text" name="contactphone[]" placeholder="Phone" class="form-control name_list" /></td>
                                                 <td><button  type="button" name="add" id="adds" class="btn btn-success">Add More</button> 
                                                 </td>  
                                                 </tr>  
                                                 </table> 
                                               </div>                                               
                                           </div>
                                       </div>
                                 </div>
                      </div>

              <!-- /.box-footer -->
            </form>
            </div>
          </div>
         
        </div>
        </div>
        <?php
	    }
		else {
	    ?>
        <?php
		if($mode=="delete"){
			 $message=$helper->delete($table, array("user_id"=>$user_id,$field_id=>$_REQUEST['contentid']));
		}
		?>
          <?php
		if($mode=="approve"){
			$helper->update($table, array("status"=>1),array("user_id"=>$user_id,$field_id=>$_REQUEST['contentid']), array());
		}
		?>
         <?php
		if($mode=="pending"){
			$helper->update($table, array("status"=>2),array("user_id"=>$user_id,$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"/>
                <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"><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->selectsearch($table,"*",array("companyname"=>$search),$start,$limit);
			} else {
			$listtypes=$helper->selectlimit($table,"*",array(),$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>Company Name</th>
                  <th>Email</th>
                  <th>Contact Details</th>
                  <th>Image</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;
				  }
				 
                   //$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><?php echo $type['companyname'];?></td>
                  <td><?php echo $type['email'];?></td>
                  <td><?php if($type['phone']!=""){?>Tel: <?php echo $type['phone'];?><br/><?php } ?>
                     <?php if($type['mobile']!=""){?>
                      Mob: <?php echo $type['mobile'];?><br/>
                      <?php } ?>
                       <?php if($type['fax']!=""){?>
                      Fax: <?php echo $type['fax'];?>
                      <?php } ?>
                      </td>
                  <td>
                   <?php
				  if($type['image']!=""){
				?>
                <img src="<?php echo $site_url;?>thumb.php?src=<?php echo $site_url;?>images/company/<?php echo $type['image'];?>&h=50&w=50"/>
                <?php
				  }
				  ?>
                  </td>
                  <td>
                  <?php
				  
				  if($type['status']==1){
				  ?>
<a href="<?php echo $admin_url;?><?php echo $page;?>?mode=pending&contentid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>"><span class="label label-success"><?php echo AL_APPROVED;?></span></a>
                  <?php
				  }
				  else {
				  ?>
				  <a href="<?php echo $admin_url;?><?php echo $page;?>?mode=approve&contentid=<?php echo $type[$field_id];?>&start=<?php echo $limitpage;?>"><span class="label label-danger"><?php echo AL_PENDING;?></span></a>
                  <?php
				  }
				  ?>
                  </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
				$i=$i+1;
				}
				?>
                
                
              </table>
              <?php
			if($search!=""){
				$counter_list= $helper->countofsearch($table,array("companyname"=>$search),$limit,array("status"=>"ASC"));
			 
			} else {
			 $counter_list= $helper->countofdata($table,array(),$limit,array("status"=>"ASC"));
			}
			?>
              <?php
			  
			   include('pagination/pagination.php');
			  ?>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
      </div>
      <?php
		}
		?>

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