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.Net.Mail;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
using System.Net;
public partial class Transactions_TA_UI_Transactions_LeaveApplicationView : Base.BasePage
{
    #region ForEastman
    internal enum permissionHeader
    {
        FIRST = 1,
        SECOND = 2,
        THIRD = 3,
        FOURTH = 4,
        FIFTH = 5
    }
    Panel pn = new Panel();
    DataSet prevDs = new DataSet();
    #endregion
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcls_Common = new cls_Common();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    PermissionEventData objPermissionEventData = new PermissionEventData();
    DataSet ds_LeaveType = new DataSet();
    DataSet ds_PermissionApplication = new DataSet();
    DataSet ds_RemainingDays = new DataSet();
    DataSet ds_HoursAlreadyTaken = new DataSet();
    DataSet ds_CheckLeaveApplication = new DataSet();
    DataSet ds_MaxNoOfHours = new DataSet();
    DataSet dsEmailIdOfSanctionAuthority = new DataSet();
    DataSet ds_EmailIdOfReportingTo = new DataSet();
    DataSet ds_EmployeeDetails = new DataSet();
    bool blnCheckEmail = true;
    int iLeaveApplication_Id, iEmployeeID;
    int YearSettingID = 0;
    string strReason = string.Empty, strEmailIdReportingTo, gstrDateFormat, gstrTimeFormat, strlevelName;
    string strStatusType = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        if (Request.UrlReferrer != null)
        {
            string GetURL = Request.UrlReferrer.ToString();
        }
        else
            Response.Redirect("TA_UI_Transactions_LeaveApplication.aspx");
        base.Page_Load(sender, e);
        //get the culture info to set the language
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        loadcss();
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_LeaveType", ci) + "','" + rm.GetString("Alert_LeaveRestictHoliday", ci) + "','" + rm.GetString("Alert_Fromdate", ci) + "','" + rm.GetString("Alert_ToDate", ci) + "','" + rm.GetString("Alert_FromSession", ci) + "','" + rm.GetString("Alert_ToSession", ci) + "','" + rm.GetString("Alert_FillReasons", ci) + "','" + rm.GetString("Alert_DayEmpty", ci) + "','" + rm.GetString("Alert_DaysZero", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        UserRights();
        if (Session["User"] == null || Session["User"].ToString() == "")
            Response.Redirect("../index.aspx");

        iLeaveApplication_Id = int.Parse(Request.QueryString["LeaveApplication_Id"].ToString());
        if (!Page.IsPostBack)
        {
            ViewDetails();
            GetEmployeeName();

            btn_Delete.OnClientClick = "return confirm('" + rm.GetString("Alert_ConfirmDelete", ci) + "')";
        }
        ControlCreation();
        #region Eastman
        divLeaveQuotaDetails.Style.Add("display", "none");
        divPrint.Style.Add("display", "none");
        if (Convert.ToString(Session["packageID"]) != "")
            if (Convert.ToString(Session["packageID"]) == "155") /*EastMan*/
            {
                divPrint.Style.Add("display", "");
                leaveQuotatable();
                divLeaveQuotaDetails.Style.Add("display", "");
            }
        #endregion
    }
    #endregion
    #region UserDefinedFunctions
    /// <summary>
    /// Using Resource file, here the label and button text are changed
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        lbl_EmployeeID1.Text = rm.GetString("email_EmployeeId", ci);
        lbl_EmployeeName1.Text = rm.GetString("email_EmployeeName", ci);
        lbl_From.Text = rm.GetString("LeaveApplication_From_New", ci);
        lbl_LeaveApplicationDate1.Text = rm.GetString("LeaveApplication_LeaveApplicationDate", ci);
        lbl_LeaveRestrictedHoliday.Text = rm.GetString("LeaveApplication_LeaveRestrictedHoliday", ci);
        lbl_LeaveType.Text = rm.GetString("lbl_leavetype", ci);
        lbl_Numberofdaysapplied.Text = rm.GetString("LeaveApplication_NumberOfDaysApplied", ci);
        lbl_Reason.Text = rm.GetString("email_Reason", ci);
        lbl_To.Text = rm.GetString("LeaveApplication_To_New", ci);
        lbl_HeaderLeaveApplication.Text = rm.GetString("Transactions_LeaveApplication", ci);
        lbl_FromSession.Text = rm.GetString("Transactions_FromSession", ci);
        lbl_ToSession.Text = rm.GetString("Transactions_ToSession", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        btn_Delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
    }
    public void YearClosureUpdate(string strLeaveStatus)
    {
        if (strLeaveStatus == "ApprovalNotRequired")
        {
            int Employee_Id = Convert.ToInt32(ViewState["iEmployeeID"]);
            string strTypeOfLeave = ViewState["lblType"].ToString();
            if (strTypeOfLeave == "Leave")
            {
                double NumberOfDays = Convert.ToDouble(lblNumberofdaysapplied.Text);
                int LeaveTypeId = Convert.ToInt32(ViewState["LeaveTypeId"]);
                if (Convert.ToBoolean(ViewState["IsBorrow"]) == false)
                    objLeaveApplicationEventData.UpdateYearClosureByLeaveSanction(NumberOfDays, LeaveTypeId, Employee_Id, "R", Convert.ToInt32(Session["EmpAutoId"]), YearSettingID);
                else
                    objLeaveApplicationEventData.UpdateYearClosureByLeaveSanction(NumberOfDays, LeaveTypeId, Employee_Id, "RB", Convert.ToInt32(Session["EmpAutoId"]), YearSettingID);
            }
            // Update Processed Data Flag for Re-Processing
            objLeaveApplicationEventData.UpdateProcessedDataFlag(Employee_Id, ViewState["LeaveFromDate"].ToString(), ViewState["LeaveToDate"].ToString(), 0);
        }
    }
    /// <summary>
    /// Display Leave Application Details Based on Leave Application Id
    /// </summary>
    public void ViewDetails()
    {
        DataSet ds_LeaveApplication = new DataSet();
        ds_LeaveApplication = objLeaveApplicationEventData.SelectLeaveApplication(iLeaveApplication_Id);
        if (ds_LeaveApplication.Tables.Count > 0)
        {
            if (ds_LeaveApplication.Tables[0].Rows.Count > 0)
            {
                if (ds_LeaveApplication.Tables[0].Rows[0]["Employee_Id"] != null)
                {
                    iEmployeeID = Convert.ToInt32(ds_LeaveApplication.Tables[0].Rows[0]["Employee_Id"]);
                    ViewState["iEmployeeID"] = iEmployeeID.ToString();
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["YearSettings_Id"] != null)
                    YearSettingID = Convert.ToInt32(ds_LeaveApplication.Tables[0].Rows[0]["YearSettings_Id"]);
                if (ds_LeaveApplication.Tables[0].Rows[0]["Status"] != null)
                    ViewState["StatusOfApplication"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Status"]);
                if (ds_LeaveApplication.Tables[0].Rows[0]["Type"] != null)
                {
                    lblType.Text = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Type"]);
                    ViewState["lblType"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Type"]);

                    switch (lblType.Text)
                    {
                        case "Leave":
                            lblType.Text = rm.GetString("LeaveApplication_Leave", ci);
                            break;
                        case "Restricted Holiday":
                            lblType.Text = rm.GetString("Holiday_RestrictedHoliday", ci);
                            break;
                        case "Comp Off":
                            lblType.Text = rm.GetString("Caption_CompOff", ci);
                            break;
                        case "LOP":
                            lblType.Text = rm.GetString("lbl_LOP", ci);
                            break;
                        case "ESI":
                            lblType.Text = rm.GetString("Caption_ESI", ci);
                            break;
                        default:
                            break;
                    }
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["LeaveType"] != null)
                {
                    lblLeaveType.Text = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["LeaveType"]);
                    ViewState["LeaveTypeId"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["LeaveType_Id"]);
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["Leave_ApplicationDate"] != null)
                {
                    ViewState["vsApplicationDate"] = lbl_LeaveApplicationDate.Text = (DateTime.Parse(ds_LeaveApplication.Tables[0].Rows[0]["Leave_ApplicationDate"].ToString())).ToString(Session["MDateFormat"].ToString());
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["Leave_FromDate"] != null)
                {
                    lblFromDate.Text = (DateTime.Parse(ds_LeaveApplication.Tables[0].Rows[0]["Leave_FromDate"].ToString())).ToString(Session["MDateFormat"].ToString());
                    ViewState["LeaveFromDate"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Leave_FromDate"]);
                    ViewState["vsFromdate"] = lblFromDate.Text;
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["Leave_ToDate"] != null)
                {
                    lblToDate.Text = (DateTime.Parse(ds_LeaveApplication.Tables[0].Rows[0]["Leave_ToDate"].ToString())).ToString(Session["MDateFormat"].ToString());
                    ViewState["LeaveToDate"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Leave_ToDate"]);
                    ViewState["vsTodate"] = lblToDate.Text;
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["Leave_SessionFrom"] != null)
                {
                    lblSessionFrom.Text = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Leave_SessionFrom"]);
                    switch (lblSessionFrom.Text)
                    {
                        case "First half":
                            lblSessionFrom.Text = rm.GetString("Caption_Firsthalf", ci);
                            break;
                        case "Second half":
                            lblSessionFrom.Text = rm.GetString("Caption_Secondhalf", ci);
                            break;
                        default:
                            break;
                    }
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["Leave_SessionTo"] != null)
                {
                    lblSessionTo.Text = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Leave_SessionTo"]);
                    switch (lblSessionTo.Text)
                    {
                        case "First half":
                            lblSessionTo.Text = rm.GetString("Caption_Firsthalf", ci);
                            break;
                        case "Second half":
                            lblSessionTo.Text = rm.GetString("Caption_Secondhalf", ci);
                            break;
                        default:
                            break;
                    }
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["Reason"] != null)
                {
                    lblReason.Text = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Reason"]);
                    ViewState["vsReason"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Reason"]);
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["NumberOfDays"] != null)
                {
                    lblNumberofdaysapplied.Text = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["NumberOfDays"]);
                    ViewState["vsNumberOfDaysApplied"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["NumberOfDays"]);
                }
                if (ds_LeaveApplication.Tables[0].Rows[0]["IsBorrow"] != null)
                    ViewState["IsBorrow"] = Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["IsBorrow"]);
                if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin")
                    if (Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Status"]) == "Rejected")
                        btn_Delete.Enabled = true;
                    else
                    if (Convert.ToString(ds_LeaveApplication.Tables[0].Rows[0]["Status"]) == "Rejected")
                        btn_Delete.Enabled = false;
            }
        }



    }
    /// <summary>
    /// Get Employee Name Based on EmployeeId
    /// </summary>
    public void GetEmployeeName()
    {
        if (Session["user"] == null || Session["user"].ToString() == "")
            Response.Redirect("../Index.aspx");
        else
        {
            DataSet ds_EmployeeDetails = new DataSet();
            string strEmpFirstName, strEmpLastName;
            ds_EmployeeDetails = objEmployeeEventData.Select_EmployeeDetailsbyId(iEmployeeID);
            if (ds_EmployeeDetails.Tables.Count > 0)
                if (ds_EmployeeDetails.Tables[0].Rows.Count > 0)
                {
                    strEmpFirstName = Convert.ToString(ds_EmployeeDetails.Tables[0].Rows[0]["Employee_FirstName"]);
                    strEmpLastName = Convert.ToString(ds_EmployeeDetails.Tables[0].Rows[0]["Employee_LastName"]);
                    ViewState["vsEmployeeID"] = lbl_EmployeeID.Text = Convert.ToString(ds_EmployeeDetails.Tables[0].Rows[0]["Employee_Code_Id"]);
                    ViewState["vsEmployeeName"] = lbl_EmployeeName.Text = strEmpFirstName + " " + strEmpLastName;
                    ViewState["vsDateofJoining"] = Convert.ToString(ds_EmployeeDetails.Tables[0].Rows[0]["DateOfJoining"]);
                    ViewState["vslevelName"] = Convert.ToString(ds_EmployeeDetails.Tables[0].Rows[0]["LevelName"]);
                }
        }
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(22, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) != 1)
                Response.Redirect("TA_UI_Transactions_LeaveApplication.aspx");
            if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
                this.btn_Delete.Visible = true;
            else
                this.btn_Delete.Visible = false;
        }
        else
            Response.Redirect("TransactionLandingPAge.aspx");
    }

    private void loadcss()
    {
        CultureInfo ci;
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        //if Standard Demo
        htmlLeaveView.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
        else
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        //Declare Leave Status
        string strLeaveStatus = string.Empty;
        int approvalSettingsPageExists = 0;
        //Checking grade module is available or not and changed the id value from 110 to 228
        approvalSettingsPageExists = objcls_Common.CheckPageExistsInLicense(228);
        if (approvalSettingsPageExists == 1)
        {
            //Get Compnay Id of the Employeee
            int intCompanyId = base.GetCompanyId(Convert.ToInt32(ViewState["iEmployeeID"]));
            //Get Approval Settings Based on Company Id
            DataSet dsApprovalSettings = new DataSet();
            ShiftEvenData objShift = new ShiftEvenData();
            dsApprovalSettings = objShift.SelectApprovalSettings(intCompanyId);
            if (dsApprovalSettings != null)
            {
                if (dsApprovalSettings.Tables.Count > 0)
                {
                    if (dsApprovalSettings.Tables[0].Rows.Count > 0)
                    {
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["LeaveSanction"]) == false)
                        {
                            strLeaveStatus = "ApprovalNotRequired";
                        }
                    }
                    else
                    {
                        base.Message(rm.GetString("Msg_ApprovalSettings", ci));
                        return;
                    }
                }
            }
        }
        else
        {
            strLeaveStatus = "ApprovalNotRequired";
        }
        if (ViewState["StatusOfApplication"].ToString() == "Rejected")
        {
            objLeaveApplicationEventData.Delete_LeaveApplication(iLeaveApplication_Id, Convert.ToInt32(Session["EmpAutoId"]));
            //Update Year Closure
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_LeaveApplicationDeleted", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Transactions_LeaveApplication.aspx');";
            strScript = strScript + "</script>";
            //Dispaly alert messsage.
            Page.RegisterStartupScript("AlertLDel65507866", strScript);
        }
        else if (ViewState["StatusOfApplication"].ToString() == "Pending" || strLeaveStatus == "ApprovalNotRequired")
        {
            //Check Borrow Leave Exists after normal leaves
            int intBorrowCount = objLeaveApplicationEventData.CheckBorrowLeaveExists(ViewState["LeaveFromDate"].ToString(), ViewState["LeaveToDate"].ToString(), Convert.ToInt32(ViewState["LeaveTypeId"]), Convert.ToInt32(ViewState["iEmployeeID"]));
            if (intBorrowCount > 0)
            {
                base.Message(rm.GetString("Msg_BorrowLeavesareavailabletodelete", ci));
                return;
            }
            //Delete Leave Application
            objLeaveApplicationEventData.Delete_LeaveApplication(iLeaveApplication_Id, Convert.ToInt32(Session["EmpAutoId"]));
            //Update Year Closure
            YearClosureUpdate(strLeaveStatus);
            //Added Newly by Manikandan.RR on 10-06-2015 11.38 AM
            MailToReportingTo();
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_LeaveApplicationDeleted", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Transactions_LeaveApplication.aspx');";
            strScript = strScript + "</script>";
            //Dispaly alert messsage.
            Page.RegisterStartupScript("AlertLDel65507866", strScript);
        }
        else
        {
            if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
            {
                //Check Borrow Leave Exists after normal leaves
                int intBorrowCount = objLeaveApplicationEventData.CheckBorrowLeaveExists(ViewState["LeaveFromDate"].ToString(), ViewState["LeaveToDate"].ToString(), Convert.ToInt32(ViewState["LeaveTypeId"]), Convert.ToInt32(ViewState["iEmployeeID"]));
                if (intBorrowCount > 0)
                {
                    base.Message(rm.GetString("Msg_BorrowLeavesareavailabletodelete", ci));
                    return;
                }
                //Delete Leave Application
                objLeaveApplicationEventData.Delete_LeaveApplication(iLeaveApplication_Id, Convert.ToInt32(Session["EmpAutoId"]));
                //Update Year Closure
                YearClosureUpdate(strLeaveStatus);
                MailToReportingTo();
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_LeaveApplicationDeleted", ci) + "');";
                strScript = strScript + "location.replace('TA_UI_Transactions_LeaveApplication.aspx');";
                strScript = strScript + "</script>";
                //Dispaly alert messsage.
                Page.RegisterStartupScript("AlertLDel65507866", strScript);
            }
            else
            {
                ControlCreation();
                Page.RegisterStartupScript("statusalert", "<script language='javascript'>alert('" + rm.GetString("Alert_Cannotdeletethisrecordd", ci) + "');</script>");
            }
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Transactions_LeaveApplication.aspx");
    }
    #endregion
    private void MailToReportingTo()
    {
        string strEmployeeEmailId = string.Empty;
        string strEmailIdSanctioningAuthority = string.Empty;
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        gstrTimeFormat = Convert.ToString(Session["MTimeFormat"]);
        strStatusType = "deleted";
        ds_EmployeeDetails = objLeaveApplicationEventData.GetEmployeeDetails(Convert.ToInt32(ViewState["iEmployeeID"]));
        if (ds_EmployeeDetails.Tables[0].Rows.Count > 0)
        {
            strlevelName = ds_EmployeeDetails.Tables[0].Rows[0]["levelName"].ToString();
            strEmployeeEmailId = ds_EmployeeDetails.Tables[0].Rows[0]["emailId"].ToString();
        }
        ds_EmailIdOfReportingTo = objPermissionEventData.SelectMailIdOfReportingTo(Convert.ToInt32(ViewState["iEmployeeID"]));
        dsEmailIdOfSanctionAuthority = objPermissionEventData.SelectMailIdOfSanctionAuthority(Convert.ToInt32(ViewState["iEmployeeID"]));
        strEmailIdSanctioningAuthority = string.Empty;
        if (dsEmailIdOfSanctionAuthority != null)
        {
            if (dsEmailIdOfSanctionAuthority.Tables.Count > 0)
            {
                if (dsEmailIdOfSanctionAuthority.Tables[0].Rows.Count > 0)
                {
                    if (dsEmailIdOfSanctionAuthority.Tables[0].Rows[0]["EmailId"] != DBNull.Value)
                    {
                        if (dsEmailIdOfSanctionAuthority.Tables[0].Rows[0]["EmailId"].ToString() != "")
                        {
                            strEmailIdSanctioningAuthority = dsEmailIdOfSanctionAuthority.Tables[0].Rows[0]["EmailId"].ToString();
                        }
                    }
                }
            }
        }
        if (ds_EmailIdOfReportingTo.Tables[0].Rows.Count > 0)
        {
            if (ds_EmailIdOfReportingTo.Tables[0].Rows[0]["EmailId"] != DBNull.Value)
            {
                if (ds_EmailIdOfReportingTo.Tables[0].Rows[0]["EmailId"].ToString() != string.Empty)
                {
                    strEmailIdReportingTo = ds_EmailIdOfReportingTo.Tables[0].Rows[0]["EmailId"].ToString();
                    if (Convert.ToInt32(ConfigurationManager.AppSettings.Get("isRequiredEmail")) == 1)
                    {
                        SendMail(strEmployeeEmailId, strEmailIdReportingTo, strEmailIdSanctioningAuthority, Convert.ToDouble(lblNumberofdaysapplied.Text), lblFromDate.Text, lblToDate.Text, lbl_LeaveApplicationDate.Text, lbl_EmployeeName.Text, lbl_EmployeeID.Text, lblReason.Text, strlevelName, lblLeaveType.Text);
                    }
                }
            }
        }
    }

    private void ControlCreation()
    {
        // getting leave conversation 
        DataSet dsLeaveConversation = objLeaveApplicationEventData.GetLeaveConversation(iLeaveApplication_Id);

        if (dsLeaveConversation.Tables[0].Rows.Count > 0)
        {
            Table leaveConversation = new Table();
            //leaveConversation.BorderWidth = 1;
            //leaveConversation.CellPadding = 5;
            //leaveConversation.CellSpacing = 0;
            //leaveConversation.BorderColor = System.Drawing.Color.White;
            leaveConversation.Width = Unit.Percentage(100);
            leaveConversation.CssClass = "table das-table1";
            //leaveConversation.Attributes.Add("border", "1");

            TableHeaderRow leaveConversationHr = new TableHeaderRow();

            TableHeaderCell leaveConversationHeading3 = new TableHeaderCell();
            leaveConversationHeading3.Text = rm.GetString("TerminalDataType_Date_And_Time", ci);
            leaveConversationHeading3.HorizontalAlign = HorizontalAlign.Left;
            leaveConversationHr.Cells.Add(leaveConversationHeading3);

            TableHeaderCell leaveConversationHeading1 = new TableHeaderCell();
            leaveConversationHeading1.Text = rm.GetString("leaveConversationHeading1", ci);
            leaveConversationHeading1.HorizontalAlign = HorizontalAlign.Left;
            leaveConversationHr.Cells.Add(leaveConversationHeading1);

            TableHeaderCell leaveConversationHeading2 = new TableHeaderCell();
            leaveConversationHeading2.Text = rm.GetString("leaveConversationHeading2", ci);
            leaveConversationHeading2.HorizontalAlign = HorizontalAlign.Left;
            leaveConversationHr.Cells.Add(leaveConversationHeading2);
            leaveConversationHr.CssClass = "empheader";
            leaveConversation.Rows.Add(leaveConversationHr);

            if (dsLeaveConversation != null && dsLeaveConversation.Tables.Count > 0)
            {
                if (dsLeaveConversation.Tables[0].Rows.Count > 0)
                {
                    int rowIndex = 0;
                    foreach (DataRow drLeaveConversation in dsLeaveConversation.Tables[0].Rows)
                    {
                        TableRow trLeaveConversation = new TableRow();

                        TableCell tcLeaveConversationRepliedOn = new TableCell();
                        //tcLeaveConversationRepliedOn.BorderColor = System.Drawing.Color.White;

                        Label lblLeaveConversationRepliedOn = new Label();
                        //tcLeaveConversationRepliedOn.BorderWidth = 1;
                        tcLeaveConversationRepliedOn.Width = 200;

                        lblLeaveConversationRepliedOn.ID = "lblLeaveConversationRepliedOn" + rowIndex;
                        lblLeaveConversationRepliedOn.Text = Convert.ToDateTime(drLeaveConversation["TimeStamp"]).ToString(Session["MDateFormat"].ToString()) + "  " + Convert.ToDateTime(drLeaveConversation["TimeStamp"]).ToString(Session["MTimeFormat"].ToString());

                        tcLeaveConversationRepliedOn.Controls.Add(lblLeaveConversationRepliedOn);
                        trLeaveConversation.Cells.Add(tcLeaveConversationRepliedOn);

                        TableCell tcLeaveConversationCommentsBy = new TableCell();
                        //tcLeaveConversationCommentsBy.BorderColor = System.Drawing.Color.White;
                        Label lblLeaveConversationCommentsBy = new Label();
                        //tcLeaveConversationCommentsBy.BorderWidth = 1;
                        tcLeaveConversationCommentsBy.Width = 200;

                        lblLeaveConversationCommentsBy.ID = "lblLeaveConversationCommentsBy" + rowIndex;
                        //if (drLeaveConversation["CommentsBy"].ToString() == "0")
                        //{
                        //    lblLeaveConversationCommentsBy.Text = lbl_EmployeeName.Text;
                        //}
                        //else if (drLeaveConversation["CommentsBy"].ToString() == "1")
                        //{
                        //    lblLeaveConversationCommentsBy.Text = objLeaveApplicationEventData.GetEmployeeName(Convert.ToInt32(drLeaveConversation["Emp_Id"].ToString()));
                        //}
                        //else if (drLeaveConversation["CommentsBy"].ToString() == "2")
                        //{
                        //    lblLeaveConversationCommentsBy.Text = objLeaveApplicationEventData.GetEmployeeName(Convert.ToInt32(drLeaveConversation["Emp_Id"].ToString()));
                        //}
                        //else if (drLeaveConversation["CommentsBy"].ToString() == "3")
                        //{
                        lblLeaveConversationCommentsBy.Text = objLeaveApplicationEventData.GetEmployeeName(Convert.ToInt32(drLeaveConversation["Emp_Id"].ToString()));
                        // }
                        tcLeaveConversationCommentsBy.Controls.Add(lblLeaveConversationCommentsBy);
                        trLeaveConversation.Cells.Add(tcLeaveConversationCommentsBy);

                        TableCell tcLeaveConversationComments = new TableCell();
                        //tcLeaveConversationComments.BorderColor = System.Drawing.Color.White;
                        Label lblLeaveConversationComments = new Label();
                        //tcLeaveConversationComments.BorderWidth = 1;
                        //tcLeaveConversationComments.ColumnSpan = 2;

                        lblLeaveConversationComments.ID = "lblLeaveConversationComments" + rowIndex;
                        lblLeaveConversationComments.Text = drLeaveConversation["Comments"].ToString();
                        tcLeaveConversationComments.Controls.Add(lblLeaveConversationComments);
                        trLeaveConversation.Cells.Add(tcLeaveConversationComments);
                        leaveConversation.Rows.Add(trLeaveConversation);
                        rowIndex += 1;
                    }
                }
            }

            divLeaveConversation.Controls.Add(leaveConversation);
        }
    }

    #region Sending mail to the reporting to person.
    //<summary>
    //Sending mail to the reporting to person
    //</summary>
    //<param name="ToMailId"></param>
    //<param name="NumberOfDaysApplied"></param>
    //<param name="FromDate"></param>
    //<param name="ToDate"></param>
    //<param name="ApplicationDate"></param>
    private void SendMail(string strEmployeeEmailId, string ToMailId, string SanctionMailId, double NumberOfDaysApplied, string FromDate, string ToDate, string ApplicationDate, string EmployeeName, string Employee_Code_Id, string strReason, string strlevelName, string strLeaveType)
    {
        string strFromEmailId = string.Empty;
        MailMessage mailMessage = new MailMessage();
        string strMailStatus = "Deleted";
        try
        {
            string scriptText;
            DataTable dtTableScript = new DataTable();
            dtTableScript = objLeaveApplicationEventData.GetEmailScripts("New Leave Application Form");
            if (dtTableScript.Rows.Count > 0)
            {
                switch (strLeaveType)
                {
                    case "Restricted Holiday":
                        strLeaveType = rm.GetString("Holiday_RestrictedHoliday", ci);
                        break;
                    case "Comp Off":
                        strLeaveType = rm.GetString("Caption_CompOff", ci);
                        break;
                    case "LOP":
                        strLeaveType = strLeaveType.Replace("LOP", rm.GetString("lbl_LOP", ci));
                        break;
                    case "ESI":
                        strLeaveType = rm.GetString("Caption_ESI", ci);
                        break;
                    default:
                        if (strLeaveType.Contains("Leave"))
                            strLeaveType = strLeaveType.Replace("Leave", rm.GetString("LeaveApplication_Leave", ci));
                        break;
                }
                scriptText = Convert.ToString(dtTableScript.Rows[0]["ScriptText"]);
                // As per elite partner requisition, mail should be trigger from admin id, whatever we configured in setting page.
                //25-Dec-2019 02:06 PM
                //if (Session["loginEmailId"] != null && Convert.ToString(Session["loginEmailId"]) != string.Empty)
                //{
                //    mailMessage.From = new MailAddress(Convert.ToString(Session["loginEmailId"]));
                //    strFromEmailId = Convert.ToString(Session["loginEmailId"]);
                //}
                //else
                //{
                mailMessage.From = new MailAddress(ConfigurationManager.AppSettings.Get("FromId"));
                strFromEmailId = Convert.ToString(ConfigurationManager.AppSettings.Get("FromId"));
                //}
                mailMessage.To.Add(new MailAddress(ToMailId));

                if (Convert.ToString(mailMessage.From) != strEmployeeEmailId)
                {
                    if (strEmployeeEmailId != string.Empty)
                    {
                        mailMessage.CC.Add(new MailAddress(strEmployeeEmailId));
                    }
                }
                //mailMessage.Subject = rm.GetString("email_LeaveApplicationForm", ci) + " - " + ApplicationDate + " | " + Employee_Code_Id + " - " + EmployeeName + " | " + strlevelName + " - " + strMailStatus;
                //mailMessage.Subject = rm.GetString("email_YourLeaveApplicationFor", ci) + " - " + FromDate + " - " + ToDate + " | " + strMailStatus;
                mailMessage.Subject = rm.GetString("email_YourLeaveApplicationFor", ci) + " - " + ApplicationDate + " | " + Employee_Code_Id + " - " + EmployeeName + " | " + strlevelName + " - " + strMailStatus;
                mailMessage.Body = scriptText.Replace("<<lblLeaveApplicationForm>>", rm.GetString("email_LeaveApplicationForm", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<ltr_rtl>>", rm.GetString("Direction", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<lblEmployeeId>>", rm.GetString("email_EmployeeId", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<EmployeeId>>", Employee_Code_Id);
                mailMessage.Body = mailMessage.Body.Replace("<<EmployeeName>>", EmployeeName);
                mailMessage.Body = mailMessage.Body.Replace("<<lblEmployeeName>>", rm.GetString("email_EmployeeName", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<ApplicationDate>>", ApplicationDate);
                mailMessage.Body = mailMessage.Body.Replace("<<lblApplicationDate>>", rm.GetString("email_LeaveApplicationDate", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<LeaveType>>", strLeaveType);
                mailMessage.Body = mailMessage.Body.Replace("<<lblLeaveType>>", rm.GetString("lbl_leavetype", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<NumberDaysApplied>>", Convert.ToString(NumberOfDaysApplied));
                mailMessage.Body = mailMessage.Body.Replace("<<lblNumberDaysApplied>>", rm.GetString("email_NumberOfDaysApplied", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<lblLeaveDate>>", rm.GetString("email_LeaveDate", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<FromDate>>", FromDate);
                mailMessage.Body = mailMessage.Body.Replace("<<lblFromDate>>", rm.GetString("email_FromDate", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<ToDate>>", ToDate);
                mailMessage.Body = mailMessage.Body.Replace("<<lblToDate>>", rm.GetString("email_ToDate", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<lblSession>>", rm.GetString("email_Session", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<FromSession>>", lblSessionFrom.Text);
                mailMessage.Body = mailMessage.Body.Replace("<<lblFromSession>>", rm.GetString("email_FromDate", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<ToSession>>", lblSessionTo.Text);
                mailMessage.Body = mailMessage.Body.Replace("<<lblToSession>>", rm.GetString("email_ToDate", ci));
                mailMessage.Body = mailMessage.Body.Replace("<<Reason>>", strReason);
                mailMessage.Body = mailMessage.Body.Replace("<<lblReason>>", rm.GetString("email_Reason", ci));
                objcls_Common.SendMail(mailMessage);
            }
            else
            {
                blnCheckEmail = false;
            }
        }
        catch
        {
            blnCheckEmail = false;
            objLeaveApplicationEventData.InsertLeaveApplicationDetailsInEmailTrigger(iLeaveApplication_Id, strFromEmailId, ToMailId, strEmployeeEmailId, rm.GetString("email_LeaveApplicationForm", ci) + " - " + ApplicationDate + " | " + Employee_Code_Id + " - " + EmployeeName + " | " + strlevelName + " - " + strMailStatus, Convert.ToString(mailMessage.Body), "", "LeaveApplication");
        }
    }
    #endregion

    protected void btn_print_Click(object sender, EventArgs e)
    {
        #region Eastman
        if (Convert.ToString(Session["packageID"]) != "")
            if (Convert.ToString(Session["packageID"]) == "155") /*EastMan*/
            {
                GenerateLeaveApplicationTableControls();
            }
        #endregion
    }
    private void AssignDefaultLeaveValues()
    {
        ViewState["vsLeaveBal"] = 0;
        ViewState["vsAvailedLeave"] = 0;
        ViewState["vsAvailedLOPLeave"] = 0;
        ViewState["vsTotalLeaveTaken"] = 0;
        ViewState["vsWaiveoff"] = 0;
    }
    private void GenerateLeaveApplicationTableControls()
    {
        string scriptText = "";
        DataTable dtTableScript = new DataTable();
        dtTableScript = objLeaveApplicationEventData.GetEmailScripts("LeaveApplication_Print");
        if (dtTableScript.Rows.Count > 0)
        {
            scriptText = Convert.ToString(dtTableScript.Rows[0]["ScriptText"]);
            scriptText = scriptText.Replace("<<applicationdatefield>>", Convert.ToString(ViewState["vsApplicationDate"]));
            scriptText = scriptText.Replace("<<pagenofield>>", Convert.ToString(""));
            scriptText = scriptText.Replace("<<namefield>>", Convert.ToString(ViewState["vsEmployeeName"]));
            scriptText = scriptText.Replace("<<dojfield>>", Convert.ToString(ViewState["vsDateofJoining"]));
            scriptText = scriptText.Replace("<<employeeidfield>>", Convert.ToString(ViewState["vsEmployeeID"]));
            scriptText = scriptText.Replace("<<departmentfield>>", Convert.ToString(ViewState["vslevelName"]));
            scriptText = scriptText.Replace("<<concernfield>>", Convert.ToString(ViewState["vsConcernName"]));

            scriptText = scriptText.Replace("<<leavetakenfield>>", Convert.ToString(ViewState["vsTotalLeaveTaken"]));
            scriptText = scriptText.Replace("<<earnedleavefield>>", Convert.ToString(ViewState["vsAvailedLeave"]));
            scriptText = scriptText.Replace("<<lopfield>>", Convert.ToString(ViewState["vsAvailedLOPLeave"]));
            scriptText = scriptText.Replace("<<leavebalancefield>>", Convert.ToString(ViewState["vsLeaveBal"]));
            scriptText = scriptText.Replace("<<accidentfield>>", Convert.ToString(ViewState["vsWaiveoff"]));

            scriptText = scriptText.Replace("<<numberofdaysfield>>", Convert.ToString(ViewState["vsNumberOfDaysApplied"]));
            scriptText = scriptText.Replace("<<fromdatefield>>", Convert.ToString(ViewState["vsFromdate"]));
            scriptText = scriptText.Replace("<<todatefield>>", Convert.ToString(ViewState["vsTodate"]));
            scriptText = scriptText.Replace("<<reasonfield>>", Convert.ToString(ViewState["vsReason"]));

            pn.Controls.Add(new LiteralControl(scriptText));
            Session["ctrl"] = pn;
            string strScript = "";
            strScript = strScript + "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
            strScript = strScript + "<meta http-equiv='X-UA-Compatible' content='IE=edge' />";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "window.open('Print.aspx','','left=10px, top=60px, width=1000px, height=600px, scrollbars=yes, status=no, resizable=yes');";
            strScript = strScript + "</script>";
            Response.Write(strScript);
            return;
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Table script is missing,contact support team of timecheck');", true);
            return;
        }

    }
    private void leaveQuotatable()
    {
        #region Eastman
        if (Convert.ToString(Session["packageID"]) != "")
            if (Convert.ToString(Session["packageID"]) == "155") /*EastMan*/
            {
                DataSet dsTotalLeaveDays = new DataSet();
                dsTotalLeaveDays = objLeaveApplicationEventData.GetPreviousLeaveApplicationDaysValues(Convert.ToInt64(iLeaveApplication_Id));
                if (dsTotalLeaveDays.Tables.Count > 0)
                {
                    if (dsTotalLeaveDays.Tables[0].Rows.Count > 0)
                    {
                        ViewState["vsLeaveBal"] = Convert.ToDecimal(dsTotalLeaveDays.Tables[0].Rows[0]["LeaveBal"]);
                        ViewState["vsAvailedLeave"] = Convert.ToDecimal(dsTotalLeaveDays.Tables[0].Rows[0]["EarnedLeave"]);
                        ViewState["vsAvailedLOPLeave"] = Convert.ToDecimal(dsTotalLeaveDays.Tables[0].Rows[0]["LOP"]);
                        ViewState["vsTotalLeaveTaken"] = Convert.ToDecimal(dsTotalLeaveDays.Tables[0].Rows[0]["LeaveTaken"]);
                        ViewState["vsWaiveoff"] = Convert.ToDecimal(dsTotalLeaveDays.Tables[0].Rows[0]["WaiveOff"]);
                        ViewState["vsConcernName"] = Convert.ToString(dsTotalLeaveDays.Tables[0].Rows[0]["ConcernName"]);

                        Table leaveQuotaTable = new Table();
                        leaveQuotaTable.Width = Unit.Percentage(100);
                        leaveQuotaTable.CssClass = "table das-table1";

                        TableHeaderRow leaveQuotaTblHeader = new TableHeaderRow();
                        leaveQuotaTblHeader.CssClass = "empheader";

                        TableHeaderCell leaveTakenHeader = new TableHeaderCell();
                        leaveTakenHeader.Text = "Leave Taken";
                        leaveTakenHeader.Style.Add("text-align", "center");
                        leaveQuotaTblHeader.Cells.Add(leaveTakenHeader);

                        TableHeaderCell earnedLeaveHeader = new TableHeaderCell();
                        earnedLeaveHeader.Text = "Earned Leave";
                        earnedLeaveHeader.Style.Add("text-align", "center");
                        leaveQuotaTblHeader.Cells.Add(earnedLeaveHeader);

                        TableHeaderCell lopHeader = new TableHeaderCell();
                        lopHeader.Text = "LOP";
                        lopHeader.Style.Add("text-align", "center");
                        leaveQuotaTblHeader.Cells.Add(lopHeader);

                        TableHeaderCell leaveBalanceHeader = new TableHeaderCell();
                        leaveBalanceHeader.Text = "Leave Balance";
                        leaveBalanceHeader.Style.Add("text-align", "center");
                        leaveQuotaTblHeader.Cells.Add(leaveBalanceHeader);

                        TableHeaderCell waiveoffHeader = new TableHeaderCell();
                        waiveoffHeader.Text = "Waive Off";
                        leaveBalanceHeader.Style.Add("text-align", "center");
                        leaveQuotaTblHeader.Cells.Add(waiveoffHeader);
                        leaveQuotaTable.Rows.Add(leaveQuotaTblHeader);

                        TableRow leaveQuotavalueRow = new TableRow();
                        TableCell leaveTakenvalue = new TableCell();
                        leaveTakenvalue.Text = Convert.ToString(dsTotalLeaveDays.Tables[0].Rows[0]["LeaveTaken"]);
                        leaveTakenvalue.HorizontalAlign = HorizontalAlign.Left;
                        leaveQuotavalueRow.Cells.Add(leaveTakenvalue);

                        TableCell earnedLeavevalue = new TableCell();
                        earnedLeavevalue.Text = Convert.ToString(dsTotalLeaveDays.Tables[0].Rows[0]["EarnedLeave"]);
                        earnedLeavevalue.HorizontalAlign = HorizontalAlign.Left;
                        leaveQuotavalueRow.Cells.Add(earnedLeavevalue);

                        TableCell lopvalue = new TableCell();
                        lopvalue.Text = Convert.ToString(dsTotalLeaveDays.Tables[0].Rows[0]["LOP"]);
                        lopvalue.HorizontalAlign = HorizontalAlign.Left;
                        leaveQuotavalueRow.Cells.Add(lopvalue);

                        TableCell leaveBalancevalue = new TableCell();
                        leaveBalancevalue.Text = Convert.ToString(dsTotalLeaveDays.Tables[0].Rows[0]["LeaveBal"]);
                        leaveBalancevalue.HorizontalAlign = HorizontalAlign.Left;
                        leaveQuotavalueRow.Cells.Add(leaveBalancevalue);

                        TableCell waiveoffvalue = new TableCell();
                        waiveoffvalue.Text = Convert.ToString(dsTotalLeaveDays.Tables[0].Rows[0]["WaiveOff"]);
                        waiveoffvalue.HorizontalAlign = HorizontalAlign.Left;
                        leaveQuotavalueRow.Cells.Add(waiveoffvalue);
                        leaveQuotaTable.Rows.Add(leaveQuotavalueRow);
                        divLeaveQuotaDetails.Controls.Add(leaveQuotaTable);
                    }
                    else
                        AssignDefaultLeaveValues();
                }
                else
                    AssignDefaultLeaveValues();
            }
        #endregion



    }
}