using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DevExpress.Web;
public partial class Masters_TA_UI_Master_ProjectList : Base.BasePage
{
    #region "Variable Declaration"
    LeaveEventData objLeave = new LeaveEventData();
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    ProjectEventData objProjectEventData = new ProjectEventData(); 
    int EmployeeID = 0;
    string gstrUserGroupName = string.Empty, strScript = string.Empty, source_Language;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        gv_Project.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }       
        if (!IsPostBack)
        {
            base.CheckPages(283);
            LoadStrings(ci);
            
            UserRights();
            objcommon.menu("Configuration", "283");

        }
        EmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
        BindGridView();
        btn_add.Focus();
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_ProjectAdd.aspx");
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_ProjectList.aspx");
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int Project_ID = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_ProjectAdd.aspx?Project_Id=" + Project_ID);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int ProjectId = Convert.ToInt32(btn.CommandName);
        objProjectEventData.DeleteProject(ProjectId, Convert.ToInt32(Session["EmpAutoID"]));
        base.MessageWithRedirect(rm.GetString("Project_Alertdeletedsuccessfully", ci), "TA_UI_Master_ProjectList.aspx");
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                objProjectEventData.DeleteProject(Convert.ToInt32(ChkBoxIDs[i]), Convert.ToInt32(Session["EmpAutoID"]));
            }
            base.MessageWithRedirect(rm.GetString("Project_Alertdeletedsuccessfully", ci), "TA_UI_Master_ProjectList.aspx");
        }
        else
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    #endregion
    #region UserDefinedFunction 
    public void BindGridView()
    {
        DataSet ds = new DataSet();
        ds = objProjectEventData.SelectProjectDetails(Convert.ToInt32(Session["EmpAutoID"]));
        gv_Project.DataSource = ds;
        gv_Project.DataBind();
        string strChkIds = string.Empty;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            strChkIds = strChkIds + Convert.ToString(ds.Tables[0].Rows[i][0]) + ",";
        }
        if (strChkIds != string.Empty && strChkIds != null)
        {
            strChkIds = strChkIds.Substring(0, strChkIds.Length - 1);
            hdnGetAllid.Value = Convert.ToString(strChkIds);
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_ProjectMaster.Text = rm.GetString("ProjectMaster", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Cancel.Text = rm.GetString("btn_Back", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        gv_Project.Columns[2].Caption = rm.GetString("Project_ProjectCode", ci);
        gv_Project.Columns[3].Caption = rm.GetString("Project_Name", ci);
        gv_Project.Columns[4].Caption = rm.GetString("Project_Startdate", ci);
        gv_Project.Columns[5].Caption = rm.GetString("Project_Enddate", ci);
        gv_Project.Columns[6].Caption = rm.GetString("Project_Status", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
        lblProjectList.Text = rm.GetString("Project_ListPage", ci); 
        gvProjectList.Columns[1].HeaderText = rm.GetString("Project_ProjectCode", ci);
        gvProjectList.Columns[2].HeaderText = rm.GetString("Project_Name", ci);
        gvProjectList.Columns[3].HeaderText = rm.GetString("Project_Startdate", ci);
        gvProjectList.Columns[4].HeaderText = rm.GetString("Project_Enddate", ci);
        gvProjectList.Columns[5].HeaderText = rm.GetString("Project_Status", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(283, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_add.Visible = true;
            }
            else
            {
                this.btn_add.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                gv_Project.Columns[9].Visible = true;
                btn_PermanentDelete.Visible = true;
            }
            else
            {
                gv_Project.Columns[9].Visible = false;
                btn_PermanentDelete.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.gv_Project.Columns[8].Visible = true;
            }
            else
            {
                this.gv_Project.Columns[8].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                this.gv_Project.Columns[7].Visible = true;
            }
            else
            {
                this.gv_Project.Columns[7].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    } 
    #endregion
    #region GridviewEvents
    protected void gv_Project_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        ds_Checkuser = objCheckUser.checkRights(273, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (e.RowType == GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            Label DeletedRecord = (Label)gv_Project.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True" || DeletedRecord.Text == rm.GetString("Grid_True", ci))
                {
                    DeletedRecord.ForeColor = System.Drawing.Color.Red;
                }
            }
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)gv_Project.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            if (ds_Checkuser.Tables[0].Rows.Count > 0)
            {
                if (ds_Checkuser.Tables[0].Rows.Count > 0)
                {
                    if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
                    {
                        LinkButton LnkbtnDelete = (LinkButton)gv_Project.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");

                        if (deleteChkBxItem != null)
                        {
                            int ProjectId = Convert.ToInt32(e.GetValue("Project_Id"));
                            int Count = objProjectEventData.CountProject(ProjectId, Convert.ToString(Session["UserGroupName"]));
                            if (Count > 0)
                            {
                                deleteChkBxItem.Disabled = false;
                                LnkbtnDelete.Enabled = true;
                            }
                            else
                            {
                                deleteChkBxItem.Disabled = true;
                                LnkbtnDelete.Enabled = false;
                            }
                        }
                    }
                }
            }
        
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = Convert.ToString(e.GetValue("Project_Id"));
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(Convert.ToString(e.GetValue("Project_Id"))) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + Convert.ToString(e.GetValue("Project_Id"));
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    protected void gvProjectList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProjectList.PageIndex = e.NewPageIndex;
    }
    #endregion

}