<?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'] . " ";
}
$supplier_id = utility::app_crypt($_REQUEST['supplier_id'], 'd');
if ($_REQUEST['id'] != $supplier_id) {
	die("Invaild URL");
}
$restricted_groups = [12, 13, 14];
$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 = "supplier_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);
$supplier_row = mysqli_fetch_assoc($rs);
$supplier_row = utility::validate_crypt_data($supplier_row);
?>
<style>
	.form-group .control-label {
		padding-top: 0px;
	}

	.content {
		min-height: 0px;
	}

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

	#example1 {
		border-collapse: collapse;
	}

	#example1 th,
	#example1 td {
		border: none;
		padding: 8px;
	}
</style>
<section class="content-header">
	<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;">
						Supplier Details - <span style='color:#ed5c5c;'><?= $supplier_row['supplier_name']; ?></span>
					</h1>
				</div>
				<?
				$query_string = (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
				$query_string = str_ireplace("page=view_supplier_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_supplier_progress")) {
					?>
					<a href="index?page=add&from=view_supplier_progress&action=edit&supplier_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>
				<? } ?>
				<?php } ?>
			</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: 300px;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="Supplier Name" class="col-sm-4 control-label">Supplier Name</label>
										<div class="col-sm-8">
											<? echo ucfirst($supplier_row['supplier_name']); ?>
										</div>
									</div>
									<div class="form-group col-lg-12 d-flex">
										<label for="Supplier Code" class="col-sm-4 control-label">Supplier Code</label>
										<div class="col-sm-8">
											<? echo $supplier_row['supplier_code']; ?>
										</div>
									</div>
									<div class="form-group col-lg-12 d-flex">
										<label for="Address" class="col-sm-4 control-label">Address</label>
										<div class="col-sm-8">
											<?php
											if (isset($supplier_row["address_1"]) && $supplier_row["address_1"] != "") {
												echo $supplier_row["address_1"];
											}
											if (isset($supplier_row["address_2"]) && $supplier_row["address_2"] != "") {
												echo " " . $supplier_row["address_2"];
											}
											if (isset($supplier_row["address_3"]) && $supplier_row["address_3"] != "") {
												echo " " . $supplier_row["address_3"];
											}
											if (isset($supplier_row["supplier_postcode"]) && $supplier_row["supplier_postcode"] != "") {
												echo " " . $supplier_row["supplier_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: 300px;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 = "supplier_documents/";

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

													if ($ext == "jpg" || $ext == "jpeg" || $ext == "png") {
														$image_path = '<a href="../' . $dir . $supplier_row['document_upload'] . '" target="_blank"><img class="primary text-primary" style="width:40px;border:1px solid #777" src="../' . $dir . $supplier_row['document_upload'] . '"></a>';
													} else if ($ext == "pdf") {
														$image_path = '<a href="../' . $dir . $supplier_row['document_upload'] . '" target="_blank"><span class="primary text-primary" title="' . $supplier_row['document_upload'] . '">' . utility::ellipsis($supplier_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;">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="Tax Code" class="col-sm-4 control-label">Tax Code</label>
											<div class="col-sm-8">
												<? echo $gsi->getTaxCodeListById($supplier_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($supplier_row["default_country_code"]); ?>
											</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: 300px;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;">Accreditations</h4>
									<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
									<!--<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">-->
									<?

									$accreditations_query = "SELECT * FROM supplier_accreditation_mapping WHERE supplier_id='" . $supplier_row['id'] . "' ";
									$supplier_accreditation_sql = mysqli_query($conn, $accreditations_query);
									$supplier_accreditation_result = mysqli_num_rows($supplier_accreditation_sql);

									$j = 0;

									?>
									<div class="table">
										<table id="example1" class="table">
											<thead>
												<tr>
													<!--<th>#</th>-->
													<th class="col-lg-6">Accreditation Name</th>
													<th class="col-lg-6">Expiry Date</th>
												</tr>
											</thead>
											<tbody>
												<? while ($mapp_row = mysqli_fetch_array($supplier_accreditation_sql)) {
													?>
													<tr>
														<td><?php echo ucfirst($mapp_row["accreditation_name"]); ?>
														</td>
														<td><?php echo utility::getFormatedDate($mapp_row['accreditations_expirey_date']) ?>
														</td>
													</tr>
												<? } ?>
											</tbody>
										</table>
									</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>
										<!-- Contact 1 -->
										<?php if (!empty($supplier_row["contact_1_first_name"]) || !empty($supplier_row["contact_1_surname"]) || !empty($supplier_row["contact_1_email_address"]) || !empty($supplier_row["contact_1_phone_no"])): ?>
											<tr>
												<td><?php echo Keycode::getKeycodeById($supplier_row["contact_1_title"]); ?>
												</td>
												<td><?php echo ucfirst($supplier_row['contact_1_first_name']); ?></td>
												<td><?php echo ucfirst($supplier_row['contact_1_surname']); ?></td>
												<td><?php echo $supplier_row['contact_1_email_address']; ?></td>
												<td><?php echo $supplier_row['contact_1_phone_no']; ?></td>
											</tr>
										<?php endif; ?>

										<!-- Contact 2 -->
										<?php if (!empty($supplier_row["contact_2_first_name"]) || !empty($supplier_row["contact_2_surname"]) || !empty($supplier_row["contact_2_email_address"]) || !empty($supplier_row["contact_2_phone_no"])): ?>
											<tr>
												<td><?php echo Keycode::getKeycodeById($supplier_row["contact_2_title"]); ?>
												</td>
												<td><?php echo ucfirst($supplier_row['contact_2_first_name']); ?></td>
												<td><?php echo ucfirst($supplier_row['contact_2_surname']); ?></td>
												<td><?php echo $supplier_row['contact_2_email_address']; ?></td>
												<td><?php echo $supplier_row['contact_2_phone_no']; ?></td>
											</tr>
										<?php endif; ?>

										<!-- Contact 3 -->
										<?php if (!empty($supplier_row["contact_3_first_name"]) || !empty($supplier_row["contact_3_surname"]) || !empty($supplier_row["contact_3_email_address"]) || !empty($supplier_row["contact_3_phone_no"])): ?>
											<tr>
												<td><?php echo Keycode::getKeycodeById($supplier_row["contact_3_title"]); ?>
												</td>
												<td><?php echo ucfirst($supplier_row['contact_3_first_name']); ?></td>
												<td><?php echo ucfirst($supplier_row['contact_3_surname']); ?></td>
												<td><?php echo $supplier_row['contact_3_email_address']; ?></td>
												<td><?php echo $supplier_row['contact_3_phone_no']; ?></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_org_document" alt="<?= $_REQUEST['id']; ?>"
									data-supplier_name="<? echo ucfirst($supplier_row['supplier_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?supplier_id=<?= $_REQUEST['id']; ?>&hide_add=1"></iframe>
						</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_org_document").click(function () {
			var supplier_name = $(this).attr("data-supplier_name");
			$("#myModalLabel").html("Supplier Documents  <span style='color:Tomato'>" + supplier_name + "</span>");
			var hide_top_left = "&hide_top=1&hide_left=1";
			var supplier_id = $(this).attr("alt");
			$('.modal-dialog').css({ "max-width": "76%" });
			$(".modal_iframe").attr("src", "<?= $config->frontEndURL ?>/admin/modules/fileuploads/image-files.php?supplier_id=" + supplier_id + "&page=view_client_progress&source=o" + hide_top_left);
		});
		$(".close_button").click(function () {
			$(".modal_iframe").attr("src", "");
			location.reload();
		});
	});
</script>