<?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("screening_date " . $screening_date);
$patient_id = $_REQUEST['patient_id'];
$remarks = $_REQUEST['s_remarks'];
$notes = $_REQUEST['notes'];
$temp = $_REQUEST['temp'];
$spo2 = $_REQUEST['spo2'];
$status = $_REQUEST['attendedStatus'];
$approval_status = $_REQUEST['approval_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'];
$status_and_losing = $_REQUEST['status_and_losing'];
$rolecode = $_SESSION['RoleCode'];
$forUpdate=$_REQUEST['forUpdate'];
error_log('Rolecode'.$forUpdate);
begin();

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 . "',temp='" . $temp . "',capa='" . $capa . "',communication_notes='" . $communication_notes . "',responsibility='" . $responsibility . "',completion='" . $completion . "',status_and_losing='" . $status_and_losing . "',spo2='" . $spo2 . "',ref_no='" . $ref_no . "',job_profile='" . $job_profile . "',canteen_location='" . $canteen_location . "',approval_user='" . $_SESSION['user_id'] . "',modified_by='" . $_SESSION['user_id'] . "' where response_id='" . $response_id . "' ";
    } else {
        if($forUpdate!=null){
            $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 . "',temp='" . $temp . "',capa='" . $capa . "',communication_notes='" . $communication_notes . "',responsibility='" . $responsibility . "',completion='" . $completion . "',status_and_losing='" . $status_and_losing . "',spo2='" . $spo2 . "',ref_no='" . $ref_no . "',job_profile='" . $job_profile . "',canteen_location='" . $canteen_location . "',approval_user='" . $_SESSION['user_id'] . "',modified_by='" . $_SESSION['user_id'] . "' where medical_examination_no='" . $forUpdate . "' and patient_id='".$patient_id."' ";
        }
        else{
        $new_ffi_no = creatingFFINo();
        $main_query = "insert into questionaire_master_response set screen_date=STR_TO_DATE('" . $screening_date . "', '%d/%m/%Y %h:%i %p'),patient_id='" . $patient_id . "',f_status='" . $status . "',medical_examination_no= '".$new_ffi_no."',remarks='" . $remarks . "',approval_status='" . $approval_status . "',notes='" . $notes . "',form_type='" . $ftype . "',temp='" . $temp . "',spo2='" . $spo2 . "',job_profile='" . $job_profile . "',canteen_location='" . $canteen_location . "',ref_no='" . $ref_no . "',observation_of_consern='" . $consern . "',capa='" . $capa . "',communication_notes='" . $communication_notes . "',responsibility='" . $responsibility . "',completion='" . $completion . "',status_and_losing='" . $status_and_losing . "',medical_attend='" . $_SESSION['user_id'] . "',modified_by='" . $_SESSION['user_id'] . "',ohc_type_id='" . $_SESSION['current_ohcttype'] . "' ";
        }
    }
} else {

    $main_query = "insert into questionaire_master_response set screen_date=STR_TO_DATE('" . $screening_date . "', '%d/%m/%Y %h:%i %p'),patient_id='" . $patient_id . "',f_status='" . $status . "',medical_examination_no='" . $ffi_no . "',remarks='" . $remarks . "',approval_status='" . $approval_status . "',notes='" . $notes . "',form_type='" . $ftype . "',temp='" . $temp . "',spo2='" . $spo2 . "',job_profile='" . $job_profile . "',canteen_location='" . $canteen_location . "',ref_no='" . $ref_no . "',observation_of_consern='" . $consern . "',capa='" . $capa . "',communication_notes='" . $communication_notes . "',responsibility='" . $responsibility . "',completion='" . $completion . "',status_and_losing='" . $status_and_losing . "',medical_attend='" . $_SESSION['user_id'] . "',modified_by='" . $_SESSION['user_id'] . "',ohc_type_id='" . $_SESSION['current_ohcttype'] . "' ";
}
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);