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.Resources;
using System.Globalization;
using System.Threading;

public partial class Organization_Settings_Organization_CCMailGroupModify : Base.BasePage
{

    ResourceManager rm;

    ShiftEvenData objShift = new ShiftEvenData();
    cls_Common objClassCommon = new cls_Common();
    CCGroupEventData_Angler objCCGroup = new CCGroupEventData_Angler();


    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    DataSet dsCCGroupDetails = new DataSet();
    DataSet dsCCGroupById = new DataSet();
    string gstrUserGroupName = string.Empty;
    int intLevelId;
    int intDesignationId;
    int intCCGroupId;

    protected override void Page_Load(object sender, EventArgs e)
    {

        // Session Checking
        base.Page_Load(sender, e);

        // Get the culture info to set the language
        if (!Page.IsPostBack)
        {
            //base.CheckPages(110);
        }

        CultureInfo ci;

        string source_Language;
        source_Language = Session["Session_ActiveLanguage"].ToString();
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);

        gstrUserGroupName = Session["UserGroupName"].ToString();

        intCCGroupId = Convert.ToInt32(Request.QueryString["CCGroupId"].ToString());

        if (!IsPostBack)
        {
            //Gets the CC group based on the CCGroupId
            dsCCGroupById = objCCGroup.GetCCGroupById(intCCGroupId);

            if (dsCCGroupById.Tables[0] != null)
            {
                if (dsCCGroupById.Tables[0].Rows.Count > 0)
                {
                    intLevelId = Convert.ToInt16(dsCCGroupById.Tables[0].Rows[0]["LevelID"].ToString());
                    ViewState["LevelID"] = intLevelId;
                    intDesignationId = Convert.ToInt16(dsCCGroupById.Tables[0].Rows[0]["DesignationID"].ToString());
                    ViewState["DesignationId"] = intDesignationId;
                }
            }

            //Gets the oganization and usergroups for loading.
            dsCCGroupDetails = objCCGroup.GetCCGroupDetails(gstrUserGroupName, Convert.ToString(Session["LevelRights"]));
            ddlOrganization.Focus();
            OrganizationBinding();
            UserGroupBinding();
            cblPlanned.PreRender += LB_PreRender;
            cblUnPlanned.PreRender += LB_PreRender;
            cblOthers.PreRender += LB_PreRender;
            cblODApproval.PreRender += LB_PreRender;
            DesignationBinding(intLevelId);

            ddlOrganization.SelectedValue = Convert.ToString(intLevelId);
            ddlDesination.SelectedValue = Convert.ToString(intDesignationId);

            //Fills the organization and designation 
            lblOrganizationData.Text = ddlOrganization.SelectedItem.Text;
            lblDesignationData.Text = ddlDesination.SelectedItem.Text;

            //Fills the user groups.
            CheckUserGroups();

            //Fills the planned status for up-line supervisor
            if (Convert.ToInt16(dsCCGroupById.Tables[0].Rows[0]["MailTo_SecondLevel_Planned"]) == 1)
            {
               rbYes_Planned.Checked=true;
               rbNo_Planned.Checked = false;
            }
            else
            {
                rbNo_Planned.Checked=true;
                rbYes_Planned.Checked = false;
            }

            //Fills the unplanned status for up-line supervisor
            if (Convert.ToInt16(dsCCGroupById.Tables[0].Rows[0]["MailTo_SecondLevel_UnPlanned"]) == 1)
            {
                rbYes_UnPlanned.Checked = true;
                rbNo_UnPlanned.Checked = false;
            }
            else
            {
                rbNo_UnPlanned.Checked = true;
                rbYes_UnPlanned.Checked = false;
            }

        }

        //Set UserRights
        //UserRights();
    }

    //Checks the appropriate user groups.
    private void CheckUserGroups()
    {
        //Planned user groups
        if (dsCCGroupById.Tables[1] != null)
        {
            if (dsCCGroupById.Tables[1].Rows.Count > 0)
            {
                foreach (DataRow drPlanned in dsCCGroupById.Tables[1].Rows)
                {
                    cblPlanned.Items.FindByValue(drPlanned["CC_Planned"].ToString()).Selected = true;
                }
            }
        }

        //UnPlanned user groups
        if (dsCCGroupById.Tables[2] != null)
        {
            if (dsCCGroupById.Tables[2].Rows.Count > 0)
            {
                foreach (DataRow drPlanned in dsCCGroupById.Tables[2].Rows)
                {
                  cblUnPlanned.Items.FindByValue(drPlanned["CC_UnPlanned"].ToString()).Selected = true;
                }
            }
        }

        //Others user groups
        if (dsCCGroupById.Tables[3] != null)
        {
            if (dsCCGroupById.Tables[3].Rows.Count > 0)
            {
                foreach (DataRow drPlanned in dsCCGroupById.Tables[3].Rows)
                {
                   cblOthers.Items.FindByValue(drPlanned["CC_Others"].ToString()).Selected = true;
                }
            }
        }

        //OnDuty Approval user groups
        if (dsCCGroupById.Tables[4] != null)
        {
            if (dsCCGroupById.Tables[4].Rows.Count > 0)
            {
                foreach (DataRow drPlanned in dsCCGroupById.Tables[4].Rows)
                {
                    cblODApproval.Items.FindByValue(drPlanned["CC_ODApproval"].ToString()).Selected = true;
                }
            }
        }
    }

    //Loads the Organization based on the level rights.
    private void OrganizationBinding()
    {
        if (dsCCGroupDetails != null)
        {
            if (dsCCGroupDetails.Tables[0].Rows.Count > 0)
            {
                ddlOrganization.DataSource = dsCCGroupDetails.Tables[0];
                ddlOrganization.DataTextField = "CompanyName";
                ddlOrganization.DataValueField = "CompanyId";
                ddlOrganization.DataBind();
            }
        }
    }

     //Loads the designation.
    private void DesignationBinding(int LevelId)
    {
        ddlDesination.Items.Clear();
        ddlDesination.Items.Insert(0, "-Select-");
        DataSet dsDesignation = new DataSet();
        dsDesignation = objCCGroup.GetCCGroupDesignation(LevelId);
        if (dsDesignation != null)
        {
            if (dsDesignation.Tables[0].Rows.Count > 0)
            {
                ddlDesination.DataSource = dsDesignation.Tables[0];
                ddlDesination.DataTextField = "DesignationName";
                ddlDesination.DataValueField = "DesignationId";
                ddlDesination.DataBind();
            }
        }
    }

    //Loads the UserGroup in all the list boxes.
    private void UserGroupBinding()
    {
        if (dsCCGroupDetails != null)
        {
            if (dsCCGroupDetails.Tables[1].Rows.Count > 0)
            {
                cblPlanned.DataSource = dsCCGroupDetails.Tables[1];
                cblUnPlanned.DataSource = dsCCGroupDetails.Tables[1];
                cblOthers.DataSource = dsCCGroupDetails.Tables[1];
                cblODApproval.DataSource = dsCCGroupDetails.Tables[1];


                cblPlanned.DataTextField = "UserGroupName";
                cblPlanned.DataValueField = "UserGroupId";
                cblUnPlanned.DataTextField = "UserGroupName";
                cblUnPlanned.DataValueField = "UserGroupId";
                cblOthers.DataTextField = "UserGroupName";
                cblOthers.DataValueField = "UserGroupId";
                cblODApproval.DataTextField = "UserGroupName";
                cblODApproval.DataValueField = "UserGroupId";

                cblPlanned.DataBind();
                cblUnPlanned.DataBind();
                cblOthers.DataBind();
                cblODApproval.DataBind();
            }
        }
    }

    // Getting Information from resource file and display it in label

    private void LoadStrings(CultureInfo ci)
    {
        //lbl_Header.Text = rm.GetString("OrganisationSettings_ApprovalSettings", ci);
        
    }
   
    public void UserRights()
    {
        try
        {
            ds_Checkuser = objCheckUser.checkRights(115, 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"]) == 0)
                {
                    Response.Redirect("OrganizationLandingPage.aspx");
                }

                if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
                {
                    this.btnModify.Visible = true;
                }
                else
                {
                    this.btnModify.Visible = false;
                }
            }
            else
            {
                Response.Redirect("OrganizationLandingPage.aspx");
            }
        }
        catch
        {
            //
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        int intPlannedStatus = 0;
        int intUnPlannedsttaus = 0;
        //check the mail to up-line supervisor for planned
        if (rbYes_Planned.Checked == true)
        {
            intPlannedStatus = 1;
        }
        else if (rbNo_Planned.Checked == true)
        {
            intPlannedStatus = 0;
        }
        //check the mail to up-line supervisor for unplanned
        if (rbYes_UnPlanned.Checked == true)
        {
            intUnPlannedsttaus = 1;
        }
        else if (rbNo_UnPlanned.Checked == true)
        {
            intUnPlannedsttaus = 0;
        }

        //Updates the cc group.
        //Note: The intValue will be zero for update.
        if (ViewState["LevelID"] != null && ViewState["DesignationId"] != null)
        {
            int intValue = objCCGroup.InsertUpdateDelete_CCGroup(Convert.ToInt16(ViewState["LevelID"]), Convert.ToInt16(ViewState["DesignationId"]), intPlannedStatus, intUnPlannedsttaus, "U");
        }
        InsertPlannedCC(intCCGroupId);
        InsertUnPlannedCC(intCCGroupId);
        InsertOthersCC(intCCGroupId);
        InsertODApprovalCC(intCCGroupId);
        base.MessageWithRedirect("Modified succesfully", "Organization_CCMailGroup.aspx");
    }

    //Deletes and Inserts Planned CC user groups using bulk insert.
    public void InsertPlannedCC(int intCcGroupId)
    {
        DataTable dtPlannedCC = new DataTable("TA_Tbl_CCGroup_Planned");
        DataColumn dcCCGroupID = new DataColumn("CCGroupID", System.Type.GetType("System.Int32"));
        DataColumn dcCCPlanned = new DataColumn("CC_Planned", System.Type.GetType("System.Int32"));
        dtPlannedCC.Columns.Add(dcCCGroupID);
        dtPlannedCC.Columns.Add(dcCCPlanned);
        DataRow drPlannedCC;
        foreach (ListItem liPlannedCC in cblPlanned.Items)
        {
            if (liPlannedCC.Selected == true)
            {
                drPlannedCC = dtPlannedCC.NewRow();
                drPlannedCC.BeginEdit();
                drPlannedCC["CCGroupID"] = intCcGroupId;
                drPlannedCC["CC_Planned"] = liPlannedCC.Value;
                drPlannedCC.EndEdit();
                dtPlannedCC.Rows.Add(drPlannedCC);
            }
        }

        //Delets the CC group in the child table(planned)
        objCCGroup.delete_CCGroupsInChildTables(Convert.ToString(intCCGroupId), "P");

        //Inserts the user groups into TA_Tbl_CCGroup_Planned.
        objCCGroup.InsertInto_TA_Tbl_CCGroup_Planned(dtPlannedCC);
    }

    //Deletes and Inserts UnPlanned CC user groups using bulk insert.
    public void InsertUnPlannedCC(int intCcGroupId)
    {
        DataTable dtUnPlannedCC = new DataTable("TA_Tbl_CCGroup_UnPlanned");
        DataColumn dcCCGroupID = new DataColumn("CCGroupID", System.Type.GetType("System.Int32"));
        DataColumn dcCCUnPlanned = new DataColumn("CC_UnPlanned", System.Type.GetType("System.Int32"));
        dtUnPlannedCC.Columns.Add(dcCCGroupID);
        dtUnPlannedCC.Columns.Add(dcCCUnPlanned);
        DataRow drUnPlannedCC;
        foreach (ListItem liUnPlannedCC in cblUnPlanned.Items)
        {
            if (liUnPlannedCC.Selected == true)
            {
                drUnPlannedCC = dtUnPlannedCC.NewRow();
                drUnPlannedCC.BeginEdit();
                drUnPlannedCC["CCGroupID"] = intCcGroupId;
                drUnPlannedCC["CC_UnPlanned"] = liUnPlannedCC.Value;
                drUnPlannedCC.EndEdit();
                dtUnPlannedCC.Rows.Add(drUnPlannedCC);
            }
        }

        //Delets the CC group in the child table(unplanned)
        objCCGroup.delete_CCGroupsInChildTables(Convert.ToString(intCCGroupId), "U");

        //Inserts the user groups into TA_Tbl_CCGroup_UnPlanned.
        objCCGroup.InsertInto_TA_Tbl_CCGroup_UnPlanned(dtUnPlannedCC);
    }

    //Deletes and Inserts Others(approval/modification/cancel) CC user groups using bulk insert.
    public void InsertOthersCC(int intCcGroupId)
    {
        DataTable dtOtherCC = new DataTable("TA_Tbl_CCGroup_Others");
        DataColumn dcCCGroupID = new DataColumn("CCGroupId", System.Type.GetType("System.Int32"));
        DataColumn dcCCOthers = new DataColumn("CC_Others", System.Type.GetType("System.Int32"));
        dtOtherCC.Columns.Add(dcCCGroupID);
        dtOtherCC.Columns.Add(dcCCOthers);
        DataRow drOtherCC;
        foreach (ListItem liOtherCC in cblOthers.Items)
        {
            if (liOtherCC.Selected == true)
            {
                drOtherCC = dtOtherCC.NewRow();
                drOtherCC.BeginEdit();
                drOtherCC["CCGroupID"] = intCcGroupId;
                drOtherCC["CC_Others"] = liOtherCC.Value;
                drOtherCC.EndEdit();
                dtOtherCC.Rows.Add(drOtherCC);
            }
        }

        //Delets the CC group in the child table(others)
        objCCGroup.delete_CCGroupsInChildTables(Convert.ToString(intCCGroupId), "O");

        //Inserts the user groups into TA_Tbl_CCGroup_Others.
        objCCGroup.InsertInto_TA_Tbl_CCGroup_Others(dtOtherCC);
    }

    //Inserts OnDuty Approval CC user groups using bulk insert.
    public void InsertODApprovalCC(int intCcGroupId)
    {
        DataTable dtODApprovalCC = new DataTable("TA_Tbl_CCGroup_ODApproval");
        DataColumn dcCCGroupID = new DataColumn("CCGroupId", System.Type.GetType("System.Int32"));
        DataColumn dcCCODApproval = new DataColumn("CC_ODApproval", System.Type.GetType("System.Int32"));
        dtODApprovalCC.Columns.Add(dcCCGroupID);
        dtODApprovalCC.Columns.Add(dcCCODApproval);
        DataRow drODApproval;
        foreach (ListItem liOtherCC in cblODApproval.Items)
        {
            if (liOtherCC.Selected == true)
            {
                drODApproval = dtODApprovalCC.NewRow();
                drODApproval.BeginEdit();
                drODApproval["CCGroupID"] = intCcGroupId;
                drODApproval["CC_ODApproval"] = liOtherCC.Value;
                drODApproval.EndEdit();
                dtODApprovalCC.Rows.Add(drODApproval);
            }
        }

        //Delets the CC group in the child table(others)
        objCCGroup.delete_CCGroupsInChildTables(Convert.ToString(intCCGroupId), "OD");

        //Inserts the user groups into TA_Tbl_CCGroup_Others.
        objCCGroup.InsertInto_TA_Tbl_CCGroup_ODApproval(dtODApprovalCC);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        objCCGroup.delete_CCGroupsInChildTables(Convert.ToString(intCCGroupId), "A");
        base.MessageWithRedirect("Deleted succesfully", "Organization_CCMailGroup.aspx");
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Organization_CCMailGroup.aspx");
    }

    protected void ddlOrganization_SelectedIndexChanged(object sender, EventArgs e)
    {
        DesignationBinding(Convert.ToInt16(ddlOrganization.SelectedValue));
    }

    private void LB_PreRender(object sender, System.EventArgs e)
    {
        foreach (ListItem item in cblPlanned.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
        foreach (ListItem item in cblUnPlanned.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
        foreach (ListItem item in cblOthers.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
        foreach (ListItem item in cblODApproval.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }

}
