<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
$noOfRows = $_POST['count_items'];
error_log("rows: " . $noOfRows);
$screening_date = $_REQUEST['comm_date'];
$job_profile = $_REQUEST['jobprofile'];
error_log("job profile ".$job_profile);
$canteen_location = $_REQUEST['canteen_location'];
error_log("canteen_location ::::: " . $canteen_location);
$patient_id = $_REQUEST['patient_id'];
$remarks = $_REQUEST['s_remarks'];
$notes = $_REQUEST['notes'];
$temp = $_REQUEST['temp'];
$spo2 = $_REQUEST['spo2'];
$status = "DRP";
$approval_status = "approval";
error_log("status ". $status);
$ffi_no = $_REQUEST['ffi_no'];
$response_id = $_REQUEST['response_id'];
$ref_no = $_REQUEST['ref'];
$consern = $_REQUEST['consern'];
$capa = $_REQUEST['capa'];
$communication_notes = $_REQUEST['communication_notes'];
$responsibility = $_REQUEST['responsibility'];
$completion = $_REQUEST['completion'];

begin();
$roleCode =  $_SESSION['RoleCode'];
error_log("role Code ". $rolecode);

if (!empty($response_id)) {
    if ($rolecode == 'DOC') {
        $main_query = "update questionaire_master_response set screen_date=STR_TO_DATE('" . $screening_date . "', '%d/%m/%Y %h:%i %p'),remarks='" . $remarks . "',f_status='".$status."',notes='" . $notes ."',observation_of_consern='" . $consern . "',approval_status='".$approval_status."',approval_user='". $_SESSION['logged_user_empid'] ."',temp='" . $temp . "',capa='". $capa . "',communication_notes='". $communication_notes . "',responsibility='". $responsibility ."',completion='".$completion."',spo2='" . $spo2 . "',ref_no='" . $ref_no ."',job_profile='". $job_profile . "',canteen_location='". $canteen_location . "',medical_attend='" . $_SESSION['user_id'] . "',modified_by='" . $_SESSION['user_id']."' where response_id='" . $response_id . "' ";
	}else{
		$main_query = "update questionaire_master_response set screen_date=STR_TO_DATE('" . $screening_date . "', '%d/%m/%Y %h:%i %p'),remarks='" . $remarks . "',f_status='".$status."',notes='" . $notes ."',observation_of_consern='" . $consern . "',approval_status='".'pending'."',approval_user='". $_SESSION['logged_user_empid'] ."',temp='" . $temp . "',capa='". $capa . "',communication_notes='". $communication_notes . "',responsibility='". $responsibility ."',completion='".$completion."',spo2='" . $spo2 . "',ref_no='" . $ref_no ."',job_profile='". $job_profile . "',canteen_location='". $canteen_location . "',medical_attend='" . $_SESSION['user_id'] . "',modified_by='" . $_SESSION['user_id'] . "' where response_id='" . $response_id . "' ";
	}
    
}
error_log("main  query" . $main_query);
if (!$result = mysqli_query($conn, $main_query)) {
    error_log("error in main " . mysqli_error($conn));
    rollback();
} else {

    if(empty($response_id)){
        $r_id = mysqli_insert_id($conn);
    }else{
        $r_id = $response_id;
    }

    for ($i = 0; $i < $noOfRows; $i++) {


        $get_query = "select count(*) as total,a.response_id from questionaire_master_response a left join questionaire_master_response_details b on a.response_id = b.response_id where b.question_id='" . ${"question_id$i"} . "' and a.medical_examination_no='" . $ffi_no . "' and a.patient_id='" . $patient_id . "'";
        error_log("query decides the update and delete " . $get_query);
        $result1 = mysqli_query($conn, $get_query);
        $row1 = mysqli_fetch_assoc($result1);
        $count1 = $row1['total'];

        if ($count1 != 0) {
            error_log('update query');
            $query1 = "update questionaire_master_response_details set question_remarks='" . ${"remarks$i"} . "',question_answer='" . ${"question_ans$i"} . "', modified_by='" . $_SESSION['user_id'] . "' where response_id='" . $r_id . "' and question_id='" . ${"question_id$i"} . "' ";
        } else {
            $query1 = "insert into questionaire_master_response_details set question_id='" . ${"question_id$i"} . "',question_remarks='" . ${"remarks$i"} . "',question_answer='" . ${"question_ans$i"} . "',response_id='" . $r_id . "'";
        }
        error_log("ffi details insert query " . $query1);
        if (!$result = @mysqli_query($conn, $query1)) {
            error_log("ffi details insert query error " . mysqli_error($conn));
            rollback();
        }
        if (mysqli_num_rows($result) > 0) {
            while ($row = @mysqli_fetch_assoc($result)) {
                $data = $row;
            }
        }
    }
}

if ($data == null) {
    $data['status'] = 200;
    $data['message'] = "Data not found!";
}
commit();
echo json_encode($data);