using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;

public partial class Masters_FolderConfiguration : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    EmployeeEventData objEmployee = new EmployeeEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    string source_Language;
    #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        
        string 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"]);
        if (!IsPostBack)
        {
            UserRights();
            LoadStrings(ci);
            //Todo:need to uncomment below line
            objcommon.menu("Configuration", "276");
            //Todo:need to comment below line
            //objcommon.menu("Configuration", "16");
            bindFolderData();
        }
    }
    private void LoadStrings(CultureInfo ci)
    {

    }
    private void UserRights()
    {
        if (ViewState["vs_ds_Checkuser"] == null)
        {
            //Todo:need to uncomment below line
            ds_Checkuser = objCheckUser.checkRights(243, Convert.ToInt32(Session["userGroup_Id"]), "");
            //Todo:need to comment below line
            //ds_Checkuser = objCheckUser.checkRights(16, Convert.ToInt32(Session["userGroup_Id"]), "");
            ViewState["vs_ds_Checkuser"] = ds_Checkuser;
        }
        else
            ds_Checkuser = (DataSet)ViewState["vs_ds_Checkuser"];
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_Save.Visible = true;
            }
            else
            {
                this.btn_Save.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void bindFolderData()
    {
        DataSet dsFolder = new DataSet();
        dsFolder = objEmployee.GetFolderPathDetails();
        if (dsFolder.Tables.Count > 0)
        {
            if (dsFolder.Tables[0].Rows.Count > 0)
            {
                if (Convert.ToString(dsFolder.Tables[0].Rows[0]["EmployeeFolderPath"]) != "")
                    txtempMaster.Text = Convert.ToString(dsFolder.Tables[0].Rows[0]["EmployeeFolderPath"]);
                if (Convert.ToString(dsFolder.Tables[0].Rows[0]["LeaveFolderPath"]) != "")
                    txtApprovedAbsence.Text = Convert.ToString(dsFolder.Tables[0].Rows[0]["LeaveFolderPath"]);
                if (Convert.ToString(dsFolder.Tables[0].Rows[0]["OndutyFolderPath"]) != "")
                    txtApprovedAttendance.Text = Convert.ToString(dsFolder.Tables[0].Rows[0]["OndutyFolderPath"]);
                if (Convert.ToString(dsFolder.Tables[0].Rows[0]["AttendanceData"]) != "")
                    txtAttendancedata.Text = Convert.ToString(dsFolder.Tables[0].Rows[0]["AttendanceData"]);
            }
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            string str = "";
            if (Convert.ToString(txtempMaster.Text) == "" && Convert.ToString(txtApprovedAttendance.Text) == "" && Convert.ToString(txtApprovedAbsence.Text) == "" && Convert.ToString(txtAttendancedata.Text) == "")
            {
                str = "alert('Please mention any one folder path for saving');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
            else
            {
                if (Convert.ToString(txtempMaster.Text) != "")
                    if (!Directory.Exists(Convert.ToString(txtempMaster.Text)))
                    {
                        str = "alert('The specified folder in employee master field is not available, please ensure the specified folder path is correct.');";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                    }
                if (Convert.ToString(txtApprovedAttendance.Text) != "")
                    if (!Directory.Exists(Convert.ToString(txtApprovedAttendance.Text)))
                    {
                        str = "alert('The specified folder in approved attendance field is not available, please ensure the specified folder path is correct.');";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                    }
                if (Convert.ToString(txtApprovedAbsence.Text) != "")
                    if (!Directory.Exists(Convert.ToString(txtApprovedAbsence.Text)))
                    {
                        str = "alert('The specified folder in approved absence is not available, please ensure the specified folder path is correct.');";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                    }
                if (Convert.ToString(txtAttendancedata.Text) != "")
                    if (!Directory.Exists(Convert.ToString(txtAttendancedata.Text)))
                    {
                        str = "alert('The specified folder in Timecheck - Attendance Data is not available, please ensure the specified folder path is correct.');";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                    }
                if (Convert.ToString(txtempMaster.Text) != "" || Convert.ToString(txtApprovedAttendance.Text) != "" || Convert.ToString(txtApprovedAbsence.Text) != "" || Convert.ToString(txtAttendancedata.Text) != "")
                {
                    objEmployee.InsertFolderPath(Convert.ToString(txtempMaster.Text), Convert.ToString(txtApprovedAttendance.Text), Convert.ToString(txtApprovedAbsence.Text), Convert.ToString(txtAttendancedata.Text), Convert.ToInt32(Session["EmpAutoID"]));
                    bindFolderData();
                    str = "alert('Folder details are saved successfully.');";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                }
            }
        }
    }
}