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

    }
    [WebMethod]

    public static object Getdata(string clasr, string division, string acdyear)
    {
        string data = " ";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("SELECT inst.*,class_master.class as clsname,division_master.division as divsname,package_detail.*,package_detail.id as pkgid,date_format(st.today,'%d-%m-%Y') as admdate,date_format(inst.chequedate,'%d-%m-%Y') as chequesdt,(SELECT (COALESCE(SUM(installmentstudent.paidamt),0)) FROM installmentstudent where installmentstudent.studid = st.studid and installmentstudent.classids ='" + clasr + "' and installmentstudent.divisionsids= '" + division + "' and installmentstudent.yearsids = '" + acdyear + "' and installmentstudent.paymode = 'cheque') AS totalpaidamt,st.* FROM installmentstudent AS inst LEFT JOIN student_detail AS st ON inst.studid = st.studid left join class_master on st.classd = class_master.id left join division_master on st.division = division_master.id left join package_detail on st.packageid = package_detail.id WHERE inst.paymode='cheque' and inst.classids ='" + clasr + "' and inst.divisionsids= '" + division + "' and inst.yearsids = '" + acdyear + "' GROUP BY inst.studid ", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                DataSet ds = new DataSet();
                using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                {
                    da.Fill(ds); con.Close();
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        data = JsonConvert.SerializeObject(ds.Tables[0]);
                    }


                }
            }
        }
        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; }
    }
      [WebMethod]

      public static object Export(string clasr, string division, string acdyear)
      {
          string data = " ";
          using (MySqlConnection con = new MySqlConnection(constring))
          {
              using (MySqlCommand cmd = new MySqlCommand("SELECT inst.*,class_master.class as clsname,division_master.division as divsname,package_detail.*,package_detail.id as pkgid,date_format(st.today,'%d-%m-%Y') as admdate,date_format(inst.chequedate,'%d-%m-%Y') as chequesdt,(SELECT (COALESCE(SUM(installmentstudent.paidamt),0)) FROM installmentstudent where installmentstudent.studid = st.studid and installmentstudent.classids ='" + clasr + "' and installmentstudent.divisionsids= '" + division + "' and installmentstudent.yearsids = '" + acdyear + "' and installmentstudent.paymode = 'cheque') AS totalpaidamt,st.* FROM installmentstudent AS inst LEFT JOIN student_detail AS st ON inst.studid = st.studid left join class_master on st.classd = class_master.id left join division_master on st.division = division_master.id left join package_detail on st.packageid = package_detail.id WHERE inst.paymode='cheque' and inst.classids ='" + clasr + "' and inst.divisionsids= '" + division + "' and inst.yearsids = '" + acdyear + "' GROUP BY inst.studid ", con))
              {
                  if (con.State != ConnectionState.Open) { con.Open(); }
                  DataSet ds = new DataSet();
                  using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                  {
                      da.Fill(ds); con.Close();
                      if (ds.Tables[0].Rows.Count > 0)
                      {
                          data = JsonConvert.SerializeObject(ds.Tables[0]);
                      }


                  }
              }
          }
          return data;
      } 
    [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> 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 clsid)
    {
        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 = '" + clsid + "'", 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;
    }

}