using FCOMMON;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FCM0000
{
    public partial class fHolidaytable : fBase
    {
        public string Title { get; set; }
       // DataTable dt;

        public fHolidaytable()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.KeyPreview = true;
            this.KeyDown += (s1, e1) => { if (e1.KeyCode == Keys.Escape) this.Close(); };
            this.FormClosed += fHolidaytable_FormClosed;
        }

        void fHolidaytable_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
        
        private void fLovItem_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            this.textBox1.Text = DateTime.Now.ToString("yyyy-MM");
            refreshData();

        }
        void refreshData()
        {
            string month = textBox1.Text.Trim() + "%";
            this.ta.Fill(this.dsMSSQL.HolidayLIst, month);
            if (this.dsMSSQL.HolidayLIst.Rows.Count < 1)
            {
                //자료를 추가해야함
                DateTime sdt = DateTime.Parse(textBox1.Text.Trim() + "-01");
                DateTime edt = sdt.AddMonths(1).AddDays(-1);
                var ts = edt - sdt;
                for (int i = sdt.Day; i <= edt.Day; i++)
                {
                    DateTime dayvalue = DateTime.Parse(textBox1.Text.Trim() + "-" + i.ToString("00"));
                    var newdr = this.dsMSSQL.HolidayLIst.NewHolidayLIstRow();
                    newdr.wuid = FCOMMON.info.Login.no;
                    newdr.wdate = DateTime.Now;
                    newdr.pdate = dayvalue.ToString("yyyy-MM-dd");// textBox1.Text.Trim() + "-" + i.ToString("00");
                    if (dayvalue.DayOfWeek == DayOfWeek.Saturday )
                    {
                        newdr.free = true;
                        newdr.memo = "토요일";                        
                    }
                    else if (dayvalue.DayOfWeek == DayOfWeek.Sunday)
                    {
                        newdr.free = true;
                        newdr.memo = "일요일";
                    }
                    else
                    {
                        newdr.free = false;
                        newdr.memo = "";
                    }
                    this.dsMSSQL.HolidayLIst.AddHolidayLIstRow(newdr);
                }
            }
            refreshStatus();
        }

        void refreshStatus()
        {
            var smsg = "근무일수 : {0} / 휴일 : {1}";            
            int freeday =this.dsMSSQL.HolidayLIst.Where(t=>t.free==true).Count();
            int workday = this.dsMSSQL.HolidayLIst.Rows.Count - freeday;
            lbStatus.Text = string.Format(smsg, workday, freeday);
        }

        private void bs_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void dv_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void itemsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            try
            {
                ta.Update(this.dsMSSQL.HolidayLIst);
                this.dsMSSQL.HolidayLIst.AcceptChanges();
                refreshStatus();
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

        private void dv_DoubleClick(object sender, EventArgs e)
        {
            btSave.PerformClick();
        }

        private void btrefresh_Click(object sender, EventArgs e)
        {
            refreshData();

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btrefresh.PerformClick();
        }
    }
}
