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 FEQ0000
{
    public partial class fMonthAmount : fBase
    {
        public string Title { get; set; }
        // DataTable dt;

        public fMonthAmount()
        {
            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.tbYear.Text = DateTime.Now.Year.ToString("0000");//.ToString("yyyy-MM");
            refreshData();

        }
        void refreshData()
        {
            string month = tbYear.Text.Trim() + "%";
            this.ta.Fill(this.dsMSSQL.EETGW_MonPurchaseAmount, FCOMMON.info.Login.gcode, month);
            if (this.dsMSSQL.EETGW_MonPurchaseAmount.Rows.Count < 1)
            {
                //자료를 추가해야함
                //DateTime sdt = DateTime.Parse(tbYear.Text.Trim() + "-01");
                //DateTime edt = sdt.AddMonths(1).AddDays(-1);
                //var ts = edt - sdt;

            }

            //없느낮료생성
            if (dsMSSQL.EETGW_MonPurchaseAmount.Where(t => t.pdate.Equals(tbYear.Text.Trim())).Any() == false)
            {
                var newdr = this.dsMSSQL.EETGW_MonPurchaseAmount.NewEETGW_MonPurchaseAmountRow();
                newdr.wuid = FCOMMON.info.Login.no;
                newdr.wdate = DateTime.Now;
                newdr.gcode = FCOMMON.info.Login.gcode;
                newdr.pdate = tbYear.Text.Trim();// dayvalue.Year.ToString() + $"-{i:00}";
                this.dsMSSQL.EETGW_MonPurchaseAmount.AddEETGW_MonPurchaseAmountRow(newdr);
            }

            for (int i = 1; i <= 12; i++)
            {
                DateTime dayvalue = DateTime.Parse(tbYear.Text.Trim() + "-01-01");
                var pdate = dayvalue.Year.ToString() + $"-{i:00}";
                if (this.dsMSSQL.EETGW_MonPurchaseAmount.Where(t => t.pdate.Equals(pdate)).Any() == false)
                {
                    var newdr = this.dsMSSQL.EETGW_MonPurchaseAmount.NewEETGW_MonPurchaseAmountRow();
                    newdr.wuid = FCOMMON.info.Login.no;
                    newdr.wdate = DateTime.Now;
                    newdr.gcode = FCOMMON.info.Login.gcode;
                    newdr.pdate = pdate;// dayvalue.Year.ToString() + $"-{i:00}";
                    this.dsMSSQL.EETGW_MonPurchaseAmount.AddEETGW_MonPurchaseAmountRow(newdr);
                }
            }


            refreshStatus();
        }

        void refreshStatus()
        {
            //var smsg = "근무일수 : {0} / 휴일 : {1}";            
            //int freeday =this.dsMSSQL.EETGW_MonPurchaseAmount.Where(t=>t.free==true).Count();
            //int workday = this.dsMSSQL.EETGW_MonPurchaseAmount.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.EETGW_MonPurchaseAmount);
                this.dsMSSQL.EETGW_MonPurchaseAmount.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();
            tbYear.Focus();
            tbYear.SelectAll();
        }

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