using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
using System.Web.UI;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.Util;
using System.Text;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Web.UI.WebControls;

public partial class Masters_SMS_LogDetails : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    SMSNotification objsms = new SMSNotification();
    DataSet ds_Checkuser = new DataSet();
    DataSet ds_Templates = new DataSet();
    string gstrUserGroupName = string.Empty;
    int EmployeeID = 0;
    string source_Language;
    string strScript = string.Empty;
    string strFromdate, gstrDateFormat, strTodate;
    #endregion
    #region PageLoadEvent
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        //Set UserRights
        if (!IsPostBack)
        {
            objCommon.menu("Configuration", "245");
        }
        UserRights();
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        // LoadStrings Function fills the Label from the Resource File
        LoadStrings(ci);
        EmployeeID = Convert.ToInt32(Convert.ToString(Session["EmpAutoID"]));
        hdnDateTime.Value = Convert.ToString(Session["IDateFormat"]);
        if (!IsPostBack)
        {
            txtFromdate.Text = (Convert.ToDateTime(objCommon.CurrentMonthStartDate())).ToString(Convert.ToString(Session["MDateFormat"]));
            txtTodate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Convert.ToString(Session["MDateFormat"]));
            BindTemplates();
        }
        BindGridview();
    }
    #endregion
    #region UserDefinedFunctions
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("SMSLogDetails", ci);
        lblTemplate.Text = rm.GetString("SMSTemplateList_TemplateName", ci);
        lblReceivers.Text = rm.GetString("Receivers", ci);
        lblFromDate.Text = rm.GetString("OnDuty_InDate", ci);
        lblToDate.Text = rm.GetString("OnDuty_OutDate", ci);
        lblStatus.Text = rm.GetString("email_Status", ci);
        btn_Search.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Search", ci);
        btn_Clear.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Clear", ci);
        lnkbtnPdf.Text = "&nbsp;&nbsp;" + rm.GetString("PDF", ci);
        lnkbtnCSV.Text = "&nbsp;&nbsp;" + rm.GetString("CSV", ci);
        lnkbtnExcel.Text = "&nbsp;&nbsp;" + rm.GetString("Excel", ci);
        grdSmslog.Columns[1].Caption = rm.GetString("SMS_Grid_TemplateName", ci);
        grdSmslog.Columns[2].Caption = rm.GetString("SMS_Grid_Receiver", ci);
        grdSmslog.Columns[3].Caption = rm.GetString("SMS_Grid_Message", ci);
        grdSmslog.Columns[4].Caption = rm.GetString("SMS_Grid_TriggeredTimings", ci);
        grdSmslog.Columns[5].Caption = rm.GetString("SMS_Grid_Status", ci);      

    }
    private void UserRights()
    {
        //Todo: Need to change ID value in below
        ds_Checkuser = objCheckUser.checkRights(245, Convert.ToInt32(Session["userGroup_Id"]), "ViewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {

        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void BindTemplates()
    {
        DataSet dsTemplates = new DataSet();
        dsTemplates = objsms.SelectTemplateDetails(0);
        if (dsTemplates.Tables.Count > 0)
        {
            if (dsTemplates.Tables[0].Rows.Count > 0)
            {
                ddlTemplate.DataSource = dsTemplates;
                ddlTemplate.DataTextField = "TemplateName";
                ddlTemplate.DataValueField = "TemplateId";
                ddlTemplate.DataBind();
            }
        }
    }
    private void BindGridview()
    {
       gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strFromdate = txtFromdate.Text;
        strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        strTodate = txtTodate.Text;
        strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);

        DataSet dsSMSLogDetails = new DataSet();
        if (Convert.ToString(Session["LogDetails"]) != "")
        {
            dsSMSLogDetails = (DataSet)Session["LogDetails"];
        }
        else
        {

            dsSMSLogDetails = objsms.SelectLogDetails(Convert.ToInt32(Session["parentLevelId"]),
                Convert.ToString(ddlTemplate.SelectedItem.Text), Convert.ToString(txtReceivers.Text),
                strFromdate, strTodate,
                Convert.ToInt16(ddlStatus.SelectedItem.Value), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]));
        }
        if (dsSMSLogDetails.Tables.Count > 0)
        {
            if (dsSMSLogDetails.Tables[0].Rows.Count > 0)
            {
                grdSmslog.DataSource = dsSMSLogDetails;
                grdSmslog.DataBind();
                Session["LogDetails"] = dsSMSLogDetails;
            }
        }
    }    
    #endregion
    #region ButtonEvents
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        DataSet dsSMSLogDetails = new DataSet();
        try
        {
            if (Page.IsValid == true)
            {
                gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
                strFromdate = txtFromdate.Text;
                strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
                strTodate = txtTodate.Text;
                strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
                dsSMSLogDetails = objsms.SelectLogDetails(Convert.ToInt32(Session["parentLevelId"]), Convert.ToString(ddlTemplate.SelectedItem.Text), Convert.ToString(txtReceivers.Text), strFromdate, strTodate, Convert.ToInt16(ddlStatus.SelectedItem.Value), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]));
                if (dsSMSLogDetails.Tables.Count > 0)
                {
                    if (dsSMSLogDetails.Tables[0].Rows.Count > 0)
                    {
                        grdSmslog.DataSource = dsSMSLogDetails;
                        grdSmslog.DataBind();
                        Session["LogDetails"] = dsSMSLogDetails;
                    }
                    else
                    {
                        grdSmslog.DataSource = "";
                        grdSmslog.DataBind();
                        Session["LogDetails"] = "";
                    }
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
   
    protected void btnExport_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        grdSmslog.Columns[0].Visible = false;
        ASPxGridViewExporter1.FileName = "List of SMS Logs";
        ASPxGridViewExporter1.Landscape = true;
        ASPxGridViewExporter1.LeftMargin = 30;
        ASPxGridViewExporter1.RightMargin = 30;
        ASPxGridViewExporter1.MaxColumnWidth = 200;
        switch (btn.CommandName)
        {
            case "excel":
                ASPxGridViewExporter1.WriteXlsxToResponse();
                break;
            case "pdf":
                ASPxGridViewExporter1.WritePdfToResponse();
                break;
            default:
                break;
        }
    }
    #endregion
}
