<?php
require_once ('config.php');
require_once('f_logging.php');
$error_message;
$valid = true;
$json_return = array();
$json_return = [];

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




foreach($result->Header as $Heade){
	
	//echo $x= $x+1;
	//echo $Heade;
	$site_cd				= $Heade->site_cd;	
	$rowid 					= $Heade->rowid;
	
	$wko_isp_varchar1 		= $Heade->wko_isp_varchar1;
	$wko_isp_varchar2 		= $Heade->wko_isp_varchar2;
	$wko_isp_varchar3 		= $Heade->wko_isp_varchar3;
	$wko_isp_numeric1 		= $Heade->wko_isp_numeric1;
	
	$wko_isp_datetime1 		= $Heade->wko_isp_datetime1;
	
	$wko_isp_checkbox1 		= $Heade->wko_isp_checkbox1;
	
	$wko_isp_dropdown1 		= $Heade->wko_isp_dropdown1;
	
	$audit_user 			= $Heade->audit_user;
	$audit_date 			= $Heade->audit_date;
	$mbl_audit_user 		= $Heade->mbl_audit_user;
	$mbl_audit_date 		= $Heade->mbl_audit_date;

	$LOGINID 				= $Heade->LOGINID;
	$dvc_id 				= $Heade->dvc_id;
	$sync_step 				= $Heade->sync_step;
	$sync_time 				= $Heade->sync_time;
	$sync_status 			= $Heade->sync_status;
	$sync_url 				= $Heade->sync_url;

		if($sync_status =="online"){

		}else{
			if ($x == 1) {
				f_logging($json,"off_update_work_order_check_list.php",$sync_url,$LOGINID,$sync_step,$sync_time,$site_cd);
				$x ++;
			}
		}

		if ($wko_isp_varchar1 == ""){

            $wko_isp_varchar1 = null;
			
        }else{

            $wko_isp_varchar1 = "".$wko_isp_varchar1."";				
			
		 }	
		
		
        if ($wko_isp_varchar2 == "")
        {
            $wko_isp_varchar2 = null;
        }
        else
        {
            $wko_isp_varchar2 = "".$wko_isp_varchar2."";
			
        }
		
         if ($wko_isp_varchar3 == "")
        {
            $wko_isp_varchar3 = null;
        }
        else
        {
            $wko_isp_varchar3 = "".$wko_isp_varchar3."";
        } 
		
		
		if ($wko_isp_numeric1 == "")
        {
            $wko_isp_numeric1 = null;
        }
        else
        {
            $wko_isp_numeric1 =  $wko_isp_numeric1;			
			
        }
		
	
		if ($wko_isp_datetime1 == "")
        {
            $wko_isp_datetime1 = null;
        }
        else
        {
            $wko_isp_datetime1 =  "".$wko_isp_datetime1."";
			
        }
		

		if ($wko_isp_checkbox1 == false)
        {
            $wko_isp_checkbox1 = "0";
        }
        else
        {
            $wko_isp_checkbox1 =  "".$wko_isp_checkbox1."";
			
        }
		
	
		if ($wko_isp_dropdown1 == "")
        {
            $wko_isp_dropdown1 = null;
        }
        else
        {
            $wko_isp_dropdown1 =  "".$wko_isp_dropdown1."";
			
        }
		
		
		
		
		 $sql = "UPDATE wko_isp SET 
				wko_isp_varchar1 = ?,
				wko_isp_varchar2 = ?,
				wko_isp_varchar3 = ?,
				wko_isp_numeric1 = ?,
				wko_isp_datetime1 = ?,
				wko_isp_checkbox1 = ?,
				wko_isp_dropdown1 = ?,
				audit_user = ?,
				audit_date = ?,
				mbl_audit_user = ?, 
				mbl_audit_date = ? 				
				WHERE RowID = " . $rowid . " 
				And wko_isp.site_cd = '" . $site_cd . "'";
				
				
		$params_wko_isp = array( 	$wko_isp_varchar1,
									$wko_isp_varchar2,
									$wko_isp_varchar3,
									$wko_isp_numeric1, 				
									$wko_isp_datetime1, 			
									$wko_isp_checkbox1, 			
									$wko_isp_dropdown1, 			
									$audit_user,
									$audit_date,
									$mbl_audit_user,
									$mbl_audit_date);
								

        $stmt = sqlsrv_query($conn, $sql,$params_wko_isp);

        if (!$stmt)
        {
            $error_message = "Error updating table (wko_isp) - rowid (".$rowid.")";
            returnError($error_message);
            die(print_r(sqlsrv_errors() , true));
        }
		sqlsrv_free_stmt($stmt);
	
}



if ($valid)
{

	returnData($json_return);
}
sqlsrv_close($conn);
function returnData($json)
{
    $returnData = array(
        'status' => 'SUCCESS',
        'message' => 'Successfully Update',
        'data' => $json
    );

    echo json_encode($returnData);
}

function returnError($error_message)
{
    $json = array();

    $returnData = array(
        'status' => 'ERROR',
        'message' => $error_message,
        'data' => $json
    );

    echo json_encode($returnData);  
    exit();
}





?>
