using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Transactions_NumberofRemainingHoursCount : Base.BasePage
{
    DataSet ds_MaxNoOfHours = new DataSet();
    DataSet ds_HoursAlreadyTaken = new DataSet();
    int EmployeeId;
    string MaxPermitHours, strApplicationDate, gstrDateFormat;
    DateTime permissionDate;
    bool CheckPermissionHours;
    PermissionEventData objPermissionEventData = new PermissionEventData();
    cls_Common objClassCommon = new cls_Common();
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        if (Request.QueryString["EmpId"].ToString() != "" || Request.QueryString["permissionDate"].ToString() != "")
        {
            EmployeeId = Convert.ToInt32(Request.QueryString["EmpId"]);
            //Convert Date Format to MM/dd/yyyy Format
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strApplicationDate = objClassCommon.DateConvertion(gstrDateFormat, Request.QueryString["permissionDate"]);
            permissionDate = Convert.ToDateTime(strApplicationDate);
            CheckPermissionHours = true;
            CheckRemainingHours();
        }
        else
        {
            Response.Clear();
            Response.Write("Fail");
            Response.End();
        }
    }
    public void CheckRemainingHours()
    {
        ds_MaxNoOfHours = objPermissionEventData.GetMaxNoOfHours_ByLeavePolicy(EmployeeId);
        if (ds_MaxNoOfHours.Tables[0].Rows.Count != 0)
        {
            foreach (DataRow dRow in ds_MaxNoOfHours.Tables[0].Rows)
            {
                if (dRow["Permission_MaxHrs_Month"] != null)
                {
                    if (Convert.ToString(dRow["Permission_MaxHrs_Month"]) == "")
                    {
                        CheckPermissionHours = false;
                    }
                    else
                    {
                        MaxPermitHours = dRow["Permission_MaxHrs_Month"].ToString();
                        //Added By manikandan.R
                        //on 22-01-2014
                        CheckPermissionHours = true;
                    }
                }
                else
                {
                    CheckPermissionHours = false;
                    // MaxPermitHours = "00:00";
                }
            }
            //Comment on 21-Aug-07
            //string[] MaxPermissionHours = MaxPermitHours.Split(':');
            // MaxPermitHours = MaxPermissionHours[0] + "." + MaxPermissionHours[1];
            if (CheckPermissionHours == false)
            {
                Response.Write("Fail");
                Response.End();
            }
            else
            {
                ds_HoursAlreadyTaken = objPermissionEventData.GetHoursAlreadyTaken(EmployeeId, permissionDate);
                if (Convert.ToInt32(ds_HoursAlreadyTaken.Tables[0].Rows[0]["InstancesTaken"]) >= Convert.ToInt32(ds_MaxNoOfHours.Tables[0].Rows[0]["Permission_MaxInstances_Month"]))
                {
                    Response.Clear();
                    Response.Write("MaxInstancesMonthExceed");
                    Response.End();
                }
                // Comment on 21-Aug_07 , Because null check in db and values return in hh:mm format
                //TimeSpan diffHours;
                double MaxPermitHours_Mins = ConvertHoursMinsToMinutes(MaxPermitHours);
                double HoursTakenInMinutes_Mins = ConvertHoursMinsToMinutes(ds_HoursAlreadyTaken.Tables[0].Rows[0]["HoursTakenInMinutes"].ToString());
                double diffHours_Mins = MaxPermitHours_Mins - HoursTakenInMinutes_Mins;
                //diffHours = TimeSpan.Parse(ConvertMinutesToHoursMins(diffHours_Mins));
                //DateTime.Parse(MaxPermitHours).Subtract(DateTime.Parse(ds_HoursAlreadyTaken.Tables[0].Rows[0]["HoursTakenInMinutes"].ToString()));
                string s;
                //s = diffHours.ToString();
                s = ConvertMinutesToHoursMins(diffHours_Mins);
                int sReturn = s.LastIndexOf(':');
                string RemainHours = s;
                //s.Remove(sReturn);
                Response.Clear();
                //Change on 21-Aug_08
                // Response.Write(Math.Round(tsDiff.TotalHours,1) + "," + MaxPermitHours);
                Response.Write(RemainHours + "," + MaxPermitHours);
                Response.End();
            }
        }
        else
        {
            Response.Clear();
            Response.Write("Fail");
            Response.End();
        }
    }
    /// <summary>
    /// Added by AMnjula.P on 09-Jan-2014 for 24:00 hours issue if the max permissionhours is 24:00 
    /// </summary>
    /// <param name="inHoursMins"></param>
    /// <returns></returns>
    public int ConvertHoursMinsToMinutes(string inHoursMins)
    {
        try
        {
            int intTotalMinutes = 0, intHours = 0, intMinutes = 0;
            string[] HoursMins = inHoursMins.Split(':');
            intHours = Int16.Parse(HoursMins[0]);
            intMinutes = Int16.Parse(HoursMins[1]);
            intTotalMinutes = (intHours * 60) + intMinutes;
            return (intTotalMinutes);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    // Function to Convert Total Minutes to hh:mm format
    public string ConvertMinutesToHoursMins(double inTotalMinutes)
    {
        string strHoursMins = "00:00";
        try
        {
            double dTotHrs = Math.Floor(inTotalMinutes / 60);
            double dTotMins = inTotalMinutes % 60;
            int iTotHrs = Convert.ToInt32(dTotHrs);
            int iTotMins = Convert.ToInt32(dTotMins);
            if (Convert.ToInt32(dTotMins) >= 60)
            {
                iTotMins = Convert.ToInt32(dTotMins) % 60;
                iTotHrs += Convert.ToInt32(dTotMins) / 60;
            }
            strHoursMins = iTotHrs.ToString("00") + ":" + iTotMins.ToString("00");
        }
        catch (Exception ex)
        {
            throw ex;
        } return (strHoursMins);
    }
}
