using System;
using System.Data;
using System.Data.OleDb;
using DayPilot.Web.Ui;
using DayPilot.Web.Ui.Enums;

public partial class _Default : System.Web.UI.Page
{
    OleDbConnection bag, bag2;
    int tarihekle = 0;
    int tarihcikar = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Giris"] != "Yapıldı")
        {
            Response.Redirect("Giris.aspx");
        }
        bag2 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/App_Data/BSoylu.mdb") + "");
        
            DayPilotCalendar1.StartDate = firstDayOfWeek(DateTime.Now, DayOfWeek.Monday);
            getir();      
       

    }
    protected void getir()
    {
            OleDbCommand komut2;
            string sql2;
            OleDbDataReader oku1;
            sql2 = "SELECT * FROM Veriler";
            komut2 = new OleDbCommand(sql2, bag2);
            bag2.Open();
            oku1 = komut2.ExecuteReader();
            while (oku1.Read())
            {
                DayPilotCalendar1.DataSource = getData();
            }
            DataBind();
            oku1.Close();
            bag2.Close();
    }
    protected DataTable getData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("start", typeof(DateTime));
        dt.Columns.Add("end", typeof(DateTime));
        dt.Columns.Add("name", typeof(string));
        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("color", typeof (string));

       
       
        bag = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/App_Data/BSoylu.mdb") + "");
                    OleDbCommand komut2;
                    string sql2;
                    OleDbDataReader oku1;
                    sql2 = "SELECT * FROM Veriler";
                    komut2 = new OleDbCommand(sql2, bag);
                    bag.Open();
                        oku1 = komut2.ExecuteReader();
                        while (oku1.Read())
                        {
                             DataRow dr = dt.NewRow();
                             dr["id"] = oku1["ID"].ToString();
                            dr["start"] = Convert.ToDateTime(oku1["Tarih"] + " " + oku1["Saat1"].ToString());
                            dr["end"] = Convert.ToDateTime(oku1["Tarih2"] + " " + oku1["Saat2"].ToString());
                            dr["name"] = oku1["Adi"].ToString();
                            dr["color"] = oku1["renk"].ToString();
                            dt.Rows.Add(dr); 
                            
                        }
                        
                        oku1.Close(); 
        bag.Close();
return dt;
    }

    /// <summary>
    /// Gets the first day of a week where day (parameter) belongs. weekStart (parameter) specifies the starting day of week.
    /// </summary>
    /// <returns></returns> 
    private static DateTime firstDayOfWeek(DateTime day, DayOfWeek weekStarts)
    {
        DateTime d = day;
        while (d.DayOfWeek != weekStarts)
        {
            d = d.AddDays(-1);
        }

        return d;
    }

    protected void DayPilotCalendar1_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Calendar.BeforeEventRenderEventArgs e)
    {
        string color = e.DataItem["color"] as string;
        if (!String.IsNullOrEmpty(color))
        {
            e.DurationBarColor = color;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        tarihekle += 7;
        DayPilotCalendar1.StartDate = firstDayOfWeek(DateTime.Now.AddDays(+7), DayOfWeek.Monday);
        getir();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        
        DayPilotCalendar1.StartDate = firstDayOfWeek(DateTime.Now.AddDays(-7), DayOfWeek.Monday);
        getir();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (DropDownList1.SelectedValue)
        {
            case "hide":
                DayPilotCalendar1.HeightSpec = HeightSpecEnum.BusinessHoursNoScroll;
                break;
            case "hideifpossible":
                DayPilotCalendar1.HeightSpec = HeightSpecEnum.Full;
                DayPilotCalendar1.HideFreeCells = true;
                break;
            case "show":
                DayPilotCalendar1.HeightSpec = HeightSpecEnum.Full;
                DayPilotCalendar1.HideFreeCells = false;
                break;
        }
    }
}
