<?php include('header.php');?>
<?php include('sidebar.php');?>
<?php 
include('includes/class.usertype.php');
$usertype = new Usertype();
 
if(!isset($user_id)){
	header('Location: dashboard.php');
}
?>
<?php
$maintitle="Staffs";
$page="staff.php";
$table="tbl_staff";
$field_id="staffid";
$mode=$_REQUEST['mode'];
$content_id=$_REQUEST['content_id'];
if(isset($_REQUEST['start'])){
$start=$_REQUEST['start']-1;
}
else {
$start=0;
}
?>
<?php
 
if(isset($_REQUEST['submit'])=="submit"){ 
$dataexist=array("email");
$required = array("firstname","lastname","email","phone","mobile");
$values = $_POST;
unset($values['countrytomanage']);
unset($values['submit']);
unset($values['mode']);
unset($values['password']);
unset($values['conpassword']);
if(isset($_POST['password'])){
	if($_POST['password']!=$_POST['conpassword']){
		$passworderror="Passowrd entered is not same";
		
	}
$password=array("password"=>md5($_POST['password']));
$values=array_merge($values,$password);
}
/*$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'],"users");
$photo=array("image"=>$image);
if($photo!=""){
$values=array_merge($values,$photo);
}

if($_REQUEST['mode']=="add"){
$messagein=$helper->insert($table,$values,$required,$dataexist);
            $hotelcountries=$_POST['countrytomanage'];
            $hotelcountry = implode(',',$hotelcountries);
            $hotelallowedvalues=array("allowedcountries"=> $hotelcountry,"staffid"=>$messagein['id']);
            $helper->insert("tbl_countriestomanage",$hotelallowedvalues,array(),array());
}
 if($_REQUEST['mode']=="edit"){
     $message=$helper->update($table, $values,array($field_id=>$_REQUEST['contentid']),$required,$dataexist);
     $hotelcountries=$_POST['countrytomanage'];
            $hotelcountry = implode(',',$hotelcountries);
            $hotelallowedvalues=array("allowedcountries"=> $hotelcountry);
             
                $helper->delete("tbl_countriestomanage",array("staffid"=>$_REQUEST['contentid']));
                $hotelallowedvalues=array("allowedcountries"=> $hotelcountry,"staffid"=>$_REQUEST['contentid']);
                $helper->insert("tbl_countriestomanage",$hotelallowedvalues,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;?>"><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']));
 
}
?>

     <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($messagein!=""){
			?>
            <button class="btn btn-block btn-success btn-flat" type="button"><?php echo $messagei['message'];?></button>
			<?php	
			}
			?>
            <?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">
              <div class="box-body">
              <input type="hidden" name="mode" value="<?php echo $_REQUEST['mode'];?>"/>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">User Roles</label>

                  <div class="col-sm-4">                   
                      <select name="role" class="form-control" id="userrole">
                          <option value=""><?php echo AL_CHOOSEONE;?></option>
                           <?php
				foreach($usertype->list_product_categories_dropdown() as $cat){							  
			    ?>
                               <option <?php if($cat['typeid']==$contentdetails[0]['role']){ echo "selected"; } ?> value="<?php  echo $cat['typeid'];?>"><?php  echo $cat['title'];?></option>                              
                           <?php
			    }
		           ?>                         
                          
                        </select>
                    
                  </div>
                </div>
              <?php
              $curallowededcountry=$helper->select("tbl_countriestomanage","*",array("staffid"=>$_REQUEST['contentid']),array());
               $hotelcountries=explode(",",$curallowededcountry[0]['allowedcountries']);
              $allowedcountries=$helper->allowedcountries();
              ?>
              <div class="form-group" id="managecountry">
                <?php
                if($contentdetails[0]['role']==2){
                 ?>
                  <label for="inputEmail3" class="col-sm-3 control-label">Managing Country</label>
                  <div class="col-sm-8"> 
                      <select name="countrytomanage[]" multiple="multiple" class="form-control">
                        <?php
                        foreach($allowedcountries as $allowedcountry){
                         ?>
                          <option <?php if (in_array($allowedcountry['countryid'], $hotelcountries)){ echo "selected";}?> value="<?php echo $allowedcountry['countryid'];?>"><?php echo $allowedcountry['country'];?></option>
                          <?php
                        }
                        ?>
                         
                      </select>
                  </div>
                  <?php
                }
                ?>
              </div>
                <div class="form-group">
                     
                  <label for="inputEmail3" class="col-sm-3 control-label"><?php echo AL_FIRSTNAME;?></label>

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

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

                  <div class="col-sm-8">
                    <input type="text" class="form-control" id="lastname" value="<?php echo $contentdetails[0]['lastname'];?>" name="lastname" placeholder="Last 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());				   
				  ?>
                  <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">
				 <?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/users/<?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 class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">New Password</label>

                  <div class="col-sm-8">
                    <input <?php if($mode=="add"){?> required="required" <?php } ?> type="password" name="password" class="form-control" id="password" placeholder="Password">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Confirm Password</label>

                  <div class="col-sm-8">
                    <input <?php if($mode=="add"){?> required="required" <?php } ?> type="password" name="conpassword" class="form-control" id="conpassword" placeholder="Confirm Password">
                  </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>
              <!-- /.box-footer -->
            </form>
          </div>
         
        </div>
        </div>
        <?php
	    }
		else {
	    ?>
         <?php
		if(isset($user_id)){
		if($mode=="delete"){
			 $message=$helper->delete($table, array($field_id=>$_REQUEST['contentid']));
		}
		 
		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"><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(),$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;?></button>
			<?php	
			}
			?>
              <table class="table table-hover">
                <tr>
                  <th><?php echo AL_SERIALNO;?></th>
                  <th><?php echo AL_NAME;?></th>
                  <th><?php echo AL_EMAIL;?></th>
                  <th>Role</th>
                  <th><?php echo AL_PICTURE;?></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['firstname']." ".$type['middlename']." ".$type['laststname'];?></td>
                  <td>
                  <?php echo $type['email'];?>
                  </td>
                   <td>
                  <?php
                  $role= $helper->select("tbl_user_type","*",array("typeid"=>$type['role']));
                  echo $role[0]['title'];
		  ?>
                  </td>
                  
                  <td>
                  <?php
				  if($type['image']!=""){
				?>
                <img src="<?php echo $site_url;?>thumb.php?src=<?php echo $site_url;?>images/users/<?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>
                   <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>
                   </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');  ?>