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

$error_message;
$valid = true;
$json_return = array();
$json_return = [];
$json= array();

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	

    $json = $_REQUEST["json"];
    $json = json_decode($json);
	
	$cnt_checklist = count($json->data->wko_isp);	
	
    for ($x = 0; $x < $cnt_checklist; $x++)
    {
		//echo $x;
		//echo $json->data->wko_isp[$x]->rowid;
		//echo $json->data->wko_isp[$x]->wko_isp_varchar1;
		$x = $x + 0;
        $site_cd 			= $json->data->wko_isp[$x]->site_cd;
        $rowid 				= $json->data->wko_isp[$x]->rowid;
        $mst_rowid 			= $json->data->wko_isp[$x]->mst_rowid;
        
        $wko_isp_varchar1 	= $json->data->wko_isp[$x]->wko_isp_varchar1;
        $wko_isp_varchar2 	= $json->data->wko_isp[$x]->wko_isp_varchar2;
        $wko_isp_varchar3 	= $json->data->wko_isp[$x]->wko_isp_varchar3;
		
        $wko_isp_numeric1 	= $json->data->wko_isp[$x]->wko_isp_numeric1;
        
		
        $wko_isp_datetime1 	= $json->data->wko_isp[$x]->wko_isp_datetime1;
        
		
        $wko_isp_checkbox1 	= $json->data->wko_isp[$x]->wko_isp_checkbox1;
        
		
        $wko_isp_dropdown1 	= $json->data->wko_isp[$x]->wko_isp_dropdown1;
        
		
		$audit_user 		= $json->data->wko_isp[$x]->audit_user;
		$audit_date 		= $json->data->wko_isp[$x]->audit_date;
		$mbl_audit_user 	= $json->data->wko_isp[$x]->mbl_audit_user;
		$mbl_audit_date 	= $json->data->wko_isp[$x]->mbl_audit_date;
		
		
		
		
		
        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));
        }

        // $json = array();

        // do
        // {
            // while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
            // {
                // $json[] = $row;
            // }
        // }
        // while (sqlsrv_next_result($stmt));
    }
}

$rows_affected = sqlsrv_rows_affected($stmt);
$json_return['row_update'] = $rows_affected;
$json_return['action'] = "UPDATE";

if ($valid)
{
	sqlsrv_free_stmt($stmt);
	sqlsrv_close($conn);

	returnData($json_return);
}

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();
}

?>
