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;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing.Printing;
using System.Drawing;
using System.Drawing.Text;

using Newtonsoft.Json;
using RestSharp;
using System.Runtime.InteropServices.ComTypes;

namespace SUNWEB
{
    public partial class OperatingHours : 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();
        // 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_OperatingHours = 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 BranchAlias = string.Empty;
        string Vw_Chk = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

            PID = "48";    // This is for OperatingHours 


            if (!IsPostBack)
            {
               
            }
            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_OperatingHours"].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
                        {
                            

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

                        }


                    }

                }

            }
            else
            {

            }


        }


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


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

            if (ISHQ != "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].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_OperatingHours = DtLicense.Rows[0]["OPERATINGHOURS"].ToString();
                Session["L_OperatingHours"] = L_OperatingHours;
            }
            if (Session["L_OperatingHours"].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);

                string OperativeHoursType = Get_Generic_Webordingini(Cmb_BranchName, "63");

                if (OperativeHoursType == "0")
                {
                    Type1.Visible = true;
                    Type2.Visible = false;
                    LoadOperatingHours(ActiveCallvalue);
                    
                }
                else if (OperativeHoursType == "1")
                {
                    Type1.Visible = false;
                    Type2.Visible = true;
                    LoadShiftOperativeHours(ActiveCallvalue);
                }

                

            }

        }


        public void LoadBranchName()
        {
            DtNormalUser = ObjUserBL.GetBranchOnLoginValues8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].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 cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(1000);
            if (cmbBranches.SelectedIndex == 0)
            {
                Response.Redirect("PromoCode.aspx");
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Value));
                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;
                }

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

                if (ISHQ != "")
                {
                    ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].ToString());
                    UPDATEBRANCH(ActiveCallvalue);
                }
                else
                {
                    BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);    // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + cmbBranches.SelectedItem.Value + " and cd_branchalias='" + Cmb_BranchName + "';");
                    RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
                    DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);

                }

                UPDATEBRANCH(Cmb_BranchName);
                if (DtLicense.Rows.Count > 0)
                {
                    L_OperatingHours = DtLicense.Rows[0]["OPERATINGHOURS"].ToString();
                    Session["L_OperatingHours"] = L_OperatingHours;
                }

                if (Session["L_OperatingHours"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {
                    string OperativeHoursType = Get_Generic_Webordingini(Cmb_BranchName, "63");

                    if (OperativeHoursType == "0")
                    {
                        Type1.Visible = true;
                        Type2.Visible = false;
                        LoadOperatingHours(Cmb_BranchName);

                    }
                    else if (OperativeHoursType == "1")
                    {
                        Type1.Visible = false;
                        Type2.Visible = true;
                        LoadShiftOperativeHours(Cmb_BranchName);
                    }



                }

            }





        }

        private string Get_Generic_Webordingini(string DB, string IniID)
        {
            if (cmbBranches.Visible == true)
            {
                #region  Validation Between HQ Branch and Normal Branch 
                BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                if (BranchFullName == "HQ")
                {
                    BranchAlias = Session["Branch_HQ"].ToString();
                }
                else
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    BranchAlias = Cmb_BranchName;

                }
                #endregion
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }




            BL_API objAPI = new BL_API();
            string returnvalue = string.Empty;
            string sBearerToken = objAPI.GetBarerToken();

            string JSONRetValue = objAPI.API_GET_INIWebOrderingIniValue(sBearerToken, DB, IniID);
            var sObject = "[" + JsonConvert.DeserializeObject(JSONRetValue).ToString().Trim() + "]";
            DataTable dtResult = (DataTable)JsonConvert.DeserializeObject(sObject, (typeof(DataTable)));
            if (dtResult.Rows.Count == 0)
            {
                returnvalue = "";
            }
            else
            {
                returnvalue = (dtResult.Rows[0]["ini_value"]).ToString();
            }
            return returnvalue;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            else
            {
                BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);     // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }



            foreach (GridViewRow gvrow in grdOperatingHours.Rows)
            {
                var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                if (checkbox.Checked)
                {
                    Label lblid = gvrow.FindControl("lblid") as Label;
                    Label lblDays_Value = gvrow.FindControl("lblDays") as Label;
                    CheckBox  checkBox_Val = gvrow.FindControl("ChoiceCheckBox") as CheckBox;

                    CheckBox checkBox5_Val = gvrow.FindControl("ChoiceCheckBox5") as CheckBox;


                    TextBox txtRT1_StTm = (TextBox)gvrow.FindControl("txtT1_StTm");
                    TextBox txtRT1_EndTm = (TextBox)gvrow.FindControl("txtT1_EndTm");
                    TextBox txtRspl_EndTm = (TextBox)gvrow.FindControl("txtSpl_EndTm");


                    string Added = txtRT1_StTm.Text;
                    string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                    string Added1 = txtRT1_EndTm.Text;
                    string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);
                    string Added2 = txtRspl_EndTm.Text;
                    string NewSpl_EndtDt = string.Concat(Curr_DateOnly, " ", Added2);


                    string chk_Values = string.Empty;
                    string chk5_Values = string.Empty;
                    if (checkBox_Val.Checked == true)
                    {
                        chk_Values = "1";


                    }
                    else if (checkBox_Val.Checked == false)
                    {
                        chk_Values = "0";
                    }

                    if (checkBox5_Val.Checked == true)
                    {
                        chk5_Values = "1";
                    }
                    else if (checkBox5_Val.Checked == false)
                    {
                        chk5_Values = "0";
                    }

                    string Query = "Update  {0}.Operatinghours set StartTm=@T1_StTm, EndTm=@T1_EndTm,Active=@Active,Special_EndTm=@Special_EndTm,IsSpecialHours=@IsSpecialHours  where id=@id AND Days=@Days; ";
                    string command = string.Format(Query, BranchAlias);

                    MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@T1_StTm",NewStartDt),
                     new MySqlParameter("@T1_EndTm",NewEndtDt),
                     new MySqlParameter("@id",lblid.Text),
                     new MySqlParameter("@Active",chk_Values),
                     new MySqlParameter("@Days",lblDays_Value.Text),
                     new MySqlParameter("@Special_EndTm",NewSpl_EndtDt),
                     new MySqlParameter("@IsSpecialHours",chk5_Values),
                   };

                   K = ObjMain.ExecuteQuery(command, parameters);

                    if (K > 0)
                    {
                        Page page = HttpContext.Current.Handler as Page;
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Operating Hours Updated Succeessfully.');location.href='OperatingHours.aspx';", true);
                    }

                }

            }

        }

        protected void grdOperatingHours_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string sRetVal = string.Empty;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
                CheckBox checkBox_Val = (e.Row.FindControl("ChoiceCheckBox5") as CheckBox);
                TextBox Read_EndTime = (e.Row.FindControl("txtT1_EndTm") as TextBox);
                TextBox Read_SPL_EndTime = (e.Row.FindControl("txtSpl_EndTm") as TextBox);
                if (cmbBranches.SelectedIndex != 0)
                {
                    BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);

                   if (checkBox_Val.Checked == true)
                    {
                        Read_EndTime.Enabled = false;
                    }
                    else if (checkBox_Val.Checked ==false)
                    { 
                      Read_SPL_EndTime.Enabled = false;
                    }
                   
                }

            }
        }

       public void LoadOperatingHours(string Branchname)
        {
            string query = " select id ,Days,StartTm,EndTm,Active ,Special_EndTm,IsSpecialHours, CASE Active "
                           + " WHEN 0 THEN 'Inactive' "
                           + " WHEN 1 THEN 'Active' "
                           + " end StatusText "
                           + " from {0}.Operatinghours; ";
            string command = string.Format(query, Branchname);
            DT = ObjMain.GetData_DT(command);
            grdOperatingHours.DataSource = DT;
            grdOperatingHours.DataBind();
        }
        public void LoadShiftOperativeHours(string BranchName)
        {
            string query = "SELECT id, Days, Shift1,TriggerTime_S1,TriggerTime_E1,Shift2,TriggerTime_S2,TriggerTime_E2 FROM {0}.Shiftschedule; ";
            string Command = string.Format(query, BranchName);
            DT = ObjMain.GetData_DT(Command);
            grdShiftOperativeHours.DataSource = DT;
            grdShiftOperativeHours.DataBind();
            string isDifferentDay= Get_Generic_Webordingini(BranchName, "64");

            if (isDifferentDay == "0")
            {
                
              lblShiftOperativeHrs.Text = "Note: Operative Shift Hours lies on Same Day Ini Has Set.. Eg 06:00 To 12:00   & 14:00 To 21:30 ( Here Both Shifts Timing lies on same Day)";
                
            }
            else if (isDifferentDay == "1")
            {
              lblShiftOperativeHrs.Text = "Note: Operative Shift Hours lies on Different Day Ini Has Set..Eg  06:00 To 12:00   & 14:00 To 5:59 (Here 5:59 Refers Next Day Morning)";
            }             

        }
        protected void btnSave2_Click(object sender, EventArgs e)
        {
            // Insert Function  


            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            else
            {
                BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);     // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }



            foreach (GridViewRow gvrow in grdShiftOperativeHours.Rows)
            {
                var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                if (checkbox.Checked)
                {
                    Label lblid = gvrow.FindControl("lblid") as Label;
                    Label lblDays_Value = gvrow.FindControl("lblDays") as Label;
                    CheckBox Read_Shift1_checkBox_Val = gvrow.FindControl("Shift1CheckBox") as CheckBox;
                    CheckBox Read_Shift2_checkBox_Val = gvrow.FindControl("Shift2CheckBox") as CheckBox;
                    TextBox txtRT1_StTm = (TextBox)gvrow.FindControl("txtTriggerTime_S1");
                    TextBox txtRT1_EndTm = (TextBox)gvrow.FindControl("txtTriggerTime_E1");
                    TextBox txtRT2_StTm = (TextBox)gvrow.FindControl("txtTriggerTime_S2");
                    TextBox txtRT2_EndTm = (TextBox)gvrow.FindControl("txtTriggerTime_E2");


                    string Added = txtRT1_StTm.Text;
                    string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                    string Added1 = txtRT1_EndTm.Text;
                    string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);

                    string Added2 = txtRT2_StTm.Text;
                    string Start_Dt2 = string.Concat(Curr_DateOnly, " ", Added2);
                    string Added3 = txtRT2_EndTm.Text;
                    string End_Dt2 = string.Concat(Curr_DateOnly, " ", Added3);


                    string chk_Values = string.Empty;
                    string chk5_Values = string.Empty;
                    if (Read_Shift1_checkBox_Val.Checked == true)
                    {
                        chk_Values = "1";


                    }
                    else if (Read_Shift1_checkBox_Val.Checked == false)
                    {
                        chk_Values = "0";
                    }

                    if (Read_Shift2_checkBox_Val.Checked == true)
                    {
                        chk5_Values = "1";
                    }
                    else if (Read_Shift2_checkBox_Val.Checked == false)
                    {
                        chk5_Values = "0";
                    }

                    string Query = " Update  {0}.Shiftschedule set Shift1=@Shift1,TriggerTime_S1=@TriggerTime_S1,TriggerTime_E1=@TriggerTime_E1,Shift2=@Shift2 , " +
                        " TriggerTime_S2=@TriggerTime_S2, TriggerTime_E2=@TriggerTime_E2 where id=@id AND Days=@Days; ";
                    string command = string.Format(Query, BranchAlias);

                    MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@Shift1",chk_Values),
                     new MySqlParameter("@TriggerTime_S1",NewStartDt),
                     new MySqlParameter("@TriggerTime_E1",NewEndtDt),
                     new MySqlParameter("@Shift2",chk5_Values),
                     new MySqlParameter("@TriggerTime_S2",Start_Dt2),
                     new MySqlParameter("@TriggerTime_E2",End_Dt2),
                     new MySqlParameter("@Days",lblDays_Value.Text),
                     new MySqlParameter("@id",lblid.Text),
                   };

                    K = ObjMain.ExecuteQuery(command, parameters);

                    if (K > 0)
                    {
                        Page page = HttpContext.Current.Handler as Page;
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Operating Shift Hours Updated Succeessfully.');location.href='OperatingHours.aspx';", true);
                    }

                }

            }

        }

        protected void grdShiftOperativeHours_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string sRetVal = string.Empty;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                CheckBox Shift1_checkBox_Val = (e.Row.FindControl("Shift1CheckBox") as CheckBox);

                CheckBox shift2_checkBox_Val = (e.Row.FindControl("Shift2CheckBox") as CheckBox);

                if (cmbBranches.SelectedIndex != 0)
                {
                    BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);

                    if (Shift1_checkBox_Val.Checked == true)
                    {
                        
                    }
                    else if (shift2_checkBox_Val.Checked == false)
                    {
                        
                    }

                }

            }


        }
    }
}