using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                        Created On  : 12-Aug-2007
* Module          : Leave Policy                                     Modified On : 
* Created By      : Arun Vinoth.T
* Modified By     : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          : \\Windev\EIT\Projects\Inprogress\SCREEN1206\code\TimecheckAVBackup
****************************************************************************************************************************/
public partial class Masters_LeavePolicy_RemoteModify : Base.BasePage
{
    EmployeeEventData objEmployee = new EmployeeEventData();
    YearClosureEventdata objYearClosureEventdata = new YearClosureEventdata();
    cls_Common objCommon = new cls_Common();
    int EmpID;
    int intPolicyID;
    string strPolicyName;
    int i;
    ResourceManager rm;
    CultureInfo ci;
    string strdata = "";
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //Session["del_option"] = null;
        GC.Collect();
        //get the culture info to set the language
        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 (Request.QueryString["type"] != null && Request.QueryString["type"] != "")
        {
            int count = 0;
            //Check the Year Closed
            if (Request.QueryString["EmpID"] != null && Request.QueryString["EmpID"] != "")
            {
                EmpID = Convert.ToInt32(Request.QueryString["EmpID"].ToString());
            }
            count = objYearClosureEventdata.CheckYearClosed(EmpID);
            if (count > 0)
            {
                #region PolicyMappedInYearClosure
                GenerateControlsMappedPolicy();
                #endregion
            }
            else
            {
                #region PolicyNotMapped
                int type = Convert.ToInt32(Request.QueryString["type"].ToString());
                Response.Clear();
                if (type == 2)
                {
                    if (Request.QueryString["EmpID"] != null && Request.QueryString["EmpID"] != "" && Request.QueryString["PolicyId"] != null && Request.QueryString["PolicyId"] != "" && Request.QueryString["PolicyName"] != null && Request.QueryString["PolicyName"] != "")
                    {
                        EmpID = Convert.ToInt32(Request.QueryString["EmpID"].ToString());
                        intPolicyID = Convert.ToInt32(Request.QueryString["PolicyId"].ToString());
                        strPolicyName = Convert.ToString(Request.QueryString["PolicyName"].ToString());
                        string strGender = Request.QueryString["Gender"].ToString();
                        int Religion = Convert.ToInt32(Request.QueryString["Religion"].ToString());
                        int EmpCategory = Convert.ToInt32(Request.QueryString["EmployeeCategory"].ToString());
                        int EmpType = Convert.ToInt32(Request.QueryString["EmployeeType"].ToString());
                        int LeavePolicyCount;
                        LeavePolicyCount = objEmployee.ValidateLeavePolicyByFilters(EmpCategory, EmpType, strGender, Religion, intPolicyID);
                        if (LeavePolicyCount == 0)
                        {
                            Response.Write("LeavePolicyNotExists");
                            Response.End();
                        }
                        else
                        {
                            //for new Leave Policy
                            newPolicy();
                        }
                    }
                    //else
                    //{
                    //    //Response.Write("Fail");
                    //    Response.End();
                    //}
                }
                if (type == 1)
                {
                    if (Request.QueryString["EmpID"] != null && Request.QueryString["EmpID"] != "")
                    {
                        EmpID = Convert.ToInt32(Request.QueryString["EmpID"].ToString());
                        // create controls for getting from date & to date
                        GenerateControls();
                    }
                }
                #endregion
            }
        }
    }
    private void GenerateControls()
    {
        DataSet ds_LP = new DataSet();
        ds_LP = objEmployee.select_EmpLeavepolicyDetails(EmpID);
        string str_tblpolicy = "";
        if (ds_LP != null)
        {
            DataTable dt_LP = new DataTable();
            dt_LP = ds_LP.Tables[0];
            if (dt_LP.Rows.Count > 0)
            {
                Session["Rowcount"] = dt_LP.Rows.Count;
                //str_tblpolicy += "<table id='tbl_LeavePolicy'>";
                i = 1;
                foreach (DataRow dr in dt_LP.Rows)
                {
                    string strEmpId = EmpID.ToString();
                    string PolicyId = dr["LeavePolicy_Id"].ToString();
                    string Policy = dr["PolicyName"].ToString();
                    string From = (Convert.ToDateTime(dr["FromDate"].ToString())).ToString();
                    string To = (Convert.ToDateTime(dr["ToDate"].ToString())).ToString();
                    int intmapped = 0;
                    intmapped = objEmployee.CheckLeaveAndPermissionIsExistsWhileDeleteEmpLeavePolicy(EmpID, From, To);
                    From = (Convert.ToDateTime(dr["FromDate"].ToString())).ToString(Session["MDateFormat"].ToString());
                    To = (Convert.ToDateTime(dr["ToDate"].ToString())).ToString(Session["MDateFormat"].ToString());
                    str_tblpolicy += "<div id='tbl_LeavePolicy" + PolicyId + "'>";
                    str_tblpolicy += "<div class='row form-group'><div class='col-md-2'>" + Policy + "</div>";
                    str_tblpolicy += "<div class='col-md-3'>";
                    str_tblpolicy += "<input type='text' value ='" + From + "' ID='txt_From" + i + "' Class='form-control' onpaste='return false' ondrop='return false' oncut='return false' EnableViewState='true' runat='server' MaxLength='10' placeholder='From Date' onmouseover=\"javascript:return DynamicDatePicker('" + Convert.ToString(Session["IDateFormat"]) + "','txt_From" + i + "');\" onblur=\"javascript:return assign_hidden_policy_Mod(this);\"/>";
                    str_tblpolicy += "</div>";
                    //str_tblpolicy += "<input type=\"image\" src=\"../Images/dateicon.gif\" name=\"buttxt_From" + i + "\" onclick=\"Calendar.setup({inputField     :    'txt_From" + i + "',ifFormat       :    '" + Convert.ToString(Session["IDateFormat"]) + "',showsTime      :    false, button         :    'buttxt_From" + i + "',  singleClick    :    true,   step           :    1 });return false\"/></td>";
                    str_tblpolicy += "<div class='col-md-3'>";
                    str_tblpolicy += "<input type='text' value ='" + To + "' ID='txt_To" + i + "' Class='form-control' onpaste='return false' ondrop='return false' oncut='return false' EnableViewState='true' runat='server' MaxLength='10' placeholder='To Date' onmouseover=\"javascript:return DynamicDatePicker('" + Convert.ToString(Session["IDateFormat"]) + "','txt_To" + i + "');\" onblur=\"javascript:return assign_hidden_policy_Mod(this);\"/>";
                    str_tblpolicy += "</div>";
                    string strStyle = Request.Browser.Browser.ToLower().Contains("firefox") ? "style=\"margin-top:0px;\"" : string.Empty;
                    //str_tblpolicy += "<input type=\"image\" src=\"../Images/dateicon.gif\" name=\"buttxt_To1" + i + "\" onclick=\"Calendar.setup({inputField     :    'txt_To" + i + "',ifFormat       :    '" + Convert.ToString(Session["IDateFormat"]) + "',showsTime      :    false, button         :    'buttxt_To" + i + "',  singleClick    :    true,   step           :    1 });return false\"/></td>";
                    if (intmapped > 0)
                    {
                        str_tblpolicy += "<div class='col-md-3 pull-left-button'><input Class=\"btn save-btn\" id=\"Del_Button" + i + "\" type=\"button\" disabled value=\"" + rm.GetString("btn_Delete", ci) + "\" runat=\"server\" OnClick=\"javascript:return removeRow(" + PolicyId + ",this, " + strEmpId + ");\"" + strStyle + "/><div runat='server' id='div_" + i + "' style='display:none' >" + PolicyId + "</div></div>";
                    }
                    else
                    {
                        str_tblpolicy += "<div class='col-md-3 pull-left-button'><input Class=\"btn save-btn\" id=\"Del_Button" + i + "\" type=\"button\" value=\"" + rm.GetString("btn_Delete", ci) + "\" runat=\"server\" OnClick=\"javascript:return removeRowModify(" + PolicyId + ",this, " + strEmpId + ");\"" + strStyle + "/><div runat='server' id='div_" + i + "' style='display:none'>" + PolicyId + "</div></div>";
                        //str_tblpolicy += "<td class='td' align='center'><input runat='server' Class=\"but\" id=\"Del_Button" + i + "\" type=\"button\" value=\"" + rm.GetString("btn_Delete", ci) + "\" runat=\"server\" OnClick=\"javascript:return removeRow(" + intPolicyID + ",this," + EmpID + ");\"/><div runat='server' id='div_" + i + "' style='display:none' >" + intPolicyID + "</div></td><tr>";
                    }
                    str_tblpolicy += "</div></div>";
                    i++;
                }
                Session["Rowcount"] = i;
                //str_tblpolicy += "<INPUT type='hidden' id='hdf_RowsMod_old' runat='server' value='" + Session["Rowcount"] + "'/>";

                //appending Leave policy details & stored in hidden field while loading modify page for  delete option
                foreach (DataRow dr in dt_LP.Rows)
                {
                    if (strdata == "")
                    {
                        strdata += (Convert.ToDateTime(dr["FromDate"].ToString())).ToString(Session["MDateFormat"].ToString()) + "," + (Convert.ToDateTime(dr["ToDate"].ToString())).ToString(Session["MDateFormat"].ToString()) + "," + dr["LeavePolicy_Id"].ToString();
                    }
                    else
                    {
                        strdata += "*" + (Convert.ToDateTime(dr["FromDate"].ToString())).ToString(Session["MDateFormat"].ToString()) + "," + (Convert.ToDateTime(dr["ToDate"].ToString())).ToString(Session["MDateFormat"].ToString()) + "," + dr["LeavePolicy_Id"].ToString();
                    }
                }
                //Session["del_option"] = strdata;
            }
            ds_LP.Dispose();
        }
        //str_tblpolicy += "<hr color='#EAEAEA' size='1'>";
        //leave policy - leave details
        DataSet ds_LPL = new DataSet();
        ds_LPL = objEmployee.select_EmpLeavepolicyLeaveDetails(EmpID);
        if (ds_LPL != null)
        {
            DataTable dt_LPL = new DataTable();
            dt_LPL = ds_LPL.Tables[0];
            if (ds_LPL.Tables[0].Rows.Count > 0)
            {
                str_tblpolicy += "<div id='div_LeavePolicydetails'><table id='tbl_LeavePolicydetails' class='table das-table1 TableWidth1'>";
                int policyID = 0;
                int introw;
                introw = dt_LPL.Rows.Count;
                foreach (DataRow dr1 in dt_LPL.Rows)
                {
                    string PolicyName = dr1["PolicyName"].ToString();
                    if (policyID == 0)
                    {
                        str_tblpolicy += "<tr><th class='empheader pull-left-button' colspan=" + introw + " style='padding:5px 10px 0px 10px'><label>" + rm.GetString("LeavePermissionPolicy_PolicyName", ci) + "</label> : <b>" + PolicyName + "</b></th></tr><tr>";
                    }
                    if (policyID != 0 && policyID != Convert.ToInt32(dr1["Emp_LeavePolicyId"]))
                    {
                        str_tblpolicy += "</tr></table><br/><table id='tbl_LeavePolicydetails" + policyID + "' class='table das-table1 TableWidth1'><tr><th class='empheader pull-left-button' colspan=" + introw + " style='padding:5px 10px 0px 10px'><label>" + rm.GetString("LeavePermissionPolicy_PolicyName", ci) + "</label> : <b>" + PolicyName + "</b></th></tr><tr>";
                    }
                    if (policyID == 0 || policyID != Convert.ToInt32(dr1["Emp_LeavePolicyId"]))
                    {
                        policyID = Convert.ToInt32(dr1["Emp_LeavePolicyId"]);
                    }
                    string LeaveType = dr1["LeaveType"].ToString();
                    string No_Of_Days = dr1["No_Of_Days"].ToString();
                    str_tblpolicy += "<td><b>" + LeaveType + "</b><br>";
                    str_tblpolicy += "<br>" + No_Of_Days + "</td>";
                }
                str_tblpolicy += "</tr></table></div><br/> ~#~" + Session["Rowcount"] + "";
                str_tblpolicy += "~#~" + strdata;
            }
            ds_LPL.Dispose();
        }
        Response.Write(str_tblpolicy);
        Response.End();
    }
    private void newPolicy()
    {
        // creating controls for new leave policy
        i = Convert.ToInt32(Session["Rowcount"]);
        i++;
        string str_tblpolicy = "";
        str_tblpolicy += "<div id='tbl_LeavePolicynew" + intPolicyID + "'>";
        str_tblpolicy += "<div class='row form-group'><div class='col-md-2'>" + strPolicyName + "</div>";
        str_tblpolicy += "<div class='col-md-3'>";
        str_tblpolicy += "<input type='text' Style='word-wrap: break-word;'  ID='txt_From" + i + "' Class='form-control' onpaste='return false' EnableViewState='true' runat='server' MaxLength='10' onmouseover=\"javascript:return DynamicDatePicker('" + Convert.ToString(Session["IDateFormat"]) + "','txt_From" + i + "');\" placeholder='From Date' onblur=\"javascript:return assign_hidden_policy_Mod(this);\"/>";
        str_tblpolicy += "</div>";
        //str_tblpolicy += "<input type=\"image\" src=\"../Images/dateicon.gif\" name=\"buttxt_From" + i + "\" onclick=\"Calendar.setup({inputField     :    'txt_From" + i + "',ifFormat       :    '"+ Convert.ToString(Session["IDateFormat"]) +"',showsTime      :    false, button         :    'buttxt_From" + i + "',  singleClick    :    true,   step           :    1 });return false\"/></td>";
        str_tblpolicy += "<div class='col-md-3'>";
        str_tblpolicy += "<input type='text'  ID='txt_To" + i + "' Class='form-control' onpaste='return false' EnableViewState='true' runat='server' MaxLength='10' onmouseover=\"javascript:return DynamicDatePicker('" + Convert.ToString(Session["IDateFormat"]) + "','txt_To" + i + "');\" placeholder='To Date'  onblur=\"javascript:return assign_hidden_policy_Mod(this);\"/>";
        str_tblpolicy += "</div>";
        //        str_tblpolicy += "<input type=\"image\" src=\"../Images/dateicon.gif\" name=\"buttxt_To1" + i + "\" onclick=\"Calendar.setup({inputField     :    'txt_To" + i + "',ifFormat       :    '" + Convert.ToString(Session["IDateFormat"]) + "',showsTime      :    false, button         :    'buttxt_To" + i + "',  singleClick    :    true,   step           :    1 });return false\"/></td>";
        //str_tblpolicy += "<td class='td' align='center' width='30%'><input Class=\"but\" id=\"Del_Button" + i + "\" type=\"button\" value=\"" + rm.GetString("btn_Delete", ci) + "\" runat=\"server\" OnClick=\"javascript:return removeRow(" + intPolicyID + ",this," + EmpID + ");\"/><div id='div_" + i + "' style=display:'none' >" + intPolicyID + "</div></td><tr>";
        string strStyle = Request.Browser.Browser.ToLower().Contains("firefox") ? "style=\"margin-top:-2px;\"" : string.Empty;
        str_tblpolicy += "<div class='col-md-2'><input runat='server' Class=\"btn save-btn\" id=\"Del_Button" + i + "\" type=\"button\" value=\"" + rm.GetString("btn_Delete", ci) + "\" runat=\"server\" OnClick=\"javascript:return removeRow(" + intPolicyID + ",this," + EmpID + ");\"" + strStyle + "/><div runat='server' id='div_" + i + "' style='display:none' >" + intPolicyID + "</div></div>";
        //str_tblpolicy += "<td class='td' align='center' width='10%'><input runat='server' Class=\"but\" id=\"Del_Button" + i + "\" type=\"button\" value=\"" + rm.GetString("btn_Delete", ci) + "\" runat=\"server\" OnClick=\"javascript:return removeRow(" + intPolicyID + ",this," + EmpID + ");\"/><div runat='server' id='div_" + i + "' style='display:none' >" + intPolicyID + "</div></td><tr>";
        Session["Rowcount"] = i;
        str_tblpolicy += "</div ></div>~#~" + Session["Rowcount"] + "";
        //str_tblpolicy += "<INPUT type='hidden' id='hdf_RowsMod' runat='server' value='" + Session["Rowcount"] + "'/>";
        Response.Write(str_tblpolicy);
        Response.End();
    }
    private void GenerateControlsMappedPolicy()
    {
        Response.Clear();
        DataSet ds_LP = new DataSet();
        ds_LP = objEmployee.select_EmpLeavepolicyDetails(EmpID);
        DataTable dt_LP = new DataTable();
        dt_LP = ds_LP.Tables[0];
        string str_tblpolicy = "";
        str_tblpolicy += "<table id='tbl_LeavePolicy'>";
        str_tblpolicy += "<tr><td class='ModuleHeadtd pull-left-button' width='30%'>" + rm.GetString("Employee_LeavePolicy", ci) + "</td>";
        str_tblpolicy += "<td class='ModuleHeadtd' width='30%'>" + rm.GetString("email_FromDate", ci) + "</td>";
        str_tblpolicy += "<td class='ModuleHeadtd' width='30%'>" + rm.GetString("email_ToDate", ci) + "</td></tr>";
        foreach (DataRow dr in dt_LP.Rows)
        {
            string Policy = dr["PolicyName"].ToString();
            string From = (Convert.ToDateTime(dr["FromDate"].ToString())).ToString(Session["MDateFormat"].ToString());
            string To = (Convert.ToDateTime(dr["ToDate"].ToString())).ToString(Session["MDateFormat"].ToString());
            str_tblpolicy += "<tr><td class='td pull-left-button' width='30%'>" + Policy + "</td>";
            str_tblpolicy += "<td class='td' width='30%'>" + From + "</td>";
            str_tblpolicy += "<td class='td' width='30%'>" + To + "</td></tr>";
        }
        str_tblpolicy += "</table >";
        ds_LP.Dispose();
        //str_tblpolicy += "<hr color='#EAEAEA' size='1'>";
        // leave policy leave details
        DataSet ds_LPL = new DataSet();
        ds_LPL = objEmployee.select_EmpLeavepolicyLeaveDetails(EmpID);
        DataTable dt_LPL = new DataTable();
        dt_LPL = ds_LPL.Tables[0];
        str_tblpolicy += "<div id='div_LeavePolicydetails'><table id='tbl_LeavePolicydetails' class='table das-table1 TableWidth1'>";
        int policyID = 0;
        int introw;
        introw = dt_LPL.Rows.Count;
        foreach (DataRow dr1 in dt_LPL.Rows)
        {
            string PolicyName = dr1["PolicyName"].ToString();
            if (policyID == 0)
            {
                str_tblpolicy += "<tr><th class='empheader pull-left-button' colspan=" + introw + " style='padding:5px 10px 0px 10px'><label>" + rm.GetString("Caption_Policy", ci) + "</label> : <b>" + PolicyName + "</b></th></tr><tr>";
            }
            if (policyID != 0 && policyID != Convert.ToInt32(dr1["Emp_LeavePolicyId"]))
            {
                str_tblpolicy += "</tr></table><br/><table id='tbl_LeavePolicydetails" + policyID + "' class='table das-table1 TableWidth1'><tr><th class='empheader pull-left-button' colspan=" + introw + " style='padding:5px 10px 0px 10px'><label>" + rm.GetString("Caption_Policy", ci) + "</label> : <b>" + PolicyName + "</b></th></tr><tr>";
            }
            if (policyID == 0 || policyID != Convert.ToInt32(dr1["Emp_LeavePolicyId"]))
            {
                policyID = Convert.ToInt32(dr1["Emp_LeavePolicyId"]);
            }
            string LeaveType = dr1["LeaveType"].ToString();
            string No_Of_Days = dr1["No_Of_Days"].ToString();
            str_tblpolicy += "<td class='td'><b>" + LeaveType + "</b><br>";
            str_tblpolicy += "<br>" + No_Of_Days + "</td>";
        }
        str_tblpolicy += "</tr></table></div><br/>";
        Response.Write(str_tblpolicy + "~#~" + "MappedPolicy@~%");
        Response.End();
    }
}
