using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Default6 : System.Web.UI.Page
{
    TelerikSchedulerDataContext telerik = new TelerikSchedulerDataContext();
       
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RebindScheduler(RadScheduler1, Convert.ToInt32(RadComboBox1.SelectedValue));
        }
      
    }
    protected void Page_Init(object sender, EventArgs e)
    {       
        ResourceType resRoom = new ResourceType("Room");
        resRoom.ForeignKeyField = "RoomID";
        resRoom.KeyField = "ID";
        resRoom.TextField = "RoomName";
        resRoom.DataSource = telerik.Rooms;        
        RadScheduler1.ResourceTypes.Add(resRoom);
    }
    protected void RadScheduler1_AppointmentInsert(object sender, SchedulerCancelEventArgs e)
    {
        RadScheduler scheduler = sender as RadScheduler;
        Resource room = e.Appointment.Resources.GetResourceByType("Room");
        int? roomId = room != null ? (int?) room.Key : null;

        telerik.insertAppointment1(e.Appointment.Subject, e.Appointment.Start, e.Appointment.End, roomId, e.Appointment.RecurrenceRule, (int?)e.Appointment.RecurrenceParentID);

        RebindScheduler(scheduler, Convert.ToInt32(RadComboBox1.SelectedValue));
    }
    protected void RadScheduler1_AppointmentUpdate(object sender, AppointmentUpdateEventArgs e)
    {
        RadScheduler scheduler = sender as RadScheduler;
        Resource room = e.ModifiedAppointment.Resources.GetResourceByType("Room");
        int? roomId = room != null ? (int?) room.Key : null;

        telerik.updateAppointment(Convert.ToInt32(e.Appointment.ID), e.ModifiedAppointment.Subject, e.ModifiedAppointment.Start, e.ModifiedAppointment.End, roomId, e.ModifiedAppointment.RecurrenceRule, (int?)e.ModifiedAppointment.RecurrenceParentID);

        RebindScheduler(scheduler, Convert.ToInt32(RadComboBox1.SelectedValue));
    }
    protected void RadScheduler1_AppointmentDelete(object sender, SchedulerCancelEventArgs e)
    {
        RadScheduler scheduler = sender as RadScheduler;
        telerik.deleteAppointment(Convert.ToInt32(e.Appointment.ID));
        RebindScheduler(scheduler, Convert.ToInt32(RadComboBox1.SelectedValue));
    }
    protected void RebindScheduler(RadScheduler scheduler, int RoomID)
    {
        scheduler.DataSource = telerik.AppointmentsFilteredByRoom(RoomID);
        scheduler.DataBind();
    }
    protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RebindScheduler(RadScheduler1, Convert.ToInt32(e.Value));
    }
    protected void RadScheduler1_FormCreated(object sender, SchedulerFormCreatedEventArgs e)
    {
        //When inserting a new appointment, you might want to preselect a value for 
        //the room's RadComboBox in the advanced form based on the currently selected room's 
        //filter RadCombobBox
        if (e.Container.Mode == SchedulerFormMode.AdvancedInsert)
        {
            RadComboBox resRoomDDL = (RadComboBox)e.Container.FindControl("ResRoom");
            resRoomDDL.SelectedIndex = Convert.ToInt16(RadComboBox1.SelectedValue);
        }
    }
}
