<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
        <script type="text/javascript">
            function rowDropping(sender, eventArgs) {
                // Fired when the user drops a grid row
                var htmlElement = eventArgs.get_destinationHtmlElement();
                var scheduler = $find('<%= RadScheduler1.ClientID %>');

                if (isPartOfSchedulerAppointmentArea(htmlElement)) {
                    // The row was dropped over the scheduler appointment area
                    // Find the exact time slot and save its unique index in the hidden field
                    var timeSlot;

                    if ($telerik.$(htmlElement).parents(".rsApt").length != 0)
                        timeSlot = scheduler.getAppointmentFromDomElement(htmlElement).get_timeSlot();
                    else
                        timeSlot = scheduler._activeModel.getTimeSlotFromDomElement(htmlElement);

                    $get("<%=TargetSlotHiddenField.ClientID %>").value = timeSlot.get_index();

                    // The HTML needs to be set in order for the postback to execute normally
                    eventArgs.set_destinationHtmlElement("<%=TargetSlotHiddenField.ClientID %>");
                }
                else {
                    // The node was dropped elsewhere on the document
                    eventArgs.set_cancel(true);
                }
            }

            function isPartOfSchedulerAppointmentArea(htmlElement) {
                // Determines if an html element is part of the scheduler appointment area
                // This can be either the rsContent or the rsAllDay div (in day and week view)
                return $telerik.$(htmlElement).parents().is("div.rsAllDay") ||
                                   $telerik.$(htmlElement).parents().is("div.rsContent")
            }

            function onRowDoubleClick(sender, args) {
                sender.get_masterTableView().editItem(args.get_itemIndexHierarchical());
            }
        </script>
    </telerik:RadScriptBlock>
</head>
<body>
    <form id="form1" runat="server">
        <input type="hidden" runat="server" id="TargetSlotHiddenField" />
        <telerik:RadScriptManager runat="server"></telerik:RadScriptManager>
        <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" EnableAJAX="true">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadScheduler1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadScheduler1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>

        <telerik:RadSplitter runat="server" ID="RadSplitter1" Height="552px" Width="880px"
            CssClass="exampleContainer" Orientation="Horizontal" Skin="Office2007">
            <telerik:RadPane runat="server" ID="TopPane" Height="400px" Scrolling="None">
                <telerik:RadScheduler ID="RadScheduler1" runat="server" Skin="Office2007" GroupBy="Speaker"
                    Width="100%" RowHeaderWidth="52" OverflowBehavior="Auto" ShowFooter="false" DataSourceID="SchedulerDataSource"
                    DataKeyField="AppointmentID" DataStartField="Start" DataEndField="End" DataSubjectField="Subject"
                    CustomAttributeNames="Due, Priority" RowHeight="37px" OnAppointmentCommand="RadScheduler1_AppointmentCommand"
                    OnAppointmentInsert="RadScheduler1_AppointmentInsert">
                    <AdvancedForm Modal="true"></AdvancedForm>
                    <AppointmentTemplate>
                        <%# Eval("Subject") %>
                        <asp:Button runat="server" ID="UnscheduleAppointment" CssClass="simpleButton" CommandName="Unschedule"
                            Text="&nbsp;" ToolTip="Unschedule this appointment"></asp:Button>
                    </AppointmentTemplate>
                    <TimeSlotContextMenuSettings EnableDefault="true"></TimeSlotContextMenuSettings>
                    <AppointmentContextMenuSettings EnableDefault="true"></AppointmentContextMenuSettings>
                    <ResourceTypes>
                        <telerik:ResourceType KeyField="SpeakerID" Name="Speaker" TextField="FullName" ForeignKeyField="SpeakerID"
                            DataSourceID="SpeakersDataSource"></telerik:ResourceType>
                    </ResourceTypes>
                </telerik:RadScheduler>

            </telerik:RadPane>
            <telerik:RadSplitBar runat="Server" ID="RadSplitBar1">
            </telerik:RadSplitBar>
            <telerik:RadPane runat="server" ID="BottomPane">
                <div style="float: left; height: 100%; width: 55px; background: #E3EFFF; text-align: center; border-right: 1px solid #6593CF;">
                    <img src="images/tasks.gif" alt="" />
                </div>
                <div style="margin-left: 56px; border: none;">
                    <telerik:RadGrid runat="server" ID="RadGrid1" DataSourceID="GridDataSource" GridLines="None"
                        AutoGenerateColumns="False" OnRowDrop="RadGrid1_RowDrop" Skin="Office2007" Style="border: none; outline: 0"
                        Height="100%" AllowAutomaticInserts="True" AllowAutomaticUpdates="true"
                        AllowAutomaticDeletes="true" ShowFooter="true" OnItemCreated="RadGrid1_ItemCreated"
                        OnItemCommand="RadGrid1_ItemCommand" AllowSorting="true">
                        <ClientSettings AllowRowsDragDrop="True">
                            <Selecting AllowRowSelect="True"></Selecting>
                            <ClientEvents OnRowDropping="rowDropping" OnRowDblClick="onRowDoubleClick"></ClientEvents>
                        </ClientSettings>
                        <MasterTableView DataKeyNames="AppointmentID,SpeakerID" InsertItemDisplay="Bottom" EditMode="InPlace">
                            <SortExpressions>
                                <telerik:GridSortExpression FieldName="Due" SortOrder="Ascending"></telerik:GridSortExpression>
                            </SortExpressions>
                            <Columns>
                                <telerik:GridTemplateColumn DataField="Subject" HeaderText="Task">
                                    <ItemTemplate>
                                        <asp:Label runat="server" ID="Label1" Text='<%# Bind("Subject") %>'></asp:Label>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:LinkButton Text="Add new task" CommandName="<%# RadGrid.InitInsertCommandName %>"
                                            runat="server" ID="LinkButton1"></asp:LinkButton>
                                    </FooterTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox runat="Server" ID="TextBox1" Text='<%# Bind("Subject") %>' Width="100%"></asp:TextBox>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridDateTimeColumn DataField="Due" HeaderText="Due Date" DataFormatString="{0:d}"
                                    HeaderStyle-Width="200px">
                                </telerik:GridDateTimeColumn>
                                <telerik:GridTemplateColumn DataField="Priority" HeaderText="Priority" HeaderStyle-Width="200px">
                                    <ItemTemplate>
                                        <asp:Label runat="Server" ID="Label2" Text='<%#Bind("Priority") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <telerik:RadComboBox runat="server" ID="priorityComboBox" SelectedValue='<%#Bind("Priority") %>'
                                            Skin="Office2007" Width="80px">
                                            <Items>
                                                <telerik:RadComboBoxItem Text="High" Value="High"></telerik:RadComboBoxItem>
                                                <telerik:RadComboBoxItem Text="Medium" Value="Medium"></telerik:RadComboBoxItem>
                                                <telerik:RadComboBoxItem Text="Low" Value="Low"></telerik:RadComboBoxItem>
                                            </Items>
                                        </telerik:RadComboBox>
                                        <asp:LinkButton ID="btnUpdate" Text="Save" runat="server" CommandName='<%# (Container is GridDataInsertItem) ? RadGrid.PerformInsertCommandName : RadGrid.UpdateCommandName %>'></asp:LinkButton>
                                        <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName='<%# RadGrid.CancelCommandName %>'></asp:LinkButton>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridButtonColumn ConfirmText="Delete this Appointment?" ButtonType="ImageButton"
                                    CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
                                    <HeaderStyle Width="20px"></HeaderStyle>
                                    <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
                                </telerik:GridButtonColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
                </div>
            </telerik:RadPane>
        </telerik:RadSplitter>

        <asp:SqlDataSource ID="SpeakersDataSource" runat="server"
            ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>" SelectCommand="SELECT * FROM [Speakers]"></asp:SqlDataSource>

        <asp:SqlDataSource ID="SchedulerDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:TelerikConnectionString35 %>"
            DeleteCommand="DELETE FROM [Appointments_DragAndDrop] WHERE [AppointmentID] = @AppointmentID"
            InsertCommand="INSERT INTO [Appointments_DragAndDrop] ([Start], [End], [Subject], [SpeakerID]) VALUES (@Start, @End, @Subject, @SpeakerID)"
            SelectCommand="SELECT * FROM [Appointments_DragAndDrop]" UpdateCommand="UPDATE [Appointments_DragAndDrop] SET [Start] = @Start, [End] = @End, [Subject] = @Subject, [SpeakerID] = @SpeakerID WHERE [AppointmentID] = @AppointmentID">
            <DeleteParameters>
                <asp:Parameter Name="AppointmentID" Type="Int32"></asp:Parameter>
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Start" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="End" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="Subject" Type="String"></asp:Parameter>
                <asp:Parameter Name="SpeakerID" Type="Int32"></asp:Parameter>
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="SpeakerID" Type="Int32"></asp:Parameter>
                <asp:Parameter Name="Start" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="End" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="Subject" Type="String"></asp:Parameter>
                <asp:Parameter Name="AppointmentID" Type="Int32"></asp:Parameter>

            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:TelerikConnectionString35 %>"
            DeleteCommand="DELETE FROM [Appointments_DragAndDrop] WHERE [AppointmentID] = @AppointmentID"
            InsertCommand="INSERT INTO [Appointments_DragAndDrop] ([Subject], [Due], [Priority], [SpeakerID] ) VALUES (@Subject, @Due, @Priority,  @SpeakerID)"
            SelectCommand="SELECT * FROM [Appointments_DragAndDrop] WHERE ([Start] IS NULL) AND ([End] IS NULL)"
            UpdateCommand="UPDATE [Appointments_DragAndDrop] SET [Subject] = @Subject, [Start] = @Start, [End] = @End, [Due] = @Due,  [Priority] = @Priority  WHERE [AppointmentID] = @AppointmentID">
            <DeleteParameters>
                <asp:Parameter Name="AppointmentID" Type="Int32"></asp:Parameter>
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Priority" Type="String"></asp:Parameter>
                <asp:Parameter Name="Due" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="Subject" Type="String"></asp:Parameter>
                <asp:Parameter Name="SpeakerID" Type="Int32"></asp:Parameter>
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Subject" Type="String"></asp:Parameter>
                <asp:Parameter Name="Start" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="End" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="Due" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="Priority" Type="String"></asp:Parameter>
                <asp:Parameter Name="AppointmentID" Type="Int32"></asp:Parameter>
                <asp:Parameter Name="SpeakerID" Type="Int32"></asp:Parameter>
            </UpdateParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>
