<!--- Alter patients_ai table Pt_Gender column for all agency databases --->
<cfinclude template='__Security-Block.cfm' />

<!--- Get all agencies to update tables for all --->
<cfquery name="GetAllAgencies" datasource="#Application.DataSrc#">
    SELECT DISTINCT Agency_ID 
    FROM #Request.prefix_db_lookup#.Agency
    WHERE Status = 0
    ORDER BY Agency_ID
</cfquery>

<cfset updateResults = [] />

<!--- Loop through all agencies --->
<cfloop query="GetAllAgencies">
    <cfset agency_db = 'agency_' & GetAllAgencies.Agency_ID />
    <cfset currentAgency = GetAllAgencies.Agency_ID />
    
    <!--- Check if patients_ai table exists for this agency --->
    <cftry>
        <cfquery name="CheckTableExists" datasource="#Application.DataSrc#">
            SELECT 1 FROM #agency_db#.patients_ai LIMIT 1
        </cfquery>
        <cfset tableExists = true />
        <cfcatch type="any">
            <cfset tableExists = false />
        </cfcatch>
    </cftry>
    
    <cfif tableExists>
        <!--- Check if Pt_Gender column exists --->
        <cftry>
            <cfquery name="CheckColumnExists" datasource="#Application.DataSrc#">
                SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE, COLUMN_DEFAULT
                FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_SCHEMA = <cfqueryparam value="#agency_db#" cfsqltype="cf_sql_varchar">
                AND TABLE_NAME = 'patients_ai'
                AND COLUMN_NAME = 'Pt_Gender'
            </cfquery>
            <cfset columnExists = CheckColumnExists.recordCount GT 0 />
            <cfcatch type="any">
                <cfset columnExists = false />
            </cfcatch>
        </cftry>
        
        <cfif columnExists>
            <!--- Execute ALTER TABLE query --->
            <cftry>
                <cfquery name="AlterTable" datasource="#Application.DataSrc#">
                    ALTER TABLE `#agency_db#`.`patients_ai` 
                    CHANGE COLUMN `Pt_Gender` `Pt_Gender` VARCHAR(255) NULL DEFAULT NULL
                </cfquery>
                
                <!--- Track successful update --->
                <cfset arrayAppend(updateResults, {
                    agency_id: currentAgency,
                    status: "Success",
                    message: "Pt_Gender column altered successfully"
                }) />
                
                <cfcatch type="any">
                    <!--- Track error --->
                    <cfset arrayAppend(updateResults, {
                        agency_id: currentAgency,
                        status: "Error",
                        message: "Failed to alter column: #cfcatch.message#"
                    }) />
                </cfcatch>
            </cftry>
        <cfelse>
            <!--- Column doesn't exist --->
            <cfset arrayAppend(updateResults, {
                agency_id: currentAgency,
                status: "Skipped",
                message: "Pt_Gender column does not exist in patients_ai table"
            }) />
        </cfif>
    <cfelse>
        <!--- Table doesn't exist for this agency --->
        <cfset arrayAppend(updateResults, {
            agency_id: currentAgency,
            status: "Skipped",
            message: "patients_ai table does not exist"
        }) />
    </cfif>
    
</cfloop>

<cfoutput>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Alter patients_ai Pt_Gender Column - All Agencies</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
                background-color: ##f5f5f5;
            }
            .container {
                background-color: white;
                padding: 20px;
                border-radius: 5px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .alert {
                padding: 15px;
                margin-bottom: 20px;
                border: 1px solid transparent;
                border-radius: 4px;
            }
            .alert-success {
                color: ##3c763d;
                background-color: ##dff0d8;
                border-color: ##d6e9c6;
            }
            .table {
                width: 100%;
                border-collapse: collapse;
                margin-top: 20px;
            }
            .table th, .table td {
                padding: 12px;
                text-align: left;
                border-bottom: 1px solid ##ddd;
            }
            .table th {
                background-color: ##f8f9fa;
                font-weight: bold;
            }
            .table tr:hover {
                background-color: ##f5f5f5;
            }
            .label {
                display: inline-block;
                padding: 4px 8px;
                font-size: 12px;
                font-weight: bold;
                border-radius: 3px;
            }
            .label-success {
                background-color: ##5cb85c;
                color: white;
            }
            .label-danger {
                background-color: ##d9534f;
                color: white;
            }
            .label-warning {
                background-color: ##f0ad4e;
                color: white;
            }
            .label-default {
                background-color: ##777;
                color: white;
            }
            .text-muted {
                color: ##777;
                font-size: 14px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Alter patients_ai Pt_Gender Column</h1>
            
            <div class="alert alert-success">
                <strong>Processing Complete!</strong> The ALTER TABLE query has been executed for all agency databases.
                <br><br>
                <strong>Query Executed:</strong>
                <pre style="background-color: ##f5f5f5; padding: 10px; border-radius: 3px; overflow-x: auto;">
ALTER TABLE `agency_{id}`.`patients_ai` 
CHANGE COLUMN `Pt_Gender` `Pt_Gender` VARCHAR(255) NULL DEFAULT NULL</pre>
                <br>
                <strong>Change:</strong> Modified Pt_Gender column to VARCHAR(255) with NULL allowed and NULL default.
            </div>
            
            <strong>Results for All Agencies:</strong>
            <table class="table">
                <thead>
                    <tr>
                        <th>Agency ID</th>
                        <th>Database Name</th>
                        <th>Status</th>
                        <th>Message</th>
                    </tr>
                </thead>
                <tbody>
                    <cfloop array="#updateResults#" index="result">
                        <tr>
                            <td>#result.agency_id#</td>
                            <td>agency_#result.agency_id#</td>
                            <td>
                                <cfif result.status eq "Success">
                                    <span class="label label-success">#result.status#</span>
                                <cfelseif result.status eq "Error">
                                    <span class="label label-danger">#result.status#</span>
                                <cfelse>
                                    <span class="label label-warning">#result.status#</span>
                                </cfif>
                            </td>
                            <td>#result.message#</td>
                        </tr>
                    </cfloop>
                </tbody>
            </table>
            
            <!--- Summary Statistics --->
            <cfset successCount = 0 />
            <cfset errorCount = 0 />
            <cfset skippedCount = 0 />
            <cfloop array="#updateResults#" index="result">
                <cfif result.status eq "Success">
                    <cfset successCount = successCount + 1 />
                <cfelseif result.status eq "Error">
                    <cfset errorCount = errorCount + 1 />
                <cfelse>
                    <cfset skippedCount = skippedCount + 1 />
                </cfif>
            </cfloop>
            
            <div style="margin-top: 20px; padding: 15px; background-color: ##f8f9fa; border-radius: 4px;">
                <strong>Summary:</strong>
                <ul style="margin: 10px 0; padding-left: 20px;">
                    <li><strong>Total Agencies Processed:</strong> #arrayLen(updateResults)#</li>
                    <li><strong style="color: ##5cb85c;">Successful:</strong> #successCount#</li>
                    <li><strong style="color: ##d9534f;">Errors:</strong> #errorCount#</li>
                    <li><strong style="color: ##f0ad4e;">Skipped:</strong> #skippedCount#</li>
                </ul>
            </div>
            
            <p class="text-muted"><small>Completed at: #DateFormat(Now(), 'mm/dd/yyyy')# #TimeFormat(Now(), 'HH:mm:ss')#</small></p>
        </div>
    </body>
    </html>
</cfoutput>

