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 Condiment : 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_CondimentAdd = string.Empty;

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

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

        Int32 iAddCondiment = 0;
        Int32 iUpdateCondiment = 0;
        Int32 iAddCondimentGroup = 0;
        Int32 iUpdateCondimentGroup = 0;
        Int32 iAddCondimentGroupDetail = 0;
        Int32 iDeleteCondimentGroupID = 0;
        Int32 iUpdateCondimehtGroupDetailbyCondiment = 0;
        string sCheckCondimentIFExist = string.Empty;

        string Vw_Chk = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

            PID = "29";    // This is for Condiment 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_CondimentAdd"].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);
                ShowCondiments(Cmb_BranchName); //Show Condiment List
                ShowCondimentGroups(Cmb_BranchName); //Show Condiment Group
                ShowCondimentGroupLinked(Cmb_BranchName);
                LoadGroupCondiment(Cmb_BranchName);
            }



        }


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


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

            if (ISHQ != "")
            {
                ActiveCallvalue = ObjSel.GETBrachAlias8(Session["CM_ID"].ToString(), Session["Branch_HQ"].ToString(), Session["UserId"].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_CondimentAdd = DtLicense.Rows[0]["CONDIMENT"].ToString();
                Session["L_CondimentAdd"] = L_CondimentAdd;
            }
            if (Session["L_CondimentAdd"].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.GetBranchOnSuperAdmin8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].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);
                ShowCondiments(Cmb_BranchName); //Show Condiment List
                ShowCondimentGroups(Cmb_BranchName); //Show Condiment Group
                ShowCondimentGroupLinked(Cmb_BranchName);
            }
            UPDATEBRANCH(Cmb_BranchName);
        }

        protected void btnViewLinked_Click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            if (txtLGCondimentID.Text != "")
            {
                ShowLinkedCondimentbyGCID(Cmb_BranchName, int.Parse(txtLGCondimentID.Text.Trim()));
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Condiment Group ID ')", true);
            }
            aCondiment.Attributes.Add("class", "nav-link");
            hCondiment.Attributes.Add("class", "tab-pane fade");
            aCondimentLinked.Attributes.Add("class", "nav-link");
            hCondimentLinked.Attributes.Add("class", "tab-pane fade");
            aCondimentLinked.Attributes.Add("Class", "nav-link active border-left-0");
            hCondimentLinked.Attributes.Add("class", "tab-pane fade show active");

            //aMisc.Attributes.Add("class", "nav-link");
            //Misc.Attributes.Add("class", "tab-pane fade");
            //aBasic.Attributes.Add("class", "nav-link active border-left-0");
            //home.Attributes.Add("class", "tab-pane fade show active");
        }

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

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ID"), new DataColumn("GCondimentname") });
            foreach (GridViewRow row in grdGCLinked.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkSelect") as CheckBox);
                    if (chkRow.Checked)
                    {
                        sRow1 = (row.Cells[1].FindControl("lblGCondimentID") as Label).Text;
                        sRow2 = (row.Cells[2].FindControl("lblGCondimentName") as Label).Text;



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

                    }
                    else
                    {
                        //txtID.Text = "";
                        //txtName.Text = "";
                    }

                }
            }

            grdLinked.DataSource = null;
            grdLinked.DataBind();
            aCondiment.Attributes.Add("class", "nav-link");
            hCondiment.Attributes.Add("class", "tab-pane fade");
            aCondimentGroupID.Attributes.Add("class", "nav-link");
            hCondimentGroup.Attributes.Add("class", "tab-pane fade");
            aCondimentLinked.Attributes.Add("Class", "nav-link active border-left-0");
            hCondimentLinked.Attributes.Add("class", "tab-pane fade show active");
            ShowLinkedCondimentbyGCID(Cmb_BranchName, int.Parse(txtLGCondimentID.Text));
        }

        protected void chkSelectCondiment_Changed(object sender, EventArgs e)
        {
            //string sRow1;
            //string sRow2;

            //CheckBox chkRow = (grdLinked.SelectedRow.Cells[0].FindControl("chkSelectCondiment") as CheckBox);
            //if (chkRow.Checked)
            //{
            //    sRow1 = (grdLinked.SelectedRow.Cells[3].FindControl("lblGCondimentGroupName") as Label).Text;
            //    if (sRow1 != "")
            //    {
            //        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "warning_msg", "alert(' Condiment Already Under GroupCondiment ')", true);
            //         chkRow.Checked = false;
            //    }
            //}

        }


        private void ShowCondiments(string sDB)
        {


            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            grdCondimentList.DataSource = null;
            Cn.Open();
            //MySqlCommand cmd = new MySqlCommand("Select condimentid,condimentname,condimentdesc from " + sDB + ".condiment;", Cn);
            MySqlCommand cmd = new MySqlCommand("Select A.condimentid,A.condimentname,condimentdesc,C.CondimentGroupID,CondimentGroupName from " + sDB + ".condiment A " +
                                                " join " + sDB + ".CondimentGroupDetails B on A.CondimentID = B.CondimentID " +
                                                " join " + sDB + ".CondimentGroup C on B.CondimentGroupID = C.CondimentGroupID;", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            grdCondimentList.DataSource = Dt;
            grdCondimentList.DataBind();

        }

        private void ShowCondimentGroups(string sDB)
        {
            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            grdCondimentGroupList.DataSource = null;
            Cn.Open();
            MySqlCommand cmd = new MySqlCommand("Select CondimentGroupID,condimentGroupName from " + sDB + ".CondimentGroup ", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            grdCondimentGroupList.DataSource = Dt;
            grdCondimentGroupList.DataBind();
        }


        private void ShowCondimentGroupLinked(string sDB)
        {
            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            grdGCLinked.DataSource = null;
            Cn.Open();
            MySqlCommand cmd = new MySqlCommand("Select CondimentGroupID,condimentGroupName from " + sDB + ".CondimentGroup ", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            grdGCLinked.DataSource = Dt;
            grdGCLinked.DataBind();
        }

        private string CheckCondimentofExist(string sDB, int iGroupID, int iCondimentID)
        {
            string CheckCondimentofExist = string.Empty;
            string sCmd = "Select *from " + sDB + ".CondimentGroupDetails where condimentgroupid = " + iGroupID + " and condimentid = " + iCondimentID + " ;";
            sCheckCondimentIFExist = ObjMain.ExecuteScalar(sCmd);
            if (sCheckCondimentIFExist == "")
            {
                CheckCondimentofExist = "False";
            }
            else
            {
                CheckCondimentofExist = "True";
            }
            return CheckCondimentofExist;


        }


        private void ShowLinkedCondimentbyGCID(string sDB, int iCondimentGroupID)
        {

            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            grdLinked.DataSource = null;
            Cn.Open();
            //MySqlCommand cmd = new MySqlCommand("Select B.CondimentID,B.CondimentName from " + sDB + ".condimentgroupdetails A " +
            //                    " join " + sDB + ".condiment B on A.CondimentID = B.CondimentID  " +
            //                    " where A.condimentgroupid = '" + iCondimentGroupID + "';", Cn);
            //MySqlCommand cmd = new MySqlCommand("Select 'True' as selected,a.CondimentID,a.CondimentName  " +
            //                                    " From " + sDB + ".Condiment A Left Join " + sDB + ".CondimentGroupDetails B On A.CondimentID=B.CondimentID " +
            //                                    " Where B.CondimentGroupID = " + iCondimentGroupID + " " +
            //                                    " Union All " +
            //                                    " Select 'False' as selected,A.CondimentID,A.CondimentName " +
            //                                    " From " + sDB + ".Condiment a Where Not Exists (select a.CondimentID From " + sDB + ".CondimentGroupDetails B " +
            //                                    " Where B.Condimentid=A.condimentID and B.CondimentGroupID= " + iCondimentGroupID + ")", Cn);

            MySqlCommand cmd = new MySqlCommand("Select 'True' as selected,a.CondimentID,a.CondimentName,CondimentGroupname  " +
                                    " From " + sDB + ".Condiment A Left Join " + sDB + ".CondimentGroupDetails B On A.CondimentID=B.CondimentID " +
                                    " left join " + sDB + ".CondimentGroup C on B.CondimentGroupID = C.CondimentGroupID " +
                                    " Where B.CondimentGroupID = " + iCondimentGroupID + " " +
                                    " Union All  " +
                                    " Select 'False' as selected,A.CondimentID,A.CondimentName, CondimentGroupname " +
                                    " From " + sDB + ".Condiment a  " +
                                    " left join " + sDB + ".CondimentGroupDetails b on a.CondimentID = b.CondimentID " +
                                    " left join " + sDB + ".CondimentGroup C on B.CondimentGroupID = C.CondimentGroupID " +
                                    " Where Not Exists (select a.CondimentID From " + sDB + ".CondimentGroupDetails B " +
                                    " Where B.Condimentid=A.condimentID and B.CondimentGroupID= " + iCondimentGroupID + ") order by condimentid;", Cn);

            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            grdLinked.DataSource = Dt;
            grdLinked.DataBind();

        }


        private void AddCondiment(string sDB)
        {
            string sCmd = string.Empty;
            int iCondimentid = int.Parse(ObjSel.GenerateCondimentID(sDB));
            sCmd = "Insert into " + sDB + ".Condiment(condimentid,condimentname,condimentdesc)values('" + iCondimentid + "','" + txtCondimentname.Text.Trim() + "','" + txtCondimentname.Text.Trim() + "');";
            iAddCondiment = ObjCloudMain.ExecuteNonQuery(sCmd);
            //cmbBranches.SelectedItem.Text
            if (dpGroupCondiment.SelectedItem.Text != "Select CondimentGroup") //john added on 19-May-2021
            {
                AddCondimentGroupDetails(sDB, int.Parse(ObjSel.GetCondimetGroupIDbyName(sDB, dpGroupCondiment.SelectedItem.Text)), iCondimentid); //john added on 19-May-2021 Insert CondimentGroupDetail
            }
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "success_msg", "alert(' Condiment Successfully Added ')", true);
            txtCondimentname.Text = "";
            txtDescription.Text = "";
            dpGroupCondiment.SelectedIndex = 0;
            ShowCondiments(sDB);

        }

        private void UpdateCondiment(string sDB)
        {
            string sCmd = string.Empty;
            sCmd = "Update " + sDB + ".Condiment set condimentname='" + txtCondimentname.Text.Trim() + "',condimentdesc = '" + txtDescription.Text.Trim() + "' where CondimentID = '" + txtCondimentID.Text.Trim() + "'";
            iUpdateCondiment = ObjCloudMain.ExecuteNonQuery(sCmd);
            if (dpGroupCondiment.SelectedItem.Text != "Select CondimentGroup") //john added on 19-May-2021
            {
                UpdateCondimentGroupDetailbyCondiment(sDB, int.Parse(ObjSel.GetCondimetGroupIDbyName(sDB, dpGroupCondiment.SelectedItem.Text)), int.Parse(txtCondimentID.Text.Trim()));
                //AddCondimentGroupDetails(sDB, int.Parse(ObjSel.GetCondimetGroupIDbyName(sDB, dpGroupCondiment.SelectedItem.Text)), iCondimentid); //john added on 19-May-2021 Insert CondimentGroupDetail
            }
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "success_msg", "alert(' Condiment Successfully Update ')", true);
            txtCondimentname.Text = "";
            txtDescription.Text = "";
            dpGroupCondiment.SelectedIndex = 0;
            ShowCondiments(sDB);


        }

        private void AddCondimentGroup(string sDB)
        {
            string sCmd = string.Empty;
            int iCondimentGroupID = int.Parse(ObjSel.GenerateCondimentGroupID(sDB));
            sCmd = "Insert into " + sDB + ".condimentgroup(condimentgroupid,condimentgroupname)values('" + iCondimentGroupID + "','" + txtGCondimentName.Text.Trim() + "')";
            iAddCondimentGroup = ObjCloudMain.ExecuteNonQuery(sCmd);
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "success_msg", "alert(' Condiment Group Successfully Added ')", true);
            txtCondimentname.Text = "";
            ShowCondimentGroups(sDB);
        }

        private void UpdateCondimentGroup(string sDB, int iGroupID)
        {
            string sCmd = string.Empty;
            sCmd = "Update " + sDB + ".CondimentGroup set Condimentgroupname  = '" + txtGCondimentName.Text.Trim() + "' where condimentgroupid='" + iGroupID + "'";
            iUpdateCondimentGroup = ObjCloudMain.ExecuteNonQuery(sCmd);
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "success_msg", "alert(' Condiment Group Successfully Update ')", true);
            txtGCondimentName.Text = "";
            ShowCondimentGroups(sDB);
        }

        private void AddCondimentGroupDetails(string sDB, int iCondimentGroupID, int iCondimentID)
        {
            string sCmd = "Insert Into " + sDB + ".CondimentGroupDetails(CondimentGroupID,CondimentID)values('" + iCondimentGroupID + "','" + iCondimentID + "')";
            iAddCondimentGroupDetail = ObjMain.ExecuteNonQuery(sCmd);
            if (txtCondimentID.Text != "")
            {
                ShowLinkedCondimentbyGCID(sDB, int.Parse(txtLGCondimentID.Text));
            }
        }

        public void DeleteCondimentGroupDetails(string sDB, int iCondimentGroupID, int iCondimentID)
        {
            string sCmd = "Delete from  " + sDB + ".CondimentGroupDetails where CondimentGroupID=" + iCondimentGroupID + " and CondimentID = " + iCondimentID + "";
            iDeleteCondimentGroupID = ObjMain.ExecuteNonQuery(sCmd);
            if (txtCondimentID.Text != "")
            {
                ShowLinkedCondimentbyGCID(sDB, int.Parse(txtLGCondimentID.Text));
            }

        }

        public void UpdateCondimentGroupDetailbyCondiment(string sDB, int iCondimentGroupID, int iCondimentID)
        {
            string sCmd = "update " + sDB + ".condimentgroupdetails set CondimentGroupID = '" + iCondimentGroupID + "' where condimentid = '" + iCondimentID + "'";
            iUpdateCondimehtGroupDetailbyCondiment = ObjMain.ExecuteNonQuery(sCmd);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            if (txtCondimentname.Text != "")
            {
                if (txtCondimentID.Text == "")
                {
                    AddCondiment(Cmb_BranchName);
                }
                else
                {
                    UpdateCondiment(Cmb_BranchName);
                }

                aCondiment.Attributes.Add("class", "nav-link active border-left-0");
                hCondiment.Attributes.Add("class", "tab-pane fade show active");
                aCondimentGroupID.Attributes.Add("class", "nav-link");
                hCondimentGroup.Attributes.Add("class", "tab-pane fade");
                aCondimentLinked.Attributes.Add("class", "nav-link");
                hCondimentLinked.Attributes.Add("class", "tab-pane fade");
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Condiment Name ')", true);
            }
        }

        protected void btnSaveGCondiment_Click(object sender, EventArgs e)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            if (txtGCondimentName.Text != "")
            {
                if (txtGCondimentID.Text == "")
                {
                    AddCondimentGroup(Cmb_BranchName);
                    aCondiment.Attributes.Add("class", "nav-link");
                    hCondiment.Attributes.Add("class", "tab-pane fade");
                    aCondimentGroupID.Attributes.Add("class", "nav-link active border-left-0");
                    hCondimentGroup.Attributes.Add("class", "tab-pane fade show active");
                    aCondimentLinked.Attributes.Add("class", "nav-link");
                    hCondimentLinked.Attributes.Add("class", "tab-pane fade");
                }
                else
                {
                    UpdateCondimentGroup(Cmb_BranchName, int.Parse(txtGCondimentID.Text));
                    aCondiment.Attributes.Add("class", "nav-link");
                    hCondiment.Attributes.Add("class", "tab-pane fade");
                    aCondimentGroupID.Attributes.Add("class", "nav-link active border-left-0");
                    hCondimentGroup.Attributes.Add("class", "tab-pane fade show active");
                    aCondimentLinked.Attributes.Add("class", "nav-link");
                    hCondimentLinked.Attributes.Add("class", "tab-pane fade");
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Invalid Condiment Group Name ')", true);
            }
        }

        protected void btnUpdateGCondiment_Click(object sender, EventArgs e)
        {


            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            foreach (GridViewRow row in grdLinked.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkbox = (row.Cells[0].FindControl("chkSelectCondiment") as CheckBox);
                    Label lCondimentID = (row.Cells[1].FindControl("lblGCondimentID") as Label);

                    if (chkbox.Checked)
                    {
                        if (CheckCondimentofExist(Cmb_BranchName, int.Parse(txtGCondimentID.Text), int.Parse(lCondimentID.Text)) == "False")
                        {
                            // AddCondimentGroupDetails(Cmb_BranchName, int.Parse(txtLGCondimentID.Text), int.Parse(lCondimentID.Text));
                            AddCondimentGroupDetails(Cmb_BranchName, int.Parse(txtGCondimentID.Text), int.Parse(lCondimentID.Text));
                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        //DeleteCondimentGroupDetails(Cmb_BranchName, int.Parse(txtLGCondimentID.Text), int.Parse(lCondimentID.Text));
                        DeleteCondimentGroupDetails(Cmb_BranchName, int.Parse(txtGCondimentID.Text), int.Parse(lCondimentID.Text));
                    }
                }
            }
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "success_msg", "alert(' Condiment Group Successfully Updated ')", true);
            //ScriptManager.RegisterStartupScript(this, GetType(), "CallFunction", "CloseModal();", true);
            //Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            aCondiment.Attributes.Add("class", "nav-link");
            hCondiment.Attributes.Add("class", "tab-pane fade");
            aCondimentGroupID.Attributes.Add("Class", "nav-link active border-left-0");
            hCondimentGroup.Attributes.Add("class", "tab-pane fade show active");
            //aCondimentLinked.Attributes.Add("Class", "nav-link active border-left-0");
            //hCondimentLinked.Attributes.Add("class", "tab-pane fade show active");


        }

        protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            string sCondimentID = (grdCondimentList.SelectedRow.FindControl("lblCondimentID") as Label).Text;
            string sCondimentName = (grdCondimentList.SelectedRow.FindControl("lblCondimentName") as Label).Text;
            string sCondimentDesc = (grdCondimentList.SelectedRow.FindControl("lblDescription") as Label).Text;
            string sCondimentGroupID = (grdCondimentList.SelectedRow.FindControl("lblCondimentGroupID") as Label).Text;
            string sCondimentGroupName = (grdCondimentList.SelectedRow.FindControl("lblCondimentGroupname") as Label).Text;

            txtCondimentID.Text = sCondimentID.ToString().Trim();
            txtCondimentname.Text = sCondimentName.ToString().Trim();
            txtDescription.Text = sCondimentDesc.ToString().Trim();
            dpGroupCondiment.SelectedIndex = dpGroupCondiment.Items.IndexOf(dpGroupCondiment.Items.FindByValue(sCondimentGroupID));
            aCondiment.Attributes.Add("class", "nav-link active border-left-0");
            hCondiment.Attributes.Add("class", "tab-pane fade show active");
            aCondimentGroupID.Attributes.Add("class", "nav-link");
            hCondimentGroup.Attributes.Add("class", "tab-pane fade");
            aCondimentLinked.Attributes.Add("class", "nav-link");
            hCondimentLinked.Attributes.Add("class", "tab-pane fade");
        }

        protected void OnSelectedIndexChangedGroup(object sender, EventArgs e)
        {

            //Accessing TemplateField Column controls.
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

            string sCondimentGroupID = (grdCondimentGroupList.SelectedRow.FindControl("lblGCondimentID") as Label).Text;
            string sCondimentGroupName = (grdCondimentGroupList.SelectedRow.FindControl("lblGCondimentName") as Label).Text;


            txtGCondimentID.Text = sCondimentGroupID.ToString().Trim();
            txtGCondimentName.Text = sCondimentGroupName.ToString().Trim();

            aCondiment.Attributes.Add("class", "nav-link");
            hCondiment.Attributes.Add("class", "tab-pane fade");
            aCondimentGroupID.Attributes.Add("class", "nav-link active border-left-0");
            hCondimentGroup.Attributes.Add("class", "tab-pane fade show active");
            aCondimentLinked.Attributes.Add("Class", "nav-link");
            hCondimentLinked.Attributes.Add("class", "tab-pane fade");


            //lblGCondimentCaption.Text = "Group Condiment: " + txtGCondimentName.Text;
            ShowLinkedCondimentbyGCID(Cmb_BranchName, int.Parse(txtGCondimentID.Text.Trim()));
            //DisableRowWithGroupCondiment();
            DisableRowWithGroupCondiment();
            lblCondimentGroupHeader.Text = "Condiment Group : " + txtGCondimentName.Text;
        }

        public void LoadGroupCondiment(string DB)
        {
            BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
            Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
            DataTable DTCondimentGroup = new DataTable();
            DTCondimentGroup = ObjSel.GetCondimentGroup(DB.ToString().Trim());
            if (DTCondimentGroup.Rows.Count > 0)
            {
                dpGroupCondiment.DataSource = DTCondimentGroup;
                dpGroupCondiment.DataTextField = "CondimentGroupName";
                dpGroupCondiment.DataValueField = "CondimentGroupID";
                dpGroupCondiment.DataBind();
                dpGroupCondiment.Items.Insert(0, "Select CondimentGroup");
            }

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

        protected void grdLinked_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    if (e.Row.Cells[2].Text != "")//orderstatus index
            //    {
            //        e.Row.Enabled = false;
            //    }
            //    else
            //    {
            //        e.Row.Enabled = true;

            //    }

            //}
        }

        private void DisableRowWithGroupCondiment()
        {
            for (int i = 0; i < this.grdLinked.Rows.Count; i++)
            {
                CheckBox chkBox = (CheckBox)grdLinked.Rows[i].FindControl("chkSelectCondiment");
                Label lblGCondiment = (Label)grdLinked.Rows[i].FindControl("lblGCondimentGroupName");

                if (lblGCondiment.Text != "")
                {
                    if (chkBox.Checked == false)
                    {
                        grdLinked.Rows[i].Enabled = false;
                        grdLinked.Rows[i].ForeColor = System.Drawing.Color.FromArgb(211, 211, 211);
                    }

                }

            }
        }

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

        private void ShowCondimentsPopUp(string sDB)
        {
            DataTable DTResult = new DataTable();
            string sCondtion = string.Empty;
            string strQuery = string.Empty;

            if (cmbSearch.SelectedValue == "Name")
            {
                if (txtInput.Text != "")
                {
                    sCondtion = " where CondimentName like '" + txtInput.Text + "%'";
                }
            }
            else if (cmbSearch.SelectedValue == "Desc")
            {
                if (txtInput.Text != "")
                {
                    sCondtion = " where CondimentDesc like '" + txtInput.Text + "%'";
                }
            }

            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            grdCondimentList.DataSource = null;
            Cn.Open();
            //MySqlCommand cmd = new MySqlCommand("Select condimentid,condimentname,condimentdesc from " + sDB + ".condiment;", Cn);
            MySqlCommand cmd = new MySqlCommand("Select A.condimentid,A.condimentname,condimentdesc,C.CondimentGroupID,CondimentGroupName from " + sDB + ".condiment A " +
                                                " join " + sDB + ".CondimentGroupDetails B on A.CondimentID = B.CondimentID " +
                                                " join " + sDB + ".CondimentGroup C on B.CondimentGroupID = C.CondimentGroupID " + sCondtion + ";", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            grdCondimentList.DataSource = Dt;
            grdCondimentList.DataBind();

            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ShowModalCondimentList();", true);

        }

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


        private void ShowCondimentGroupsPopup(string sDB)
        {
            string sCondition = string.Empty;

            if (txtInputCondimentGroup.Text != "") {
                sCondition = " Where CondimentGroupName like '" + txtInputCondimentGroup.Text.Trim() + "%'";
            }
            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            grdCondimentGroupList.DataSource = null;
            Cn.Open();
            MySqlCommand cmd = new MySqlCommand("Select CondimentGroupID,condimentGroupName from " + sDB + ".CondimentGroup " + sCondition  + " ", Cn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable Dt = new DataTable();
            da.Fill(Dt);
            Cn.Close();
            grdCondimentGroupList.DataSource = Dt;
            grdCondimentGroupList.DataBind();

            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ShowModalCondimentGroupList();", true);
        }


    }
}