<cfinclude template="__Security-Block.cfm">

<!--- Query to get physicians with status 0 --->
<cfquery name="getsphy" datasource="#Application.DataSrc#">
   SELECT * 
   FROM #Request.prefix_db_lookup#.pPhysicians
   WHERE Status = <cfqueryparam value="0" cfsqltype="cf_sql_integer">
   <!--- AND Phys_NPI = 1528423019 --->
   GROUP BY Phys_NPI
</cfquery>

<!--- Reset session variable for NPIs --->
<cfset session.Phys_NPIs = "" />

<!--- Query to clear NPIs (if needed) --->
<cfquery name="clear_NPIs" datasource="#Application.DataSrc#">
    UPDATE #Request.prefix_db_lookup#.pPhysicians
    SET Phys_verified = 0 WHERE  status = 0
</cfquery>

<!--- Process each physician --->
<cfoutput query="getsphy">
    <cfif len(Phys_NPI) EQ 10>
        <cfscript>
            // Define the API URL with query parameters
            apiURL = "https://clinicaltables.nlm.nih.gov/api/npi_org/v3/search?terms=" & getsphy.Phys_NPI;
            //writeDump(var = apiURL);
            // Make the HTTP GET request
            cfhttp(
                url = apiURL,
                method = "GET",
                result = "apiResponse",
                charset = "utf-8"
            );

            // Check if the request was successful
            if (apiResponse.StatusCode == "200 OK") {
                // Parse the JSON response
                jsonData = deserializeJson(apiResponse.FileContent);

                // Output the parsed data (for debugging)
                //writeDump(var = jsonData, label = "API Response");

                 if (arrayLen(jsonData) > 2)
                 {
                    if (arrayLen(jsonData[2]) > 1)
                    {
                          session.Phys_NPIs = listAppend(session.Phys_NPIs, getsphy.Phys_NPI);
                    }
                 }
                
                // Process the data as needed
                // For example, you might want to access specific fields:
                if (isDefined("jsonData.result")) {
                    writeOutput("<h2>Results:</h2>");
                    for (item in jsonData.result) {
                        writeOutput("<p>Name: " & item.name & "<br>");
                        writeOutput("NPI: " & item.npi & "<br>");
                        writeOutput("Type: " & item.type & "<br>");
                        writeOutput("Practice Address: " & item.address & "</p>");
                    }
                } else {
                   // writeOutput("No results found.");
                }
            } else {
                // Handle error
               // writeOutput("Error: " & apiResponse.StatusCode );
            }
        </cfscript>
        <!--- Check if session.Phys_NPIs length exceeds 1000 or if its the last record --->
        <cfif len(session.Phys_NPIs) GT 1000 OR getsphy.recordcount EQ getsphy.CurrentRow>
            <cfif len(session.Phys_NPIs) gt 0 >
                <cfquery name="update_NPI" datasource="#Application.DataSrc#">
                    UPDATE #Request.prefix_db_lookup#.pPhysicians
                    SET Phys_verified = 1
                    WHERE Phys_NPI IN (#session.Phys_NPIs#)
                    AND status = 0
                </cfquery>
            </cfif>
            <cfset session.Phys_NPIs = "" />
        </cfif>

  </cfif>
</cfoutput>