<?php
$decodedJob = json_decode($job);
?>
<div class="container">
	<div class="row">
		<div class="col-lg-12">
			<div class="card">
				<div class="card-header">
					<h2 class="card-title"><?= $decodedJob->job->title ?></h2>

				</div>

				<div class="card-body" style="padding: 10px 0">
					<div class="row">
						<div class="col-lg-8">
							<p> Category : <b class="bg-warning " style="padding: 5px 10px; border-radius: 5px; "> <?= $decodedJob->category ?> </b> </p>
							<p> Skills :
								<?php
								$skills = $decodedJob->skills;
								foreach ($skills as $key => $skill) {
									echo ' <b class="bg-warning " style="padding: 5px 10px; border-radius: 5px; display: inline-block; ">' . $skill->name . '</b> ';
								}
								?>
							</p>
							<p>Budget : <?= $decodedJob->job->budget ?> Tk.</p>
							<p>Deadline : <?= $decodedJob->job->deadline ?> </p>
						</div>

						<div class="col-lg-4">
							<p>Published By : <?= $decodedJob->job->merchant_name ?></p>
						</div>
					</div>

					<div class="col-lg-12">
						<p> Job Description :
							<br> <br>
							<?= $decodedJob->job->description ?>
						</p>
					</div>

					<div class="col-lg-12">
						<hr>


						<p> Job Files : </p>
						<?php
						$images = ['jpg', 'jpeg', 'png', 'svg'];
						$imageFiles = null;
						$documents = null;
						foreach ($decodedJob->job_files as $key => $image) {
							$ext = pathinfo($image->filename, PATHINFO_EXTENSION);

							if (in_array($ext, $images)) {
								$imageFiles .= '
											<div class="col-lg-4">
												<div class="border2" style="border:1px solid #FFE000;border-radius: 5px;padding: 8px;text-align: center;">
													<div class="imgs" style="height: 200px;width: 100%;overflow: hidden">
														<img src="' . $decodedJob->url . $image->filename . '" alt="" style="height: 100%;">
													</div>
													
													<a href="' . $decodedJob->url . $image->filename . '" target="_blank" class="btn btn-success"; style="padding:5px 10px; border-radius:5px; margin-top:7px;"> Download ' . $ext . ' </a>
													
												</div> 

											</div>
									';
							} else {
								$documents .= ' <a href="' . $decodedJob->url . $image->filename . '" target="_blank" class="btn btn-success"; style="padding:5px 10px; border-radius:5px;"> Download ' . $ext . ' </a> ';
							}
						}
						?>

						<div class="row">
							<?= $imageFiles ?>

							<div class="col-lg-12">
								<br>
								<br>
								<?= $documents ?>
							</div>

							<div class="col-lg-12">

								<div class="card">
									<div class="card-header">
										<h3 class="card-title">ALL Bids</h3>
									</div>
									<div class="card-body" style="padding: 10px 0">
										<div class="row">
											<!--  Start -->
											<?php
											if (count($decodedJob->bids) > 0) {
												/* Any Bid approved status checking start */
												$bidApprovedId = 0;
												foreach ($decodedJob->bids as $key => $bid) {
													if ($bid->status == 1) {
														$bidApprovedId = $bid->id;
													}
												}
												/* Any Bid approved status checking end */


												foreach ($decodedJob->bids as $key => $bid) {
													echo '	
															<div class="col-lg-8">
																<div class="media" style="border: 1px solid rgba(255,224,0,1);padding: 10px;margin-bottom: 3px;border-radius:5px;">
																	<div class="media-left">
																		<img src="https://work.mali-mart.com/app-assets/images/members/avater_3.png"
																			 class="media-object" style="width:60px">
																	</div>
																	<div class="media-body text-left">
																		<div class="row" style="padding: 0">
																			<div class="col-lg-8">
																				<h4 class="media-heading ">' . $bid->merchant_name . '</h4>
																				<p style="">
																					Bid Amount : ' . $bid->amount . ' Tk.
																					<br>
																					Bid date : ' . substr($bid->created_at, 0, 10) . '
																				</p>
																			</div>																			
																			<div class="col-lg-4">
																			';
													if ($bidApprovedId != 0) {
														if ($bid->id == $bidApprovedId) {
															$hireButton = '
																				 			<p class="bg-success" style="padding: 5px 10px; display: inline-block; border-radius: 5px;"> <i class="fa-check fa"></i> hired </p>
																				 			
																				 			<button 
																				 			 	style="padding: 5px 10px; border-radius: 5px;"
																								data-id="' . $bid->id . '"
																								data-url="https://myadmin.mali-mart.com/api/job-bid-hire-cancel/' . $bid->id . '"
																								 class="bg-warning hireCancel"> 
																									 Cancel 
																							 </button>
																				 ';
														} else {
															$hireButton = ' <p class="btn btn-warning"> closed </p>';
														}
													} else {
														$hireButton = '<button 
																					data-id="' . $bid->id . '"
																					data-url="https://myadmin.mali-mart.com/api/job-bid-hire/' . $bid->id . '"
																					 class="btn btn-success hire"> 
																						 Hire 
																				 </button>';
													}
													echo $hireButton . '
																				
																			</div>																			
																		</div>
																	</div>
															</div>
														<div class="col-lg-4"></div>
														';
												}
											} else {
												echo ' <p class="bg-warning" style="padding: 5px 10px;border-radius: 5px;"> No Bid Found </p> ';
											}
											?>
											<!--  End -->
										</div>
									</div>
								</div>

							</div>
						</div>
					</div>

				</div>

			</div>
		</div>
	</div>
</div>

<script>
	$(".hire").click(function() {
		var url = $(this).attr("data-url");
		var id = $(this).attr("data-id");

		$.ajax({
			method: "post",
			url: url,
			data: {
				id: id
			},
			dataType: 'json',
			success: function(res) {
				if (res.status == 1) {
					location.reload();
				} else {
					alert(res.success);
				}
			},
			error: function(err) {
				console.log(err);
			}
		});

	});

	$(".hireCancel").click(function() {
		var url = $(this).attr("data-url");
		var id = $(this).attr("data-id");

		$.ajax({
			method: "post",
			url: url,
			data: {
				id: id
			},
			dataType: 'json',
			success: function(res) {
				if (res.success == 1) {
					location.reload();
				} else {
					alert("Insufficiant Balance. Please Deposit.");
				}
			},
			error: function(err) {
				console.log(err);
			}
		});

	});
</script>