<?php
    include "../db/headerApi.php";
    include "../db/connection_main.php";

    $json = file_get_contents('php://input');
    $data = json_decode($json);

    $whereBrand = "";
    if (!empty($data->brand)) {
        $whereBrand = "AND a.brand_id = '".$data->brand->ID."'";
    }

    if ($data->comp == 'MST') {
      $sql = " SELECT DISTINCT inventory_item_id, a.sku id, a.description text
                 FROM apps.MST_item_mapping_skusizebrand_v a
                WHERE 1=1
                ".$whereBrand."
             ORDER BY 1";
        
    } else if ($data->comp == 'YTY') {

      $sql = " SELECT DISTINCT b.inventory_item_id, a.sku id, SUBSTR(b.description,1,INSTR(b.description,':',1,1)-1) text
                 FROM apps.YTY_ITEM_MAPPING_SKUSIZEBRAND_V a, 
                      apps.MTL_SYSTEM_ITEMS_B b
                WHERE a.sku = b.segment1
             ORDER BY 1";
    }

    $sqlarr = oci_parse($dbcon , $sql);
    $r = oci_execute($sqlarr);
    $resMsg = "OK";

    if (!$r) {
         $e = oci_error($sqlarr);
         $resMsg = json_encode($e['message']);
    }
    oci_fetch_all($sqlarr, $rows_main, null, null, OCI_FETCHSTATEMENT_BY_ROW);

    $result[] = [
        "message" => $resMsg,
        "data" => $rows_main
    ];

    print json_encode($result);
?>