<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 = 1578637229
   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">
    <!--- Only process if Phys_NPI is valid --->
    <cfif len(Phys_NPI) EQ 10>
        <cfscript>
            // Define the base API URL
            baseURL = "https://npiregistry.cms.hhs.gov/api/";
            
            // Define the query parameters
            parameters = {
                number = getsphy.Phys_NPI,
                enumeration_type = "",
                taxonomy_description = "",
                name_purpose = "",
                first_name = "",
                use_first_name_alias = "",
                last_name = "",
                organization_name = "",
                address_purpose = "",
                city = "",
                state = "",
                postal_code = "",
                country_code = "",
                limit = "",
                skip = "",
                pretty = "",
                version = "2.1"
            };
            
            // Convert parameters to query string
            queryString = "";
            for (param in parameters) {
               // if (len(parameters[param]) > 0) {
                    queryString &= param & "=" & urlEncodedFormat(parameters[param]) & "&";
                //}
            }
            if (right(queryString, 1) == "&") {
                queryString = left(queryString, len(queryString) - 1);
            }
            
            // Construct the full URL
            fullURL = baseURL & "?" & queryString;
             fullURL = lcase(fullURL);
            // Make the HTTP GET request using cfhttp
            cfhttp(
                url = fullURL,
                method = "get",
                result = "response"
                ,charset = "utf-8"
            );
            writedump (fullURL);
            // Check the response status code
            if (response.statusCode == 200) {
                responseData = deserializeJSON(response.fileContent);
                if (isDefined("responseData.result_count")) 
                	if (responseData.result_count gt 0)
                {
                    session.Phys_NPIs = listAppend(session.Phys_NPIs, getsphy.Phys_NPI);
                }
            } else {
            	writedump (response);
                writeOutput("Error fetching data for NPI: " & getsphy.Phys_NPI & "<br>");
                writeOutput("Status Code: " & response.statusCode & "<br>");
                //writeOutput("Status Text: " & response.statusText & "<br>");
            }
        </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>
