using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using CLS_BL;
using CLS_DL;
using CLS_PL;

namespace SUNWEB
{
    public partial class Condimentgroup : System.Web.UI.Page
    {
        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        Int32 K = 0;
        Int32 L = 0;
        Int32 S = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        string ConStr_Rds = ConfigurationManager.ConnectionStrings["ConRDS"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string Curr_DateOnly = System.DateTime.Now.ToString("yyyy-MM-dd");
        DateTime MonthstartDt;
        DataSet ds = new DataSet();
        MYSQLDB ObjMain = new MYSQLDB();
        DataTable DT = new DataTable();
        DataTable Dt2 = new DataTable();
        DataTable DtLicense = new DataTable();
        DataTable DtSuperAdmin = new DataTable();
        DataTable DtMultiBranch = new DataTable();
        DataTable DtNormalUser = new DataTable();
        DataTable DTProductSale = new DataTable();
        MySqlCommand Cmdv = new MySqlCommand();
        MYSQLCLOUD ObjCloudMain = new MYSQLCLOUD();
        // User Variables 
        string UserId = string.Empty;
        string UserName = string.Empty;
        string PortalTypeId = string.Empty;
        string cd_Id = string.Empty;
        string Datapath = string.Empty;
        string cd_BranchAlias = string.Empty;
        string IsMultiBranch = string.Empty;
        string IsSuperAdmin = string.Empty;
        string ActiveCallvalue = string.Empty;
        // License Variables ..
        string PID = string.Empty;
        string Page_Acess = string.Empty;
        string Read_Acess = string.Empty;
        string Write_Acess = string.Empty;
        string Isdowld_Acess = string.Empty;
        string Rcd_id = string.Empty;
        string RPortalid = string.Empty;
        string Useridvalue = string.Empty;
        string QueryCurr_Select = string.Empty;
        string L_MaxUserCnt = string.Empty;

        string L_CondimentGroup = string.Empty;
        string Branchidval = string.Empty;
        string BranchFullName = string.Empty;
        string Curr_BranchFullName = string.Empty;
        string Cmb_BranchName = string.Empty;

        string HQ = string.Empty;
        string Cm_id = string.Empty;
        string ISHQ = string.Empty;
        DataSet DS = new DataSet();

        string Vw_Chk = string.Empty;


        Int32 iAddModifier = 0;
        Int32 iUpdateModifier = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            PID = "30";    // This is for CondimentGroup Page 


            if (!IsPostBack)
            {
                //txtStartDt.Text = Curr_DateOnly;
                //txtEndDt.Text = Curr_DateOnly;
            }
            MonthstartDt = new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);
            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == "") && ((Convert.ToBoolean(Session["IsMultiBranch"])) == true))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();

                    string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                    ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
                    if (ActiveCallvalue != " " || ActiveCallvalue != "Select Branch")
                    {
                        ActiveCall();
                    }
                }
            }
            else if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();
                }
                cd_BranchAlias = cmbBranches.SelectedItem.Text;
                //LoadInventoryListGrid(cd_BranchAlias, txtStartDt.Text, txtEndDt.Text);
            }
            else if (((Convert.ToBoolean(Session["IsMultiBranch"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName();
                    ActiveCall();
                }
            }
            else if (((Session["PortalTypeId"].ToString() != "") || (Convert.ToBoolean(Session["IsSuperAdmin"])) == false) && ((Convert.ToBoolean(Session["IsMultiBranch"])) == false))
            {
                cmbBranches.Enabled = false;
                cmbBranches.Visible = false;

                Cm_id = ObjSel.GETcmid(Session["Cd_Id"].ToString());  // Get cm_id
                HQ = ObjSel.Get_HQ(Cm_id);     // Get the HQ Value
                Curr_BranchFullName = ObjSel.GetBranchFullName(HQ, Session["Branch_HQ"].ToString());
                lblBranchAliasName.ForeColor = System.Drawing.Color.DodgerBlue;
                lblBranchAliasName.Text = "Branch Name" + "&nbsp;" + "-" + "&nbsp;" + Curr_BranchFullName + "&nbsp; ";

                cd_BranchAlias = Session["Branch_HQ"].ToString();
                Datapath = Session["DataPath"].ToString();
                PortalTypeId = Session["PortalTypeId"].ToString();

                DT = ObjUserBL.GetPageRightsDetailsPid(Session["UserId"].ToString(), PID);
                if (DT.Rows.Count > 0)
                {
                    Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                    Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                    Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                    Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                    Session["Page_Acess"] = Page_Acess;
                    Session["Read_Acess"] = Read_Acess;
                    Session["Write_Acess"] = Write_Acess;
                    Session["Isdowld_Acess"] = Isdowld_Acess;
                }
                if ((Session["L_CondimentGroup"].ToString() == "N") || (Session["Page_Acess"].ToString() == "N"))
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {
                    // lblMsgTotalSelection.Text = "Item Sale  Between" + "&nbsp;&nbsp;&nbsp;&nbsp;" + txtStartDt.Text + "&nbsp;&nbsp;&nbsp;  and  &nbsp;&nbsp;&nbsp;" + txtEndDt.Text;
                    if (!IsPostBack)
                    {
                        if (Session["Isdowld_Acess"].ToString() == "N")
                        {
                            // btnExcel.Enabled = false;
                            Response.Write(@"<script language='javascript'>alert('No Access To download.')</script>");
                        }
                        else
                        {
                            btnGetReports.Visible = true;

                            if (cmbBranches.Visible == false)
                            {
                                //  BindGridview(Session["Branch_HQ"].ToString(), txtStartDt.Text, txtEndDt.Text);
                            }

                        }


                    }

                }

            }
            else
            {

            }

            if (!IsPostBack)
            {
                BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                GrdLinkView.DataSource = null;
                GrdLinkView.DataBind();
                lblHeader.Text = "";

                //Reset all fields

                ModifierList(Cmb_BranchName);
                ResetFields();
            }



        }


        public void ActiveCall()
        {
            string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
            ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
            if (ActiveCallvalue == "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].ToString());
            }


            ISHQ = ObjMain.ExecuteScalar("select BranchName from " + ActiveCallvalue + ".branches where Branchname='" + ActiveCallvalue + "' and branchID=1;");  //now 

            if (ISHQ != "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].ToString());
                UPDATEBRANCH(ActiveCallvalue);
            }

            cd_BranchAlias = ActiveCallvalue;
            Curr_BranchFullName = ObjUserBL.GetBranchCurr_BranchVal(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), cd_BranchAlias);
            Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Curr_BranchFullName);  // This will Return BranchName( Actual Database Name )
            cmbBranches.Items.FindByText(Curr_BranchFullName).Selected = true;
            Branchidval = ObjUserBL.GetBranchIDbyName(ActiveCallvalue, ActiveCallvalue);
            if (Branchidval != "")
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Branchidval);
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Session["Fistbranch"].ToString());

            }

            if (DT.Rows.Count > 0)
            {
                Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                Session["Page_Acess"] = Page_Acess;
                Session["Read_Acess"] = Read_Acess;
                Session["Write_Acess"] = Write_Acess;
                Session["Isdowld_Acess"] = Isdowld_Acess;
            }
            Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + cmbBranches.SelectedItem.Value + " and cd_branchalias='" + ActiveCallvalue + "';");
            RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
            DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);
            if (DtLicense.Rows.Count > 0)
            {


                L_CondimentGroup = DtLicense.Rows[0]["CONDIMENTGROUP"].ToString();
                Session["L_CondimentGroup"] = L_CondimentGroup;
            }
            if (Session["L_CondimentGroup"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
            {
                Response.Redirect("DeniedPage.aspx");
            }
            else
            {

                //btnExcel.Enabled = true;
                //lblMsgTotalSelection.Text = "Total Sale Selection Between" + "&nbsp;&nbsp;&nbsp;" + txtStartDt.Text + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + txtEndDt.Text;
                cd_BranchAlias = cmbBranches.SelectedItem.Text;
                // BindGridview(ActiveCallvalue, txtStartDt.Text, txtEndDt.Text);
                System.Threading.Thread.Sleep(2000);

            }

        }


        public void LoadBranchName()
        {
            DtNormalUser = ObjUserBL.GetBranchOnLoginValues(Session["Branch_HQ"].ToString(), Session["UserId"].ToString());
            if (DtNormalUser.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtNormalUser;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }
        }
        public void LoadBranchName4SuperAdmin()
        {
            DtSuperAdmin = ObjUserBL.GetBranchOnSuperAdmin(Session["Branch_HQ"].ToString(), Session["UserId"].ToString());
            if (DtSuperAdmin.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtSuperAdmin;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }
        }
        public void UPDATEBRANCH(string ActiveCallvalue)
        {
            if (!string.IsNullOrEmpty(Session["UserId"] as string))
            {
                Useridvalue = Session["UserId"].ToString();
                QueryCurr_Select = "Update portaldb.usermaster set ActiveValue='" + ActiveCallvalue + "' where Userid=" + Useridvalue + "";
                S = ObjMain.ExecuteNonQuery(QueryCurr_Select);
            }
            else
            {
                Response.Redirect("Login.aspx");

            }

        }


        protected void btnGetReports_Click(object sender, EventArgs e)
        {
        }

        protected void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBranches.SelectedIndex != 0)
            {

                BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                GrdLinkView.DataSource = null;
                GrdLinkView.DataBind();
                lblHeader.Text = "";

                //Reset all fields

                ModifierList(Cmb_BranchName);
                ResetFields();

            }
        }

        private void ResetFields()
        {
            //Reset all fields
            lblModifierControlID.Text = "";

            cmbStep1.SelectedValue = "0";
            cmbLevel1.SelectedValue = "0";
            cmbLevel2.SelectedValue = "0";
            cmbLevel3.SelectedValue = "0";
            cmbLevel4.SelectedValue = "0";
            cmbLevel5.SelectedValue = "0";

            lblStep1Desc.Text = "";
            lblLevel1Desc.Text = "";
            lblLevel2Desc.Text = "";
            lblLevel3Desc.Text = "";
            lblLevel4Desc.Text = "";
            lblLevel5Desc.Text = "";

            txtMin1.Text = "0";
            txtMin2.Text = "0";
            txtMin3.Text = "0";
            txtMin4.Text = "0";
            txtMin5.Text = "0";

            txtMax1.Text = "0";
            txtMax2.Text = "0";
            txtMax3.Text = "0";
            txtMax4.Text = "0";
            txtMax5.Text = "0";
        }


        protected void btnStep_Click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            if (cmbStep1.SelectedValue == "1")
            {
                Step1ViewList(Cmb_BranchName);
                lblHeader.Text = "Step1 " + cmbStep1.SelectedItem.Text;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Salection for Type')", true);
            }

        }

        protected void btnLevel1_click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            if (cmbLevel1.SelectedValue == "8")
            {
                LevelsViewList(Cmb_BranchName);
                lblHeader.Text = "Level 1 " + cmbLevel1.SelectedItem.Text;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Salection for Type')", true);
            }

        }

        protected void btnLevel2_click(object sender, EventArgs e)
        {

            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            if (cmbLevel2.SelectedValue == "8")
            {
                LevelsViewList(Cmb_BranchName);
                lblHeader.Text = "Level 2 " + cmbLevel2.SelectedItem.Text;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Salection for Type')", true);
            }

        }

        protected void btnLevel3_click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            if (cmbLevel3.SelectedValue == "8")
            {
                LevelsViewList(Cmb_BranchName);
                lblHeader.Text = "Level 3 " + cmbLevel3.SelectedItem.Text;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Salection for Type')", true);
            }

        }

        protected void btnLevel4_click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            if (cmbLevel4.SelectedValue == "8")
            {
                LevelsViewList(Cmb_BranchName);
                lblHeader.Text = "Level 4 " + cmbLevel4.SelectedItem.Text;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Salection for Type')", true);
            }

        }

        protected void btnLevel5_click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            if (cmbLevel5.SelectedValue == "8")
            {
                LevelsViewList(Cmb_BranchName);
                lblHeader.Text = "Level 5 " + cmbLevel5.SelectedItem.Text;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Salection for Type')", true);
            }

        }

        protected void btnSaveModifier_click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            if (cmbStep1.SelectedValue != "0")
            {
                if (lblModifierControlID.Text == "")
                {
                    SaveModifier(Cmb_BranchName);
                }
                else
                {
                    UpdateModifier(Cmb_BranchName, int.Parse(lblModifierControlID.Text.Trim()));
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Salection for Type')", true);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ControlID"), new DataColumn("ControlType") });
            foreach (GridViewRow row in grdModList.Rows)
            {

                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkSelectMod") as CheckBox);
                    if (chkRow.Checked)
                    {

                        string sRow1 = (row.Cells[1].FindControl("lblControlID") as Label).Text;
                        if (GetControlIDDetails(Cmb_BranchName, int.Parse(sRow1)) == "true")
                        {
                            chkRow.Checked = false;
                        }
                    }
                }
            }
        }

        protected void cmbStep1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            lblHeader.Text = "";
        }

        protected void cmbLevel1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            lblHeader.Text = "";
        }

        protected void cmbLevel2_SelectedIndexChanged(object sender, EventArgs e)
        {
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            lblHeader.Text = "";
        }

        protected void cmbLevel3_SelectedIndexChanged(object sender, EventArgs e)
        {
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            lblHeader.Text = "";
        }

        protected void cmbLevel4_SelectedIndexChanged(object sender, EventArgs e)
        {
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            lblHeader.Text = "";
        }

        protected void cmbLevel5_SelectedIndexChanged(object sender, EventArgs e)
        {
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            lblHeader.Text = "";
        }

        protected void chkSelect_CheckedChanged(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("row1"), new DataColumn("row2") });
            foreach (GridViewRow row in GrdLinkView.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkSelect") as CheckBox);
                    if (chkRow.Checked)
                    {
                        string sRow1 = (row.Cells[1].FindControl("lblID") as Label).Text;
                        string sRow2 = (row.Cells[2].FindControl("lblName") as Label).Text;

                        //txtLGCondimentID.Text = sRow1;
                        //txtLGCondimentName.Text = sRow2;


                        if (lblHeader.Text == "Step1 Inventory")
                        {
                            if (ValidateItem(Cmb_BranchName, sRow1) == "false")
                            {
                                lblStep1Barcode.Text = sRow1;
                                lblStep1Desc.Text = sRow2;
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Error_Msg", "alert('Item already linked ')", true);
                                chkRow.Checked = false;
                            }
                        }

                        if (lblHeader.Text == "Level 1 Condiment Group")
                        {
                            lblLevel1ID.Text = sRow1;
                            lblLevel1Desc.Text = sRow2;
                        }

                        if (lblHeader.Text == "Level 2 Condiment Group")
                        {
                            lblLevel2ID.Text = sRow1;
                            lblLevel2Desc.Text = sRow2;
                        }


                        if (lblHeader.Text == "Level 3 Condiment Group")
                        {
                            lblLevel3ID.Text = sRow1;
                            lblLevel3Desc.Text = sRow2;
                        }


                        if (lblHeader.Text == "Level 4 Condiment Group")
                        {
                            lblLevel4ID.Text = sRow1;
                            lblLevel4Desc.Text = sRow2;
                        }


                        if (lblHeader.Text == "Level 5 Condiment Group")
                        {
                            lblLevel5ID.Text = sRow1;
                            lblLevel5Desc.Text = sRow2;
                        }



                    }
                }
            }
        }

        protected void chkSelectMod_CheckedChanged(object sender, EventArgs e)
        {
            //BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            //Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            //DataTable dt = new DataTable();
            //dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ControlID"), new DataColumn("ControlType") });
            //foreach (GridViewRow row in GrdLinkView.Rows)
            //{

            //    if (row.RowType == DataControlRowType.DataRow)
            //    {
            //        CheckBox chkRow = (row.Cells[0].FindControl("chkSelect") as CheckBox);
            //        if (chkRow.Checked)
            //        {

            //            string sRow1 = (row.Cells[1].FindControl("ControlID") as Label).Text;
            //            if (GetControlIDDetails(Cmb_BranchName, int.Parse(sRow1)) == "true")
            //            {
            //            }
            //        }
            //    }
            //}
        }

        private string GetControlIDDetails(string sBD, int iControlCode)
        {
            string sGetControlIDDetails = string.Empty;
            sGetControlIDDetails = "false";
            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);

            Cn.Open();
            MySqlCommand sCmd = new MySqlCommand("Select  " +
                                " ControlID, ControlType, ControlCode, ControlName,  " +
                                " ProcessType1, ProcessCode1, ProcessName1, ProcessMin1, ProcessMax1, ProcessOption1, ProcessUpgrade1,  " +
                                " ProcessType2, ProcessCode2, ProcessName2, ProcessMin2, ProcessMax2, ProcessOption2, ProcessUpgrade2, " +
                                " ProcessType3, ProcessCode3, ProcessName3, ProcessMin3, ProcessMax3, ProcessOption3, ProcessUpgrade3,  " +
                                " ProcessType4, ProcessCode4, ProcessName4, ProcessMin4, ProcessMax4, ProcessOption4, ProcessUpgrade4,  " +
                                " ProcessType5, ProcessCode5, ProcessName5, ProcessMin5, ProcessMax5, ProcessOption5, ProcessUpgrade5" +
                                " from da5.modifiercontrol;" +
                                " ", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(sCmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            if (Dt.Rows.Count > 0)
            {
                lblModifierControlID.Text = Dt.Rows[0]["ControlID"].ToString();

                cmbStep1.Text = Dt.Rows[0]["ControlType"].ToString();
                lblStep1Barcode.Text = Dt.Rows[0]["ControlCode"].ToString();
                lblStep1Desc.Text = Dt.Rows[0]["ControlName"].ToString();

                cmbLevel1.Text = Dt.Rows[0]["ProcessType1"].ToString();
                lblLevel1ID.Text = Dt.Rows[0]["ProcessCode1"].ToString();
                lblLevel1Desc.Text = Dt.Rows[0]["ProcessName1"].ToString();
                txtMin1.Text = Dt.Rows[0]["ProcessMin1"].ToString();
                txtMax1.Text = Dt.Rows[0]["ProcessMax1"].ToString();


                cmbLevel2.Text = Dt.Rows[0]["ProcessType2"].ToString();
                lblLevel2ID.Text = Dt.Rows[0]["ProcessCode2"].ToString();
                lblLevel2Desc.Text = Dt.Rows[0]["ProcessName2"].ToString();
                txtMin2.Text = Dt.Rows[0]["ProcessMin2"].ToString();
                txtMax2.Text = Dt.Rows[0]["ProcessMax2"].ToString();

                cmbLevel3.Text = Dt.Rows[0]["ProcessType3"].ToString();
                lblLevel3ID.Text = Dt.Rows[0]["ProcessCode3"].ToString();
                lblLevel3Desc.Text = Dt.Rows[0]["ProcessName3"].ToString();
                txtMin3.Text = Dt.Rows[0]["ProcessMin3"].ToString();
                txtMax3.Text = Dt.Rows[0]["ProcessMax3"].ToString();

                cmbLevel4.Text = Dt.Rows[0]["ProcessType4"].ToString();
                lblLevel4ID.Text = Dt.Rows[0]["ProcessCode4"].ToString();
                lblLevel4Desc.Text = Dt.Rows[0]["ProcessName4"].ToString();
                txtMin4.Text = Dt.Rows[0]["ProcessMin4"].ToString();
                txtMax4.Text = Dt.Rows[0]["ProcessMax4"].ToString();

                cmbLevel5.Text = Dt.Rows[0]["ProcessType5"].ToString();
                lblLevel5ID.Text = Dt.Rows[0]["ProcessCode5"].ToString();
                lblLevel5Desc.Text = Dt.Rows[0]["ProcessName5"].ToString();
                txtMin5.Text = Dt.Rows[0]["ProcessMin5"].ToString();
                txtMax5.Text = Dt.Rows[0]["ProcessMax5"].ToString();


                sGetControlIDDetails = "true";
                Cn.Close();
            }
            else
            {
                sGetControlIDDetails = "false";
                Cn.Close();
            }

            return sGetControlIDDetails;

        }

        private void Step1ViewList(string sDB)
        {

            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            Cn.Open();
            MySqlCommand sCmd = new MySqlCommand("Select Barcode as row1,Inventoryname as row2 from " + sDB + ".Inventory where barcode <> 'Round'", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(sCmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            GrdLinkView.DataSource = Dt;
            GrdLinkView.DataBind();
        }


        private void LevelsViewList(string sDB)
        {
            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            GrdLinkView.DataSource = null;
            GrdLinkView.DataBind();
            Cn.Open();
            MySqlCommand sCmd = new MySqlCommand("Select CondimentGroupID as row1,CondimentGroupName as row2 from " + sDB + ".Condimentgroup", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(sCmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            GrdLinkView.DataSource = Dt;
            GrdLinkView.DataBind();
        }

        private void ModifierList(string sDB)
        {

            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            grdModList.DataSource = null;
            grdModList.DataBind();
            Cn.Open();
            MySqlCommand sCmd = new MySqlCommand("Select  " +
                                " ControlID, ControlType, ControlCode, ControlName,  " +
                                " ProcessType1, ProcessCode1, ProcessName1, ProcessMin1, ProcessMax1, ProcessOption1, ProcessUpgrade1,  " +
                                " ProcessType2, ProcessCode2, ProcessName2, ProcessMin2, ProcessMax2, ProcessOption2, ProcessUpgrade2, " +
                                " ProcessType3, ProcessCode3, ProcessName3, ProcessMin3, ProcessMax3, ProcessOption3, ProcessUpgrade3,  " +
                                " ProcessType4, ProcessCode4, ProcessName4, ProcessMin4, ProcessMax4, ProcessOption4, ProcessUpgrade4,  " +
                                " ProcessType5, ProcessCode5, ProcessName5, ProcessMin5, ProcessMax5, ProcessOption5, ProcessUpgrade5" +
                                " from " + sDB + ".modifiercontrol;" +
                                " ", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(sCmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            grdModList.DataSource = Dt;
            grdModList.DataBind();
        }

        private string ValidateItem(string sDB, string sControlCode)
        {
            string sValidateItem = string.Empty;
            string sCmd = "Select *from " + sDB + ".modifiercontrol where ControlCode = '" + sControlCode + "';";
            sValidateItem = ObjMain.ExecuteScalar(sCmd);
            if (sValidateItem == "")
            {
                sValidateItem = "false";
            }
            else
            {
                sValidateItem = "true";
            }
            return sValidateItem;
        }



        private void SaveModifier(string sDB)
        {
            int iContorID = int.Parse(ObjSel.GenerateModifierID(sDB));
            string sCmd = "Insert Into " + sDB + ".ModifierControl(ControlID, ControlType, ControlCode, ControlName, " +
                        " ProcessType1, ProcessCode1, ProcessName1, ProcessMin1, ProcessMax1, ProcessOption1, ProcessUpgrade1, " +
                        " ProcessType2, ProcessCode2, ProcessName2, ProcessMin2, ProcessMax2, ProcessOption2, ProcessUpgrade2, " +
                        " ProcessType3, ProcessCode3, ProcessName3, ProcessMin3, ProcessMax3, ProcessOption3, ProcessUpgrade3, " +
                        " ProcessType4, ProcessCode4, ProcessName4, ProcessMin4, ProcessMax4, ProcessOption4, ProcessUpgrade4, " +
                        " ProcessType5, ProcessCode5, ProcessName5, ProcessMin5, ProcessMax5, ProcessOption5, ProcessUpgrade5) " +
                //, " +
                //" ProcessType6, ProcessCode6, ProcessName6, ProcessMin6, ProcessMax6, ProcessOption6, ProcessUpgrade6, " +
                //" ProcessType7, ProcessCode7, ProcessName7, ProcessMin7, ProcessMax7, ProcessOption7, ProcessUpgrade7, " +
                //" ProcessType8, ProcessCode8, ProcessName8, ProcessMin8, ProcessMax8, ProcessOption8, ProcessUpgrade8, " +
                //" ProcessType9, ProcessCode9, ProcessName9, ProcessMin9, ProcessMax9, ProcessOption9, ProcessUpgrade9, " +
                //" ProcessType10, ProcessCode10, ProcessName10, ProcessMin10, ProcessMax10, ProcessOption10, ProcessUpgrade10, LinkID) " +
                        " values " +
                        "(" + iContorID + ",'" + cmbStep1.SelectedValue + "','" + lblStep1Barcode.Text.Trim() + "','" + lblStep1Desc.Text.Trim() + "'," +
                        "'" + cmbLevel1.SelectedValue + "','" + lblLevel1ID.Text.Trim() + "','" + lblLevel1Desc.Text.Trim() + "','" + txtMin1.Text.Trim() + "','" + txtMax1.Text.Trim() + "','0','0'," +
                        "'" + cmbLevel2.SelectedValue + "','" + lblLevel2ID.Text.Trim() + "','" + lblLevel2Desc.Text.Trim() + "','" + txtMin2.Text.Trim() + "','" + txtMax2.Text.Trim() + "','0','0'," +
                        "'" + cmbLevel3.SelectedValue + "','" + lblLevel3ID.Text.Trim() + "','" + lblLevel3Desc.Text.Trim() + "','" + txtMin3.Text.Trim() + "','" + txtMax3.Text.Trim() + "','0','0'," +
                        "'" + cmbLevel4.SelectedValue + "','" + lblLevel4ID.Text.Trim() + "','" + lblLevel4Desc.Text.Trim() + "','" + txtMin4.Text.Trim() + "','" + txtMax4.Text.Trim() + "','0','0'," +
                        "'" + cmbLevel5.SelectedValue + "','" + lblLevel5ID.Text.Trim() + "','" + lblLevel5Desc.Text.Trim() + "','" + txtMin5.Text.Trim() + "','" + txtMax5.Text.Trim() + "','0','0'" +
                        ")";
            iAddModifier = ObjCloudMain.ExecuteNonQuery(sCmd);
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "success_msg", "alert(' Modifer Successfully Save ')", true);
            ResetFields();
        }

        private void UpdateModifier(string sDB, int lblModifierControlID)
        {
            string sCmd = "Update Modifier set" +
                        " ControlType = '" + cmbStep1.SelectedValue + "',ControlCode = '" + lblStep1Barcode.Text.Trim() + "',ControlName = '" + lblStep1Desc.Text.Trim() + "' , " +
                        " ProcessType1 = '" + cmbLevel1.SelectedValue + "',ProcessCode1 = '" + lblLevel1ID.Text.Trim() + "',ProcessName1 = '" + lblLevel2Desc.Text.Trim() + "',ProcessMin1 = '" + txtMin1.Text.Trim() + "',ProcessMax1= '" + txtMax1.Text.Trim() + "'," +
                        " ProcessType2 = '" + cmbLevel2.SelectedValue + "',ProcessCode2 = '" + lblLevel2ID.Text.Trim() + "',ProcessName2 = '" + lblLevel2Desc.Text.Trim() + "',ProcessMin2 = '" + txtMin2.Text.Trim() + "',ProcessMax2= '" + txtMax2.Text.Trim() + "'," +
                        " ProcessType3 = '" + cmbLevel3.SelectedValue + "',ProcessCode3 = '" + lblLevel3ID.Text.Trim() + "',ProcessName3 = '" + lblLevel3Desc.Text.Trim() + "',ProcessMin3 = '" + txtMax3.Text.Trim() + "',ProcessMax3 = '" + txtMax3.Text.Trim() + "'," +
                        " ProcessType3 = '" + cmbLevel4.SelectedValue + "',ProcessCode3 = '" + lblLevel4ID.Text.Trim() + "',ProcessName3 = '" + lblLevel4Desc.Text.Trim() + "',ProcessMin3 = '" + txtMax4.Text.Trim() + "',ProcessMax3 = '" + txtMax4.Text.Trim() + "'," +
                        " ProcessType3 = '" + cmbLevel5.SelectedValue + "',ProcessCode3 = '" + lblLevel5ID.Text.Trim() + "',ProcessName3 = '" + lblLevel5Desc.Text.Trim() + "',ProcessMin3 = '" + txtMax5.Text.Trim() + "',ProcessMax3 = '" + txtMax5.Text.Trim() + "'" +
                        " where ControlID = '" + lblModifierControlID + "'";
            iUpdateModifier = ObjCloudMain.ExecuteNonQuery(sCmd);
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "success_msg", "alert(' Modifer Successfully Update ')", true);
            ResetFields();
        }

    }
}
