<?php
error_reporting(E_ERROR | E_PARSE);
include('includes/config/config.php');
include('includes/functions.php');

if (!empty($_POST['med_arr'])) {
    $med = implode(",", $_POST['med_arr']);

    $med = preg_replace('/,+/', ',', $med);
    $med = rtrim($med, ',');
    error_log("med " . $med);
    $sql_med = "select * from tbl_items where item_id in ($med)";

    error_log("query " . $sql_med);

    if (!$result_med = @mysqli_query($conn, $sql_med)) {
        die(mysqli_error($conn));
    }
    $data = array();

    if (mysqli_num_rows($result_med) > 0) {
        while ($row_med = mysqli_fetch_assoc($result_med)) {
            //extract($row);
            $alternate = get_alternate_medicine_data($row_med['item_id']);
            $composition = get_med_salt_details($row_med['item_id']);
            $indication = getCommaSeperatedValuesForInClause("select indication_name from indication", "indication_id", $row_med['indication']);

            $contra_indication = getCommaSeperatedValuesForInClause("select cindication_name from contra_indication", "cindication_id", $row_med['contra_indication']);

            $sideeffects = getCommaSeperatedValuesForInClause("select sideeffect_name from side_effects", "sideeffect_id", $row_med['side_effects']);

            $data[$row_med['item_id']] = array(
                'item_name' => $row_med['item_name'],
                'item_precaution' => $row_med['item_precaution'] == '' || $row_med['item_precaution'] == null ? "NA" : $row_med['item_precaution'],
                'item_alternate' => $alternate,
                'composition' => $composition,
                'indication' => $indication,
                'contraIndication' => $contra_indication,
                'effect' => $sideeffects,
                'is_prescription' => $row_med['is_prescription'] == '1' ? "Yes" : "No",
                'interaction' => $row_med['interaction']==null || $row_med['interaction'] == ''?"NA":$row_med['interaction']
            );
        }
    } else {
        $data['status'] = 200;
        $data['message'] = "Data not found!";
    }
}
error_log("data " . print_r($data, true));
echo  json_encode($data);