<?php
require_once('config.php');
require_once('f_send_email_notification.php');

/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}

$insert_ntf = false;
$error_message;
$valid = true;

$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json, true);

$site_cd = $data['site_cd'];
$EmpID = $data['EmpID'];
$EmpName = $data['EmpName'];
$RowID = $data['RowID'];
$dis_Dec = $data['dis_Dec'];
$LOGINID = $data['LOGINID'];



$sql = "SELECT wkr_mst.wkr_mst_wr_status		
		
		FROM 	wkr_mst (NOLOCK),wkr_det (NOLOCK)
		WHERE	wkr_mst.site_cd = '".$site_cd."' 
		AND 	wkr_mst.RowID = '".$RowID."'
		AND		wkr_mst.site_cd = wkr_det.site_cd 
		AND		wkr_mst.RowID = wkr_det.mst_RowID";			
			
		$stmt = sqlsrv_query( $conn, $sql);			
		if( !$stmt ) {
			$error_message = "Error select table (cnt_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
						
				$wkr_mst_wr_status = $row['wkr_mst_wr_status'];						
				 
			}
		} while ( sqlsrv_next_result($stmt));
		sqlsrv_free_stmt( $stmt);	
	
	
	if ($wkr_mst_wr_status === 'W') {
		
		
		$sql = " update wkr_mst SET wkr_mst_wr_status ='D' ,mbl_audit_user =?, mbl_audit_date =getDate() WHERE RowID =?";
		$params_wkr_mst = array($LOGINID,$RowID);		
		$stmt_wkr_mst = sqlsrv_query( $conn, $sql,$params_wkr_mst);	
				
		if( !$stmt_wkr_mst ) {
			$error_message = "Error update table (wkr_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_wkr_mst, SQLSRV_FETCH_ASSOC)) {
			 
			 $emp_det_wo_approval_limit = $row['emp_det_wo_approval_limit'];
			
				 
		   }
		} while ( sqlsrv_next_result($stmt_wkr_mst));
		sqlsrv_free_stmt($stmt_wkr_mst);
		
		
		$sql = " update wkr_det SET wkr_det_reject_by =?, wkr_det_reject_date = getDate(), wkr_det_reject_desc =?, mbl_audit_user =?, mbl_audit_date =getDate() WHERE mst_RowID =?";	
		$params_wkr_det = array($EmpID,$dis_Dec,$LOGINID,$RowID);		
		$stmt_wkr_det = sqlsrv_query( $conn, $sql,$params_wkr_det );	
				
		if( !$stmt_wkr_det ) {
			$error_message = "Error update table (wkr_det)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_wkr_det, SQLSRV_FETCH_ASSOC)) {
			 
			 $emp_det_wo_approval_limit = $row['emp_det_wo_approval_limit'];
			
				 
		   }
		} while ( sqlsrv_next_result($stmt_wkr_det));
		sqlsrv_free_stmt($stmt_wkr_det);
		
		
		$sql = "select 	wkr_mst_wr_no,
						wkr_mst_wr_descs,
						wkr_mst_assetno,
						wkr_mst_work_area 	= COALESCE(wkr_mst_work_area +' : '+ mst_war_desc,''),
						wkr_mst_location 	= COALESCE(wkr_mst_location  +' : '+ ast_lvl_desc,''),	
						wkr_mst_assetlocn  	= COALESCE(wkr_mst_assetlocn +' : '+ ast_loc_desc,''),
						wkr_det_reject_by 	= COALESCE(wkr_det_reject_by +' : '+ emp_mst_name,''),
						wkr_det_reject_date,
						wkr_det_reject_desc,
						wkr_mst_originator		
						
		from   wkr_mst (NOLOCK) 
		
		INNER JOIN		wkr_det (NOLOCK)
		ON				wkr_mst.site_cd  = wkr_det.site_cd
		AND				wkr_mst.rowid	 = wkr_det.mst_rowid

		LEFT 
		OUTER 
		JOIN		mst_war (NOLOCK)
		ON			wkr_mst.site_cd = mst_war.site_cd
		AND			wkr_mst.wkr_mst_work_area = mst_war.mst_war_work_area
						

		LEFT 
		OUTER 
		JOIN		ast_loc (NOLOCK)
		ON			wkr_mst.site_cd = ast_loc.site_cd
		AND			wkr_mst.wkr_mst_assetlocn = ast_loc.ast_loc_ast_loc
						
						
		LEFT 
		OUTER 
		JOIN		ast_lvl (NOLOCK)
		ON			wkr_mst.site_cd = ast_lvl.site_cd
		AND			wkr_mst.wkr_mst_location = ast_lvl.ast_lvl_ast_lvl

		LEFT 
		OUTER 
		JOIN		emp_mst (NOLOCK)						
		ON			wkr_det.site_cd = emp_mst.site_cd
		AND			wkr_det.wkr_det_reject_by = emp_mst.emp_mst_empl_id
		
		WHERE			wkr_mst.site_cd  = '".$site_cd."' 
		AND 			wkr_mst.RowID    = '".$RowID."'";			
		$stmt = sqlsrv_query( $conn, $sql);			
		if( !$stmt ) {
			$error_message = "Error select table (cnt_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
			
				$wkr_mst_wr_no = $row['wkr_mst_wr_no'];				
				$wkr_mst_wr_descs = $row['wkr_mst_wr_descs'];				
				$wkr_mst_assetno = $row['wkr_mst_assetno'];				
				$wkr_mst_work_area = $row['wkr_mst_work_area'];				
				$wkr_mst_location = $row['wkr_mst_location'];				
				$wkr_mst_assetlocn = $row['wkr_mst_assetlocn'];	
				
				$wkr_det_reject_by = $row['wkr_det_reject_by'];				
				$wkr_det_reject_date = $row['wkr_det_reject_date'];				
				$wkr_det_reject_desc = $row['wkr_det_reject_desc'];				
				$wkr_mst_originator = $row['wkr_mst_originator'];				
			
			}
		}while ( sqlsrv_next_result($stmt));
		 sqlsrv_free_stmt( $stmt);
		 
		 
		 
		 $sql = "select 		emp_det_email_id						
				from   			emp_mst (NOLOCK) 
				
				INNER JOIN		emp_det (NOLOCK)
				ON				emp_mst.site_cd  = emp_det.site_cd
				AND				emp_mst.rowid	 = emp_det.mst_rowid

				WHERE			emp_mst.site_cd  = '".$site_cd."' 
				AND 			emp_mst.emp_mst_empl_id    = '".$wkr_mst_originator."'";			
				$stmt = sqlsrv_query( $conn, $sql);			
				if( !$stmt ) {
					$error_message = "Error select table (emp_mst)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}
				do {
					while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
					
						$emp_det_email_id = $row['emp_det_email_id'];				
											
					}
				}while ( sqlsrv_next_result($stmt));
				 sqlsrv_free_stmt( $stmt);
		
		
		$body = '<!DOCTYPE html>
					<html lang="en">
						<head>
							<meta charset="UTF-8">
							<meta name="viewport" content="width=device-width, initial-scale=1.0">
							<title>Work Request Disapproval</title>
							<style>
								/* Reset styles for better compatibility */
								body, table, td, a {
									-webkit-text-size-adjust: 100%;
									-ms-text-size-adjust: 100%;
								}
								table, td {
									mso-table-lspace: 0pt;
									mso-table-rspace: 0pt;
								}
								img {
									-ms-interpolation-mode: bicubic;
								}
								/* Style adjustments */
								body {
									margin: 0;
									padding: 0;
									width: 100%;
									background-color: #f4f7fa;
									font-family: Arial, sans-serif;
									color: #333;
								}
								.container {
									width: 100%;
									max-width: 600px;
									margin: 20px auto;
									background-color: #ffffff;
									border-radius: 8px;
									overflow: hidden;
								}
								.header, .footer {
									background-color: #d9534f;
									color: #ffffff;
									padding: 20px;
									text-align: center;
								}
								.header h1 {
									font-size: 24px;
									margin: 0;
								}
								.card {
									background-color: #ffffff;
									margin: 20px;
									padding: 20px;
									border: 1px solid #e3e3e3;
									border-radius: 8px;
									box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
								}
								.card h2 {
									font-size: 18px;
									color: #d9534f;
									margin: 0 0 10px;
								}
								.content p {
									font-size: 16px;
									margin: 0 0 10px;
								}
								.table {
									width: 100%;
									border-collapse: collapse;
									margin: 10px 0 0;
								}
								.table th, .table td {
									padding: 8px 12px;
									text-align: left;
									border: 1px solid #e3e3e3;
								}
								.table th {
									background-color: #fbe5e5;
									font-weight: bold;
									color: #d9534f;
								}
								.footer a {
									color: #ffffff;
									text-decoration: underline;
								}
							</style>
						</head>
						<body>
							<table role="presentation" cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<td align="center">
										<table class="container" width="100%" cellpadding="0" cellspacing="0">
											<!-- Header Section -->
											<tr>
												<td class="header">
													<h1>Work Request Disapproval</h1>
													<p>We regret to inform you that your request has been disapproved.</p>
												</td>
											</tr>

											<!-- Rejection Summary Card -->
											<tr>
												<td>
													<div class="card">
														<h2>Rejection Details</h2>
														<p><strong>Rejected By:</strong> '.($wkr_det_reject_by).'</p>
														<p><strong>Reject Date:</strong> '.($wkr_det_reject_date->format('Y-m-d H:i:s')).'</p>
														<p><strong>Reject Reason:</strong> '.($wkr_det_reject_desc).'</p>
													</div>
												</td>
											</tr>

											<!-- Work Request Details Card -->
											<tr>
												<td>
													<div class="card">
														<h2>Work Request Details</h2>
														<table class="table">
															<tr>
																<th>WR Number</th>
																<td>'.($wkr_mst_wr_no).'</td>
															</tr>
															 <tr>
																<th>Work Requested</th>
																<td>'.($wkr_mst_wr_descs).'</td>
															</tr>
															<tr>
																<th>Asset Number</th>
																<td>'.($wkr_mst_assetno).'</td>
															</tr>
															<tr>
																<th>Work Area</th>
																<td>'.($wkr_mst_work_area).'</td>
															</tr>
															<tr>
																<th>Level</th>
																<td>'.($wkr_mst_location).'</td>
															</tr>
															<tr>
																<th>Asset Location</th>
																<td>'.($wkr_mst_assetlocn).'</td>
															</tr>
														</table>
													</div>
												</td>
											</tr>

											<!-- Footer Section -->
											<tr>
												<td class="footer">
													<p>If you need further assistance, please reach out to us at 
													   <a href="mailto:support@example.com">support@example.com</a>.
													</p>
													<p>&copy; 2024 Your Company Name</p>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
						</body>
					</html>';



					if($emp_det_email_id){
						
							 $title  = 'CMMS Work Request ' .$wkr_mst_wr_no. ' is disapproved ';
							 $eml_log_from='CMMS ADMIN';
							 $eml_log_to_email = array();
							 $eml_log_to_email[] = $emp_det_email_id;
							 $audit_user    =  $LOGINID;
							 $module   =     'Work Request';
							 $action   =     'Work Request Disapproved';
							 $trigger_from   =     NULL;
					 
							 send_email($site_cd,$eml_log_from,$eml_log_to_email,$title,$body,$audit_user,$wkr_mst_wr_no,$module,$action,$trigger_from,NULL,NULL,NULL);
					}
		
		
    
	} else {
		
		$sql = "SELECT 	wkr_det_wo FROM wkr_det (NOLOCK) WHERE	wkr_det.site_cd = '".$site_cd."' AND wkr_det.mst_RowID = '".$RowID."'";			
		$stmt = sqlsrv_query( $conn, $sql);			
		if( !$stmt ) {
			$error_message = "Error select table (cnt_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
			
			$wkr_det_wo = $row['wkr_det_wo'];				
			
			}
		}while ( sqlsrv_next_result($stmt));
		 sqlsrv_free_stmt( $stmt);
		 sqlsrv_rollback( $conn );
		 
		 if ($wkr_mst_wr_status === 'A') {
			$error_message = 'Work Request is already Approved Work Order No: ' .$wkr_det_wo. '';
			returnalert($error_message);
		 }else if ($wkr_mst_wr_status === 'D') {
			 $error_message = 'Work Request is already Disapproved';
			 returnalert($error_message);
		 }
		
	}


if($stmt_wkr_mst && $stmt_wkr_det){
	
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData();
}else{
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}
		
 
 
function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Work request disapproved');
	echo json_encode($returnData);
}

function returnError($error_message){	
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message);	
	echo json_encode($returnData);
	exit();
}

function returnalert($error_message){	
	
	$returnData = array(
	'status' => 'alert',
	'message' => $error_message);	
	echo json_encode($returnData);
	exit();
}






 
 
?>