<?php
session_start();
$where = " WHERE 1=1 ";
$next_page_condition = "";
$paging = "";

if ($_REQUEST['start']) {
	$paging .= "&start=" . $_REQUEST['start'];
}
if (isset($_REQUEST["from"]) && $_REQUEST["from"] != "") {
	$_SESSION["from"] = $_REQUEST["from"];
}
if (isset($_REQUEST['id']) && $_REQUEST['id'] != "") {
	$where .= " AND id = " . $_REQUEST['id'] . " ";
}
$customer_id = utility::app_crypt($_REQUEST['customer_id'], 'd');
if ($_REQUEST['id'] != $customer_id) {
	die("Invaild URL");
}
$restricted_groups = [12, 13, 14, 16];
$can_view = !in_array($_SESSION['admin_group_id'], $restricted_groups);
###########################################  PAGING WITH PER PAGE  #####################################
$file_name = "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name = "client_management";
$paging_table_name = $table_name;
$paging_query = $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ";
$rs = mysqli_query($conn, $query);
$client_row = mysqli_fetch_assoc($rs);
$client_row = utility::validate_crypt_data($client_row);
?>
<style>
	.form-group .control-label {
		padding-top: 0px;
	}

	.content {
		min-height: 0px;
	}

	.content-header h1 {
		float: left;
		padding-bottom: 10px;
	}

	.responsebar {
		position: sticky;
		/* Makes the section sticky */
		top: 0;
		/* Keeps it at the top of the viewport */
		z-index: 1037;
		/* Ensures it stays above other elements */
		background: #fff;
		/* Keeps a white background for clarity */
		transition: box-shadow 0.3s ease, background 0.3s ease;
		/* Adds smooth effects */
	}

	.responsebar_active {
		box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
		/* Adds a shadow for visual effect */
		background: #efefef;
		/* Changes the background when scrolled */
	}
</style>
<section class="content-header responsebar">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12 col-md-6 col-sm-6">
				<div class="col-sm-6">
					<h1 style="float:left;clear:right;">
						Customer Details - <span style='color:#ed5c5c;'><?= $client_row['customer_name']; ?></span>
					</h1>
				</div>
				<?
				$query_string = (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
				$query_string = str_ireplace("page=view_client_progress", "page=view", $query_string);
				?>
				<button type="button" id="back" name="back" value="back" style="float:right;margin-bottom: 15px;"
					class="btn btn-primary"
					onclick="window.location.href = 'index?<?= $query_string . $next_page_condition . $paging; ?>'">Back</button>
                <?php if($can_view){ ?>
				<? if ((isset($_REQUEST["from"]) && $_REQUEST["from"] == "view") || (isset($_REQUEST["from"]) && $_REQUEST['from'] == "view_client_progress")) {
					?>
					<a href="index?page=add&from=view_client_progress&action=edit&customer_id=<?= urlencode(utility::app_crypt($_REQUEST['id'], 'e')); ?>&id=<?= $_REQUEST['id'] . $next_page_condition; ?>"
						style="float:right;margin-right:5px;" class="btn btn-primary">Edit</a>
				<? } ?>
				<? } ?>
			</div>
		</div>
	</div>
</section>

<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12 no-padding">
				<div class="row">
					<!----------------------- Message ------------------------------->
					<? if (isset($_SESSION['info_message'])) { ?>
					<div class="col-sm-12 alert alert-info alert-dismissable">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
						<? echo $_SESSION['info_message'];
						unset($_SESSION['info_message']); ?>
					</div>
					<? } ?>
					<!----------------------- End Message ------------------------------->
					<!-- general form elements -->
					<?
					$col_width = 12;
					?>
					<div class="col-lg-4 col-md-4 col-sm-12" style="/* min-height:700px; */">
						<div class="scrollbar-default box box-warning card"
							style="height: 350px;overflow: hidden;overflow-y: scroll;">
							<form class="form-horizontal" method="post" name="form1" id="form1"
								action="index?page=add_supp&action=<? echo $form_action; ?>"
								onsubmit="return validateForm(this)" enctype="multipart/form-data">
								<div class="box-body">
									<h4 class="" style="margin-top: 0; margin-bottom: 0; margin-left: 5px; color:#9c4893;">Details</h4>
									<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
									<div class="form-group col-lg-12 d-flex">
										<label for="Customer Name" class="col-sm-5 control-label">Customer Name</label>
										<div class="col-sm-7">
											<? echo ucfirst($client_row['customer_name']); ?>
										</div>
									</div>
									<div class="form-group col-lg-12 d-flex">
										<label for="Customer Code" class="col-sm-5 control-label">Customer Code</label>
										<div class="col-sm-7">
											<? echo $client_row['customer_code']; ?>
										</div>
									</div>

									<div class="form-group col-lg-12 d-flex">
										<label for="Delivery Address" class="col-sm-5 control-label">Delivery Address</label>
										<div class="col-sm-7">
											<?php
											$delivery_address_sql = mysqli_query($conn, "SELECT * FROM client_delivery_address_mapping WHERE customer_id='" . $client_row['id'] . "' ORDER BY id ASC");
											if ($delivery_address_sql && mysqli_num_rows($delivery_address_sql) > 0):
												while ($mapp_row = mysqli_fetch_array($delivery_address_sql)):
													$addr_parts = array_filter([
														$mapp_row['address_code'],
														$mapp_row['delivery_address_1'],
														$mapp_row['delivery_address_2'],
														$mapp_row['delivery_address_3'],
														$mapp_row['delivery_add_postcode'],
													]);
											?>
												<div style="margin-bottom:6px;">
													<?php echo implode(' ', $addr_parts); ?>
												</div>
											<?php
												endwhile;
											else:
											?>
												<span class="text-muted">No delivery address found.</span>
											<?php endif; ?>
										</div>
									</div>
									<div class="form-group col-lg-12 d-flex">
										<label for="Delivery Address" class="col-sm-5 control-label">Invoice Address</label>
										<div class="col-sm-7">
											<?php
											if (isset($client_row["invoice_address_1"]) && $client_row["invoice_address_1"] != "") {
												echo $client_row["invoice_address_1"];
											}
											if (isset($client_row["invoice_address_2"]) && $client_row["invoice_address_2"] != "") {
												echo " " . $client_row["invoice_address_2"];
											}
											if (isset($client_row["invoice_address_3"]) && $client_row["invoice_address_3"] != "") {
												echo " " . $client_row["invoice_address_3"];
											}
											if (isset($client_row["invoice_add_postcode"]) && $client_row["invoice_add_postcode"] != "") {
												echo " " . $client_row["invoice_add_postcode"];
											}
											?>
										</div>
									</div>


								</div>
								<!-- /.box-body -->
							</form>
						</div><!-- /.box -->
					</div>
					<div class="col-lg-4 col-md-4 col-sm-12" style="/* min-height:700px; */">
						<div class="scrollbar-default box box-warning card"
							style="height: 350px;overflow: hidden;overflow-y: scroll;">
							<form class="form-horizontal" method="post" name="form1" id="form1"
								action="index?page=add_placement&action=<? echo $form_action; ?>"
								onsubmit="return validateForm(this)" enctype="multipart/form-data">
								<div class="box-body">
									<h4 class="" style="margin-top: 0; margin-bottom: 0; margin-left: 5px; color:#9c4893;">Files</h4>
									<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
									<div class="row">
										<div class="form-group col-lg-12">
											<label for="File" class="col-sm-4">File</label>
											<div class="col-sm-8">
												<?php
												$frontEndURL = $config->frontEndURL;
												$dir = "customer_documents/";

												if (is_file($dir . $client_row['document_upload'])) {
													$ext = pathinfo($client_row['document_upload'], PATHINFO_EXTENSION);

													if ($ext == "jpg" || $ext == "jpeg" || $ext == "png") {
														$image_path = '<a href="../' . $dir . $client_row['document_upload'] . '" target="_blank"><img class="primary text-primary" style="width:40px;border:1px solid #777" src="../' . $dir . $client_row['document_upload'] . '"></a>';
													} else if ($ext == "pdf") {
														$image_path = '<a href="../' . $dir . $client_row['document_upload'] . '" target="_blank"><span class="primary text-primary" title="' . $client_row['document_upload'] . '">' . utility::ellipsis($client_row['document_upload'], 20) . '</span></a>';
													}
												} else {
													$image_path = '<img class="primary" style="width:40px;border:1px solid #777" src="../' . $dir . 'no-image.gif">';
												}

												echo $image_path;
												?>
											</div>
										</div>
									</div>
									<h4 class="" style="margin-top: 0; margin-bottom: 0; margin-left: 5px; color:#9c4893;">On Hold for</h4>
									<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
									<div class="row">
										<div class="form-group col-lg-6 d-flex">
											<label for="Deliveries" class="col-sm-8 control-label">Deliveries</label>
											<div class="col-sm-4">
												<?php echo ($client_row["is_deliveries"] == 1) ? 'Yes' : 'No'; ?>
											</div>
										</div>
										<div class="form-group col-lg-6 d-flex">
											<label for="VAC from works order" class="col-sm-8 control-label">VAC from works order</label>
											<div class="col-sm-4">
												<?php echo ($client_row["is_vac_from_works_order"] == 1) ? 'Yes' : 'No'; ?>
											</div>
										</div>

										<div class="form-group col-lg-6 d-flex">
											<label for="CNC works Order" class="col-sm-8 control-label">CNC works order</label>
											<div class="col-sm-4">
												<?php echo ($client_row["is_cnc_works_order"] == 1) ? 'Yes' : 'No'; ?>
											</div>
										</div>
										<div class="form-group col-lg-6 d-flex">
											<label for="Assembly works order" class="col-sm-8 control-label">Assembly works order</label>
											<div class="col-sm-4">
												<?php echo ($client_row["is_assembly_works_order"] == 1) ? 'Yes' : 'No'; ?>
											</div>
										</div>
									</div>
								</div>
								<!-- /.box-body -->
							</form>
						</div><!-- /.box -->
					</div>
					<div class="col-lg-4 col-md-4 col-sm-12" style="/* min-height:700px; */">
						<div class="scrollbar-default box box-warning card"
							style="height: 350px;overflow: hidden;overflow-y: scroll;">
							<form class="form-horizontal" method="post" name="form1" id="form1"
								action="index?page=add_placement&action=<? echo $form_action; ?>"
								onsubmit="return validateForm(this)" enctype="multipart/form-data">
								<div class="box-body">
									<h4 class="" style="margin-top: 0; margin-bottom: 0; margin-left: 5px; color:#9c4893;">Finance</h4>
									<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
									<div class="row">
										<div class="form-group col-lg-12 d-flex">
											<label for="Currency" class="col-sm-4 control-label">Currency</label>
											<div class="col-sm-8">
												<? echo $gsi->getCurrencyById($client_row["currency"]); ?>
											</div>
										</div>
										<div class="form-group col-lg-12 d-flex">
											<label for="Tax Code" class="col-sm-4 control-label">Tax Code</label>
											<div class="col-sm-8">
												<? echo $gsi->getTaxCodeListById($client_row["tax_code"]); ?>
											</div>
										</div>

										<div class="form-group col-lg-12 d-flex">
											<label for="Default Country Code" class="col-sm-6 control-label">Default Country Code</label>
											<div class="col-sm-6">
												<? echo $gsi->getCountryCodeById($client_row["default_country_code"]); ?>
											</div>
										</div>
									</div>
								</div>
								<!-- /.box-body -->
							</form>
						</div><!-- /.box -->
					</div>
					<div class="container-fluid">
						<div class="row">
							<div class="col-12">
								<table class="table table-bordered">
									<thead>
										<tr style="background-color: #9c4893; color: #fff;">
											<th>Contact Title</th>
											<th>First Name</th>
											<th>Surname</th>
											<th>Email</th>
											<th>Phone No.</th>
										</tr>
									</thead>
									<tbody>
											<?php
											$contacts_view_sql = mysqli_query($conn, "SELECT * FROM client_contact_mapping WHERE customer_id='" . $client_row['id'] . "' ORDER BY id ASC");
											if ($contacts_view_sql && mysqli_num_rows($contacts_view_sql) > 0):
												while ($contact_row = mysqli_fetch_assoc($contacts_view_sql)):
													$contact_row = utility::validate_crypt_data($contact_row);
											?>
												<tr>
													<td><?php echo Keycode::getKeycodeById($contact_row['title']); ?></td>
													<td><?php echo ucfirst($contact_row['first_name']); ?></td>
													<td><?php echo ucfirst($contact_row['surname']); ?></td>
													<td><?php echo $contact_row['email_address']; ?></td>
													<td><?php echo $contact_row['phone_no']; ?></td>
												</tr>
											<?php
												endwhile;
											else:
											?>
												<tr>
													<td colspan="5" class="text-center">No contacts found.</td>
												</tr>
											<?php endif; ?>
										</tbody>

								</table>
							</div>

							<!-- Section below the table for listing -->

						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>
<section class="content hide">
	<div class="container-fluid">
		<div class="row">
			<div class="col-12">
				<div id="" class="card">
					<div class="card-header">
						<h3 class="card-title">Documents</h3>
						<div class="card-tools">
							<div class="input-group input-group-sm" style="">
								<a href="" data-toggle="modal" data-target="#myModal"
									class="btn btn-primary add_new_customer_document" alt="<?= $_REQUEST['id']; ?>"
									data-customer_name="<? echo ucfirst($client_row['customer_name']); ?>"
									style="float:right;margin-right:5px;margin-bottom:5px;">Add Document</a>
							</div>
						</div>
					</div>
					<div class="card-body table-responsive" style="height: auto;">
						<div class="box box-primary" id="overflow_div" style="height:185px;">
							<!-- Start  records header -->
							<iframe width="100%" height="185px" style="border:0;"
								src="<? echo $config->siteURL; ?>/modules/fileuploads/image-files.php?customer_id=<?= $_REQUEST['id']; ?>&hide_add=1"></iframe>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-12">
				<div id="" class="card">
					<div class="card-header">
						<h3 class="card-title">Quotation</h3>
					</div>
					<?
					$query_quotation = "SELECT * FROM quotation WHERE customer_id = " . $_REQUEST['id'] . " ORDER BY created_on DESC";
					$sql_quotation = mysqli_query($conn, $query_quotation);
					$result_quotation = mysqli_num_rows($sql_quotation);

					$total_no_of_quotation = mysqli_num_rows(mysqli_query($conn, $query_quotation));
					?>
					<div class="card-body table-responsive" style="height: auto;">
						<? if ($result_quotation > 0) { ?>
							<div class="box box-primary" id="overflow_div">
								<!-- Start  records header -->
								<table id="example1" class="table table-bordered table-striped">
									<thead>
										<tr>
											<th>QAF Number</th>
											<th>Customer Name</th>
											<th>Part Number</th>
											<th>Type</th>
											<th>Annual Value</th>
										</tr>
									</thead>
									<tbody>

										<!-- End records header -->

										<?
										while ($row_quotation = mysqli_fetch_array($sql_quotation)) {
											?>
											<!-- Start data -->
											<tr>
												<td class=""><?php echo $row_quotation['quotation_number'] ?></td>
												<td nowrap><?= $gsi->getCustomerById($row_quotation['customer_id']); ?></td>
												<td nowrap><?= ($row_quotation['part_number']); ?></td>
												<td nowrap><?= $gsi->getYesNoById($row_quotation['type']); ?></td>
												<td nowrap><?= ($row_quotation['anuual_value']); ?></td>
											</tr>
											<!-- End data -->
										<? } ?>

									</tbody>
								</table>
							</div>
						<? } else { ?>
							<div class="box box-primary p-15">No Quotation found...</div>
						<? } ?>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>
<style>
	#overflow_div::-webkit-scrollbar {
		width: 5px;
		/* for vertical scrollbars */
		height: 5px;
		/* for horizontal scrollbars */
	}

	#overflow_div::-webkit-scrollbar-track {
		background: #E7E8E9;
	}

	#overflow_div::-webkit-scrollbar-thumb {
		background: #3c8dbc;
	}

	#overflow_div {
		height: 200px;
		overflow-y: auto;
	}

	@media print {
		body {
			visibility: hidden;
		}

		#overflow_div {
			height: auto;
		}

		.printable {
			visibility: visible;
		}

		.printable {
			position: absolute;
			left: 0;
			top: 0;
		}
	}
</style>
<script type="text/javascript">
	$(function () {
		$(".add_new_customer_document").click(function () {
			var customer_name = $(this).attr("data-customer_name");
			$("#myModalLabel").html("Customer Documents  <span style='color:Tomato'>" + customer_name + "</span>");
			var hide_top_left = "&hide_top=1&hide_left=1";
			var customer_id = $(this).attr("alt");
			$('.modal-dialog').css({ "max-width": "76%" });
			$(".modal_iframe").attr("src", "<?= $config->frontEndURL ?>/admin/modules/fileuploads/image-files.php?customer_id=" + customer_id + "&page=view_client_progress&source=o" + hide_top_left);
		});
		$(".close_button").click(function () {
			$(".modal_iframe").attr("src", "");
			location.reload();
		});
	});

</script>