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

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


if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	/* $json = file_get_contents('php://input');
	echo $json;
	$result = json_decode($json, true); */

    $json = $_REQUEST["json"];
	//echo $json;
    $json = json_decode($json);
	
	

    $cnt_checklist = count($json->data->wko_isp);
	//echo$cnt_checklist;
	// $i=0;
	// foreach( $json->data->wko_isp as $row){
		// $echo $row[i]->site_cd;
		// $i+=1;
	// }
    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_asset_no 	= $json->data->wko_isp[$x]->wko_isp_asset_no;
        $wko_isp_job_cd 	= $json->data->wko_isp[$x]->wko_isp_job_cd;
        $wko_isp_job_desc 	= $json->data->wko_isp[$x]->wko_isp_job_desc;
        $wko_isp_sec_no 	= $json->data->wko_isp[$x]->wko_isp_sec_no;
        $wko_isp_sec_desc 	= $json->data->wko_isp[$x]->wko_isp_sec_desc;
        $wko_isp_stp_no 	= $json->data->wko_isp[$x]->wko_isp_stp_no;
        $wko_isp_stp_desc 	= $json->data->wko_isp[$x]->wko_isp_stp_desc;
        $wko_isp_stp_datatype = $json->data->wko_isp[$x]->wko_isp_stp_datatype;
        $wko_isp_stp_rowid 	= $json->data->wko_isp[$x]->wko_isp_stp_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_numeric2 	= $json->data->wko_isp[$x]->wko_isp_numeric2;
        $wko_isp_numeric3 	= $json->data->wko_isp[$x]->wko_isp_numeric3;
        $wko_isp_datetime1 	= $json->data->wko_isp[$x]->wko_isp_datetime1;
        $wko_isp_datetime2 	= $json->data->wko_isp[$x]->wko_isp_datetime2;
        $wko_isp_datetime3 	= $json->data->wko_isp[$x]->wko_isp_datetime3;
        $wko_isp_checkbox1 	= $json->data->wko_isp[$x]->wko_isp_checkbox1;
        $wko_isp_checkbox2 	= $json->data->wko_isp[$x]->wko_isp_checkbox2;
        $wko_isp_checkbox3 	= $json->data->wko_isp[$x]->wko_isp_checkbox3;
        $wko_isp_dropdown1 	= $json->data->wko_isp[$x]->wko_isp_dropdown1;
        $wko_isp_dropdown2 	= $json->data->wko_isp[$x]->wko_isp_dropdown2;
        $wko_isp_dropdown3 	= $json->data->wko_isp[$x]->wko_isp_dropdown3;
		$audit_user 		= $json->data->wko_isp[$x]->audit_user;
		$audit_date 		= $json->data->wko_isp[$x]->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_numeric2 == "")
        {
            $wko_isp_numeric2 = "null";
        }
        else
        {
            $wko_isp_numeric2 = $wko_isp_numeric2;
        }
        if ($wko_isp_numeric3 == "")
        {
            $wko_isp_numeric3 = "null";
        }
        else
        {
            $wko_isp_numeric3 = $wko_isp_numeric3;
        }
		
		
		
		if ($wko_isp_datetime1 == "")
        {
            $wko_isp_datetime1 = "null";
        }
        else
        {
            $wko_isp_datetime1 =  "'".$wko_isp_datetime1."'";
			
        }
		
		
        if ($wko_isp_datetime2 == "")
        {
            $wko_isp_datetime2 = "null";
        }
        else
        {
            $wko_isp_datetime2 = "'".$wko_isp_datetime2."'";
        }
        if ($wko_isp_datetime3 == "")
        {
            $wko_isp_datetime3 = "null";
        }
        else
        {
            $wko_isp_datetime3 = "'".$wko_isp_datetime3."'";
        }
		
		
		if ($wko_isp_checkbox1 == false)
        {
            $wko_isp_checkbox1 = "null";
        }
        else
        {
            $wko_isp_checkbox1 =  "'".$wko_isp_checkbox1."'";
			
        }
		
		
        if ($wko_isp_checkbox2 == false)
        {
            $wko_isp_checkbox2 = "null";
        }
        else
        {
            $wko_isp_checkbox2 = "'".$wko_isp_checkbox2."'";
        }
        if ($wko_isp_checkbox3 == false)
        {
            $wko_isp_checkbox3 = "null";
        }
        else
        {
            $wko_isp_checkbox3 = "'".$wko_isp_checkbox3."'";
        }
		
		
		if ($wko_isp_dropdown1 == "")
        {
            $wko_isp_dropdown1 = "null";
        }
        else
        {
            $wko_isp_dropdown1 =  "'".$wko_isp_dropdown1."'";
			
        }
		
		//echo 'Befoure wko_isp_dropdown1'.$wko_isp_varchar3 ;
        if ($wko_isp_dropdown2 == "")
        {
            $wko_isp_dropdown2 = "null";
        }
        else
        {
            $wko_isp_dropdown2 = "'".$wko_isp_dropdown2."'";
        }
        if ($wko_isp_dropdown3 == "")
        {
            $wko_isp_dropdown3 = "null";
        }
        else
        {
            $wko_isp_dropdown3 = "'".$wko_isp_dropdown3."'";
        }

        $sql = "UPDATE wko_isp SET 
				wko_isp_varchar1 = " . $wko_isp_varchar1 . ",
				wko_isp_varchar2 = " . $wko_isp_varchar2 . ",
				wko_isp_varchar3 = " . $wko_isp_varchar3 . ",
				wko_isp_numeric1 = " . $wko_isp_numeric1 . ",
				wko_isp_numeric2 = " . $wko_isp_numeric2 . ",
				wko_isp_numeric3 = " . $wko_isp_numeric3 . ",
				wko_isp_datetime1 = " . $wko_isp_datetime1 . ",
				wko_isp_datetime2 = " . $wko_isp_datetime2 . ",
				wko_isp_datetime3 = " . $wko_isp_datetime3 . ",
				wko_isp_checkbox1 = " . $wko_isp_checkbox1 . ",
				wko_isp_checkbox2 = " . $wko_isp_checkbox2 . ",
				wko_isp_checkbox3 = " . $wko_isp_checkbox3 . ",
				wko_isp_dropdown1 = " . $wko_isp_dropdown1 . ",
				wko_isp_dropdown2 = " . $wko_isp_dropdown2 . ",
				wko_isp_dropdown3 = " . $wko_isp_dropdown3 . ",
				audit_user = '" .$audit_user. "',
				audit_date = '" .$audit_date. "' WHERE RowID = " . $rowid . " And wko_isp.site_cd = '" . $site_cd . "'";

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

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

?>
