using DevExpress.Web;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class Transactions_TA_UI_Transaction_AutoShiftConfiguration : Base.BasePage
{
    #region Variable Declaration
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    GetReports rptEmp = new GetReports();
    DataSet ds_Checkuser = new DataSet();
    HeaderInformation hdrTable = new HeaderInformation();
    ResourceManager rm;
    ShiftPatternEventData objShiftPatternOverLap = new ShiftPatternEventData();
    ShiftAllocationEvendata objShiftAllocation = new ShiftAllocationEvendata();
    CultureInfo ci;
    cls_Common objCommon = new cls_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    string strFromdate, gstrDateFormat, strTodate;
    Rpt_Common rptcommon = new Rpt_Common();
    int intIndividualUser = 0;
    string strEmpAutoId = "0";
    DiscrepancyEventData objDis = new DiscrepancyEventData();
    ShiftEvenData objShift = new ShiftEvenData();
    bool chkShiftAllocationExists;
    DataSet dsOverLapping = new DataSet();
    DataSet ds_Empdetails = new DataSet();
    DataSet ds_ShiftAllocdetails = new DataSet();
    DataSet ds = new DataSet();
    DataSet ds_ShiftPattern = new DataSet();
    DataTable dtEmpDetails = new DataTable();
    int PatternDaysCount = 0;
    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language     
        grdAutoShift.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objCommon.menu("ShiftTransaction", "267");
        }
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EnterValidFromDate", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validmonthdatefourdigit", ci) + "','" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        //Set UserRights
        UserRights();
        //grdAutoShift.Settings.HorizontalScrollBarMode = ScrollBarMode.Hidden;
        if (!IsPostBack)
        {
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            Hidden2.Value = Convert.ToString(Session["IDateFormat"]);
            // Bindings the employee category and type.
            BindingEmployeeCategoryandType();
            FillDesignation();
            ddlDesignation.PreRender += LB_PreRender;
            if (Session["parentLevelId"] != null || Convert.ToInt16(Session["parentLevelId"]) != 0)
            {
                Session["parentLevelId"] = null;
            }
        }
        //Convert date time to MM/dd/YYYY format
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
    }
    /// <summary>
    /// Fills the designation in the drop down
    /// </summary>
    private void FillDesignation()
    {
        DataSet ds_designation = new DataSet();
        ds_designation = objEmployee.select_CommonDesignation();
        if (ds_designation != null)
        {
            if (ds_designation.Tables[0].Rows.Count > 0)
            {
                ddlDesignation.DataSource = ds_designation;
                ddlDesignation.DataTextField = "TitleName";
                ddlDesignation.DataValueField = "TitleId";
                ddlDesignation.DataBind();
            }
            ds_designation.Dispose();
            foreach (ListItem item in ddlDesignation.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    /// <summary>
    /// Bindings the employee category and type.
    /// </summary>
    private void BindingEmployeeCategoryandType()
    {
        // employee category binding
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
            foreach (ListItem item in ddlEmpCatagory.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        // employee type binding
        DataSet dsType = new DataSet();
        dsType = objEmployee.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
            foreach (ListItem item in ddlEmpType.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    /// <summary>
    /// Getting Information from resource file and display it in label
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        //Todo: need to add data in resource file
        lblShiftReport.Text = rm.GetString("AutoShiftConfigurationHeader", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        ddlEmpCatagory.Items[0].Text = rm.GetString("Select", ci);
        ddlEmpType.Items[0].Text = rm.GetString("Select", ci);
        lblDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lblGender.Text = rm.GetString("EmployeeMaster_Gender", ci);
        ddlGender.Items[0].Text = rm.GetString("Caption_Both", ci);
        ddlGender.Items[1].Text = rm.GetString("rbtn_EmployeeMaster_Male", ci);
        ddlGender.Items[2].Text = rm.GetString("rbtn_EmployeeMaster_Female", ci);
        ddlDesignation.Items[0].Text = rm.GetString("ExportData_All", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        btnSave.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        grdAutoShift.Columns[1].Caption = rm.GetString("email_EmployeeId", ci);
        grdAutoShift.Columns[2].Caption = rm.GetString("email_EmployeeName", ci);
        grdAutoShift.Columns[3].Caption = rm.GetString("AutoShiftConfigddl1", ci);
        grdAutoShift.Columns[4].Caption = rm.GetString("AutoShiftConfigddl2", ci);
        grdAutoShift.Columns[5].Caption = rm.GetString("AutoShiftConfigddl3", ci);
        grdAutoShift.Columns[6].Caption = rm.GetString("AutoShiftConfigddl4", ci);
        grdAutoShift.Columns[7].Caption = rm.GetString("AutoShiftConfigddl5", ci);
        btnImport.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Import", ci);
    }
    /// <summary>
    /// To check rights for the logged in user
    /// </summary>
    private void UserRights()
    {
        //Todo: PageId 267 Vaighai   
        ds_Checkuser = objCheckUser.checkRights(267, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                btn_View.Visible = true;
            }
            else
            {
                btn_View.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                btnSave.Visible = true;
            }
            else
            {
                btnSave.Visible = false;
            }
        }
        else
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
    /// <summary>
    /// To apply tooltip for the designation in the drop down
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void LB_PreRender(object sender, System.EventArgs e)
    {
        foreach (ListItem item in ddlDesignation.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    /// <summary>
    /// Load Shift Dropdown
    /// </summary>
    protected void btn_View_Click(object sender, EventArgs e)
    {
        int employeeCategory = 0, employeeType = 0;
        try
        {
            if (Page.IsValid == true)
            {
                string dtFrom = "";
                string dtTo = "";
                if (Convert.ToInt32(ddlEmpCatagory.SelectedValue) != 0)
                {
                    employeeCategory = Convert.ToInt32(ddlEmpCatagory.SelectedValue);
                }
                else
                {
                    employeeCategory = 0;
                }
                if (Convert.ToInt32(ddlEmpType.SelectedValue) != 0)
                {
                    employeeType = Convert.ToInt32(ddlEmpType.SelectedValue);
                }
                else
                {
                    employeeType = 0;
                }
                if (Session["ReportsBasedOn"] != null)
                {
                    strEmpAutoId = (Convert.ToString(Session["ReportsBasedOn"]) == "False") ? "0" : Convert.ToString(Session["EmpAutoID"]);
                }
                string Level = string.Empty;
                string employeeName = string.Empty;
                string empName = string.Empty;
                string empId = string.Empty;
                string EmployeeId = string.Empty;
                int companyId = 0;
                if (intIndividualUser >= 1)
                {
                    empName = string.Empty;
                    Level = string.Empty;
                    empId = Convert.ToString(Session["EmployeeCodeId"]);
                    EmployeeId = Convert.ToString(Session["EmployeeCodeId"]);
                }
                else
                {
                    EmployeeId = txtEmployeeId.Text.Trim();
                    empName = txtEmployeeName.Text.ToString();
                    empId = txtEmployeeId.Text.Trim().ToString();
                    employeeName = this.txtEmployeeName.Text.Replace("'", "''");
                    Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
                }
                //
                // Change grade level from session
                //
                companyId = Convert.ToInt16(Session["parentLevelId"]);
                GetAutoShiftDetails(empId, empName, employeeCategory, employeeType, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), companyId, Convert.ToInt32(ddlDesignation.SelectedValue), Convert.ToString(ddlGender.SelectedValue), Convert.ToInt64(strEmpAutoId));
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            #region SaveLeaveDetails
            int ddlShiftID1 = 0;
            int ddlShiftID2 = 0;
            int ddlShiftID3 = 0;
            int ddlShiftID4 = 0;
            int ddlShiftID5 = 0;
            bool isInserted = false;
            int empAutoID = 0;
            string EmpName = "";
            for (int i = 0; i < grdAutoShift.VisibleRowCount; i++)
            {
                HtmlInputCheckBox chkbox = (HtmlInputCheckBox)grdAutoShift.FindRowCellTemplateControl(i, (GridViewDataColumn)grdAutoShift.Columns["AutoID"], "chkSelect");
                if (chkbox != null)
                {
                    if (chkbox.Checked == true)
                    {
                        int[] Shiftselectedarray = new int[5];
                        DropDownList grddlshift1 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(i, null, "ddlshift1");
                        if (grddlshift1 != null)
                        {
                            ddlShiftID1 = Convert.ToInt16(grddlshift1.SelectedItem.Value);
                            Shiftselectedarray[0] = ddlShiftID1;
                        }
                        DropDownList grddlshift2 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(i, null, "ddlshift2");
                        if (grddlshift2 != null)
                        {
                            ddlShiftID2 = Convert.ToInt16(grddlshift2.SelectedItem.Value);
                            Shiftselectedarray[1] = ddlShiftID2;
                        }
                        DropDownList grddlshift3 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(i, null, "ddlshift3");
                        if (grddlshift3 != null)
                        {
                            ddlShiftID3 = Convert.ToInt16(grddlshift3.SelectedItem.Value);
                            Shiftselectedarray[2] = ddlShiftID3;
                        }
                        DropDownList grddlshift4 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(i, null, "ddlshift4");
                        if (grddlshift4 != null)
                        {
                            ddlShiftID4 = Convert.ToInt16(grddlshift4.SelectedItem.Value);
                            Shiftselectedarray[3] = ddlShiftID4;
                        }
                        DropDownList grddlshift5 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(i, null, "ddlshift5");
                        if (grddlshift5 != null)
                        {
                            ddlShiftID5 = Convert.ToInt16(grddlshift5.SelectedItem.Value);
                            Shiftselectedarray[4] = ddlShiftID5;
                        }
                        Label lblEmployeeAutoID = (Label)grdAutoShift.FindRowCellTemplateControl(i, (GridViewDataColumn)grdAutoShift.Columns["EmployeeAutoID"], "lblEmployeeAutoID");
                        if (lblEmployeeAutoID != null)
                        {
                            empAutoID = Convert.ToInt16(lblEmployeeAutoID.Text);
                        }
                        //
                        // TODO: Any one ddl compulsory selection validation
                        //
                        //if (ddlShiftID1 == 0 && ddlShiftID2 == 0 && ddlShiftID3 == 0 && ddlShiftID4 == 0 && ddlShiftID5 == 0)
                        //{
                        //    string strScript = "<script language='javascript' type='text/javascript'>";
                        //    strScript += "alert('" + rm.GetString("AutoShiftAllocation_FistShiftMandatory", ci) + "');";
                        //    strScript += " </script>";
                        //    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                        //    isInserted = false;
                        //}
                        //else
                        //{ }
                        //
                        // TODO: first ddl compulsory selection validation //|| (ddlShiftID1 == 0 && (ddlShiftID2 != 0 || ddlShiftID3 != 0 || ddlShiftID4 != 0 || ddlShiftID5 != 0)) 
                        //
                        if ((ddlShiftID1 != 0 && (ddlShiftID2 == 0 && ddlShiftID3 == 0 && ddlShiftID4 == 0 && ddlShiftID5 == 0)) || (ddlShiftID1 != 0 && (ddlShiftID2 != 0 || ddlShiftID3 != 0 || ddlShiftID4 != 0 || ddlShiftID5 != 0)) || (ddlShiftID1 == 0 && ddlShiftID2 == 0 && ddlShiftID3 == 0 && ddlShiftID4 == 0 && ddlShiftID5 == 0)) // Ordered Drop down selection
                        {
                            if ((ddlShiftID2 != 0 && (ddlShiftID3 != 0 || ddlShiftID4 != 0 || ddlShiftID5 != 0)) || ((ddlShiftID2 == 0 || ddlShiftID2 != 0) && (ddlShiftID3 == 0 && ddlShiftID4 == 0 && ddlShiftID5 == 0)))  // Ordered Drop down selection
                            {
                                if ((ddlShiftID3 != 0 && (ddlShiftID4 != 0 || ddlShiftID5 != 0)) || ((ddlShiftID3 == 0 || ddlShiftID3 != 0) && (ddlShiftID4 == 0 && ddlShiftID5 == 0)))// Ordered Drop down selection
                                {
                                    if ((ddlShiftID4 != 0 && (ddlShiftID5 != 0)) || ((ddlShiftID4 == 0 || ddlShiftID4 != 0) && (ddlShiftID5 == 0)))// Ordered Drop down selection
                                    {
                                        bool ValidShift = true;
                                        //int a = Shiftselectedarray.Length;                                            
                                        //var dict = new Dictionary<int, int>();
                                        //foreach (var value in Shiftselectedarray)
                                        //{
                                        //    if (dict.ContainsKey(value))
                                        //        dict[value]++;
                                        //    else
                                        //        dict[value] = 1;
                                        //}
                                        //foreach (var pair in dict)
                                        //{
                                        //    if (pair.Key!= 0 && pair.Value > 1)
                                        //    {
                                        //        string strScript1 = "<script language='javascript' type='text/javascript'>";
                                        //        strScript1 += "alert('" + rm.GetString("AutoShiftAllocation_SameShiftinMultipleddl", ci) + "'+ '" + EmpName + "');";                                                  
                                        //        strScript1 += " </script>";
                                        //        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript1, false);
                                        //        isInserted = false;
                                        //        ValidShift = false;
                                        //        break;
                                        //    }                                           
                                        //}
                                        if (ValidShift == true)
                                        {
                                            objShiftAllocation.InsertAutoShiftDetails(empAutoID, ddlShiftID1, ddlShiftID2, ddlShiftID3, ddlShiftID4, ddlShiftID5, Convert.ToInt32(Session["EmpAutoID"]));
                                            isInserted = true;
                                        }
                                    }
                                    else
                                    {
                                        string strScript = "<script language='javascript' type='text/javascript'>";
                                        strScript += "alert('" + rm.GetString("AutoShiftAllocation_FourthShiftMandatory", ci) + "');";
                                        strScript += " </script>";
                                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                                        isInserted = false;
                                    }
                                }
                                else
                                {
                                    string strScript = "<script language='javascript' type='text/javascript'>";
                                    strScript += "alert('" + rm.GetString("AutoShiftAllocation_ThirdShiftMandatory", ci) + "');";
                                    strScript += " </script>";
                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                                    isInserted = false;
                                }
                            }
                            else
                            {
                                string strScript = "<script language='javascript' type='text/javascript'>";
                                strScript += "alert('" + rm.GetString("AutoShiftAllocation_SecondShiftMandatory", ci) + "');";
                                strScript += " </script>";
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                                isInserted = false;
                            }
                        }
                        else
                        {
                            string strScript = "<script language='javascript' type='text/javascript'>";
                            strScript += "alert('" + rm.GetString("AutoShiftAllocation_FistShiftMandatory", ci) + "');";
                            strScript += " </script>";
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                            isInserted = false;
                        }
                    }
                }
            }
            if (isInserted)
            {
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "alert('" + rm.GetString("AutoShift_Configuration_SavedSuccessfully", ci) + "');";
                strScript += " </script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                divAutoShift.Style.Add("display", "none");
                grdAutoShift.DataSource = "";
                grdAutoShift.Visible = false;
                hdnSelectRow.Value = "";
                btnSave.Visible = false;
            }
            else
            {
                return;
            }
            #endregion
        }
        else
        {
            string strScript = string.Empty;
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("AutoShiftAllocation_SelectItem", ci) + "');";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertPAdd996266", strScript);
        }
    }
    protected void GetAutoShiftDetails(string EmployeeId, string empName, int employeeCategory, int employeeType, string UserGroupName, string GradeLevel, int ParentId, int DesignationId, string Gender, Int64 EmpAutoID)
    {
        DataSet dsAutoShiftDetails = new DataSet();
        dsAutoShiftDetails = rptEmp.GetAutoShiftDetails_ShiftAllocation(EmployeeId, empName, employeeCategory, employeeType, UserGroupName, GradeLevel, ParentId, DesignationId, Gender, EmpAutoID);
        if (dsAutoShiftDetails.Tables.Count > 0)
        {
            if (dsAutoShiftDetails.Tables[0].Rows.Count == 0)
            {
                string strMsg;
                grdAutoShift.DataSource = "";
                grdAutoShift.DataBind();
                btnSave.Visible = false;
                grdAutoShift.Visible = false;
                divAutoShift.Style.Add("display", "none");  //codechanged
                strMsg = "<script language='javascript'>";
                strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');window.close();";
                strMsg = strMsg + "</script>";
                Page.RegisterStartupScript("Display", strMsg);
                return;
            }
            else
            {
                divAutoShift.Style.Add("display", "block");
                if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
                {
                    btnSave.Visible = true;
                }
                else
                {
                    btnSave.Visible = false;
                }
                grdAutoShift.Visible = true;
                grdAutoShift.DataSource = dsAutoShiftDetails;
                grdAutoShift.DataBind();
                ViewState["Dataset_AutoShift"] = dsAutoShiftDetails;
            }
        }
    }
    public int OverLappedValidation(DateTime dtmStartDate, int EmpID, int StartDateShiftId, int EndDateShiftId)
    {
        int intOverLapped = 0;
        intOverLapped = ConstructPermanentShiftAllocation(EmpID, dtmStartDate, dtmStartDate, StartDateShiftId, EndDateShiftId);
        return intOverLapped;
    }
    public int ConstructPermanentShiftAllocation(int EmployeeId, DateTime dtmStartDate, DateTime dtmToDate, int StartDateShiftId, int EndDateShiftId)
    {
        chkShiftAllocationExists = false;
        int PreviousDayShiftId = 0;
        int NextDayShiftId = 0;
        dtmStartDate = dtmStartDate.AddDays(-1);
        dtmToDate = dtmToDate.AddDays(1);
        DataSet dsPermanentTemporaryShift = new DataSet();
        dsPermanentTemporaryShift = objShiftAllocation.SelectPermanentTemporaryShiftDates(dtmStartDate, dtmToDate, EmployeeId.ToString());
        if (dsPermanentTemporaryShift.Tables[0].Rows.Count > 0)
        {
            if (dsPermanentTemporaryShift.Tables[0].Rows[0]["P_FromShiftId"] != null && dsPermanentTemporaryShift.Tables[0].Rows[0]["P_FromShiftId"].ToString() != string.Empty)
            {
                chkShiftAllocationExists = true;
                PreviousDayShiftId = Convert.ToInt32(dsPermanentTemporaryShift.Tables[0].Rows[0]["P_FromShiftId"]);
            }
            if (dsPermanentTemporaryShift.Tables[0].Rows[0]["P_ToShiftId"] != null && dsPermanentTemporaryShift.Tables[0].Rows[0]["P_ToShiftId"].ToString() != string.Empty)
            {
                chkShiftAllocationExists = true;
                NextDayShiftId = Convert.ToInt32(dsPermanentTemporaryShift.Tables[0].Rows[0]["P_ToShiftId"]);
            }
            if (dsPermanentTemporaryShift.Tables[0].Rows[0]["T_FromShiftId"] != null && dsPermanentTemporaryShift.Tables[0].Rows[0]["T_FromShiftId"].ToString() != string.Empty)
            {
                chkShiftAllocationExists = true;
                PreviousDayShiftId = Convert.ToInt32(dsPermanentTemporaryShift.Tables[0].Rows[0]["T_FromShiftId"]);
            }
            if (dsPermanentTemporaryShift.Tables[0].Rows[0]["T_ToShiftId"] != null && dsPermanentTemporaryShift.Tables[0].Rows[0]["T_ToShiftId"].ToString() != string.Empty)
            {
                chkShiftAllocationExists = true;
                NextDayShiftId = Convert.ToInt32(dsPermanentTemporaryShift.Tables[0].Rows[0]["T_ToShiftId"]);
            }
        }
        if (chkShiftAllocationExists == true)
        {
            int intOverLap = 0;
            int ShiftId1 = PreviousDayShiftId;
            int ShiftId2 = StartDateShiftId;
            dsOverLapping = objShiftPatternOverLap.CheckShiftOverLapping(ShiftId1, ShiftId2);
            if (dsOverLapping.Tables.Count > 0)
            {
                if (dsOverLapping.Tables[0].Rows.Count > 0)
                {
                    intOverLap = Convert.ToInt32(dsOverLapping.Tables[0].Rows[0]["ShiftOverLapping"]);
                }
            }
            if (intOverLap > 0)
            {
                return intOverLap;
            }
            dsOverLapping.Tables.Clear();
            ShiftId1 = EndDateShiftId;
            ShiftId2 = NextDayShiftId;
            dsOverLapping = objShiftPatternOverLap.CheckShiftOverLapping(ShiftId1, ShiftId2);
            if (dsOverLapping.Tables.Count > 0)
            {
                if (dsOverLapping.Tables[0].Rows.Count > 0)
                {
                    intOverLap = Convert.ToInt32(dsOverLapping.Tables[0].Rows[0]["ShiftOverLapping"]);
                }
            }
            if (intOverLap > 0)
            {
                return intOverLap;
            }
        }
        return 0;
    }
    /// <summary>
    protected void btnImport_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Transaction_AutoShiftImport.aspx");
    }
    protected void grdAutoShift_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        try
        {
            if (e.RowType == DevExpress.Web.GridViewRowType.Data)
            {
                DataSet dsAutoShift = new DataSet();
                DropDownList ddlshift1 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(e.VisibleIndex, null, "ddlshift1");
                DropDownList ddlshift2 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(e.VisibleIndex, null, "ddlshift2");
                DropDownList ddlshift3 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(e.VisibleIndex, null, "ddlshift3");
                DropDownList ddlshift4 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(e.VisibleIndex, null, "ddlshift4");
                DropDownList ddlshift5 = (DropDownList)grdAutoShift.FindRowCellTemplateControl(e.VisibleIndex, null, "ddlshift5");
                string lbl_Companyid = Convert.ToString(Session["parentLevelId"]);
                string lblAutoShiftid1 = Convert.ToString(e.GetValue("ShiftID1"));
                string lblAutoShiftid2 = Convert.ToString(e.GetValue("ShiftID2"));
                string lblAutoShiftid3 = Convert.ToString(e.GetValue("ShiftID3"));
                string lblAutoShiftid4 = Convert.ToString(e.GetValue("ShiftID4"));
                string lblAutoShiftid5 = Convert.ToString(e.GetValue("ShiftID5"));
                string source_Language;
                source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
                rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
                ci = Thread.CurrentThread.CurrentCulture;
                if (ddlshift1 != null)
                {
                    dsAutoShift = objShift.SelectShiftsByCompany(Convert.ToInt32(lbl_Companyid));
                    if (dsAutoShift != null)
                    {
                        if (dsAutoShift.Tables.Count > 0)
                        {
                            if (dsAutoShift.Tables[0].Rows.Count > 0)
                            {
                                DataRow[] drow = dsAutoShift.Tables[0].Select("Shift_Name='No Shift'");
                                if (drow.Length > 0)
                                {
                                    foreach (DataRow r in drow)
                                    {
                                        r.Delete();
                                    }
                                    dsAutoShift.Tables[0].AcceptChanges();
                                }
                                ddlshift1.DataSource = dsAutoShift;
                                ddlshift1.DataTextField = "Shift_ShortName";
                                ddlshift1.DataValueField = "Shift_Id";
                                ddlshift1.DataBind();

                                ddlshift2.DataSource = dsAutoShift;
                                ddlshift2.DataTextField = "Shift_ShortName";
                                ddlshift2.DataValueField = "Shift_Id";
                                ddlshift2.DataBind();
                                ddlshift3.DataSource = dsAutoShift;
                                ddlshift3.DataTextField = "Shift_ShortName";
                                ddlshift3.DataValueField = "Shift_Id";
                                ddlshift3.DataBind();
                                ddlshift4.DataSource = dsAutoShift;
                                ddlshift4.DataTextField = "Shift_ShortName";
                                ddlshift4.DataValueField = "Shift_Id";
                                ddlshift4.DataBind();
                                ddlshift5.DataSource = dsAutoShift;
                                ddlshift5.DataTextField = "Shift_ShortName";
                                ddlshift5.DataValueField = "Shift_Id";
                                ddlshift5.DataBind();
                            }
                            dsAutoShift.Dispose();
                            bindToolTip(ddlshift1);
                            bindToolTip(ddlshift2);
                            bindToolTip(ddlshift3);
                            bindToolTip(ddlshift4);
                            bindToolTip(ddlshift5);
                            if (lblAutoShiftid1 != null && Convert.ToString(lblAutoShiftid1) != "" && Convert.ToString(lblAutoShiftid1) != "0")
                            {
                                ddlshift1.SelectedValue = Convert.ToString(lblAutoShiftid1);
                            }
                            if (lblAutoShiftid2 != null && Convert.ToString(lblAutoShiftid2) != "" && Convert.ToString(lblAutoShiftid2) != "0")
                            {
                                ddlshift2.SelectedValue = Convert.ToString(lblAutoShiftid2);
                            }
                            if (lblAutoShiftid3 != null && Convert.ToString(lblAutoShiftid3) != "" && Convert.ToString(lblAutoShiftid3) != "0")
                            {
                                ddlshift3.SelectedValue = Convert.ToString(lblAutoShiftid3);
                            }
                            if (lblAutoShiftid4 != null && Convert.ToString(lblAutoShiftid4) != "" && Convert.ToString(lblAutoShiftid4) != "0")
                            {
                                ddlshift4.SelectedValue = Convert.ToString(lblAutoShiftid4);
                            }
                            if (lblAutoShiftid5 != null && Convert.ToString(lblAutoShiftid5) != "" && Convert.ToString(lblAutoShiftid5) != "0")
                            {
                                ddlshift5.SelectedValue = Convert.ToString(lblAutoShiftid5);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string strScript = "<script language='javascript' type='text/javascript'>";
            strScript += "alert('" + ex.Message + "');";
            strScript += " </script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
    }

    private static void bindToolTip(DropDownList ddl)
    {
        foreach (ListItem item in ddl.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
}