<?php
    global $dbcon;
    include "../db/headerApi.php";
    include "../db/connection_main.php";

    $json = file_get_contents('php://input');
    $data = json_decode($json);

    $whereLoc = "AND 1=2";
    if ($data->location == "Lumut") {
        $whereLoc = " AND a.org_id IN ('86','102','122') ";

    } else if ($data->location == "Lekir") {
        $whereLoc = " AND a.org_id IN ('84') ";
    }

    $sql = "SELECT a.unit_name || ' - ' || b.plt_nm ID, a.unit_name || ' - ' || b.plt_nm TEXT
              FROM yty_g_plant_name a, production.plant_master b
             WHERE a.org_id = b.org_id
               ". $whereLoc . "
             ORDER BY 1";

    $sqlarr = oci_parse($dbcon , $sql);
    $r = oci_execute($sqlarr);
    $rows_main = [];
    oci_fetch_all($sqlarr, $rows_main, null, null, OCI_FETCHSTATEMENT_BY_ROW);

    if (!$r) {
        $e = oci_error($sqlarr);
        print json_encode($e['message']);
    }

    print json_encode($rows_main);
?>