using System;
using System.Data;
using System.Data.OleDb;
using DayPilot.Web.Ui.Events;
using DayPilot.Web.Ui.Events.Scheduler;

public partial class SchedulerMonth : System.Web.UI.Page
{
    OleDbConnection bag;
    static int arttır = 0;
    static int cikar = 0;
    DateTime tarih1, tarih2;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Giris"] != "Yapıldı")
        {
            Response.Redirect("Giris.aspx");
        }
        if (!IsPostBack)
        {
            arttır = 0;
            DayPilotScheduler1.StartDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            DayPilotScheduler1.Days = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
            Label1.Text = DayPilotScheduler1.StartDate.ToString("MMMM yyyy");
            DayPilotScheduler1.DataSource = getData();
            DataBind();
        }
    }

    protected DataTable getData()
    {
        DataTable dt;
        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("resource", 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["resource"] = "A";
            dr["id"] = oku1["Adi"].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;

    }

    protected void DayPilotScheduler1_BeforeEventRender(object sender, BeforeEventRenderEventArgs e)
    {
        string color = e.DataItem["color"] as string;
        if (!String.IsNullOrEmpty(color))
        {
            e.DurationBarColor = color;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        arttır += 1;
        tarih1 = DateTime.Now.AddMonths(arttır);
        DateTime zaman = Convert.ToDateTime(tarih1);
        DayPilotScheduler1.StartDate = new DateTime(DateTime.Today.Year, zaman.Month, 1);
        DayPilotScheduler1.Days = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
        Label1.Text = DayPilotScheduler1.StartDate.ToString("MMMM yyyy");
        DayPilotScheduler1.DataSource = getData();
        DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        cikar += 1;
        tarih2 = DateTime.Now.AddMonths(-cikar);
        DateTime zaman = Convert.ToDateTime(tarih2);
        DayPilotScheduler1.StartDate = new DateTime(DateTime.Today.Year, zaman.Month, 1);
        DayPilotScheduler1.Days = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
        Label1.Text = DayPilotScheduler1.StartDate.ToString("MMMM yyyy");
        DayPilotScheduler1.DataSource = getData();
        DataBind();
    }
}
