<?php
session_start();
require_once("../../includes/display_internal_user_dryout.inc.php");
require_once("../../includes/functions_internal_user_dryout.inc.php");
$obj = new data();
$connect = $obj->funConnect();
$display = new display();
$display->logout_iu();
$pagename = basename($_SERVER['PHP_SELF']);
$msg = NULL;
if (isset($_REQUEST['selBType']) && $_REQUEST['selBType'] != "") {
	$selBType = $_REQUEST['selBType'];
} else if (isset($_SESSION['selBType'])) {
	$selBType = $_SESSION['selBType'];
} else {
	$selBType = '';
}

if (isset($_POST['id']) && isset($_POST['val']) && isset($_POST['iden'])) {


	$duedate = date("Y-m-d", strtotime(trim(addslashes($_POST['val']))));


	if ($_POST['iden'] == 'ffispostfix') {

		$sql1 = "select * from ffi_track_master where COMID='" . $_POST['id'] . "'";
		$res1 = mysql_query($sql1) or die($sql1);
		$rec1 = mysql_num_rows($res1);

		if ($rec1 == 0) {

			$sql6 = "insert into ffi_track_master(POST_FIX, COMID,Iden) values ('" . $_POST['val'] . "', '" . $_POST['id'] . "','VC')";
			$insert_detials = mysql_query($sql6);

			if ($insert_detials) {
				echo true;
				return;
			}
		} else {

			$change_detials = mysql_query("UPDATE ffi_track_master SET POST_FIX='" . $_POST['val'] . "',Iden='VC' WHERE COMID='" . $_POST['id'] . "'");

			if ($change_detials) {
				echo true;
				return;
			}
		}
	} else if ($_POST['iden'] == 'lpstatuslaytime') {

		$sql1 = "select * from ffi_track_master where COMID='" . $_POST['id'] . "'";
		$res1 = mysql_query($sql1) or die($sql1);
		$rec1 = mysql_num_rows($res1);

		if ($rec1 == 0) {

			$sql6 = "insert into ffi_track_master(LP_STATUS, COMID, Iden) values ('" . $_POST['val'] . "', '" . $_POST['id'] . "','VC')";
			$insert_detials = mysql_query($sql6);

			if ($insert_detials) {
				echo true;
				return;
			}
		} else {
			$change_detials = mysql_query("UPDATE ffi_track_master SET LP_STATUS='" . $_POST['val'] . "',Iden='VC' WHERE COMID='" . $_POST['id'] . "'");

			if ($change_detials) {
				echo true;
				return;
			}
		}
	} else if ($_POST['iden'] == 'dpstatuslaytime') {

		$sql1 = "select * from ffi_track_master where COMID='" . $_POST['id'] . "'";
		$res1 = mysql_query($sql1) or die($sql1);
		$rec1 = mysql_num_rows($res1);

		if ($rec1 == 0) {

			$sql6 = "insert into ffi_track_master(DP_STATUS, COMID, Iden) values ('" . $_POST['val'] . "', '" . $_POST['id'] . "','VC')";
			$insert_detials = mysql_query($sql6);

			if ($insert_detials) {
				echo true;
				return;
			}
		} else {

			$change_detials = mysql_query("UPDATE ffi_track_master SET DP_STATUS='" . $_POST['val'] . "',Iden='VC' WHERE COMID='" . $_POST['id'] . "'");

			if ($change_detials) {
				echo true;
				return;
			}
		}
	} else if ($_POST['iden'] == 'ffistatus') {

		$sql1 = "select * from ffi_track_master where COMID='" . $_POST['id'] . "'";
		$res1 = mysql_query($sql1) or die($sql1);
		$rec1 = mysql_num_rows($res1);

		if ($rec1 == 0) {

			$sql6 = "insert into ffi_track_master(FFI_STATUS, COMID,Iden) values ('" . $_POST['val'] . "', '" . $_POST['id'] . "','VC')";
			$insert_detials = mysql_query($sql6);

			if ($insert_detials) {
				echo true;
				return;
			}
		} else {

			$change_detials = mysql_query("UPDATE ffi_track_master SET FFI_STATUS='" . $_POST['val'] . "',Iden='VC' WHERE COMID='" . $_POST['id'] . "'");

			if ($change_detials) {
				echo true;
				return;
			}
		}
	} else if ($_POST['iden'] == 'FFISentDate') {

		$sql1 = "select * from ffi_track_master where COMID='" . $_POST['id'] . "'";
		$res1 = mysql_query($sql1) or die($sql1);
		$rec1 = mysql_num_rows($res1);

		if ($rec1 == 0) {

			$sql6 = "insert into ffi_track_master(FFI_SENT_DATE, COMID,Iden) values ('" . date("Y-m-d", strtotime(trim(addslashes($_POST['val'])))) . "', '" . $_POST['id'] . "','VC')";
			$insert_detials = mysql_query($sql6);

			if ($insert_detials) {
				echo true;
				return;
			}
		} else {

			$change_detials = mysql_query("UPDATE ffi_track_master SET FFI_SENT_DATE='" . date("Y-m-d", strtotime(trim(addslashes($_POST['val'])))) . "',Iden='VC' WHERE COMID='" . $_POST['id'] . "'");

			if ($change_detials) {
				echo true;
				return;
			}
		}
	} else if ($_POST['iden'] == 'add_rem') {

		$sql1 = "select * from ffi_track_master where COMID='" . $_POST['id'] . "'";
		$res1 = mysql_query($sql1) or die($sql1);
		$rec1 = mysql_num_rows($res1);

		if ($rec1 == 0) {

			$sql6 = "insert into ffi_track_master(ADD_REM, COMID,Iden) values ('" . $_POST['val'] . "', '" . $_POST['id'] . "','VC')";
			$insert_detials = mysql_query($sql6);

			if ($insert_detials) {
				echo true;
				return;
			}
		} else {

			$change_detials = mysql_query("UPDATE ffi_track_master SET ADD_REM='" . $_POST['val'] . "',Iden='VC' WHERE COMID='" . $_POST['id'] . "'");

			if ($change_detials) {
				echo true;
				return;
			}
		}
	}


	echo false;
	return;
}


?>
<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title><?php $display->title(); ?></title>
	<?php $display->favicon(); ?>
	<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
	<?php $display->css(); ?>
	<link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
	<link href="https://cdn.datatables.net/buttons/2.2.2/css/buttons.dataTables.min.css" rel="stylesheet" type="text/css" />

	<!-- ldj  -->
	<!-- <link href="../../css/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" /> -->
	<!-- end ldj  -->
	<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
	<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	<!--[if lt IE 9]>
  <script src="../js/html5shiv.js"></script>
  <script src="../js/respond.min.js"></script>
<![endif]-->
</head>

<body class="skin-blue fixed">
	<!-- header logo: style can be found in header.less -->
	<?php $display->header_tag(); ?>
	<div class="wrapper row-offcanvas row-offcanvas-left">
		<?php $display->leftmenu(7, 2); ?>
		<aside class="right-side">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>
					<a href="#" class="navbar-btn sidebar-toggle" data-toggle="offcanvas" role="button" style="margin:0px;"><i class="fa fa-bars" style="font-size:20px;"></i></a>&nbsp;&nbsp;Reports&nbsp;&nbsp;&nbsp;&nbsp;<small>
						<div class="headline text-center" id="time" style="color:#000; font-weight:bold; font-size:12px;"></div>
					</small>
				</h1>
				<ol class="breadcrumb">
					<li><a href="../"><i class="fa fa-home"></i> Home</a></li>
					<li>Reports</li>
					<li>Chartering</li>
					<li class="active">FFI Tracker (VC IN)</li>
				</ol>
			</section>

			<!-- Main content -->
			<section class="content">
				<!--   content put here..................-->
				<div class="box box-primary">
					<h3 style=" text-align:center;">FFI Tracker (VC IN)</h3>
					<form name="frm1" id="frm1" enctype="multipart/form-data" action="<?php echo $pagename; ?>" />
					<div class="row invoice-info" style="margin-left:10px;">
						<div class="col-sm-2 invoice-col">
							Year<span style="font-size:10px; font-style:italic;"></span>
							<select name="selYear" id="selYear" class="form-control">
								<?php echo $obj->getYearList(); ?>
							</select>
						</div>

						<div class="col-sm-2 invoice-col">
							Team<span style="font-size:10px; font-style:italic;"></span>
							<select name="selTeam" id="selTeam" class="form-control">
								<?php echo $obj->getCountyListForChartering(); ?>
							</select>
						</div>

						<!-- <div class="col-sm-2 invoice-col">
						FFI STATUS<span style="font-size:10px; font-style:italic;"></span>
						<select name="selTeam" id="selTeam" class="form-control" >
                              
                                  </select>          
						</div> -->
						<!-- <div class="col-xs-2">
                            Business Type
                            <address>
                             <select name="selBType" id="selBType" class="form-control">
                               
                              </select>  
                           </address>                
                        </div>/.col -->
						<!-- <div class="col-xs-2">
                           COA/Spot
                            <address>
                             <select name="selCOASpot" id="selCOASpot" class="form-control">
                               
                              </select>  
                           </address>                
                        </div>/.col -->
						<div class="col-sm-2 invoice-col">
							&nbsp;
							<address>
								<button type="button" class="btn btn-primary btn-flat" onClick="getData();">Search</button>
							</address>
						</div><!-- /.col -->
					</div>
					<!-- <div align="right">
                        <a href="#" title="Pdf" onClick="getExcel();"><button class="btn btn-default" style="margin-right: 5px;" type="button"><i class="fa fa-download"></i>Generate Excel</button></a>
					</div> -->
					<div style="height:10px;">
						<input name="txttblid" id="txttblid" type="hidden" value="" />
						<input name="txttblstatus" id="txttblstatus" type="hidden" value="" />
						<input type="hidden" name="action" value="submit" />
					</div>

					<div id="DivOwner" class="box-body table-responsive" style="overflow:auto;">
						<table id="sf_list" class="table table-bordered table-striped dataTable">
							<thead>
								<tr>
									<th>Sno.</th>
									<th>Fixture No.</th>
									<th>Vessel</th>
									<th>CP Date</th>
									<th>Charterers</th>
									<th>Brokers</th>

									<th>Type</th>

									<th>CHTG TEAM</th>
									<th>CHTG PIC</th>
									<th>OPC PIC</th>


									<th>Postfix PIC</th>

									<th>Hire To</th>

									<th>Loadport Name</th>
									<th>Nett Loadport (s) Amount</th>
									<th>Laytime Status Loadport</th>

									<th>Disport Name</th>
									<th>Nett Disport (s) Amount</th>
									<th>Laytime Status Disport</th>
									<th>FINAL FREIGHT INVOICE Y/N?</th>
									<th>Inv from Dem/Des module Y/N?</th>
									<th>Total FFI Amount</th>
									<th>FFI Sent Date</th>
									<th>Amount Received</th>
									<th>FFI Outstanding</th>

									<th>FFI STATUS</th>
									<th>Final Disport Ops Completed Date</th>
									<th>Overdue Date as per CP</th>
									<th>Overdue</th>
									<th>Remarks</th>

								</tr>

							</thead>
							<tbody>

							</tbody>

						</table>
					</div>
					</form>
				</div>
				<!--   content ends here..................-->
			</section><!-- /.content -->
		</aside><!-- /.right-side -->

	</div><!-- ./wrapper -->
	<?php $display->footer(); ?>
	<?php $display->js(); ?>
	<!-- <script src="../../js/plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
<script src="../../js/plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>-->



	<!-- <script src="../../js/timer.js" type="text/javascript"></script>
<link href="../../css/jquery.alerts.css" rel="stylesheet" type="text/css" />
<link href="../../css/datepicker/datepicker.css" rel="stylesheet" type="text/css" />
<script src="../../js/bootstrap-datepicker.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript" src="../../js/jquery.alerts.js"></script>  -->


	<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js" type="text/javascript"></script>

	<!-- new scripts -->
	<script src="https://cdn.datatables.net/buttons/2.2.2/js/dataTables.buttons.min.js" type="text/javascript"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js" type="text/javascript"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js" type="text/javascript"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js" type="text/javascript"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js" type="text/javascript"></script>
	<script src="https://cdn.datatables.net/buttons/2.2.2/js/buttons.html5.min.js" type="text/javascript"></script>
	<script src="https://cdn.datatables.net/buttons/2.2.2/js/buttons.print.min.js" type="text/javascript"></script>
	<link href="../../css/jquery.alerts.css" rel="stylesheet" type="text/css" />
	<link href="../../css/datepicker/datepicker.css" rel="stylesheet" type="text/css" />
	<script src="../../js/bootstrap-datepicker.js" type="text/javascript"></script>
	<script language="JavaScript" type="text/javascript" src="../../js/jquery.alerts.js"></script>
	<script language="JavaScript" type="text/javascript" src="../../js/script.js"></script>


	<script type="text/javascript">
		$(document).ready(function() {

			$("#sf_list").dataTable();

			$('#txtFrom_Date,#txtTo_Date').datepicker({
				format: 'dd-mm-yyyy',
				autoclose: true
			});
		});

		function getData() {
			if ($("#selYear").val() != "") {

				$('#DivOwner').html("");
				$('#DivOwner').html('<span style="text-align:center;display:block;"><img src="../../img/loading.gif" /></span>');
				$.post("options.php?id=157", {
					selYear: "" + $("#selYear").val() + "",
					selTeam: "" + $("#selTeam").val() + "",
					selBType: "" + $("#selBType").val() + "",
					selCOASpot: "" + $("#selCOASpot").val() + ""
				}, function(html) {
					$('#DivOwner').html("");
					$('#DivOwner').html(html);


					// 
					// Create search header
					var new_row = $("<tr class='search-header'/>");
					$('#sf_list thead th').each(function(i) {
						var title = $(this).text();
						var new_th = $('<th style="' + $(this).attr('style') + '" />');

						if (title == "Sno.") {

							$(new_th).append('<input type="text" style="width:30px;" placeholder="' + title + '" data-index="' + i + '"/>');
						} else if (title == "Fixture No.") {

							$(new_th).append('<input type="text" style="width:90px;" placeholder="' + title + '" data-index="' + i + '"/>');
						} else if (title == "Hire Days ") {

							$(new_th).append('<input type="text" style="width:90px;" placeholder="' + title + '" data-index="' + i + '"/>');
						} else if (title == "Hire Amount") {

							$(new_th).append('<input type="text" style="width:90px;" placeholder="' + title + '" data-index="' + i + '"/>');
						} else {
							$(new_th).append('<input type="text" style="width:90px;" placeholder="' + title + '" data-index="' + i + '"/>');

						}



						$(new_row).append(new_th);
					});
					$('#sf_list thead').prepend(new_row);

					var table = $('#sf_list').DataTable({
						"scrollX": true,
						"searching": true,
						paging: true,
						columnDefs: [{
							targets: [10, 14, 17, 24, 28], // Replace with your column index
							orderDataType: 'custom-input',
							type: 'string'
						}, {
							targets: [21], // Replace with your column index
							orderDataType: 'dom-input-date'
						}],
						dom: 'Blfrtip',
						buttons: [{
							extend: 'excel',
							messageTop: "FFI TRACKER VC IN",
							title: "FFI TRACKER VC IN",
							sFileName: "FFI TRACKER VC IN",
							exportOptions: {
								format: {
									body: function(inner, rowidx, colidx, node) {
										if ($(node).children("select").length > 0 || $(node).children("input").length > 0) {
											if ($(node).children("select").length > 0) {

												var selectNode = node.firstElementChild;
												// var txt = selectNode.options[selectNode.selectedIndex].value;

												var txt = selectNode.options[selectNode.selectedIndex].text;
												if (txt == '---Select from list---') {
													// console.log('ehckl');
													var txt = selectNode.options[selectNode.selectedIndex].value

												}

												// console.log(txt)
												return txt;

											} else {

												return $(node).children("input").first().val();

											}

										} else {
											return inner; // the standard cell contents
										}

									}
								}
							}
						}]
					});




					// end ldj lok
					// $("#sf_list").dataTable();

					// Filter event handler
					/*$(table.table().container()).on('keyup', 'thead input', function() {
						table
							.column($(this).data('index'))
							.search(this.value)
							.draw();
					});*/

					$(table.table().container()).on('keyup', 'thead input', function() {
						const colIndex = $(this).data('index');
						const searchVal = this.value.toLowerCase().trim();

						table.rows().every(function() {
							const cell = this.cell(this.index(), colIndex).node();
							let cellVal = '';

							if ($(cell).find('input, select').length > 0) {
								cellVal = $(cell).find('input, select').val().toLowerCase().trim();

								// Handle date format conversion: Y-m-d → d-m-Y
								if ($(cell).find('input[type="date"]').length > 0 && cellVal) {
									const parts = cellVal.split('-'); // [Y, m, d]
									cellVal = `${parts[2]}-${parts[1]}-${parts[0]}`; // d-m-Y
								}
							} else {
								cellVal = $(cell).text().toLowerCase().trim();
							}

							// Store match status on row using jQuery data
							const match = cellVal.includes(searchVal);
							$(this.node()).data('columnMatch', match);
						});

						// Redraw table with only matching rows
						table.draw();
					});


				});

			} else {
				jAlert('Please select Date From & Date To', 'Alert Dialog');
			}
		}

		function getPdf() {
			location.href = 'allPdf.php?id=30&txtFrom_Date=' + $("#txtFrom_Date").val() + '&txtTo_Date=' + $("#txtTo_Date").val() + '&selBType=' + $("#selBType").val();
		}

		function getExcel() {
			location.href = 'allExcel.php?id=25&txtFrom_Date=' + $("#txtFrom_Date").val() + '&txtTo_Date=' + $("#txtTo_Date").val() + '&selBType=' + $("#selBType").val() + '&selCOASpot=' + $("#selCOASpot").val();
		}


		const debounce = (func, wait) => {
			let timeout;

			return function executedFunction(...args) {
				const later = () => {
					clearTimeout(timeout);
					func(...args);
				};

				clearTimeout(timeout);
				timeout = setTimeout(later, wait);
			};
		};


		function updateDetInTab(elx) {

			debounce(function() {

				$.ajax({

					type: "POST",
					data: {
						id: $(elx).data('comid'),
						val: $(elx).val(),
						iden: $(elx).data('iden')
					},
					dataType: "json",

					success: function(result) {
						// when call is sucessfull
						if (result) {
							// $(clx).parent().prev().text(f_year);
							console.log('DATA POSTED');
						}
					},
					error: function(err) {
						// check the err for error details


					}
				});



			}, 500).call();

		}
	</script>

</body>

</html>