<?php
session_start();
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(1); 


header("Content-Type: application/json;charset=utf-8"); 
include('conf.php');
//include("injection.php");
//include("encrypt.php");


mysqli_query($conn,"SET NAMES 'utf8'");
mysqli_query($conn,'SET CHARACTER SET utf8');
mysqli_query($conn,"SET character_set_client=utf8");
mysqli_query($conn,"SET character_set_connection=utf8");
mysqli_query($conn,"SET character_set_database=utf8");
mysqli_query($conn,"SET character_set_results=utf8");
mysqli_query($conn,"SET character_set_server=utf8");
mysqli_query($conn,"set character_set_server='utf8'"); 
	$new_id=0;
	$user_id='1';
	$table='form_options';
	$operation='edit_option';
	$auth_check='user_update';
	$token = null;
	$status='';
	$affected_rows=0;
	$error_arr=[];
	$error_database_arr=[];
	$user_auth='';
	$data_array = array();
	$headers = apache_request_headers();
	
	
	
	//include "auth.php";
	//if($auth_errors==1){
	//   goto to_result;
	//}			
			


	$raw_data = file_get_contents('php://input');
	$post_data = json_decode($raw_data, true);
	
	//$post_data['id']=str_replace(' ','+',$post_data['id']);
	//$post_data['id']=decrypt_dynamic($post_data['id'],$headers['Authorization']);
	$modified=date('Y-m-d H:i:s');

	//استدعاء ملف تخزين اللوجز والحماية من الاسبام	
	include "spam_logs.php";
	
	//استدعاء ملف التحقق من المدخلات وكشف التلاعب
	include 'purifier.php';
	

		
	  //حقول الزامية
	  $post_data["option_text"] = $purifier->purify($post_data["option_text"]);if( $post_data["option_text"]=='' ){array_push($error_arr,'خطأ فى عنوان السؤال');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	  if(filter_var($post_data["option_answer"], FILTER_VALIDATE_INT )=== false || $post_data["option_answer"]==''){array_push($error_arr,'خطأ فى الاجابة');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	  if(filter_var($post_data["ques_id"], FILTER_VALIDATE_INT )=== false || $post_data["ques_id"]==''){array_push($error_arr,'خطأ فى لرقم السؤال');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
 	  if(filter_var($post_data["option_dgree"], FILTER_VALIDATE_FLOAT )=== false || $post_data["option_dgree"]==''){array_push($error_arr,'خطأ فى حقل درجة السؤال');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
 

	  
	  //حقول غير الزامية
	  
	  $post_data["option_images"] = $purifier->purify($post_data["option_images"]);
	  if(filter_var($post_data["option_conditions"], FILTER_SANITIZE_URL )=== false && $post_data["option_conditions"]!='' ){array_push($error_arr,'خطأ فى الفيديوهات');$status='not_done';$message='خطأ فى الشروط';goto to_result;}

	 
	 if(count($error_arr) == 0){
		  //echo 'validatation success';
		  
		  
		  $query = mysqli_prepare(
			$conn,
			"Update `$table` SET 
			`option_text` = ?  ,`option_dgree` = ?  ,`conditions` = ?  , `modified` = ? , modified_user=? 
			 where id = ?");
		
			mysqli_stmt_bind_param($query,'sissii',
			$post_data['option_text'],$post_data['option_dgree'],$post_data['conditions'],$modified,$user_id,$post_data['option_id']
			);
			
			$run=mysqli_stmt_execute($query);
			 
			 if(mysqli_error($conn)!=''){
				 $error_database_arr['err']=mysqli_error($conn);
				 $error_database_arr['err_num']= mysqli_errno($conn);
				 $error_database_arr['error_stmt']= mysqli_stmt_error($query);
			 }

			$affected_rows=mysqli_affected_rows($conn);

			if(mysqli_stmt_affected_rows($query)){
				
				
				
				
				if($affected_rows>0){
					
					$status='done';
					$message='تم الارسال بنجاح';
					log_request($conn, $new_id, $user_id, $operation, $post_data, 'success');		
					

					
				}else{
					

					if(count($error_database_arr) == 0){
					
						$status='not_done';
						$message='لم يتم ادخال البيانات . ربما تكون مكررة';
					}else{
											
						$status='not_done';
						$message='لم يتم ادخال البيانات . ربما هناك خطأ فى محتوى الحقل <br/>'.$error_database_arr['err'];	
						
					}
				}
			}else{
				
				$status='not_done';
				$message='خطأ فى كود الادخال';
			}
			  
		 }else{
			  $status='not_done';
			  $message='خطأ فى البيانات';
			  //print_r($error_arr);
		 }
	  
	  
	to_result:
	  
	$result = array(
		//'token' => $token,
		'status' => $status,
		'message' => $message,
		'affected_rows'=>$affected_rows,
		'errors' => $error_arr,
		'errors_database' => $error_database_arr,
		'structure' => $table,
		'reload' => 'true',
		'option_dgree'=> $post_data['option_dgree'],
		'option_images'=> $post_data['option_images'],
		'option_answer'=> $post_data['option_answer']
		

	);


echo json_encode($result,JSON_UNESCAPED_UNICODE);

 
	  
	  

	/*


//$user='Admin';
//$pass='123456';
//$token='';

$key = 'i am mohamed saad';
$result=[];

if($token==''){

$salt=sha1('DSAF$#%#)VFDVD>^%&BFC$#@');	
$password_salt=hash('sha256', $salt.$pass);


//print_r($data);
//echo $post_data["id"];


//echo $id;
$auth;
$companies_query="INSERT INTO `suppliers`
				  (`id`, `name`, `type`, `branchID`, `country`, `governate`, `regionCity`, `street`, `buildingNumber`, `postalCode`, `floor`, `room`, `landmark`, `additionalInformation`) 
				   VALUES 
				  ('$id', '$name', '$type', '$branchID', '$country', '$governate', '$regionCity', '$street', '$buildingNumber', '$postalCode', '$floor', '$room', '$landmark', '$additionalInformation')";
mysqli_query($conn,"SET NAMES 'utf8'");
$companies_result=mysqli_query($conn,$companies_query);	



$num_rows=mysqli_num_rows($companies_result);



if($num_rows>=1){
while($r = mysqli_fetch_assoc($companies_result)) {
	

	$payload = [
		'iss' => 'localhost',
		'aud' => 'localhost',
		'exp' => time()+10000,
		'data' => $r,

	];

	$jwt = JWT::encode($payload, $key, 'HS256');
	
	$result['message']="done";
	$result['token']=$jwt;
	//$result['decode']=$decoded;
	$result['rows'] = $r;
	$result['rows_num'] =$num_rows;
	
	
	//echo $r['title'];
}
}else{
	//$result['message']="لا توجد نتائج";
	$result['message']='login faild ';

}

}else{
	try {
		JWT::decode($token, new Key($key, 'HS256'));
		$result['message']='valid token';
	}catch(Exception $e){
		//$result['token']=$e->getMessage();
		$result['message']='invalid token';
	}
}
echo json_encode($result,JSON_UNESCAPED_UNICODE);

*/
?>