controls/script-add-schedule-modal.ps1

function New-UDScriptScheduleModal {
    param (
        [Parameter(Mandatory = $true)]
        $Script
    )
    
    Show-UDModal -Content {
        New-UDPageHeader -Text ("Creating New Schedule")
        New-UDParagraph -Text "Select a tab that represents the schedule type to apply to the script."
        New-UDTabContainer -Tabs {
            New-UDTab -Text 'Routine Schedule' -Content {

                New-UDCard -Content {

                    New-UDPageHeader -Text ("CRON Schedule")
                    New-UDParagraph -Text "Specify a CRON expression. You can use a tool like crontab.guru to generate cron expressions."
                    New-UDParagraph -Text " " #Spacer

                    New-UDTextbox -Id "AddCronScheduleTextBox" -Label "CRON" -Type text -Value "* * * * *"
                    #TODO - TIMEZONE SELECT

                    New-UDElement -Tag div -Attributes @{
                        style = @{
                            textAlign = "right"
                        }
                    } -Content {

                        New-UDButton -Text "Create Schedule" -Icon clock -OnClick {
    
                            $CRONScheduleInput = ((Get-UDElement -Id 'AddCronScheduleTextBox').Attributes["value"])

                            if ($null -ne $CRONScheduleInput) {
                                $Cron = $CRONScheduleInput
                            }
                                
                            if ($null -ne $Script -and $null -ne $Cron) {
                        
                                New-UASchedule -Script $Script -Cron $Cron
                                #Sync the Various Grids "attached to this"
                                Sync-UDElement -Id "schedule-grid" -Broadcast
                                Sync-UDElement -Id "scriptdetails-page" -Broadcast
                                Invoke-UDRedirect -Url "/script/$($Script.Id)"
                            }
                            else {
                                New-UDInputAction -Toast "Invalid CRON Selection!"
                            }
                        }
                    }
                }            
            }

            New-UDTab -Text "Easy Schedule" -Content {
                
                New-UDCard -Content {
                     # EASY CRON
            
                
                    New-UDPageHeader -Text ("Continuous Schedule")
                    New-UDParagraph -Text "Select a Preset Schedule from the selection list"
                    New-UDParagraph -Text " " #Spacer
            
                    New-UDSelect -Id "AddEasyScheduleSelect" -Label "Interval Type" -Option {
                        New-UDSelectOption -Name "Every Minute" -Value "Every Minute" -Selected $true
                        New-UDSelectOption -Name "Every 15 Minutes" -Value "Every 15 Minutes"
                        New-UDSelectOption -Name "Every 30 Minutes" -Value "Every 30 Minutes"
                        New-UDSelectOption -Name "Every Hour" -Value "Every Hour"
                        New-UDSelectOption -Name "Every 4 Hours" -Value "Every 4 Hours"
                        New-UDSelectOption -Name "Every 8 Hours" -Value "Every 8 Hours"
                        New-UDSelectOption -Name "Every Day at Midnight" -Value "Every Day at Midnight"
                        New-UDSelectOption -Name "Saturdays at Midnight" -Value "Saturdays at Midnight"
                        New-UDSelectOption -Name "Every Hour between 8am and 5pm" -Value "Every Hour between 8am and 5pm"
                        New-UDSelectOption -Name "Every Hour Outside of 8am and 5pm" -Value "Every Hour Outside of 8am and 5pm"
                    }


                    New-UDElement -Tag div -Attributes @{
                        style = @{
                            textAlign = "right"
                        }
                    } -Content {
                        New-UDButton -Text "Create Schedule" -Icon clock -OnClick {
                            $EasyScheduleInput = ((Get-UDElement -Id 'AddEasyScheduleSelect').Attributes["value"])
                            if ($null -ne $EasyScheduleInput -and $EasyScheduleInput -ne "") {
                                $Cron = switch ($EasyScheduleInput) {
                                    "Every Minute" { "* * * * *"; break }
                                    "Every 15 Minutes" { "*/15 * * * *"; break }
                                    "Every 30 Minutes" { "*/30 * * * *"; break }
                                    "Every Hour" { "0 * * * *"; break }
                                    "Every 4 Hours" { "0 */4 * * *"; break }
                                    "Every 8 Hours" { "0 */8 * * *"; break }
                                    "Every Day at Midnight" { "0 0 * * *"; break }
                                    "Saturdays at Midnight" { "0 0 * * SAT"; break }
                                    "Every Hour between 8am and 5pm" { "*/60 8-17 * * *"; break }
                                    "Every Hour Outside of 8am and 5pm" { "*/60 0-8,17-23 * * *"; break }                            
                                }
                            }
                            
                            if ($null -ne $Script -and $null -ne $Cron) {
                                New-UASchedule -Script $Script -Cron $Cron
                                #Sync the Various Grids "attached to this"
                                Sync-UDElement -Id "schedule-grid" -Broadcast
                                Sync-UDElement -Id "scriptdetails-page" -Broadcast
                                Invoke-UDRedirect -Url "/script/$($Script.Id)"
                            }
                            else {
                                New-UDInputAction -Toast "Invalid CRON Selection!"
                            }
                        }
                    }

                }
            
            }


            New-UDTab -Text "Continuous Schedule" -Content {
                
                New-UDCard -Content {

                    
                    New-UDPageHeader -Text ("Continuous Schedule")
                    New-UDParagraph -Text "You can schedule a script to run continuously. A new job will start as soon as the previous run of the script has finished. You can also configure the schedule to delay before starting the next job."
                    New-UDParagraph -Text " " #Spacer

                    $Session:ContinuousIntervalType = 'DelayHour'
                    New-UDSelect -Id "AddContinuousScheduleType" -Label "Interval Type" -Option {
                        New-UDSelectOption -Name 'Hours' -Value 'DelayHour' -Selected $true
                        New-UDSelectOption -Name 'Minutes' -Value 'DelayMinute'
                        New-UDSelectOption -Name 'Seconds' -Value 'DelaySecond'
                    } -OnChange{
                        $Session:ContinuousIntervalType = $EventData
                    }
                

                    New-UDTextbox -Id "AddContinuousScheduleValue" -Label "Interval" -Type text -Value 100
                 
                    New-UDElement -Tag div -Attributes @{
                        style = @{
                            textAlign = "right"
                        }
                    } -Content {
                        New-UDButton -Text "Create Schedule" -Icon clock -OnClick {
                            # Get Input Data
                            $ContinuousIntervalValue = ((Get-UDElement -Id 'AddContinuousScheduleValue').Attributes["value"])
                            #$ContinuousIntervalType = ((Get-UDElement -Id 'AddContinuousScheduleType').Attributes["value"])
                            $ContinuousIntervalType = $Session:ContinuousIntervalType

                            if($null -ne $ContinuousIntervalType -and $ContinuousIntervalType -ne "" -and $null -ne $ContinuousIntervalValue -and $ContinuousIntervalValue -ne "")
                            {
                                switch ($ContinuousIntervalType) {
                                    "DelayHour" {New-UASchedule -Continuous -DelayHour $ContinuousIntervalValue -Script $Script; break}
                                    "DelayMinute" {New-UASchedule -Continuous -DelayMinute $ContinuousIntervalValue -Script $Script; break}
                                    "DelaySecond" {New-UASchedule -Continuous -DelaySecond $ContinuousIntervalValue -Script $Script; break}
                                }
            
                                #Sync the Various Grids "attached to this"
                                Sync-UDElement -Id "schedule-grid" -Broadcast
                                Sync-UDElement -Id "scriptdetails-page" -Broadcast
                                Invoke-UDRedirect -Url "/script/$($Script.Id)"
                            }
                            

                        }
                    }
                
                }
            }

            New-UDTab -Text "Run Once" -Content {
                
                New-UDCard -Content {
                    New-UDPageHeader -Text ("Single Instance Schedule")
                    New-UDParagraph -Text "You can schedule a script to once and only once! A new job will start at the time specified and never again."
                    New-UDParagraph -Text " " #Spacer

                    New-UDTextbox -Id "AddSingleScheduleDateValue" -Label "Date" -Type text -Value "12/12/2019"
                    New-UDTextbox -Id "AddSingleScheduleTimeValue" -Label "Time" -Type text -Value "9:30:01 PM"

                    New-UDElement -Tag div -Attributes @{
                        style = @{
                            textAlign = "right"
                        }
                    } -Content {
                        New-UDButton -Text "Not Implemented... YET" -Icon clock -OnClick {
                            # Get Input Data
                        }
                    }

                }
            
            }

        }
    }
}