
<?php
require_once ('dbClass.php');

include("assets/forceutf8-master/src/ForceUTF8/Encoding.php");  //used for UNICODE conversion / encoding

$SpName = isset($_REQUEST['SpName']) ? $_REQUEST['SpName'] : '';
$requestParams = isset($_REQUEST['requestParams']) ? $_REQUEST['requestParams'] : '';
$PopulateField = isset($_REQUEST['PopulateField']) ? $_REQUEST['PopulateField'] : '';
$editid = isset($_REQUEST['editid']) ? $_REQUEST['editid'] : '';
$userid = isset($_REQUEST['userid']) ? $_REQUEST['userid'] : '';
$GridId = isset($_REQUEST['gridid']) ? $_REQUEST['gridid'] : '';
$FormID = isset($_REQUEST['FormID']) ? $_REQUEST['FormID'] : 0;
$FormData = isset($_POST['FormData']) ? $_POST['FormData'] : '';
$FType = isset($_POST['FType']) ? $_POST['FType'] : '';

if(strlen($requestParams) > 3){
    $requestParamsArray = [];
    $splitParams = [];
    $finalRequestParams = '';
    $requestParamsArray = explode("|",$requestParams);

    for($i=0; $i<sizeof($requestParamsArray); $i++){
        $splitParams = explode(":",$requestParamsArray[$i]);
        $dbField = trim($splitParams[0]);
        $sql2 = "SELECT FieldType FROM kgridfields WHERE GridId=$GridId AND DbFieldName='$dbField'";
        $sql2result = db::getInstanceMaster()->db_select($sql2);
        $finalRequestParams .= trim($splitParams[0]).":".trim($splitParams[1]).":".trim($sql2result['result_set'][0]['FieldType']);
        if($splitParams[2]){
            $finalRequestParams .= ":".trim($splitParams[2]);
        }
        if($i < count($requestParamsArray)-1){
            $finalRequestParams .= "|";
        }
    }
    // echo $finalRequestParams;
}

$set = [];
$val = [];
$row = $result['result_set'];
if($FType == 'GSTR1'){
    $session = 'YearCode='.$_SESSION['dbYear'].' AND DivisionId='.$_SESSION['dbDivision'].' AND CompanyId='.$_SESSION['dbCompany'].' AND FormID='.$FormID.' AND EditID=' . $editID;
    $sp['params'] = ['@form','@editid','@userid','@SESSION'];
    $sp['values'] = ["'".$requestParams."'",$editid,$userid,"'".$session."'"];
    $result = db::getInstance()->db_sp_select(trim($SpName), $sp['params'], $sp['values']);
    $checkSpResult = $result;
    $response = [];
    if($checkSpResult['error'] == '0') {
        if (isset($checkSpResult['result_set']) && count($checkSpResult['result_set']) > 0) {
            $resultSet = $checkSpResult['result_set'];
            // Extract the first array and remove it from the original array
            $sheetNameArray = array_shift($resultSet);
            $sheets = [];
            $filteredArray = array_filter($sheetNameArray, function($item) {
                return $item['cnt'] > 0;
            });
            $sheetNameArray = array_values($filteredArray);
            // echo '<pre>';
            // print_r($filteredArray);
            // print_r($resultSet);
            // print_r($sheetNameArray);
            // echo "<br />==>>" ;
            foreach ($resultSet as $index => $sheetData) {
                // echo "<br />==>>" ;
                // print_r($sheetData);
                $headers = array_keys($sheetData[0]);
                // echo "<br />>>" ;
                // print_r($headers);
                $data = [];

                foreach ($sheetData as $row) {
                    $data[] = $row;
                }

                $sheets[] = [
                    'sheetName' => $sheetNameArray[$index]['Name'],//'Sheet' . ($index + 1),
                    'headers' => $headers,
                    'data' => $data
                ];
            }
            $response = [
                'status' => 'true',
                'sheets' => $sheets
            ];
        } else {
            $response = ['status' => 'false', 'message' => 'No data found'];
        }
    } else {
        $response = ['response' => 'false', 'message' => 'Error while fetching data'];
    }
    echo json_encode($response);
}else{
    $sp['params'] = ['@form','@editid','@userid','@PopulateField'];
    $sp['values'] = ["'".$requestParams."'",$editid,$userid,"'".$PopulateField."'"];

    $result = db::getInstance()->db_sp_select(trim($SpName), $sp['params'], $sp['values']);
    // print_r($result);
    $sql1 = "SELECT * FROM kgridfields where GridId=$GridId ORDER BY DisplayOrder";
    $sql1result = db::getInstanceMaster()->db_select($sql1);
    // print_r($result);
    $msg = $result['result_set'][0][0]['msg'];
    $success = $result['result_set'][0][0]['success'];

    if($result['result_set'][3]){
        $sql3 = "SELECT FieldType FROM kmainfields WHERE FormId=$FormID AND DbFieldName='".$result['result_set'][3][0]['cursorPosition']."'";
        $sql3result = db::getInstanceMaster()->db_select($sql3);

        $cursorPosition = $result['result_set'][3][0]['cursorPosition'].":".$sql3result['result_set'][0]['FieldType'];
        // print_r($sql3result['result_set'][0]['FieldType']);
    }
    function array_change_key_case_recursive($array) {
        $result = [];
        foreach ($array as $key => $value) {
            $lowercaseKey = strtolower($key);
            if (is_array($value)) {
                $result[$lowercaseKey] = array_change_key_case_recursive($value); // Recursively change case for nested arrays
            } else {
                $result[$lowercaseKey] = $value;
            }
        }
        return $result;
    }
    $array = array_change_key_case_recursive($sql1result['result_set']);
    // print_r($result);
    $arr = array("status" => true, "msg" => $msg, "success" => $success, "data" => Encoding::fixUTF8($result['result_set'][1], Encoding::ICONV_IGNORE), "gridData" => $array, "RequestParams" => $finalRequestParams, "PopulateFieldResponse" => $result['result_set'][2], "cursorPosition" => $cursorPosition,"NonEditableFields" => $result['result_set'][4]);
    echo json_encode($arr);
}
?>