New-UDPage -Url "/ServerBuildRequestV1" -Name "ServerBuildRequestV1" -Content {
New-UDForm -Content {
    New-UDTypography -Text 'To sync vCenter to Sharepoint, enter a VMware VM name below and click the Submit button'
    New-UDRow -Columns {
        New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
            New-UDTextbox -Id 'VMName'
        }
    }          
} -OnValidate {
    $FormContent = $EventData
    if ($FormContent.VMName -eq $null -or $FormContent.VMName -eq '') {
        New-UDFormValidationResult -ValidationError "VMName is required"
    }
    else {
        New-UDFormValidationResult -Valid
    }
} -OnSubmit {
    Show-UDToast -Message ($EventData.VMName + " sync in progress.") -Duration 5000
            
    Invoke-PSUScript -Script 'Sync-ITDVMwareVMMetadataToSharePoint.ps1' -VMName $EventData.VMName -Wait | Out-Null

    Show-UDToast -Message ($EventData.VMName + " sync finished.") -Duration 10000
} -OnProcessing {
    New-UDTypography -Text ("Syncing " + $EventData.VMName + " ...")
    New-UDProgress
}

New-UDTypography -Text "`nServer Build Request request items (RITMs) submitted after 2024/06/25"

$ServerInstance = "itdintsql22p1.nd.gov\INTSQL22P1"
$Database = "ITD-Systems-Automation"
$Table = 'ServiceNow_RitmDump_ServerBuildRequestV1'

New-ITDServiceNowSession -Environment Production -Credential $Secret:snow_vmcred

$SqlQuery = "SELECT [RitmNum],
    [RitmSysId],
    [opened_at],
    [requested_for],
    [request_type],
    [environment]
      ,[host_name_ref]
      ,[host_name]
  FROM [ITD-Systems-Automation].[dbo].[ServiceNow_RitmDump_ServerBuildRequestV1]"
$SqlRecords = Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Query $SqlQuery -Credential $Secret:sql_itdpsu1 -Verbose



New-UDTable -Dense -ShowPagination -PageSize 20 -Size small -Data ($SqlRecords | Sort-Object -Descending opened_at) -Columns @(
    New-UDTableColumn -Property 'NewSP' -Title 'New' -Render {
        New-UDButton -Id "btnNewSP$($EventData.host_name)" -Text "New" -Color Secondary -Size small -OnClick {
            Show-UDToast -Message ("Creating new record " + $EventData.host_name) -Duration 5000
            switch($EventData.request_type){
                'New' {
                    $Fqdn = $EventData.host_name
                    $RitmNum = $EventData.RitmNum

                    Invoke-PSUScript -Script 'New-ITDVMwareSharePointVMRecordFromRITM.ps1' -Fqdn $Fqdn -RitmNum $RitmNum
                }
                'Removal' {
                    $Fqdn = $EventData.host_name
                    $RitmNum = $EventData.RitmNum

                }
            }
            
            }
    }
    New-UDTableColumn -Property 'SyncSP' -Title 'SyncVCtoSP' -Render {
        New-UDButton -Id "btnSyncSP$($EventData.host_name)" -Text "Sync" -Color primary -Size small -OnClick {
            Show-UDToast -Message ("Sync to SharePoint - " + $EventData.host_name) -Duration 5000
            switch($EventData.request_type){
                'Change' {
                    $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $EventData.RitmNum -IncludeVariableSet
                    ForEach($row in $Ritm.VariableSet){
                        $Cmdb_Ci = Get-ITDServiceNowRecord -Table 'cmdb_ci_server' -SysId $row.host_name_ref
                        If ($Cmdb_Ci.name.display_value -eq $EventData.host_name){
                            $Fqdn = $Cmdb_Ci.fqdn.display_value
                        }
                    }
                    Invoke-PSUScript -Script 'Sync-ITDVMwareVMMetadataToSharePoint.ps1' -VMName $Fqdn -Wait | Out-Null
                }
                'New' {
                    $Fqdn = $EventData.host_name
                }
            }
            
            }
    }

    New-UDTableColumn -Property "ImplSRM" -Title 'ImplSRMtoSP' -Render {
        New-UDButton -Id "btnImplSRM$($EventData.host_name)" -Text "SRM" -Color secondary -Size small -OnClick {
            Show-UDToast -Message ("Sync to SharePoint - " + $EventData.host_name) -Duration 5000
            switch($EventData.request_type){
                'Change' {
                    $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $EventData.RitmNum -IncludeVariableSet
                    ForEach($row in $Ritm.VariableSet){
                        $Cmdb_Ci = Get-ITDServiceNowRecord -Table 'cmdb_ci_server' -SysId $row.host_name_ref
                        If ($Cmdb_Ci.name.display_value -eq $EventData.host_name){
                            $Fqdn = $Cmdb_Ci.fqdn.display_value
                        }
                    }
                }
                'New' {
                    $Fqdn = $EventData.host_name
                }
            }
            Invoke-PSUScript -Script 'Sync-ITDVMwareVMMetadataToSharePoint.ps1' -VMName $Fqdn -SRMImplemented -Wait | Out-Null
            }
    }


    New-UDTableColumn -Property 'RitmUrl' -Title 'RitmUrl' -Render {
        New-UDLink -Url ("https://northdakota.service-now.com/nav_to.do?uri=sc_req_item.do?sys_id=" + $EventData.RitmSysId) -Id "lnkRitm" -Content {
            New-UDImage -Url "https://psuniversal.nd.gov/PSUniversal_Extras/servicenow_logo_icon_168835.png" -Height 20 -Width 20
        }
    }

    New-UDTableColumn -Property 'RitmNum' -Title 'RitmNum' -ShowFilter
    New-UDTableColumn -Property 'opened_at' -Title 'opened_at' -ShowFilter
    New-UDTableColumn -Property 'host_name' -Title 'host_name' -ShowFilter        
    New-UDTableColumn -Property 'requested_for' -Title 'requested_for' -ShowFilter
    New-UDTableColumn -Property 'request_type' -Title 'request_type' -ShowFilter
    New-UDTableColumn -Property 'environment' -Title 'environment' -ShowFilter
    #New-UDTableColumn -Property 'additional_comments' -Title 'additional_comments' -ShowFilter
)
} -Icon @{
		id = '7d67b921-68aa-4085-84e8-025e92671771'
		type = 'icon'
	}