using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Globalization;

namespace CheckSheet
{
    public class CenterFunction
    {
        public static DataTable dt_check_dayoff_type(string year,string month,string type)
        {
            DataTable dt = new DataTable();
            using(FcSql.Function fc = new FcSql.Function())
            {

                string str_dayoff = string.Format("select * from ( " +
                " select dayoff from dayoff  where flagdel=0 " +
                " union " +
                " select * from dbo.listweekend_of_year('{0}') " +
                " union " +
                " select dayoff from CalendarPlan Where FlagDel=0 and Check_type='{1}') as temp	 Where month(dayoff)={2} and year(dayoff)={3} ", year + "0101", type, month, year);
                dt = fc.SQL_DataTable(str_dayoff);



            }

            return dt;

        }

        public static string str_check_dayoff_type(string year, string month, string type)
        {
            string query_dayoff = string.Empty;
            using (FcSql.Function fc = new FcSql.Function())
            {

                query_dayoff = string.Format("select * from ( " +
                " select dayoff from dayoff  where flagdel=0 " +
                " union " +
                " select * from dbo.listweekend_of_year('{0}') " +
                " union " +
                " select dayoff from CalendarPlan Where FlagDel=0 and Check_type='{1}') as temp	 Where month(dayoff)={2} and year(dayoff)={3} ", year + "0101", type, month, year);
            
            }

            return query_dayoff;

        }


        public static string check_dayoff(string stryear, string ddate)
        {
            string dvalue = string.Empty;
            using (FcSql.Function fc = new FcSql.Function())
            {

                string str_dayoff = string.Format("select * from ( " +
                " select dayoff from dayoff  where flagdel=0 " +
                " union " +
                " select * from dbo.listweekend_of_year('{0}')) as temp Where dayoff = '{1}' ", stryear, ddate);
                DataTable dt = fc.SQL_DataTable(str_dayoff);
                if(dt.Rows.Count>0)
                {

                    for (int i = 1; i <= 5; i++)
                    {

                        str_dayoff = string.Format("select * from ( " +
                        " select dayoff from dayoff  where flagdel=0 " +
                        " union " +
                        " select * from dbo.listweekend_of_year('{0}')) as temp Where dayoff = DateAdd(Day,-{2},'{1}') ", stryear, ddate, i.ToString());
                        dt = fc.SQL_DataTable(str_dayoff);

                        if(dt.Rows.Count==0)
                        {
                            double ival = -1 * i;
                            dvalue = DateTime.Parse(ddate.ToString()).AddDays(ival).ToString("yyyy-MM-dd", new CultureInfo("en-US"));
                            break;


                        }


                    }


                }
                else
                {

                    dvalue = ddate;

                }

                fc.closeconnection();

            }

            return dvalue;
        }



    }
}