using System;
using System.Data;
using System.Data.SqlClient;
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.Text;
using System.IO;

public partial class Reports_TA_UI_LeavePermissionDailyReportGrid : System.Web.UI.Page
{
    public DataTable dtHdr;
    public DataTable dtSource;
    public DataTable rptHeader;
    public DataTable rptHeaderLabels;

    HeaderInformation hdrTable = new HeaderInformation();
    GetReports rptEmp = new GetReports();
    LeaveSettingsEventData_Angler objLeaveSettingsEventData = new LeaveSettingsEventData_Angler();
    cls_Common objCommon = new cls_Common();

    string Level = string.Empty;
    string employeeName = string.Empty;
    string empName = string.Empty;
    string empId = string.Empty;
    string ParentLevelid = string.Empty;
    int EmployeeCategory = 0, EmployeeType = 0, NoOfDays;
    string dtFrom, dtTo, UserGroupName, GradeLevel, ReportName;

    int intOrgCount = 0;
    string strOrgName = string.Empty;

    public DataTable dtDailyReportLeave;
    public DataTable dtDailyReportPermission;
    public DataTable dtDailyReportPermissionUnplanned;
    public DataTable dtDailyReportUnplanned;

    protected void Page_Load(object sender, EventArgs e)
    {
        objCommon.menu("LeaveReports", "112");
        Level = Request.QueryString["Level"];
        employeeName = Request.QueryString["EmpName"];
        empId = Request.QueryString["empid"];
        dtFrom = Request.QueryString["dtFrom"];
        dtTo = Request.QueryString["dtTo"];
        EmployeeCategory = Convert.ToInt32(Request.QueryString["EmpCategory"]);
        EmployeeType = Convert.ToInt32(Request.QueryString["EmpType"]);
        ParentLevelid = Request.QueryString["ParentLevelId"];
        UserGroupName = Request.QueryString["UserGroupName"];
        GradeLevel = Request.QueryString["GradeLevel"];
        ReportName = Request.QueryString["ReportName"];
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (!IsPostBack)
        {
            Session["TempparentLevelId"] = Session["parentLevelId"];
        }

        LoadDailyReport();
        //GridviewHeader();
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_LeavePermissionDailyReport.aspx");
    }

    //protected void GridviewHeader()
    //{
    //    GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
    //    TableCell oTableCell = new TableCell();
    //    oTableCell.Text = "Leave";
    //    oTableCell.Font.Bold = true;
    //    oTableCell.ColumnSpan = 8;
    //    oTableCell.HorizontalAlign = HorizontalAlign.Center;
    //    oGridViewRow.Cells.Add(oTableCell);
    //    grv_DailyLeave.Controls[0].Controls.AddAt(0, oGridViewRow);

    //    oTableCell = new TableCell();
    //    oTableCell.Text = "Permission";
    //    oTableCell.Font.Bold = true;
    //    oTableCell.ColumnSpan = 8;
    //    oTableCell.HorizontalAlign = HorizontalAlign.Center;
    //    oGridViewRow.Cells.Add(oTableCell);
    //    grv_Permission.Controls[0].Controls.AddAt(0, oGridViewRow);

    //    oTableCell = new TableCell();
    //    oTableCell.Text = "UnPlanned Leave";
    //    oTableCell.Font.Bold = true;
    //    oTableCell.ColumnSpan = 9;
    //    oTableCell.HorizontalAlign = HorizontalAlign.Center;
    //    oGridViewRow.Cells.Add(oTableCell);
    //    grdUnPlannedLeave.Controls[0].Controls.AddAt(0, oGridViewRow);

    //    oTableCell = new TableCell();
    //    oTableCell.Text = "UnPlanned Permission";
    //    oTableCell.Font.Bold = true;
    //    oTableCell.ColumnSpan = 8;
    //    oTableCell.HorizontalAlign = HorizontalAlign.Center;
    //    oGridViewRow.Cells.Add(oTableCell);
    //    grdUnplannedPermission.Controls[0].Controls.AddAt(0, oGridViewRow);
    //}



    public void LoadDailyReport()
    {

        string strSwipeCard = "";

        //Create the datatable ,that holds the header information
        DataTable dtHdr1 = new DataTable();
        dtHdr1 = hdrTable.CreateHeader();
        dtHdr1.TableName = "Header";

        //Add the Header information
        DataRow drHdr1 = dtHdr1.NewRow();
        drHdr1["Link"] = 1;
        drHdr1["Hdr1"] = "";
        drHdr1["Hdr2"] = "";
        drHdr1["Address1"] = "";
        drHdr1["Address2"] = "";
        drHdr1["ReportName"] = "Leave and Permission Details Report";


        drHdr1["FromDate"] = Convert.ToString(Request.QueryString["dtYear"]);
        drHdr1["ToDate"] = "";

        dtHdr1.Rows.Add(drHdr1);


        //string dtFrom = "", dtTo = "";

        if (!string.IsNullOrEmpty(dtFrom))
        {
            DateTime tmpFrom = DateTime.Parse(dtFrom);
            drHdr1["FromDate"] = tmpFrom.ToString("dd-MMM-yyyy");
        }
        else
            drHdr1["FromDate"] = "";

        drHdr1["ToDate"] = "";


        //Get the Report source from the Server and load this into the report table
        DataSet dsDailyLeave = new DataSet();

        //this is new report

        int shft = 0;
        //string Level = string.Empty;
        string employeeName = string.Empty;
        string empName = string.Empty;
        string empId = string.Empty;

        DataSet dsDailyLeaveUnplanned = new DataSet();
        DataSet dsDailyPermission = new DataSet();

        DataSet dsDailyPermissionUnplanned = new DataSet();
        string strEmpAutoId = string.Empty;
        if (Session["ReportsBasedOn"] != null)
        {
            if (Session["ReportsBasedOn"].ToString() == "False")
            {
                strEmpAutoId = "0";
            }
            else
            {
                strEmpAutoId = Session["EmpAutoID"].ToString();
            }
        }


        dsDailyLeave = rptEmp.SelectLeavePermissionDailyReport_Leaves_Angler(dtFrom, Level, "P", Convert.ToInt64(strEmpAutoId), Convert.ToInt64(Session["ddlReportingTo"]), Convert.ToInt64(Session["ddlSanctioningAuthority"]), Convert.ToInt16(Session["TempparentLevelId"]), Session["UserGroupName"].ToString());
        dsDailyLeaveUnplanned = rptEmp.SelectLeavePermissionDailyReport_Leaves_Angler(dtFrom, Level, "U", Convert.ToInt64(strEmpAutoId), Convert.ToInt64(Session["ddlReportingTo"]), Convert.ToInt64(Session["ddlSanctioningAuthority"]), Convert.ToInt16(Session["TempparentLevelId"]), Session["UserGroupName"].ToString());

        dsDailyPermission = rptEmp.SelectLeavePermissionDailyReport_Permission_Angler(dtFrom, Level, "P", Convert.ToInt64(strEmpAutoId), Convert.ToInt64(Session["ddlReportingTo"]), Convert.ToInt64(Session["ddlSanctioningAuthority"]), Convert.ToInt16(Session["TempparentLevelId"]), Session["UserGroupName"].ToString());
        dsDailyPermissionUnplanned = rptEmp.SelectLeavePermissionDailyReport_Permission_Angler(dtFrom, Level, "U", Convert.ToInt64(strEmpAutoId), Convert.ToInt64(Session["ddlReportingTo"]), Convert.ToInt64(Session["ddlSanctioningAuthority"]), Convert.ToInt16(Session["TempparentLevelId"]), Session["UserGroupName"].ToString());



        //dsDailyLeave = rptEmp.SelectLeavePermissionDailyReport_Leaves(dtFrom, Level, "P", Convert.ToInt64(strEmpAutoId));
        //dsDailyLeaveUnplanned = rptEmp.SelectLeavePermissionDailyReport_Leaves(dtFrom, Level, "U", Convert.ToInt64(strEmpAutoId));

        //dsDailyPermission = rptEmp.SelectLeavePermissionDailyReport_Permission(dtFrom, Level, "P", Convert.ToInt64(strEmpAutoId));

        //dsDailyPermissionUnplanned = rptEmp.SelectLeavePermissionDailyReport_Permission(dtFrom, Level, "U", Convert.ToInt64(strEmpAutoId));

        ////Report Header
        int orgID = Convert.ToInt16(Request.QueryString["ParentLevelId"]);
        Session["parentLevelId"] = null;
        if (orgID == 0)
        {
            //orgID = 1;
            DataSet dsOrgid = new DataSet();
            dsOrgid = rptEmp.getMinLevel();
            if (dsOrgid.Tables[0].Rows.Count > 0)
            {
                if (dsOrgid.Tables[0].Rows[0]["minID"] != DBNull.Value)
                {
                    orgID = Convert.ToInt16(dsOrgid.Tables[0].Rows[0]["minID"]);
                }
            }
        }
        int lid = 0;
        org_levelsEventData getOrg = new org_levelsEventData();
        getOrg.GetOrgParentLevels_FromID(orgID);
        DataSet ds_getOrg = new DataSet();
        ds_getOrg = getOrg.GetOrgParentLevels();

        if (ds_getOrg.Tables[0] != null)
        {
            if (ds_getOrg.Tables[0].Rows.Count > 0)
            {
                //Gets the selected LevelId based on the row count.
                intOrgCount = ds_getOrg.Tables[0].Rows.Count;
            }
        }

        if (intOrgCount == 4)
        {
            //If the last level(i.e) department is selected then department name will be displayed in mail subject.
            strOrgName = ds_getOrg.Tables[0].Rows[3]["LevelName"].ToString();
            DataSet dsDepartmentMailIds = new DataSet();

            //If department level is selected,gets the department level mails Id's
            dsDepartmentMailIds = objLeaveSettingsEventData.GetDepartmentMailIds(Convert.ToInt32(ds_getOrg.Tables[0].Rows[3]["LevelID"]));

            string strApprovalToDept = dsDepartmentMailIds.Tables[0].Rows[0]["ToMailIds"].ToString();
            string strApprovalCCDept = dsDepartmentMailIds.Tables[0].Rows[0]["CCMailIds"].ToString();
            if (!IsPostBack)
            {
                if (strApprovalToDept != string.Empty)
                {
                    txtApprovalTO.Text = strApprovalToDept;
                }
                if (strApprovalCCDept != string.Empty)
                {
                    txtApprovalCC.Text = strApprovalCCDept;
                }
            }
        }
        else
        {
            //If the last level(i.e) department is not selected then company name will be displayed in mail subject.
            strOrgName = ds_getOrg.Tables[0].Rows[0]["LevelName"].ToString();

            DataSet dsCompanyMailIds = new DataSet();

            //If department level is not selected,gets the company level mails Id's
            dsCompanyMailIds = objLeaveSettingsEventData.GetLeaveSettingsByComId(Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"]));

            string strApprovalToDept = dsCompanyMailIds.Tables[0].Rows[0]["ToMail_DailyReport"].ToString();
            string strApprovalCCDept = dsCompanyMailIds.Tables[0].Rows[0]["CCMail_DailyReport"].ToString();
            if (!IsPostBack)
            {
                if (strApprovalToDept != string.Empty)
                {
                    txtApprovalTO.Text = strApprovalToDept;
                }
                if (strApprovalCCDept != string.Empty)
                {
                    txtApprovalCC.Text = strApprovalCCDept;
                }
            }
        }

        if (ds_getOrg.Tables[0].Rows.Count > 0)
        {
            lid = Convert.ToInt16(ds_getOrg.Tables[0].Rows[0]["levelID"]);
        }
        DataSet dsHeader = new DataSet();
        dsHeader = rptEmp.getHeader1_Angler(lid);
        DataTable dtRptHeader = new DataTable();
        if (dsHeader.Tables[0].Rows.Count > 0)
        {

            dtRptHeader = dsHeader.Tables[0].Copy();
            //dtRptHeader.WriteXmlSchema(@"E:\reportHeader.xml");
            //Session["reportHeader"] = dtRptHeader;
        }
        ////End of Report Header
        Session["parentLevelId"] = null;

        DataTable dtDailyLeave = new DataTable();

        DataTable dtDailyLeaveUnplanned = new DataTable();

        DataTable dtDailyPermission = new DataTable();

        DataTable dtDailyPermissionUnplanned = new DataTable();

        dtDailyLeave = dsDailyLeave.Tables["Table"].Copy();
        grv_DailyLeave.DataSource = dtDailyLeave;
        grv_DailyLeave.DataBind();


        dtDailyLeaveUnplanned = dsDailyLeaveUnplanned.Tables["Table"].Copy();
        grdUnPlannedLeave.DataSource = dtDailyLeaveUnplanned;
        grdUnPlannedLeave.DataBind();


        dtDailyPermission = dsDailyPermission.Tables["Table"].Copy();
        grv_Permission.DataSource = dtDailyPermission;
        grv_Permission.DataBind();

        dtDailyPermissionUnplanned = dsDailyPermissionUnplanned.Tables["Table"].Copy();
        grdUnplannedPermission.DataSource = dtDailyPermissionUnplanned;
        grdUnplannedPermission.DataBind();

        //Add the link column
        DataColumn dcLink = new DataColumn("Link", Type.GetType("System.Int32"));
        DataColumn dcLink1 = new DataColumn("Link", Type.GetType("System.Int32"));
        DataColumn dcLink2 = new DataColumn("Link", Type.GetType("System.Int32"));
        DataColumn dcLink3 = new DataColumn("Link", Type.GetType("System.Int32"));

        dtDailyLeave.Columns.Add(dcLink);

        for (int i = 0; i < dtDailyLeave.Rows.Count; i++)
        {
            dtDailyLeave.Rows[i]["Link"] = 1;
        }

        dtDailyLeaveUnplanned.Columns.Add(dcLink1);

        for (int i = 0; i < dtDailyLeaveUnplanned.Rows.Count; i++)
        {
            dtDailyLeaveUnplanned.Rows[i]["Link"] = 1;
        }

        dtDailyPermission.Columns.Add(dcLink2);

        for (int i = 0; i < dtDailyPermission.Rows.Count; i++)
        {
            dtDailyPermission.Rows[i]["Link"] = 1;
        }

        dtDailyPermissionUnplanned.Columns.Add(dcLink3);

        for (int i = 0; i < dtDailyPermissionUnplanned.Rows.Count; i++)
        {
            dtDailyPermissionUnplanned.Rows[i]["Link"] = 1;
        }

        dtDailyReportLeave = dtDailyLeave;
        dtDailyReportPermission = dtDailyPermission;
        dtDailyReportPermissionUnplanned = dtDailyPermissionUnplanned;
        dtDailyReportUnplanned = dtDailyLeaveUnplanned;

        //rptFileName = Server.MapPath("DailyReport.rpt");
        dtHdr = dtHdr1;

        rptHeader = dtRptHeader;


    }

    protected void grdUnPlannedLeave_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblFrom = (Label)e.Row.Cells[3].FindControl("lblFrom");

            lblFrom.Text = (Convert.ToDateTime(lblFrom.Text.ToString())).ToString(Session["MDateFormat"].ToString());

            Label lblTo = (Label)e.Row.Cells[4].FindControl("lblTo");

            lblTo.Text = (Convert.ToDateTime(lblTo.Text.ToString())).ToString(Session["MDateFormat"].ToString());

        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Build custom header.
            GridView oGridView = (GridView)sender;
            GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell oTableCell = new TableCell();

            //Add Department
            oTableCell.Text = "UnPlanned Leave";
            oTableCell.Font.Bold = true;
            oTableCell.ColumnSpan = 9;
            oTableCell.HorizontalAlign = HorizontalAlign.Center;
            oGridViewRow.Cells.Add(oTableCell);
            oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);

        }

    }

    protected void grv_DailyLeave_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblFrom = (Label)e.Row.Cells[3].FindControl("lblFrom");

            lblFrom.Text = (Convert.ToDateTime(lblFrom.Text.ToString())).ToString(Session["MDateFormat"].ToString());

            Label lblTo = (Label)e.Row.Cells[4].FindControl("lblTo");

            lblTo.Text = (Convert.ToDateTime(lblTo.Text.ToString())).ToString(Session["MDateFormat"].ToString());

        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Build custom header.
            GridView oGridView = (GridView)sender;
            GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell oTableCell = new TableCell();

            //Add Department
            oTableCell.Text = "Leave";
            oTableCell.Font.Bold = true;
            oTableCell.ColumnSpan = 9;
            oTableCell.HorizontalAlign = HorizontalAlign.Center;
            oGridViewRow.Cells.Add(oTableCell);
            oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);

        }

    }

    protected void grdUnplannedPermission_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblPermissionDate = (Label)e.Row.Cells[3].FindControl("lblPermissionDate");

            lblPermissionDate.Text = (Convert.ToDateTime(lblPermissionDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());


            Label lblFrom = (Label)e.Row.Cells[3].FindControl("lblFrom");

            //lblFrom.Text = (Convert.ToDateTime(lblFrom.Text.ToString())).ToString(Session["MDateFormat"].ToString());
            lblFrom.Text = DateTime.Parse(lblFrom.Text).ToString(Session["MTimeFormat"].ToString());


            Label lblTo = (Label)e.Row.Cells[4].FindControl("lblTo");

            //lblTo.Text = (Convert.ToDateTime(lblTo.Text.ToString())).ToString(Session["MDateFormat"].ToString());
            lblTo.Text = DateTime.Parse(lblTo.Text).ToString(Session["MTimeFormat"].ToString());
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Build custom header.
            GridView oGridView = (GridView)sender;
            GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell oTableCell = new TableCell();

            //Add Department
            oTableCell.Text = "UnPlanned Permission";
            oTableCell.Font.Bold = true;
            oTableCell.ColumnSpan = 8;
            oTableCell.HorizontalAlign = HorizontalAlign.Center;
            oGridViewRow.Cells.Add(oTableCell);
            oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);

        }
    }

    protected void grv_Permission_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblPermissionDate = (Label)e.Row.Cells[3].FindControl("lblPermissionDate");

            lblPermissionDate.Text = (Convert.ToDateTime(lblPermissionDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());


            Label lblFrom = (Label)e.Row.Cells[3].FindControl("lblFrom");

            //lblFrom.Text = (Convert.ToDateTime(lblFrom.Text.ToString())).ToString(Session["MDateFormat"].ToString());
            lblFrom.Text = DateTime.Parse(lblFrom.Text).ToString(Session["MTimeFormat"].ToString());


            Label lblTo = (Label)e.Row.Cells[4].FindControl("lblTo");

            //lblTo.Text = (Convert.ToDateTime(lblTo.Text.ToString())).ToString(Session["MDateFormat"].ToString());
            lblTo.Text = DateTime.Parse(lblTo.Text).ToString(Session["MTimeFormat"].ToString());
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Build custom header.
            GridView oGridView = (GridView)sender;
            GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell oTableCell = new TableCell();

            //Add Department
            oTableCell.Text = "Permission";
            oTableCell.Font.Bold = true;
            oTableCell.ColumnSpan = 8;
            oTableCell.HorizontalAlign = HorizontalAlign.Center;
            oGridViewRow.Cells.Add(oTableCell);
            oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SendMail();
    }
    public void SendMail()
    {
        string CustEmailID = string.Empty;
        if (ViewState["CustEmail"] != null)
        {
            CustEmailID = ViewState["CustEmail"].ToString();
        }
        //MemoryStream memStream;
        //memStream = (MemoryStream)

        //rptDoc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        MailMessage msg = new MailMessage();


        // SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings.Get("mailServerSmtpIP"), Convert.ToInt32(ConfigurationManager.AppSettings.Get("mailServerPort")));
        MailAddress mailfrom = new MailAddress(ConfigurationManager.AppSettings.Get("FromId"));

        if (Session["loginEmailId"] == null || Convert.ToString(Session["loginEmailId"]) == "")
        {
            mailfrom = new MailAddress(ConfigurationManager.AppSettings.Get("FromId"));
        }
        else
        {
            mailfrom = new MailAddress(Convert.ToString(Session["loginEmailId"]));
        }

        //DataSet ds = new DataSet();
        //GetReports obj = new GetReports();
        //ds = obj.SelectEmailIDByLevel(Convert.ToInt32(Request.QueryString["ParentLevelId"]));
        string strTo = "";
        string strCC = "";
        //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 )
        //{

        //    if (ds.Tables[0].Rows[0]["TomailIds"] != DBNull.Value && Convert.ToString(ds.Tables[0].Rows[0]["TomailIds"])!="")
        //    {
        //        strTo = Convert.ToString(ds.Tables[0].Rows[0]["TomailIds"]);
        //    }

        //    if (ds.Tables[0].Rows[0]["CcMailIDs"] != DBNull.Value && Convert.ToString(ds.Tables[0].Rows[0]["CcMailIDs"])!="")
        //    {
        //        strCC = Convert.ToString(ds.Tables[0].Rows[0]["CcMailIDs"]);

        //    }           

        //}

        strTo = txtApprovalTO.Text;
        strCC = txtApprovalCC.Text;

        if (strTo != "" || strCC != "")
        {
            msg.From = mailfrom;

            if (strTo != "")
            {

                //msg.To.Add(strTo);
                string[] strArrEmailAddressForTo;
                strArrEmailAddressForTo = SplitCCTo(strTo);

                MailAddressCollection MailAddrCollectionTo = msg.To;

                for (int iLoop = 0; iLoop < strArrEmailAddressForTo.Length; iLoop++)
                {
                    MailAddress newCC = new MailAddress(strArrEmailAddressForTo[iLoop]);
                    MailAddrCollectionTo.Add(newCC);
                }
            }
            if (strCC != "")
            {
                //msg.CC.Add(strCC);

                string[] strArrEmailAddressForCC;
                strArrEmailAddressForCC = SplitCCTo(strCC);

                MailAddressCollection MailAddrCollectionCC = msg.CC;

                for (int iLoop = 0; iLoop < strArrEmailAddressForCC.Length; iLoop++)
                {
                    MailAddress newCC = new MailAddress(strArrEmailAddressForCC[iLoop]);
                    MailAddrCollectionCC.Add(newCC);
                }


            }
            msg.Subject = "TimeCheck | Leave & Permission | Daily Report - " + strOrgName + " | " + DateTime.Parse(Request.QueryString["dtFrom"]).ToString("dd-MMM-yyyy");

            msg.Body = "TimeCheck | Leave & Permission | Daily Report| " + DateTime.Parse(Request.QueryString["dtFrom"]).ToString("dd-MMM-yyyy");




            //Frames the Text             
            //StreamReader srGetMailBody;
            string strMailBody;
            strMailBody = "<font face='arial' size='2'><b>Dear All,</b><br><br>Kindly find the attached Leave / Permission report for " + DateTime.Parse(Request.QueryString["dtFrom"]).ToString("dd-MMM-yyyy");
            strMailBody = strMailBody + "<br><br><b>Best Regards,<br>" + Convert.ToString(Session["user"]) + "</b></font>";


            string filename = "LeavePermissionDetails_" + Convert.ToString(Request.QueryString["dtFrom"]) + ".pdf";
            //msg.Attachments.Add(new Attachment(memStream, filename));
            DataTable dtDailyLeave = new DataTable();
            //          DataView dataView;
            //          SqlDataSource obsql = new SqlDataSource();

            //          dataView = (DataView)obsql.Select(DataSourceSelectArguments.Empty);

            //if (dataView != null)
            //{

            //          //msg.Body = strMailBody;

            //          //dtDailyLeave=(DataTable)grv_DailyLeave.DataSource;
            //    dtDailyLeave = dataView.Table.Clone();
            //     }
            //            GridViewRow[] Chartarr = new GridViewRow[grv_DailyLeave.Rows.Count];
            //            grv_DailyLeave.Rows.CopyTo(Chartarr, 0);

            //            foreach (GridViewRow row in Chartarr)
            //            {
            //                DataRow datarw;
            //                datarw = dtDailyLeave.NewRow();
            //                for (int i = 0; i < row.Cells.Count; i++)
            //                {
            //                    datarw[i] = row.Cells[i].Text;
            //                }

            //                dtDailyLeave.Rows.Add(datarw);
            //            }


            this.EnableViewState = false;

            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            divLeave.RenderControl(oHtmlTextWriter);

            //msg.Body=oStringWriter.ToString();


            System.IO.StringWriter oStringWriterPer = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriterPer = new System.Web.UI.HtmlTextWriter(oStringWriterPer);
            divPermission.RenderControl(oHtmlTextWriterPer);

            System.IO.StringWriter oStringWriterLevUnp = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriterLevUnp = new System.Web.UI.HtmlTextWriter(oStringWriterLevUnp);
            divLeaveUnPlanned.RenderControl(oHtmlTextWriterLevUnp);


            System.IO.StringWriter oStringWriterPerUnp = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriterPerUnp = new System.Web.UI.HtmlTextWriter(oStringWriterPerUnp);
            divPermissionUnPlanned.RenderControl(oHtmlTextWriterPerUnp);


            string strbody = "";
            //strbody = "<font face='arial' size='2'><b>Dear All,</b><br>";
            //strbody = "<font face='arial' size='1'>";
            strbody = strbody + oStringWriter.ToString().Replace("<td", "<td style='boder-color:black; border-style:solid; border-collapse:collapse' ").Replace("width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px", "width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px; border-color:black").Replace("<th", "<th style='boder-color:black; border-style:solid; border-collapse:collapse' ")
                + oStringWriterPer.ToString().Replace("<td", "<td style='boder-color:black; border-style:solid; border-collapse:collapse' ").Replace("width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px", "width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px; border-color:black").Replace("<th", "<th style='boder-color:black; border-style:solid; border-collapse:collapse' ")
                + oStringWriterLevUnp.ToString().Replace("<td", "<td style='boder-color:black; border-style:solid; border-collapse:collapse' ").Replace("width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px", "width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px; border-color:black").Replace("<th", "<th style='boder-color:black; border-style:solid; border-collapse:collapse' ")
                + oStringWriterPerUnp.ToString().Replace("<td", "<td style='boder-color:black; border-style:solid; border-collapse:collapse' ").Replace("width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px", "width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px; border-color:black").Replace("<th", "<th style='boder-color:black; border-style:solid; border-collapse:collapse' ");
            //strbody = strbody + "<br><b>Best Regards,<br>" + Convert.ToString(Session["user"]) + "</b></font>";
            strbody = strbody + "<br><table style='width:800px;border-collapse:collapse;font-family: Arial; font-size: 11px; border-color:black'><tr><td style='width:800px;border-collapse:collapse;font-family: Arial; border-color:black'><b>Best Regards,</b></td><tr><td style='width:800px;border-collapse:collapse;font-family: Arial; border-color:black'><b>" + Convert.ToString(Session["user"]) + "</b></td></tr></table>";
            msg.Body = strbody;


            if (string.IsNullOrEmpty(strMailBody))
            {
                msg.IsBodyHtml = false;
            }
            else
            {
                msg.IsBodyHtml = true;
            }


            SmtpClient SmtpMail = new SmtpClient();
            SmtpMail.Host = ConfigurationManager.AppSettings.Get("SMTPClientHost");
            SmtpMail.UseDefaultCredentials = true;
            SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpMail.Send(msg);

            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('Email Sent Successfully');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("msg", strMsg);
        }

        else
        {
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('There is no Email id configured for the selected level');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("Display Message", strMsg);
            return;
        }

        //memStream.Close();
        //rptDoc.Close();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

        return;

    }
    private string[] SplitCCTo(string CCTo)
    {
        string[] strArrEmailAddressForCC;
        strArrEmailAddressForCC = CCTo.Split(';');

        return (strArrEmailAddressForCC);
    }
    public string buildPage(string strPageTitle, DataTable myDT)
    {
        StringBuilder sb = new StringBuilder();

        //HTML header
        sb.Append("<html>");
        sb.Append("<head>");
        sb.Append("<title>");
        sb.Append(strPageTitle);
        sb.Append("</title>");
        sb.Append("<style type='text/css'>");
        sb.Append(".Header {color: #FFFFFF; font-family: Arial;font-size: x-small;font-weight: bold;}");
        sb.Append(".rows {font-family: Arial; font-size: x-small;background-color:#FFE3B9}");
        sb.Append(".rowsNew {font-family: Arial; font-size: x-small;background-color:#FFEED5}");
        sb.Append("</style>");
        sb.Append("</head>");
        sb.Append("<body>");
        sb.Append("<table border='1' cellpadding='2' style='border-collapse: collapse' bordercolor='#666666' width='700'>");
        sb.Append("  <tr bgcolor='#003466'>");
        //Table header       
        foreach (DataColumn Col in myDT.Columns)
        {
            sb.Append("<td><span class='Header'>" + Col.ColumnName.ToString() + "</span></td>");
        }

        sb.Append("  </tr>");
        //add rows to the table for the length of the dataset
        int intWidth = 0;

        int intIncrement = 0;
        foreach (DataRow Row in myDT.Rows)
        {
            intWidth = 0;
            if (intIncrement % 2 == 0)
            {
                sb.Append("  <tr>");
            }
            else
            {
                sb.Append("  <tr>");
            }
            foreach (DataColumn Col in myDT.Columns)
            {

                sb.Append("<td><font face='Arial' size='2'>" + Row[Col.ColumnName].ToString() + "</font></td>");

                //if (intWidth == 0)
                //{
                //    if (blnPrivateNotes == true)
                //    {
                //        sb.Append("<td width='30%'><font face='Arial' size='2'>" + Row[Col.ColumnName].ToString() + "</font></td>");
                //    }
                //    else
                //    {
                //        sb.Append("<td><font face='Arial' size='2'>" + Row[Col.ColumnName].ToString() + "</font></td>");
                //    }
                //    intWidth = 1;
                //}
                //else if (intWidth != 0)
                //{
                //    if (blnPrivateNotes == true)
                //    {
                //        sb.Append("<td width='70%'><font face='Arial' size='2'>" + Row[Col.ColumnName].ToString() + "</font></td>");
                //    }
                //    else
                //    {
                //        sb.Append("<td><font face='Arial' size='2'>" + Row[Col.ColumnName].ToString() + "</font></td>");
                //    }
                //    intWidth = 0;
                //}
            }
            sb.Append("  </tr>");
            intIncrement = intIncrement + 1;
        }
        //Close the html string
        sb.Append("</table>");
        sb.Append("</body>");
        sb.Append("</html>");

        return sb.ToString();
    }
}
