<?php require_once('includes/check-login.php'); ?>
<?php require_once('includes/config.php');
$tablename="timeslot";
$viewurl="timeslot.php";
$vendor_id=$_SESSION['userid'];
if(isset($_POST['Add'])){
	    if(!file_exists(UPLOAD_SERVICEIMG)) {
          mkdir(UPLOAD_SERVICEIMG, 0777, true);
        }			
        $created=date("Y-m-d H:i:s");
        $slot_name=implode("*",$_POST['slot_name']);
        $slot_name1=explode("*",$slot_name);
        
        for($i=0;$i<count($slot_name1);$i++){
        $available=implode("*",$_POST['available']);
        $available1=explode("*",$available);
        $start_time=implode("*",$_POST['start_time']);
        $start_time1=explode("*",$start_time);
        $end_time=implode("*",$_POST['end_time']);
        $end_time1=explode("*",$end_time);
         $query = "insert into ".$tablename." SET vendor_id='".$vendor_id."',slot_name='".$slot_name1[$i]."',available='".$available1[$i]."',start_time='".$start_time1[$i]."',end_time='".$end_time1[$i]."',created='".$created."'";
		$exe = mysqli_query($con,$query);   
        }
		if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Timeslot added successfully';
		}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
		}
        header("Location:".$base_url.$viewurl."");
}
if(isset($_POST['Update'])){	
$modified=date("Y-m-d H:i:s");
$slot_name=implode("*",$_POST['slot_name']);
$slot_name1=explode("*",$slot_name);
for($i=0;$i<count($slot_name1);$i++){
        $available=implode("*",$_POST['available']);
        $available1=explode("*",$available);
        $start_time=implode("*",$_POST['start_time']);
        $start_time1=explode("*",$start_time);
        $end_time=implode("*",$_POST['end_time']);
        $end_time1=explode("*",$end_time);
        $id=implode("*",$_POST['id']);
        $id1=explode("*",$id);
        $query="update ".$tablename." SET vendor_id='".$vendor_id."',slot_name='".$slot_name1[$i]."',available='".$available1[$i]."',start_time='".$start_time1[$i]."',end_time='".$end_time1[$i]."',modified='".$modified."' WHERE id='".$id1[$i]."'";
		$exe = mysqli_query($con,$query);   
}
	if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Timeslot updated successfully';
	}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
	}
    header("Location:".$base_url.$viewurl."");
}	 
$sql = "SELECT * from ".$tablename." WHERE vendor_id = '".$vendor_id."'";
$result = $con->query($sql);
if(mysqli_num_rows($result)>0){	  
$display = "Update";
}else{
$display = "Add";
}	  
for($i=0;$i<5;$i++) {
	//echo date('D', strtotime('Tuesday')).'<br />';
}
$mydays = array('Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$daycount=count($mydays);
$start = strtotime('07:00');
$end   = strtotime('20:00');
?>
<?php require_once('includes/header.php'); ?>
<?php require_once('includes/menu.php'); ?>
<!------ Include the above in your HEAD tag ---------->
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
      <!-- Content Header (Page header) -->
            <section class="content-header">                               
                    <ol class="breadcrumb">
                        <li><a href="<?php echo $base_url;?>"><i class="fa fa-dashboard"></i>Home</a></li>                    
                        <li class="active"><a href="#">Vendor Time Slot</a></li>           
                    </ol>                
            </section>
<form id="serviceData" method="POST" enctype="multipart/form-data">
  <center><div class="message"></div></center>
    <div class="row setup-content">
      <div class="col-md-offset-1">
        <div class="col-md-12">
            <center><h3>Vendor Time Slot</h3></center>
            <?php if(mysqli_num_rows($result)>0){
            while($row=mysqli_fetch_array($result)){?>	
            <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">Days</label>
                <input type="hidden" name="id[]" class="form-control" value="<?php echo $row['id'];?>">
                <input type="text" name="slot_name[]" class="form-control" value="<?php echo $row['slot_name'];?>" readonly>
              </div> 
              </div>
              <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">Availability</label>
               	<select name="available[]" class="form-control select2" required >
				<option value="" disabled selected>Choose</option>
			    <option value="1"<?php if($row['available']==1){?>selected<?php }?>>Working</option>
			    <option value="0" <?php if($row['available']==0){?>selected<?php }?>>Not working</option>
				</select>
              </div> 
              </div>
              <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">Start Time</label>
               	<select name="start_time[]" class="form-control">
                 <?php 
                  for ($i=$start; $i<=$end; $i = $i + 30*60){
                   $currtime=date('H:i',$i);?>
                  <option value="<?php echo $currtime;?>" <?php if($row['start_time']==$currtime){?>selected<?php }?>><?php echo date('g:i A',$i);?>
                  </option>
               <?php }?>
            </select>
              </div> 
              </div>
              <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">End Time</label>
                <select name="end_time[]" class="form-control">
                 <?php 
                  for ($i=$start; $i<=$end; $i = $i + 30*60){
                  $currtime=date('H:i',$i);?>
                  <option value="<?php echo $currtime;?>" <?php if($row['end_time']==$currtime){?>selected<?php }?>><?php echo date('g:i A',$i);?>
                  </option>
               <?php }?>
            </select>
              </div> 
              </div>
            <?php }}else { for($j=0;$j<$daycount;$j++){?>
            <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">Days</label>
                <input type="text" name="slot_name[]" class="form-control" value="<?php echo $mydays[$j];?>" readonly>
              </div> 
              </div>
              <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">Availability</label>
               	<select name="available[]" class="form-control select2" required autofocus>
				<option value="" disabled selected>Choose</option>
			    <option value="1">Working</option>
			    <option value="0">Not working</option>
				</select>
              </div> 
              </div>
              <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">Start Time</label>
               	<select name="start_time[]" class="form-control">
                 <?php 
                  for ($i=$start; $i<=$end; $i = $i + 30*60){?>
                  <option value="<?php echo date('H:i',$i);?>"><?php echo date('g:i A',$i);?>
                  </option>
               <?php }?>
            </select>
              </div> 
              </div>
              <div class="col-md-3">
              <div class="form-group">
                <label class="control-label">End Time</label>
                <select name="end_time[]" class="form-control">
                 <?php 
                  for ($i=$start; $i<=$end; $i = $i + 30*60){?>
                  <option value="<?php echo date('H:i',$i);?>"><?php echo date('g:i A',$i);?>
                  </option>
               <?php }?>
            </select>
              </div> 
              </div>
              <?php }}?>
              <div class="col-md-6">
			  <div class="form-group">
            <button class="btn btn-success btn-lg pull-right" type="submit" name="<?php echo $display;?>"><?php echo $display;?></button>
			</div>
			</div>
        </div>
      </div>
    </div>
  </form>
</div>

<script src="js/tinymce/tinymce.min.js"></script>
<script>
    tinymce.init({ 
        selector:'.tinymce',
        branding: false 
    });
</script>    
<script src="js/common.js"></script>  
<script src="<?php echo $base_url;?>js/jquery-3.4.1.min.js"></script>
<script src="<?php echo $base_url;?>js/jquery.validate.min.js"></script>
<script src="<?php echo $base_url;?>js/additional-methods.min.js"></script>
<script type="text/javascript">
$(function(){
       $("#serviceData").validate({
          rules: {
               service_name: 'required',
			   service_id: 'required',
			   default_price: 'required',
			   service_image: {extension: "jpg|jpeg|png"},
			   banner_image: {extension: "jpg|jpeg|png"},
               status:'required',  
           },
            messages: {
              service_image: {extension: "Please enter a value with a valid extension(jpg,jpeg,png)"},  
              banner_image: {extension: "Please enter a value with a valid extension(jpg,jpeg,png)"},
            },
            submitHandler: function(form) {
              form.submit();
           }
    });        
});
function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
      $('#imageshow').attr('src', e.target.result);
	  $('#imageshow').css('display','initial');
    }
    reader.readAsDataURL(input.files[0]);
  }
}
$("#image").change(function() {
  readURL(this);
});
function readURL1(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
      $('#imageshow1').attr('src', e.target.result);
	  $('#imageshow1').css('display','initial');
    }
    reader.readAsDataURL(input.files[0]);
  }
}
$("#banner_image").change(function() {
  readURL1(this);
});
</script>
<?php require_once('includes/footer.php'); ?>