using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/****************************************************************************************************************************
* Project Name    : TimeCheck                    Started Date  : 18-July-2007
* Module          : Leave Application            Last Modified : 29-Aug-2007
* Programmer      : Prabakaran.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_AjaxLeaveOptions : Base.BasePage
{
    //Cls_Connect cl = new Cls_Connect();
    DataSet dsLeaveTypes = new DataSet();
    DataSet dsHolidays = new DataSet();
    DataSet dsLeaveTypesLOPDays = new DataSet();
    int ApplicationYearsetting = 0;
    string LeaveFromDate = string.Empty;
    string LeaveToDate = string.Empty;
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    cls_Common objclsCommon = new cls_Common();
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        if (!IsPostBack)
        {
            Session["sessionLeaveTypes"] = null;
            string leaveSource = Request.QueryString["leaveSource"];
            int EmployeeId = Convert.ToInt32(Request.QueryString["EmpId"]);
            //int levelid = Convert.ToInt16(Request.QueryString["LevelId"]);
            ApplicationYearsetting = Convert.ToInt32(Request.QueryString["ApplicationYearsetting"]);
            DataSet dsEmployeeDetails = new DataSet();
            dsEmployeeDetails = objLeaveApplicationEventData.GetEmployeeDetails(Convert.ToInt32(EmployeeId));
            int EmployeeLevel = Convert.ToInt32(dsEmployeeDetails.Tables[0].Rows[0]["LevelId"]);
            //SqlParameter pLeaveSource = new SqlParameter("@leaveSource",leaveSource);
            //SqlParameter pEmpId = new SqlParameter("@EmployeeId",EmployeeId);
            //SqlParameter[] pParams ={pEmpId};
            if (leaveSource == "Leave")
            {
                LeaveFromDate = Request.QueryString["LeaveFromDate"].ToString();
                LeaveToDate = Request.QueryString["LeaveToDate"].ToString();
                LeaveFromDate = objclsCommon.DateConvertion(Session["MDateFormat"].ToString(), LeaveFromDate);
                LeaveToDate = objclsCommon.DateConvertion(Session["MDateFormat"].ToString(), LeaveToDate);
                //dsLeaveTypes = cl.fillDs_SP("Proc_TA_SelectLeaveTypesOfEmployees",pParams);
                //dsLeaveTypes = objLeaveApplicationEventData.GetLeaveTypesOfEmployees(EmployeeId, Convert.ToDateTime(LeaveFromDate).ToShortDateString().ToString(), Convert.ToDateTime(LeaveToDate).ToShortDateString().ToString());
                dsLeaveTypes = objLeaveApplicationEventData.GetLeaveTypesOfEmployees(EmployeeId, LeaveFromDate, LeaveToDate);
                dsLeaveTypes.Tables[0].TableName = "Details";
                if (dsLeaveTypes.Tables["Details"].Rows.Count > 0)
                {
                    if (dsLeaveTypes.Tables[0].Rows[0][0].ToString() == "0")
                    {
                        Response.Clear();
                        Response.ContentType = "text/xml";
                        Response.Write("CrossLeavePolicy");
                    }
                    else
                    {
                        ///Implemented this newly
                        ///As per the marital status in employee master, we need to show PL and ML, WL(wedding leave)
                        ///i.e single means, we need to load wl instead of ml and pl
                        ///at that time married means, we need to display pl and ml leave type instead of wl.
                        if (Convert.ToString(Session["packageID"]) != "")
                        {
                            //MTTS client we implemented this concept
                            if (Convert.ToInt32(Session["packageID"]) == 169)
                            {
                                DataSet ds_Empdetails = new DataSet();
                                EmployeeEventData objEmployee = new EmployeeEventData();
                                ds_Empdetails = objEmployee.Select_EmployeeDetailsbyId(EmployeeId);
                                if (ds_Empdetails.Tables.Count > 0)
                                {
                                    if (ds_Empdetails.Tables[0].Rows.Count > 0)
                                    {
                                        if (ds_Empdetails.Tables[0].Rows[0]["Marital_status"] != DBNull.Value && Convert.ToString(ds_Empdetails.Tables[0].Rows[0]["Marital_status"]) != "")
                                        {
                                            if (Convert.ToString(ds_Empdetails.Tables[0].Rows[0]["Marital_status"]) == "Single")
                                            {
                                                RemoveParticularLeaveType(dsLeaveTypes, "PL", "ML");
                                            }
                                            else if (Convert.ToString(ds_Empdetails.Tables[0].Rows[0]["Marital_status"]) == "Married")
                                            {
                                                RemoveParticularLeaveType(dsLeaveTypes, "WL");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        Response.Clear();
                        Response.ContentType = "text/xml";
                        Response.Write(dsLeaveTypes.GetXml());
                        Session["sessionLeaveTypes"] = dsLeaveTypes;
                        dsLeaveTypes.Dispose();
                    }
                }
                else
                {
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write("Fail");
                }
            }
            if (leaveSource == "LOP")
            {
                LeaveFromDate = Request.QueryString["LeaveFromDate"].ToString();
                LeaveToDate = Request.QueryString["LeaveToDate"].ToString();
                LeaveFromDate = objclsCommon.DateConvertion(Session["MDateFormat"].ToString(), LeaveFromDate);
                LeaveToDate = objclsCommon.DateConvertion(Session["MDateFormat"].ToString(), LeaveToDate);
                dsLeaveTypesLOPDays = objLeaveApplicationEventData.GetLOPLeaveTypeDetails(EmployeeId, Convert.ToDateTime(LeaveFromDate).ToShortDateString().ToString(), Convert.ToDateTime(LeaveToDate).ToShortDateString().ToString());
                dsLeaveTypesLOPDays.Tables[0].TableName = "Details";
                if (dsLeaveTypesLOPDays.Tables["Details"].Rows.Count > 0)
                {
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write(dsLeaveTypesLOPDays.GetXml());
                    Session["sessionLeaveTypes"] = dsLeaveTypesLOPDays;
                    dsLeaveTypesLOPDays.Dispose();
                }
                else
                {
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write("Fail");
                }
            }
            else if (leaveSource == "Restricted Holiday")
            {
                //SqlParameter pHolidayType = new SqlParameter("@type","Y");
                //SqlParameter pHolidayId = new SqlParameter("@HolidayId", -1);
                //SqlParameter[] pHolidayParams ={ pHolidayType, pHolidayId };
                //dsHolidays = cl.fillDs_SP("Proc_TA_SelectHoliday", pHolidayParams);
                string HolidayType = "Y";
                int HolidayId = -1;
                string UserGroupName = string.Empty;
                UserGroupName = Convert.ToString(Session["UserGroupName"]);
                dsHolidays = objLeaveApplicationEventData.GetRestrictedHoliday(HolidayType, HolidayId, UserGroupName, EmployeeLevel, ApplicationYearsetting, Convert.ToInt32(Session["parentLevelId"]), EmployeeId);
                dsHolidays.Tables[0].TableName = "Details";
                if (dsHolidays.Tables["Details"].Rows.Count > 0)
                {
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write(dsHolidays.GetXml());
                    Session["sessionLeaveTypes"] = dsHolidays;
                    dsHolidays.Dispose();
                }
                else
                {
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write("Fail");
                }
            }
            else if (leaveSource == "Encashment")
            {
                dsLeaveTypes = objLeaveApplicationEventData.SelectLeavetypeByEncashment(Convert.ToString(EmployeeId));
                dsLeaveTypes.Tables[0].TableName = "Details";
                if (dsLeaveTypes.Tables["Details"].Rows.Count > 0)
                {
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write(dsLeaveTypes.GetXml());
                    Session["sessionLeaveTypes"] = dsLeaveTypes;
                    dsLeaveTypes.Dispose();
                }
                else
                {
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write("Fail");
                }
            }
            Response.End();
        }
    }
    private static void RemoveParticularLeaveType(DataSet dsRemainingNoOfLeaves, params string[] leavetype)
    {
        for (int i = dsRemainingNoOfLeaves.Tables[0].Rows.Count - 1; i >= 0; i--)
        {
            DataRow dr = dsRemainingNoOfLeaves.Tables[0].Rows[i];
            for (int j = 0; j < leavetype.Length; j++)
            {
                if (Convert.ToString(dr["LeaveType"]) == leavetype[j])
                {
                    dr.Delete();
                    dsRemainingNoOfLeaves.AcceptChanges();
                    break;
                }
            }
        }
    }
}
