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_CCMailGroupAdd : Base.BasePage
{

    #region Variable Declaration

    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();
    string gstrUserGroupName = string.Empty;

    #endregion

    #region PageLoad
    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(115);
        }

        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();


        if (!IsPostBack)
        {
            dsCCGroupDetails = objCCGroup.GetCCGroupDetails(gstrUserGroupName, Convert.ToString(Session["LevelRights"]));
            ddlOrganization.Focus();
            OrganizationBinding();
            UserGroupBinding();
            ddlOrganization.PreRender += LB_PreRender;
            //ddlDesination.PreRender += LB_PreRender;
            cblPlanned.PreRender += LB_PreRender;
            cblUnPlanned.PreRender += LB_PreRender;
            cblOthers.PreRender += LB_PreRender;
            cblODApproval.PreRender += LB_PreRender;
        }
        if (ddlOrganization.SelectedValue != "0" && ddlDesination.SelectedValue != "0")
        {
            ddlDesination.Focus();
        }

        //Set UserRights
        //UserRights();
    }
    #endregion

    #region userDefined function
    //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-");
        ddlDesination.Items.FindByText("-Select-").Value = "0";
        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();
            }
        }
        ddlDesination.PreRender += LB_PreRender;
    }

    //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.btnSave.Visible = true;
                }
                else
                {
                    this.btnSave.Visible = false;
                }
            }
            else
            {
                Response.Redirect("OrganizationLandingPage.aspx");
            }
        }
        catch
        {
            //
        }
    }

    //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);
            }
        }
       
        //Inserts the user groups into TA_Tbl_CCGroup_Planned.
        objCCGroup.InsertInto_TA_Tbl_CCGroup_Planned(dtPlannedCC);
    }

    //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);
            }
        }

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

    //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);
            }
        }

        //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);
            }
        }

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

    protected void ddlOrganization_SelectedIndexChanged(object sender, EventArgs e)
    {
        DesignationBinding(Convert.ToInt16(ddlOrganization.SelectedValue));
        if (ddlOrganization.SelectedValue != "0")
        {
            ddlDesination.Focus();
        }
        else
        {
            ddlOrganization.Focus();
        }
    } 
   
    private void LB_PreRender(object sender, System.EventArgs e)
    {
        foreach (ListItem item in ddlOrganization.Items)
        {
            item.Attributes.Add("title", item.Text);
        }

        foreach (ListItem item in  ddlDesination.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
        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);
        }
    }

    #endregion

    #region ButtonEvents

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

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //if (ddlOrganization.SelectedValue == "0")
        //{
        //    base.Message("Select Organization");
        //    return;
        //}
        //if (ddlDesination.SelectedValue == "0")
        //{
        //    base.Message("Select Deisgnation");
        //    return;
        //}
        //check whether the CC group for the same level and  designation exists or not.
        int intCCGroupExists;
        intCCGroupExists = objCCGroup.CheckCcGroupExists(Convert.ToInt32(ddlOrganization.SelectedValue), Convert.ToInt32(ddlDesination.SelectedValue));
        if (intCCGroupExists != 0)
        {
            base.Message("CC group already exists");
            return;
        }

        int intCcGroupId = 0;
        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;
        }

        intCcGroupId = objCCGroup.InsertUpdateDelete_CCGroup(Convert.ToInt32(ddlOrganization.SelectedValue), Convert.ToInt32(ddlDesination.SelectedValue), intPlannedStatus, intUnPlannedsttaus, "I");
        InsertPlannedCC(intCcGroupId);
        InsertUnPlannedCC(intCcGroupId);
        InsertOthersCC(intCcGroupId);
        InsertODApprovalCC(intCcGroupId);
        base.MessageWithRedirect("CC group saved succesfully", "Organization_CCMailGroup.aspx");
    }

    #endregion

}
