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_Confirmedregistration : System.Web.UI.Page
{
    public static string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    public static MySqlConnection con = new MySqlConnection(constring);
    public static string[] studidssplit;
    public static string studidss;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]

    public static object GetData(string studid)
    {
        string data = " ";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("select student_detail.*,student_detail.id as studids,class_master.class as clsname,division_master.division as divsname from  student_detail left join class_master on student_detail.classd = class_master.id left join division_master on student_detail.division = division_master.id  where student_detail.studid ='" + studid + "' and  packageid =''", 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 GetDataUpdate(string studid)
    {
        string data = " ";
      
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select student_detail.*,student_detail.id as studids,class_master.class as clsname,division_master.division as divsname from  student_detail left join class_master on student_detail.classd = class_master.id left join division_master on student_detail.division = division_master.id  where student_detail.studid ='"+ studid +"' and packageid != ''", 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 GetData1()
    {
        string data = " ";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("select package_detail.*,package_detail.id as pkgid,class_master.class as clsname,division_master.division as divsname,academic_year.fromdate,academic_year.todate from   package_detail left join class_master on package_detail.classdetail = class_master.id left join division_master on package_detail.divisiondet = division_master.id left join academic_year  on package_detail.year = academic_year.id", 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 GetDatapackage(string pkgids)
    {
        string data = "";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("select student_detail.*,student_detail.id as studids,class_master.class as clsname,division_master.division as divsname from  student_detail left join class_master on student_detail.classd = class_master.id left join division_master on student_detail.division = division_master.id  where student_detail.packageid ='" + pkgids + "' and  packageid =''", 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;
    }
    public class Department
    {
        public int DeptId { get; set; }
        public string DeptName { get; set; }
        public string DeptIds { get; set; }
        public string Initial { get; set; }
        public string year1 { get; set; } public string ptitle { get; set; }
        public string fname { get; set; }  public string mname { get; set; }  public string lname { get; set; }
    }
    [WebMethod]
    public static List<Department> PopulateDropDownList4()
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            using (MySqlCommand cmdnew = new MySqlCommand("SELECT *,date_format(fromdate,'%Y-%m-%d') as acdyear1,date_format(todate,'%Y-%m-%d') as acdyear2  from academic_year ", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                {
                    danew.Fill(dtnew);
                    con.Close();
                    if (dtnew.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtnew.Rows.Count; i++)
                        {
                            objDept.Add(new Department
                            {
                                DeptId = Convert.ToInt32(dtnew.Rows[i]["id"]),
                                DeptName = dtnew.Rows[i]["acdyear1"].ToString(),
                                year1 = dtnew.Rows[i]["acdyear2"].ToString(),
                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]
    public static List<Department> PopulateDropDownListstudent(string yrs1, string clsid, string divsid, string castes)
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();
        if (castes == "0")
        {
            using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
            {
                using (MySqlCommand cmdnew  = new MySqlCommand("SELECT * from student_detail where admyears ='" + yrs1 + "' and classd ='" + clsid + "' and division ='" + divsid + "' and  packageid = ''", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                    {
                        danew.Fill(dtnew);
                        con.Close();
                        if (dtnew.Rows.Count > 0)
                        {
                            for (int i = 0; i < dtnew.Rows.Count; i++)
                            {
                                objDept.Add(new Department
                                {

                                    DeptName = dtnew.Rows[i]["studid"].ToString(),
                                    ptitle = dtnew.Rows[i]["title"].ToString(),
                                    fname = dtnew.Rows[i]["fname"].ToString(),
                                    mname = dtnew.Rows[i]["mname"].ToString(),
                                    lname = dtnew.Rows[i]["lname"].ToString(),
                                });
                            }
                        }
                    }
                }
            }
        }
        else if (castes != "0")
        {
            using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
            {
                using (MySqlCommand cmdnew = new MySqlCommand("SELECT * from student_detail where admyears ='" + yrs1 + "' and classd ='" + clsid + "' and division ='" + divsid + "' and castcategory ='" + castes + "'", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                    {
                        danew.Fill(dtnew);
                        con.Close();
                        if (dtnew.Rows.Count > 0)
                        {
                            for (int i = 0; i < dtnew.Rows.Count; i++)
                            {
                                objDept.Add(new Department
                                {

                                    DeptName = dtnew.Rows[i]["studid"].ToString(),
                                    ptitle = dtnew.Rows[i]["title"].ToString(),
                                    fname = dtnew.Rows[i]["fname"].ToString(),
                                    mname = dtnew.Rows[i]["mname"].ToString(),
                                    lname = dtnew.Rows[i]["lname"].ToString(),
                                });
                            }
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]
    public static List<Department> PopulateDropDownList5()
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            using (MySqlCommand cmdnew = new MySqlCommand("SELECT * from class_master ", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                {
                    danew.Fill(dtnew);
                    con.Close();
                    if (dtnew.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtnew.Rows.Count; i++)
                        {
                            objDept.Add(new Department
                            {
                                DeptId = Convert.ToInt32(dtnew.Rows[i]["id"]),
                                DeptName = dtnew.Rows[i]["class"].ToString(),

                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]
    public static List<Department> PopulateDropDownList6(string clids)
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            using (MySqlCommand cmdnew = new MySqlCommand("SELECT * from division_master where classid = '" + clids + "' ", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                {
                    danew.Fill(dtnew);
                    con.Close();
                    if (dtnew.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtnew.Rows.Count; i++)
                        {
                            objDept.Add(new Department
                            {
                                DeptId = Convert.ToInt32(dtnew.Rows[i]["id"]),
                                DeptName = dtnew.Rows[i]["division"].ToString(),

                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]
    public static List<Department> PopulateDropDownListCaste()
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            using (MySqlCommand cmdnew = new MySqlCommand("SELECT * from fee_category", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                {
                    danew.Fill(dtnew);
                    con.Close();
                    if (dtnew.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtnew.Rows.Count; i++)
                        {
                            objDept.Add(new Department
                            {
                                DeptId = Convert.ToInt32(dtnew.Rows[i]["id"]),
                                DeptName = dtnew.Rows[i]["feesname"].ToString(),

                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]
    public static List<Department> PopulateDropDownList8(string yrs, string clsid, string divsid)
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            using (MySqlCommand cmdnew = new MySqlCommand("SELECT * from package_detail where year ='" + yrs + "' and classdetail ='" + clsid + "' and divisiondet = '"+ divsid +"'", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                {
                    danew.Fill(dtnew);
                    con.Close();
                    if (dtnew.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtnew.Rows.Count; i++)
                        {
                            objDept.Add(new Department
                            {
                                DeptId = Convert.ToInt32(dtnew.Rows[i]["id"]),
                                DeptName = dtnew.Rows[i]["packgenme"].ToString(),

                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
      [WebMethod]
    public static object updatestudetails(string studidsids, string classids, string divsid, string pkgid)
    {
        string data = "";
        studidssplit = studidsids.Split(',');
        for (int i = 0; i < studidssplit.Length; i++)
        {
            studidss = studidssplit[i].ToString();
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select * from   student_detail  where classd='" + classids + "' and division ='" + divsid + "' and id ='" + studidss + "' ", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    DataTable ds = new DataTable();
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        if (ds.Rows.Count > 0)
                        {
                            using (MySqlConnection con1 = new MySqlConnection(constring))
                            {
                                using (MySqlCommand cmd1 = new MySqlCommand("update student_detail set packageid=@packgenme where classd='" + classids + "' and division ='" + divsid + "' and id ='" + studidss + "' ", con1))
                                {
                                    if (con1.State != ConnectionState.Open) { con1.Open(); }
                                    cmd1.Parameters.AddWithValue("@packgenme", pkgid);

                                    cmd1.ExecuteNonQuery();

                                    con1.Close();

                                }
                            }
                        }
                    }
                }
            }
        }

        return data;
    }
}