<cfquery  name="getauthorizations" datasource="#Application.DataSrc#">
    SELECT  pAuths.*,pAssessments.F177,pAssessments.F178  FROM #Request.prefix_db_agency#.pAuths
    LEFT OUTER JOIN #Request.prefix_db_agency#.pAssessments ON pAuths.Assmt_ID = pAssessments.Assmt_ID 
    AND pAssessments.Status= 0
    WHERE pAuths.Auth_ID = '#pAuth_ID#' 
    AND pAuths.Status= 0
    GROUP BY pAuths.Auth_ID 
</cfquery>

<!--- <cfset strating_date = getauthorizations.Date_Start />
<cfset ending_date = getauthorizations.Date_End />--->

<cfif getauthorizations.Date_End LTE getauthorizations.F178 OR getauthorizations.recordcount EQ 0>
   <!---  <cfset strating_date = getauthorizations.Date_Start />
    <cfset ending_date = getauthorizations.Date_End /> --->
     <cfset strating_date = DateFormat(form.Date_Start,"yyyy-mm-dd") />
    <cfset ending_date = DateFormat(form.Date_End,"yyyy-mm-dd")/>

<cfelse>
    <cfset strating_date = getauthorizations.F177 />
    <cfset ending_date = getauthorizations.F178 />
</cfif>
<cfif len(strating_date) EQ 0>
  <cfset strating_date = DateFormat(form.Date_Start,"yyyy-mm-dd") />
    <cfset ending_date = DateFormat(form.Date_End,"yyyy-mm-dd")/>
</cfif>
<cfdump var="#form.Date_Start#">
<cfdump var="#strating_date#">

     <cfscript>
        // Define start and end dates for the range
        startDate = strating_date;  // Example start date: August 1, 2024
        endDate = ending_date;    // Example end date: August 31, 2024

        FirstWeekStart = createDateTime(year(strating_date), month(strating_date), day(strating_date), 0, 0, 0);
        // Initialize variables
        currentWeekStart = createDateTime(year(strating_date), month(strating_date), day(strating_date), 0, 0, 0);
        currentWeekStart = dateAdd("d", -DayOfWeek(currentWeekStart) + 1, currentWeekStart); // Start of the week (Sunday)
        weeks = []; // Array to hold week data
        week_no =  1;
        // Loop through each week in the date range
        while (currentWeekStart LTE endDate) 
        {
                // Calculate end of the week
                currentWeekEnd = dateAdd("d", 6, currentWeekStart); // End of the week (Saturday)
                if (currentWeekEnd GT endDate) {
                    currentWeekEnd = endDate; // Adjust end date if it exceeds the range
                }
                // Collect dates for the current week
                weekDates = [];
                dateIter = currentWeekStart;
                while (dateIter LTE currentWeekEnd) {
                    arrayAppend(weekDates, dateFormat(dateIter, "yyyy-mm-dd"));
                    dateIter = dateAdd("d", 1, dateIter);
                }

                // Store week information in the weeks array
                if (week_no == 1)
                {
                    arrayAppend(weeks, {start = FirstWeekStart, end = currentWeekEnd, dates = weekDates});
                }
                else
                {
                    arrayAppend(weeks, {start = currentWeekStart, end = currentWeekEnd, dates = weekDates});
                }
                //arrayAppend(weeks, {start = currentWeekStart, end = currentWeekEnd, dates = weekDates});
                week_no = week_no + 1;
                // Move to the next week
                currentWeekStart = dateAdd("d", 7, currentWeekStart);
        }

    // Output the weeks and their dates
   // for (week in weeks) {
      //  writeOutput("Week from " & dateFormat(week.start, "yyyy-mm-dd") & " to " & dateFormat(week.end, "yyyy-mm-dd") & ":<br>");
      //  writeOutput("Dates: " & arrayToList(week.dates, ", ") & "<br><br>");
   // }
</cfscript>


<cfquery name="update_pAdmit" datasource="#Application.DataSrc#"  result="result">
       UPDATE #Request.prefix_db_agency#.pAuths_weeks SET 
       Status   = <cfqueryparam VALUE="2"  cfsqltype="cf_sql_integer">,    
       Date_Change = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">,
       Change_by = <cfqueryparam VALUE="#session.employeeid#"  cfsqltype="cf_sql_integer">
       WHERE  Auth_ID = <cfqueryparam VALUE="#pAuth_ID#"  cfsqltype="cf_sql_integer">
       AND Status = 0
</cfquery>

<cfloop from="1" to="#arraylen(weeks)#" index="i">
        <cfset week_start = #weeks[i].start# >
        <cfset week_end = #weeks[i].end# >
       <!--- <cfoutput>
            INSERT INTO #Request.prefix_db_agency#.pAuths_weeks
            (
                Auth_ID, Week_No, Week_Start, Week_End, Date_Create, Created_by, Date_Change, Change_by
            )
            VALUES
            (
               "#pAuth_ID#",
               "#i#",
                "#week_start#",
                "#week_end#",
                "#now()#",
                "#session.employeeid#",
                "#now()#",
                "#session.employeeid#"
            )
        </cfoutput>--->
        <cfquery  name="getauthorizations" datasource="#Application.DataSrc#">
            INSERT INTO #Request.prefix_db_agency#.pAuths_weeks
            (
                Auth_ID, Week_No, Week_Start, Week_End,
                <cfif IsDefined("form.WeeK_Total_visits" & i) AND FORM['WeeK_Total_visits'&i] GT 0>
                    Total_visits,
                </cfif>
                 Date_Create, Created_by, Date_Change, Change_by
            )
            VALUES
            (
                <cfqueryparam cfsqltype="cf_sql_integer" value="#pAuth_ID#">,
                <cfqueryparam cfsqltype="cf_sql_integer" value="#i#">,
                <cfqueryparam cfsqltype="cf_sql_date" value="#week_start#">,
                <cfqueryparam cfsqltype="cf_sql_date" value="#week_end#">,
                <cfif IsDefined("form.WeeK_Total_visits" & i) AND FORM['WeeK_Total_visits'&i] GT 0>
                    <cfqueryparam cfsqltype="cf_sql_integer" value="#FORM['WeeK_Total_visits'&i]#">,
                </cfif>
                <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">,
                <cfqueryparam VALUE="#session.employeeid#"  cfsqltype="cf_sql_integer">,
                <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">,
                <cfqueryparam VALUE="#session.employeeid#"  cfsqltype="cf_sql_integer">
            )
        </cfquery>

</cfloop>


