<?php include "head.php" ; ?>
<?php
    session_start();
    date_default_timezone_set('Asia/Kolkata');
    if(isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && $_SESSION['user_cate'] === "vendor"){
        $user_id = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        $ticket = $_SESSION['last_ticket'] ;
        $company = $_SESSION['company'] ;
        $vendor_image = $_SESSION['vendor_image'] ;
        $check_sql = "SELECT * FROM order_master WHERE customer_id = '$user_id' And order_status = 2 ";
        $check_res = $config->query($check_sql);
        if($check_res->num_rows == 0){
            $_SESSION['check'] = "Purchase Package";
		    $_SESSION['alert'] = "warning";
		    header("Location:package.php");
        }
        
        $amt_check = $config->query("SELECT * FROM ewallet_master WHERE vendor_id = '$user_id' ");
        $amt_res = $amt_check->fetch_assoc();
        if($amt_res['total_amount'] == 0){
            $_SESSION['check'] = "Purchase Package";
		    $_SESSION['alert'] = "warning";
		    header("Location:package.php") ;
        }
        
        $check_com = $config->query("SELECT * FROM v_profile WHERE v_id = '$user_id' ");
        $res_com = $check_com->fetch_assoc();
        $com = $res_com['company'];
        if(empty($com)){
            $_SESSION['check'] = "Update Your Profile";
		    $_SESSION['alert'] = "warning";
		    header("Location:vendor_profile.php") ;
        }
        
    }else{
        header("Location:vendor_login.php") ;
    }
?>
<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){
    
	$action = $_POST['action'];
	
// post
    // post insert query starts
	if($action == "post_add"){
 		$title = $_POST['title'];
 		$sub_title = $_POST['sub_title'];
 		$sub_cate_id = $_POST['cate'];
 		
 		$pack_id = $_POST['pack'];
        $sub_sql = $config->query("SELECT * FROM package_master WHERE pack_status = 1 AND pack_id = '$pack_id' ");
        $sub_res = $sub_sql->fetch_assoc();
        $cost = $sub_res['pack_cost'];
        // $pack_comm = $sub_res['pack_commission'];
 		$wal_row['paid'];

        $wal_sql = "SELECT * FROM ewallet_master WHERE vendor_id = '$user_id' " ;
        $wal_res = $config->query($wal_sql);
        $wal_row = $wal_res->fetch_assoc();
        $total_amount = $wal_row['total_amount'];

			    
//  		if(isset($_SESSION['wallet'])){
//  		     $wallet = $_SESSION['wallet'] ;
//  		}else{
            $pay_sql = "SELECT (SELECT sum(order_price) FROM order_master WHERE customer_id = '$user_id' And paid_status = 1 ) as paid 
            ,(SELECT sum(add_on) as bonus FROM order_master WHERE customer_id = '$user_id' And paid_status = 1 ) as bonus FROM order_master " ;
            $pay_res = $config->query($pay_sql);
            $pay_row = $pay_res->fetch_assoc();
            $wallet = $pay_row['paid'] + $pay_row['bonus'];
//         }
 		
 		$type = $_POST['type'];
 		$address = $_POST['address'];
 		$area = $_POST['area'];
 		$city = $_POST['city'];
 		$desc = $_POST['description'];
 		$entry = $_POST['entry'];
 		$entry_fees = $_POST['entry_fees'];
 		$entry_types = $_POST['entry_type'];
 		$entry_type = implode(',',$entry_types);

 		$m_entry_fees = $_POST['m_entry_fees'];
 		$fees = implode(',',$m_entry_fees);
 		$e_date = $_POST['e_date'];
 		$end_date = $_POST['end_date'];
 		$l_date = $_POST['l_date'];
 		
 		$image = $_FILES['image']['name'];
 		$extension = pathinfo($image,PATHINFO_EXTENSION);
        $filename = 'event'.date('dmYHis').'.'.$extension;
 		move_uploaded_file($_FILES['image']['tmp_name'],'assets/img/event/'.$filename);
 		
 		$broucher = $_FILES['broucher']['name'];
 		$br_extension = pathinfo($broucher,PATHINFO_EXTENSION);
        $br_filename = 'broucher'.date('dmYHis').'.'.$br_extension;
 		move_uploaded_file($_FILES['broucher']['tmp_name'],'assets/img/broucher/'.$br_filename);

		$sql =  "INSERT INTO event_post(vendor_id, e_title, sub_title, sub_cate_id, pack_id, e_type, address, area, city, e_desc, entry, entry_fees, entry_type, m_entry_fees, e_date, end_date, l_date, e_image, e_broucher, e_status) 
		VALUES ('$user_id', '$title', '$sub_title', '$sub_cate_id', '$pack_id', '$type', '$address', '$area', '$city', '$desc', '$entry', '$entry_fees', '$entry_type', '$fees', '$e_date', '$end_date', '$l_date', '$filename', '$br_filename', 1) ";
		if($config->query($sql) === TRUE){
		    $last_id = $config->insert_id;
		    if($total_amount >= $cost){
		        $wallet = $total_amount - $cost;
		        $pay_date = date("Y-m-d h-i-s");
		        $cost_sql = "UPDATE ewallet_master SET total_amount = '$wallet' WHERE vendor_id = '$user_id' ;";
		        $cost_sql .= "INSERT INTO payment_request(pay_vendor_id, pay_event_id, amount, wallet, payment_date, pay_status) VALUES('$user_id', '$last_id', '$cost' ,'$wallet' ,'$pay_date' ,1) ";
                if($config->multi_query($cost_sql) === TRUE){
                    // $_SESSION['wallet'] = $wallet;
                    header("Location:listing_pending.php");
                    $_SESSION['check'] = "Uploaded Successfully";
        		    $_SESSION['alert'] = "success";
                }else {
                    $_SESSION['status'] = "Upload Failed";
        		    $_SESSION['alert'] = "error";
                }
		    }else {
                $_SESSION['check'] = "Purchase Package";
    		    $_SESSION['alert'] = "warning";
    		    header("Location:package.php") ;
            }
        }else {
            $_SESSION['status'] = "Upload Failed";
		    $_SESSION['alert'] = "error";
        }
	}
	// post insert query ends

		
}

?>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" ></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" ></script>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" >
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" ></script>

    <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.js"></script>

	<link rel="stylesheet" href="assets/css/style.css">
	<body>

		<!-- Main Wrapper -->
		<div class="main-wrapper">
		
			<!-- Header -->
			<?php include "header.php" ; ?>
			<!-- /Header -->
			
			<!-- Breadcrumb -->
			<?php include "breadcrumb.php" ; ?>
			<!-- /Breadcrumb -->
			
			<!-- Page Content -->
			<div class="content">
				<div class="container-fluid">

					<div class="row">
						<div class="col-md-5 col-lg-4 col-xl-3 theiaStickySidebar">
						
							<!-- Profile Sidebar -->
							<?php include "vendor_sidebar.php" ; ?>
							<!-- /Profile Sidebar -->
							
						</div>
						<div class="col-md-7 col-lg-8 col-xl-9">
						    <form method="post" enctype="multipart/form-data" >
    							<!-- Basic Information -->
    							<div class="card">
    								<div class="card-body">
    									<h4 class="card-title">Post Event</h4>
    									<div class="row form-row">
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="cate">Category<span class="text-danger">*</span></label>
    												<select class="form-control" name="cate" id="cate" required>
    												    <option value="">-- Select --</option>
    												    <?php
        											        $sql = "SELECT * FROM sub_category WHERE Sub_Category_Status = 1 " ;
        											        $res = $config->query($sql);
        											        while($row = $res->fetch_assoc()){
        											    ?>
    												    <option value="<?= $row['Sub_Category_id'] ?>"><?= $row['Sub_Category_Name'] ?></option>
    												    <?php } ?>
    												</select>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="pack">Package<span class="text-danger">*</span></label>
    												<select class="form-control" name="pack" id="pack" required>
    												    <option value="">-- Select --</option>
    												</select>
    												<!--<p>$pack['pack_commission'].'%'</p>-->
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="title">Event Name <span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="title" id="title" value="<?= $cate_id ?>" required>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="sub_title">Sub Title</label>
    												<input type="text" class="form-control" name="sub_title" id="sub_title">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="type">Type(i.e:trophy,cash prize)<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="type" id="type" required>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="address">Address<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="address" id="address" required>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="area">Area<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="area" id="area" required>
    											</div>
    										</div>
    										<div class="col-md-6">
    										    <div class="form-group ">
        											<label for="city">City<span class="text-danger">*</span></label>
        										    <select class="form-control" name="city" id="city" required>
        											    <option value="">-- Select --</option>
        											    <?php
        										            $sql = "SELECT * FROM city_master WHERE City_Status = 1 " ;
        											        $res = $config->query($sql);
        											        while($row = $res->fetch_assoc()){
        											    ?>
        											    <option value="<?= $row['City_Name'] ?>"><?= $row['City_Name'] ?></option>
        											    <?php } ?>
        											</select>
        										</div>
    										</div>
    										<div class="col-md-12">
    											<div class="form-group">
    												<label for="description">Description Max.Characters(500)</label>
    												<textarea type="text" class="form-control" name="description" id="description" rows="4"></textarea>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<span><i class="fa fa-upload"></i> Upload Image</span>
    											    <input type="file" class="form-control" name="image" required>
    											    <small class="form-text text-muted">Allowed JPG, GIF or PNG. Max size of 2MB (600 X 448 px)</small>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    											    <span><i class="fa fa-upload"></i> Upload Broucher</span>
    												<input type="file" class=" form-control" name="broucher">
    												<small class="form-text text-muted">Allowed JPG, GIF or PNG. Max size of 2MB</small>
    											</div>
    										</div>
    										<div class="col-md-4 mt-3">
    											<div class="form-group">
    												<label for="e_date">Event Start Date<span class="text-danger">*</span></label>
    												<input type="date" class="form-control" name="e_date" id="e_date" min="<?= date('Y-m-d');?>" required>
    											</div>
    										</div>
    										<div class="col-md-4 mt-3">
    											<div class="form-group">
    												<label for="end_date">Event End Date<span class="text-danger">*</span></label>
    												<input type="date" class="form-control" name="end_date" id="end_date" min="<?= date('Y-m-d');?>" required>
    											</div>
    										</div>
    										<div class="col-md-4 mt-3">
    											<div class="form-group">
    												<label for="l_date">Last Enrollment Date<span class="text-danger">*</span></label>
    												<input type="date" class="form-control" name="l_date" id="l_date" min="<?= date('Y-m-d');?>" required>
    											</div>
    										</div>
    										<div class="col-md-12 my-3">
    										    <div class="form-group mb-0">
            										<div id="pricing_select">
            											<div class="custom-control custom-radio custom-control-inline">
            												<input type="radio" id="free" name="entry" class="custom-control-input" value="free" onclick="show()" required>
            												<label class="custom-control-label" for="free">Free Entry</label>
            											</div>
            											<div class="custom-control custom-radio custom-control-inline">
            												<input type="radio" id="paid" name="entry" value="paid" class="custom-control-input" onclick="show()" required>
            												<label class="custom-control-label" for="paid">Paid Entry(Single)</label>
            											</div>
            											<div class="custom-control custom-radio custom-control-inline">
            												<input type="radio" id="multi" name="entry" value="multi" class="custom-control-input" onclick="show()" required>
            												<label class="custom-control-label" for="multi">Paid Entry(Multiple)</label>
            											</div>
            										</div>
            							             
            									</div>
    										</div>
    										<div class="col-md-6 mt-3">
    											<div class="form-group" id="entry-box" >
    												<label for="entry_fees">Entry Fees<span class="text-danger">*</span></label>
    												<input type="number" class="form-control" name="entry_fees" id="entry_fees" >
    											</div>
    										</div>
    										<div class="col-md-12 mt-3 multi_entry" id="multiEntry">
    										    <div class="row">
    										        <div class="col-md-5 form-group" id="" >
        												<label for="entry_type">Entry Type<span class="text-danger">*</span></label>
        												<input type="text" class="form-control" name="entry_type[]" id="entry_type" >
        											</div>
            							            <div class="col-md-5 form-group" id="" >
        												<label for="m_entry_fees">Entry Fees<span class="text-danger">*</span></label>
        												<input type="number" class="form-control" name="m_entry_fees[]" id="m_entry_fees" >
        											</div>
        											<div class="col-md-2" >
            											<div class="add-more">
                    										<a href="javascript:void(0);" class="add-multi-entry"><i class="fa fa-plus-circle"></i> Add</a>
                    									</div>
        											</div>
    										    </div>
    										</div>
    									</div>
    								</div>
    							</div>
    							<!-- /Basic Information -->
    							
    							<div class="submit-section submit-btn-bottom">
    							    <input type="hidden" name="action" value="post_add">
    								<button type="submit" class="btn btn-info submit-btn" >Submit</button>
    							</div>
							</form>
						</div>
					</div>

				</div>

			</div>		
			<!-- /Page Content -->
   
			<!-- Footer -->
			<?php include "footer.php" ; ?>
			<!-- /Footer -->
		   
		</div>
		<!-- /Main Wrapper -->
		<style>
		    #entry-box,#multiEntry{
		        display:none;
		    }
		    .change-avatar .change-photo-btn {
                width: 200px;
            }
		</style>
		<script>
    		let paid = document.getElementById("paid");
    		let entry = document.getElementById("entry-box"); 
    		let entryFees = document.getElementById("entry_fees");
    		let cate = document.getElementById("cate");
    		let multi = document.getElementById("multi"); 
    		let multiEntry = document.getElementById("multiEntry"); 
    		let entry_type = document.getElementById("entry_type"); 
    		let m_entry_fees = document.getElementById("m_entry_fees"); 
    		function show(){
    		    if(paid.checked === true){ 
                    entry.style.display = "block";
                    entryFees.setAttribute("required",true);
                    multiEntry.style.display = "none";
                    entry_type.removeAttribute("required");
                    m_entry_fees.removeAttribute("required");
                    
                }else if(multi.checked === true){ 
                    multiEntry.style.display = "block";
                    entry.style.display = "none";
                    entry_type.setAttribute("required",true);
                    m_entry_fees.setAttribute("required",true);
                    entryFees.removeAttribute("required");
                }else{ 
                    entry.style.display = "none";
                    multiEntry.style.display = "none";
                    entryFees.removeAttribute("required");
                    entry_type.removeAttribute("required");
                    m_entry_fees.removeAttribute("required");
                }
    		};
    		cate.addEventListener('change',function(){
    		    let cate_id = cate.value;
                $.ajax({
                  url: "function.php",
                  method: "POST",
                  data: { cate_id: cate_id },
                  success: function(data){
                      $("#pack").html(data)
                  }
                })
    		});
    		
		</script>
		<script>
		    let eDate = document.getElementById("e_date");
		    let lDate = document.getElementById("l_date");
		    let end_date = document.getElementById("end_date");
    		eDate.addEventListener('change',function(){
    		    let maxDate = eDate.value;
    		    lDate.setAttribute("max",maxDate) ;
    		    end_date.setAttribute("min",maxDate) ;
    		});
		</script>
    	<script>
            $('#description').summernote({
                placeholder: '',
                tabsize: 2,
                height: 100
            });
            (function($) {
    "use strict";
	
	//entry
	$(".multi_entry").on('click','.trash', function () {
		$(this).closest('.new-multi-entry').remove();
		return false;
    });
    
    $(".add-multi-entry").on('click', function(){
		
		var entrycontent = '<div class="row new-multi-entry">' +
			'<div class="col-md-5 form-group" >' +
				'<label for="entry_type">Entry Type<span class="text-danger">*</span></label>' +
				'<input type="text" class="form-control" name="entry_type[]" >' +
			'</div>' +
			'<div class="col-md-5 form-group" >' +
				'<label for="m_entry_fees">Entry Fees<span class="text-danger">*</span></label>' +
				'<input type="number" class="form-control" name="m_entry_fees[]" >' +
			'</div>' +
		    '<div class= "col-12 col-md-2 col-lg-1"><label class="d-md-block d-sm-none d-none">&nbsp;</label><a href="#" class="btn btn-danger trash"><i class="far fa-trash-alt"></i></a></div>'+
		'</div>';
		
        $(".multi_entry").append(entrycontent);
        return false;
    });
	// Education Add More
	
})(jQuery);
        </script>
        
		<!-- jQuery -->
		<script src="assets/js/jquery.min.js"></script>
		
		<!-- Bootstrap Core JS -->
		<script src="assets/js/popper.min.js"></script>
		<script src="assets/js/bootstrap.min.js"></script>
		
		<!-- Sticky Sidebar JS -->
        <script src="assets/plugins/theia-sticky-sidebar/ResizeSensor.js"></script>
        <script src="assets/plugins/theia-sticky-sidebar/theia-sticky-sidebar.js"></script>
		
		<!-- Select2 JS -->
		<script src="assets/plugins/select2/js/select2.min.js"></script>
		
		<!-- Dropzone JS -->
		<script src="assets/plugins/dropzone/dropzone.min.js"></script>
		
		<!-- Bootstrap Tagsinput JS -->
		<script src="assets/plugins/bootstrap-tagsinput/js/bootstrap-tagsinput.js"></script>
		
		<!-- Profile Settings JS -->
		<!--<script src="assets/js/profile-settings.js"></script>-->
		
		<!-- Custom JS -->
		<script src="assets/js/script.js"></script>
		<script>
    		<?php if(isset($_SESSION['status']) && !empty($_SESSION['status'])) { ?>
    		    swal({
                  title: "<?= $_SESSION['status'] ?>",
                  text: "",
                  icon: "<?= $_SESSION['alert'] ?>",
                });
            <?php unset($_SESSION['status']) ; } ?>
        </script>
	</body>

<!-- doccure/doctor-profile-settings.html  30 Nov 2019 04:12:15 GMT -->
</html>