<?php
    include "../db/headerApi.php";
    include "../db/connection_main.php";

    $json = file_get_contents('php://input');
    $data = json_decode($json);

    if (!empty($data)) {
        $default_loc = '../../webfiles/Claims/';

        //Emp Details
        $sql = "SELECT emp_id, employee_name, department, designation, orgid, claim_group
                  FROM production.employee_master
                 WHERE emp_id = :EMP_ID";  
            
        $sqlarr = oci_parse($dbcon , $sql);
        oci_bind_by_name($sqlarr, ':EMP_ID', $data->EMP_ID);
        $r = oci_execute($sqlarr);
        oci_fetch_all($sqlarr, $rows_emp, null, null, OCI_FETCHSTATEMENT_BY_ROW);

        //Claim ID
        $sql = "SELECT lpad(:EMP_ID, 9, '0')
                       || to_char(sysdate, 'RRMMDD')
                       || to_char(nvl(COUNT(1), 0)+ 1) as newid
                  FROM production.claim_info
                 WHERE employee_id = :EMP_ID";

        $sqlarr = oci_parse($dbcon , $sql);
        oci_bind_by_name($sqlarr, ':EMP_ID', $data->EMP_ID);
        $r = oci_execute($sqlarr);
        oci_fetch_all($sqlarr, $rows_main, null, null, OCI_FETCHSTATEMENT_BY_ROW);
        
        //Claim Info
        // $sql = "INSERT INTO production.claim_info 
        //                     (claim_id, employee_id, employee_name, dept_id, claim_date, 
        //                      created_by, creation_date, organization_id, claim_status, ip_address)
        //              VALUES (:CLAIM_ID, :EMPLOYEE_ID, :EMPLOYEE_NAME, :DEPT_ID, SYSDATE, 
        //                      :CREATED_BY, SYSDATE, :ORGANIZATION_ID, 'Process', 'Mobile')";
        
        // $sqlarr = oci_parse($dbcon , $sql);
        // oci_bind_by_name($sqlarr, ':CLAIM_ID', $rows_main[0]["NEWID"]);
        // oci_bind_by_name($sqlarr, ':EMPLOYEE_ID', $data->EMP_ID);
        // oci_bind_by_name($sqlarr, ':EMPLOYEE_NAME', $rows_emp[0]["EMPLOYEE_NAME"]);
        // oci_bind_by_name($sqlarr, ':DEPT_ID', $rows_emp[0]["EMP_ID"]);
        // oci_bind_by_name($sqlarr, ':CREATED_BY', $rows_emp[0]["EMPLOYEE_NAME"]);
        // oci_bind_by_name($sqlarr, ':ORGANIZATION_ID', $rows_emp[0]["ORGID"]);
        // $r = oci_execute($sqlarr, OCI_NO_AUTO_COMMIT);

        // if (!$r) {
        //     $e = oci_error($sqlarr);
        //     $errorMsg = json_encode($e['message']);
        //     goto finalStep;
        // }

        //Claim Mobile
        //foreach ($data->CLAIM_DETAIL as $dt) {

            $sql = "INSERT INTO production.claim_detail_mobile
                                (claim_type, claim_description, claim_bill_amt, claim_tax_amt, claim_currency_type, claim_exchange_rate, total_claim_amt_myr, remark, created_by, creation_date, organization_id, claim_file_extension, kilometer, rate_per_kilometer, claim_url)
                         VALUES (:CLAIM_TYPE, :CLAIM_DESCRIPTION, :CLAIM_BILL_AMT, :CLAIM_TAX_AMT, :CLAIM_CURRENCY_TYPE, :CLAIM_EXCHANGE_RATE, :TOTAL_CLAIM_AMT_MYR, :REMARK, :CREATED_BY, SYSDATE, :ORGANIZATION_ID, :CLAIM_FILE_EXTENSION, :KILOMETER, :RATE_PER_KILOMETER, :CLAIM_URL)
                         RETURNING ID INTO :NEWID";
        
            $sqlarr = oci_parse($dbcon , $sql);
            oci_bind_by_name($sqlarr, ':CLAIM_TYPE', $data->CLAIM_TYPE);
            oci_bind_by_name($sqlarr, ':CLAIM_DESCRIPTION', $data->CLAIM_DESCRIPTION);
            oci_bind_by_name($sqlarr, ':CLAIM_BILL_AMT', $data->CLAIM_BILL_AMT);
            oci_bind_by_name($sqlarr, ':CLAIM_TAX_AMT', $data->CLAIM_TAX_AMT);
            oci_bind_by_name($sqlarr, ':CLAIM_CURRENCY_TYPE', $data->CLAIM_CURRENCY_TYPE);
            oci_bind_by_name($sqlarr, ':CLAIM_EXCHANGE_RATE', $data->CLAIM_EXCHANGE_RATE);
            oci_bind_by_name($sqlarr, ':TOTAL_CLAIM_AMT_MYR', $data->TOTAL_CLAIM_AMT_MYR);
            oci_bind_by_name($sqlarr, ':REMARK', $data->REMARK);
            oci_bind_by_name($sqlarr, ':CREATED_BY', $rows_emp[0]["EMPLOYEE_NAME"]);
            oci_bind_by_name($sqlarr, ':ORGANIZATION_ID', $rows_emp[0]["ORGID"]);
            oci_bind_by_name($sqlarr, ':CLAIM_FILE_EXTENSION', $data->CLAIM_FILE_EXTENSION);
            oci_bind_by_name($sqlarr, ':KILOMETER', $data->KILOMETER);
            oci_bind_by_name($sqlarr, ':RATE_PER_KILOMETER', $data->RATE_PER_KILOMETER);
            oci_bind_by_name($sqlarr, ':CLAIM_URL', $data->CLAIM_URL);
            oci_bind_by_name($sqlarr, ':NEWID', $newid, 20);
            $r = oci_execute($sqlarr, OCI_NO_AUTO_COMMIT);

            if (!$r) {
                $e = oci_error($sqlarr);
                $errorMsg = json_encode($e['message']);
                goto finalStep;
            }

            //Upload the Attachment
            $attachment = base64_decode($data->ATTACHMENT);
            $ext = end(explode('.', $data->CLAIM_FILE_EXTENSION));
            
            $newfilename = $default_loc.$newid.".".$ext;
            file_put_contents($newfilename, $attachment);
        //}

    } else {
        $errorMsg = "Invalid request";
    }

finalStep:
    $result = [];
    if ($errorMsg == "") {
        oci_commit($dbcon);
        $result[] = [
            "message" => "OK",
            "data" => "Save Success"
        ];
    } else {
        oci_rollback($dbcon);
        $result[] = [
            "message" => $errorMsg,
            "data" => ""
        ];
    }
    print json_encode($result);
?>
