using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Web.Services;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

public partial class module_weeklyoff : System.Web.UI.Page
{
    public static string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    public static MySqlConnection con = new MySqlConnection(constring);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string savecheck(string week )
    {
        string data = "1";

        using (MySqlConnection con = new MySqlConnection(constring))
        {
            string query = "Insert into weekly_off(week) values(@week)";
            using (MySqlCommand cmd = new MySqlCommand(query, con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                cmd.Parameters.AddWithValue("@week", week);
                cmd.ExecuteNonQuery();
                con.Close();
                con.Dispose();
            }
        }
        return data;
    }
    [WebMethod]
    public static object UpdateDays(string Id, string week)
    {
        string data = "";

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
        {
            using (MySqlCommand cmd = new MySqlCommand("update weekly_off set week= @week  where id= '" + Id + "'", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                cmd.Parameters.AddWithValue("@week", week);
                cmd.ExecuteNonQuery();
                cmd.Connection = con;
                con.Close();
                con.Dispose();
                cmd.Dispose();
            }
        }
        return data;
    }
    [WebMethod]
    public static string EditData(string Id)
    {
        string _data = "";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("select * from weekly_off where id='" + Id + "'", con))
            {
                DataSet ds = new DataSet();
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        _data = JsonConvert.SerializeObject(ds.Tables[0]);
                    }
                    con.Close();
                    con.Dispose();
                }
            }
        }
        return _data;
    }
    [WebMethod]
    public static string savedata(string week , string temptenme)
    {
        string data = "1";

        using (MySqlConnection con = new MySqlConnection(constring))
        {
            string query = "Insert into weekly_off(week,temptenme) values(@week,@temptenme)";
            using (MySqlCommand cmd = new MySqlCommand(query, con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                cmd.Parameters.AddWithValue("@week", week);
                cmd.Parameters.AddWithValue("@temptenme", temptenme);
                cmd.ExecuteNonQuery();
                con.Close();
                con.Dispose();
            }
        }
        return data;
    }
    
 
    [WebMethod]
    public static object UpdateData(string id, string week , string temptenme)
    {
        string data = "";
       
        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
        {
            using (MySqlCommand cmd = new MySqlCommand("update weekly_off set week= @week,temptenme=@temptenme  where id=@id", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                cmd.Parameters.AddWithValue("@week", week);
                cmd.Parameters.AddWithValue("@temptenme", temptenme);
                cmd.ExecuteNonQuery();
                cmd.Connection = con;
                con.Close();
                con.Dispose();
                cmd.Dispose();
            }
        }
        return data;
    }
    [WebMethod]
    public static object GetData()
    {
        string data = " ";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("select * from  weekly_off", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                DataSet ds = new DataSet();
                using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        data = JsonConvert.SerializeObject(ds.Tables[0]);
                    }
                    con.Close();
                    con.Dispose();
                    cmd.Dispose();
                }
            }
        }
        return data;
    }
    [WebMethod]
    public static object DeleteData(string id)
    {
        string data = "";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            if (con.State != ConnectionState.Open) { con.Open(); }
            using (MySqlCommand cmd = new MySqlCommand("delete from weekly_off where id='" + id + "'", con))
            {

                cmd.ExecuteNonQuery();
                using (MySqlDataAdapter dr = new MySqlDataAdapter(cmd))
                {
                    con.Close();
                    con.Dispose();
                    cmd.Dispose();

                }
            }
        }
        return data;
    }
    [WebMethod]
    public static string Getdata12(string clasr, string division)
    {
        string _data = "";
        {
           
                using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
                {

                    if (con.State != ConnectionState.Open) { con.Open(); }
                    using (MySqlCommand cmdpack = new MySqlCommand("SELECT   wo.*,sd.fname,sd.mname,sd.lname,sd.classd  FROM weekly_off AS wo LEFT JOIN student_detail AS sd ON wo.id=sd.id WHERE sd.classd='" + clasr + "' AND sd.division='" + division + "' ", con))
                    {
                        using (MySqlDataAdapter sda29 = new MySqlDataAdapter(cmdpack))
                        {
                            DataTable dt29 = new DataTable();
                            sda29.Fill(dt29);
                            con.Close();

                            if (dt29.Rows.Count > 0)
                            {
                                _data = JsonConvert.SerializeObject(dt29);
                                return _data;
                            }

                        }
                    }
                }

            }
                  
        return _data;
    }
    [WebMethod]
    public static object gettemplate(string id)
    {
        string data = "";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand opd = new MySqlCommand("Select * from weekly_off  where id = '" + id + "' ", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter rdr = new MySqlDataAdapter(opd))
                {
                    DataSet ds = new DataSet();
                    rdr.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        data = JsonConvert.SerializeObject(ds.Tables[0]);
                    }
                }
                con.Close();
                con.Dispose();
            }
        }
        return data;
    }
}