<?php include("_config/dbconfig.php");
include("_includes/head.php");
$categories = $obj_category_class->getAllCategories();       
$countries_list  = $obj_jobs_class->getCountries();
//
$event_name     = "";
$organizer      = "";
$category       = "";
$event_date     = "";
$venue          = "";
$city           = "";
$country        = "";
$document       = '';
$end_date       = ""; 

if(isset($_SESSION['user_session'])){
        $agent_id = $_SESSION['user_session'];
    } 

$allAgents = $obj_jobs_class->getAgents($agent_id);
  

if(count($_POST)>0 && isset($_POST['add_form'])){
           
            /*echo "<pre>";
            print_r($_FILES);
            print_r($_POST);
            echo '</pre>'; */
            
            $event_name     = $_POST['event_name'];
            $organizer      = $_POST['organizer'];
            $category       = $_POST['category'];
            $event_date     = $_POST['event_date'];
            $venue          = $_POST['venue'];
            $city           = $_POST['city'];
            $country        = $_POST['country'];
            $filename       = '';
            $end_date       = $_POST['end_date']; 
            $agents_assign  = implode(",",$_POST['agents']);
            
            
      
        if(isset($_FILES['doc_file']) && !empty($_FILES['doc_file']['name'])){
                // settings
                $thumb_img = $_FILES['doc_file'];
                $max_file_size = 1024*3000; // 900kb 3mb
                $valid_exts = array('pdf');
                // thumbnail sizes
                $sizes = array(900 => 400);
                // Path
                $path = 'uploads/documents/';
                    // get file extension
                    $ext = strtolower(pathinfo($thumb_img['name'], PATHINFO_EXTENSION));

                    if (in_array($ext, $valid_exts)) {
                            /* resize image */
                            $uniqId  = "doc_".uniqid(5).".".$ext;
                            $fPath 	 = 'uploads/documents/'.$uniqId;
                            $filename = $uniqId;
                            $thumb_name = $uniqId;
                            //$res = $image->imageupload($thumb_img['tmp_name'], $uniqId, $path);
                            $aa = "uploads/documents/".$uniqId;
                            move_uploaded_file($thumb_img['tmp_name'], $aa);
                    } else {
                      $msg = 'Unsupported file';
                    }
                }
              
        $obj_events_class->insert($event_name, $organizer, $event_date, $end_date, $category, $venue, $city, $country, $filename, $agent_id,  $agent_id.",".$agents_assign);
        echo  "<script> window.location= 'events.php'</script>";
        die();
}
 
if(isset($_POST['duplicate_now'])){
     
            $event_name     = $_POST['event_name_new'];
            $organizer      = $_POST['organizer'];
            $category       = $_POST['category'];
            $event_date     = $_POST['event_date'];
            $venue          = $_POST['venue'];
            $city           = $_POST['city'];
            $country        = $_POST['country'];
            $market_status  = 1;
            $filename       = '';
            $end_date       = $_POST['end_date']; 
            
            $event_id  = base64_decode($_POST['event_id']);
            
            if(isset($_POST['agents'])){
                $agents_assign  = implode(",",$_POST['agents']);
            }else $agents_assign  = '';
      
            $AssignClients = $obj_events_class->getEventAssignClient($event_id, $agent_id);
            
        if(isset($_FILES['doc_file']) && !empty($_FILES['doc_file']['name'])){
                // settings
                $thumb_img = $_FILES['doc_file'];
                $max_file_size = 1024*3000; // 900kb 3mb
                $valid_exts = array('pdf');
                // thumbnail sizes
                $sizes = array(900 => 400);
                // Path
                $path = 'uploads/documents/';
                    // get file extension
                    $ext = strtolower(pathinfo($thumb_img['name'], PATHINFO_EXTENSION));

                    if (in_array($ext, $valid_exts)) {
                            /* resize image */
                            $uniqId  = "doc_".uniqid(5).".".$ext;
                            $fPath 	 = 'uploads/documents/'.$uniqId;
                            $filename = $uniqId;
                            $thumb_name = $uniqId;
                            //$res = $image->imageupload($thumb_img['tmp_name'], $uniqId, $path);
                            $aa = "uploads/documents/".$uniqId;
                            move_uploaded_file($thumb_img['tmp_name'], $aa);
                    } else {
                      $msg = 'Unsupported file';
                    }
                }
              
        $eve=$obj_events_class->insert($event_name, $organizer, $event_date, $end_date, $category, $venue, $city, $country, $filename, $agent_id,  $agent_id.",".$agents_assign, $market_status);
       
        foreach($AssignClients as $AC){
                   $obj_events_class->AddAssignEventClient($eve, $AC['client_id'], $agent_id);
                  
            }
        
        echo  "<script> window.location= 'events.php'</script>";
        die();
    
}

if(count($_POST)>0 && isset($_POST['update_form'])){
        
            $event_id       = $_POST['event_id'];
            $event_name     = $_POST['event_name'];
            $organizer      = $_POST['organizer'];
            $category       = $_POST['category'];
            $event_date     = $_POST['event_date'];
            $venue          = $_POST['venue'];
            $city           = $_POST['city'];
            $country        = $_POST['country'];
            $filename       = '';
            $end_date       = $_POST['end_date']; 
            if(isset($_POST['agents']) && count($_POST['agents'])>0){
                $agents_assign  = implode(",",$_POST['agents']);
            }else {$agents_assign  = ""; }
            
            
            
        /*if(isset($_FILES['doc_file']) && !empty($_FILES['doc_file']['name'])){
                // settings
                $thumb_img = $_FILES['doc_file'];
                $max_file_size = 1024*3000; // 900kb 3mb
                $valid_exts = array('pdf');
                // thumbnail sizes
                $sizes = array(900 => 400);
                // Path
                $path = 'uploads/documents/';
                    // get file extension
                    $ext = strtolower(pathinfo($thumb_img['name'], PATHINFO_EXTENSION));

                    if (in_array($ext, $valid_exts)) {
                             
                            $uniqId  = "doc_".uniqid(5).".".$ext;
                            $fPath 	 = 'uploads/documents/'.$uniqId;
                            $filename = $uniqId;
                            $thumb_name = $uniqId;
                            //$res = $image->imageupload($thumb_img['tmp_name'], $uniqId, $path);
                            $aa = "uploads/documents/".$uniqId;
                            move_uploaded_file($thumb_img['tmp_name'], $aa);
                    } else {
                      $msg = 'Unsupported file';
                    }
             }
            else {
                        $filename = $_POST['old_img'];
                }*/
        
        $obj_events_class->update($event_id, $event_name, $organizer, $event_date,$end_date,  $category, $venue, $city, $country, $filename, $agent_id, $agent_id.",".$agents_assign);
       
        echo "<script> window.location= 'events.php'</script>";
         die();
} 
 
if(isset($_GET['edit']) && !empty($_GET['edit'])){
    
        $id_        = base64_decode($_GET['edit']);
        $event      = $obj_events_class->getEvent($id_);
          
        $event_name     = $event['event_name'];
        $organizer      = $event['organizer'];
        $category       = $event['categories'];
        $event_date     = $event['start_date'];
        $venue          = $event['venue'];
        $city           = $event['city'];
        $country        = $event['country'];
        $document       = $event['document']; 
        $end_date       = $event['end_date']; 
        $asg_agents     = explode(",",$event['assign_agent']);
        

        $page_title = "Edit Event";
        echo '<title>Edit Event</title>';

   }

else if(isset($_GET['view']) && !empty($_GET['view'])){
        $id_        = base64_decode($_GET['view']);
        $event      = $obj_events_class->getEvent($id_);
        
        $event_name     = $event['event_name'];
        $organizer      = $event['organizer'];
        $category       = $event['categories'];
        $event_date     = $event['start_date'];
        $venue          = $event['venue'];
        $city           = $event['city'];
        $country        = $event['country'];
        $document       = $event['document']; 
        $market_status  = $event['market_status']; 
        $asg_agents     = explode(",",$event['assign_agent']);
        
?>    
<?php             $page_title = "View Exhibition";
        echo '<title>View Exhibition</title>';
   }       

else { 
       $page_title = "Add Exhibition";
       echo '<title>Add Exhibition </title>'; 
   }
?>

<?php include("_includes/header.php"); ?>

<div class="col-sm-12 right_form">
        <div class="title">
            <div class="row">
                <div class="col-md-8 col-sm-12">
                    <h1><?php echo $page_title; ?></h1>
                </div> 
                
            </div>
        </div>
    
      <ol class="breadcrumb white">
	          <li><a href="dashboard.php">Dashboard</a></li>
                  <li><a href="events.php">Exhibitions</a></li>
          	  <li class="active">Add Exhibition</li>
          </ol>
    
</div>


<div class="col-sm-12 form_inner">
    
    <form class="" id="agent_form" method="post" action="" enctype="multipart/form-data">
    <div class="col-sm-8 form-box" id="agent_form2"  >    
                <div class="row">
                        <div class="col-md-6 col-sm-6 form-group">
                                <label for="FirstName">Event Name</label>
                                <input type="text" class="form-control required" name="event_name" value="<?= $event_name?>" >
                        </div>
                        <div class="col-md-6 col-sm-6 form-group">
                                <label for="LastName">Organizer</label>
                                <input type="text" class="form-control agent_form required" name="organizer" value="<?= $organizer?>" >
                        </div>
                    <div class="clearfix"></div>
                    
                        <div class="col-md-6 col-sm-6 form-group">
                                <label for="LastName">Start Date</label>
                                <input type="text" id="start_date" class="form-control required   calendar_icon"   name="event_date" value="<?= $event_date?>" >
                        </div> 
                     
                        <div class="col-md-6 col-sm-6 form-group">
                                <label for="LastName">End Date</label>
                                <input type="text" id="end_date" class="form-control required   calendar_icon"   name="end_date" value="<?= $end_date?>" >
                        </div> 
                    
                    <div class="clearfix"></div>
                      
                        <div class="col-md-6 col-sm-6 form-group">
                                <label for="Website">Category/Industry</label> 
                               <select class="form-control required chosen-select" name="category" >
                                   <option value="">--- Select ---</option>        
                            <?php foreach($categories as $cats){  ?>
                                   <option <?php if($cats['category'] == $category) echo 'selected="selected"'; ?>><?= $cats['category']  ?></option>
                            <?php } ?>
                                   
                                </select>
                                
                              
                                 
                        </div> 
                    
                        <div class="col-md-6 col-sm-6 form-group">
                                <label for="Venue">Venue</label>
                                <textarea class="form-control required" name="venue"><?= $venue?></textarea>
                        </div>
                        <div class="clearfix"></div>
                        
                        <div class="col-md-6 col-sm-6 form-group">
                               <label for="City">City</label>
                               <input type="text" class="form-control agent_form required" name="city" value="<?= $city?>" >
                       </div>
                  
                        
                       <div class="col-md-6 col-sm-6 form-group">
                               <label for="ContactNum">Country</label> <?= $country ?>
                               <select class="form-control required chosen-select" name="country" >
                                   <option value="">--- Select ---</option>        
                            <?php foreach($countries_list as $contry){  ?>
                                   <option <?php if($contry['name'] == $country) echo 'selected="selected"'; ?>><?= $contry['name']  ?></option>
                            <?php } ?>
                                   
                                </select>
                       </div>
                        
                    <div class="clearfix"></div>
                    
                    <div class="col-md-6 col-sm-6 form-group">
                                <label for="Website">Assign Agents</label>  
                                <select class="chosen-select required"   multiple=""  name="agents[]" >
                            <?php  
                            if(count($allAgents)>0)
                            foreach($allAgents as $ags){
                                $sel = ""; 
                                   // $sel_agents = implode(",", $ags['assign_agent']);
                                    if(in_array($ags['agent_id'], $asg_agents)){
                                        $sel = "selected='selected'";
                                    }
                            ?>
                                        <option <?php echo $sel; ?> value="<?php echo $ags['agent_id'] ?>"><?php echo $ags['name'] ?></option>
                            <?php } ?>
                        </select>
                                 
                        </div> 
                    
                    

                   <div class="col-md-6 col-sm-12 form-group">
                       
                        
                   </div>
                                         
    <?php if(isset($_GET['edit']) && !empty($_GET['edit'])){ ?>
                    <div class="col-sm-12 form-group">
                        <input type="hidden" name="event_id" value="<?= $id_ ?>" >
                        <button type="submit" class="btn btn-default" name="update_form"> Submit </button>
                    </div>
    <?php } else if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
                                            
            <div class="col-sm-12 form-group"><a href="events.php">
                <button type="button" class="btn btn-default" name="add_form"> Back </button>
                </a>
            </div>
    <?php  } else { ?>
            <div class="col-sm-12 form-group">
                <button type="submit" class="btn btn-default" name="add_form"> Submit </button>
            </div>
    <?php } ?>
                                        
		</div>
    </div>
<?php  if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
    <div class="col-sm-4 form-box">
        <h4> Duplicate  Exhibition</h4>
        <div class="row">
                    <div class="col-md-12 col-sm-12 form-group">
                            <label for="FirstName">Event New Name</label>
                            <input type="text" class="form-control required" name="event_name_new" value="" required="required" >
                            <input type="hidden" name="event_id" value="<?= $_GET['view']?>" >
                            <input type="hidden" name="market_status" value="<?= $market_status ?>" >
                    </div>
                    <div class="col-sm-12 form-group">
                        <button type="submit" class="btn btn-default" name="duplicate_now"> Duplicate Now </button>
                    </div>
        </div>
    </div>
<?php } ?>
    </form>
</div>

<?php include("_includes/footer.php"); ?>

<script>
function thumbImg(){
                        var imgg = $("#profile_img").val();
                        arr = imgg.split(".");
                        imgg = arr[1].toLowerCase();

                        if(imgg == "pdf"){
                            $("#old_img").fadeOut(300);                    
                        }
                        else {
                            alert('Please select the valid file or format!');
                            $("#thumb_img").focus();
                            $("#thumb_img").val('');
                        }
            	}	
        
</script>

<?php if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
<script>
$(document).ready(function(){ 
        $('#agent_form2 .form-control').attr('readonly', 'readonly'); 
   });               

</script>
 
<?php }else {?>
<script>
            $(document).ready(function(){
                $("#agent_form").validate();
            });
</script>

    <?php } ?>
<script>
$(document).ready(function(){
    $("#start_date").datepicker({
        numberOfMonths: 1,
        onSelect: function(selected) {
          $("#end_date").datepicker("option","minDate", selected)
        }
    });
    $("#end_date").datepicker({
        numberOfMonths: 1,
        onSelect: function(selected) {
           //$("#start_date").datepicker("option","maxDate", selected)
        }
    });  
});
</script>