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 FBS0000
{
    public partial class fHolyday_Add : FCOMMON.fBase
    {
        Boolean bInit = false;
        dsMSSQL.HolydayRow dr;

        public fHolyday_Add(dsMSSQL.HolydayRow dr_)
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            this.dr = dr_;
            //if (idx == -1)
            //{
            //    dr = this.dsMSSQL.Holyday.NewHolydayRow();
            //    dr.sdate = DateTime.Parse(DateTime.Now.Year.ToString() + "-01-01");
            //    dr.edate = dr.sdate;
            //    dr.wuid = FCOMMON.info.Login.no;
            //    dr.wdate = DateTime.Now;
            //    dr.term = 0.0;
            //    dr.cate = "10";
            //    dr.gcode = FCOMMON.info.Login.gcode;
            //    dr.uid = FCOMMON.info.Login.no;
            //    this.dsMSSQL.Holyday.AddHolydayRow(dr);
            //}
            //else
            //{
            //    ta.FillByIdx(this.dsMSSQL.Holyday, FCOMMON.info.Login.gcode, idx);
            //    this.dr = this.dsMSSQL.Holyday.Rows[0] as dsMSSQL.HolydayRow;
            //    if (dr.term > 0 || dr.CrTime > 0) radCR.Checked = true;
            //    else radDR.Checked = true;
            //}

            //사용자 목록 업데이트
            this.cmbUser.DataSource = FCOMMON.DBM.getActiveUserTable();
            this.cmbUser.DisplayMember = "dispName";
            this.cmbUser.ValueMember = "id";
            // this.bs.DataSource = dr;

            var catelist = FCOMMON.DBM.getCodeList("50");
            var lst = catelist.Select(t => t.Value).ToArray();
            cmbType.Items.Clear();
            foreach (var item in lst)
                cmbType.Items.Add(item);


        }


        private void __Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            cmbUser.SelectedValue = dr.uid;// FCOMMON.info.Login.no;// .Text = FCOMMON.info.Login.nameK + "(" + FCOMMON.info.Login.no + ")";
            dateTimePicker1.Value = dr.sdate;
            dateTimePicker2.Value = dr.edate;

            cmbType.Text = dr.cate;
            contentsTextBox.Text = dr.contents;

            if (dr.IsiwolNull()) checkBox1.Checked = false;
            else checkBox1.Checked = dr.iwol;

            this.nudCrDAy.Value = (decimal)dr.term;
            this.nudCrHour.Value = (decimal)dr.CrTime;

            this.nudDrDay.Value = (decimal)dr.termDr;
            this.nudDrHour.Value = (decimal)dr.DrTime;

            bInit = true;

            if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
            if (this.RestoreBounds.X + this.RestoreBounds.Width < 300 && this.RestoreBounds.X < 0)
                this.Location = new Point(0, 0);

        }

        private void btOK_Click(object sender, EventArgs e)
        {

            //기본값처리
            this.Validate();

            if(string.IsNullOrEmpty( this.cmbType.Text))
            {
                FCOMMON.Util.MsgE("구분을 입력하세요");
                cmbType.Focus();
                return;
            }


            var vs = dateTimePicker1.Value;
            var ve = dateTimePicker2.Value;
            if(ve < vs)
            {
                FCOMMON.Util.MsgE("종료일이 시작일보다 작습니다");
                return;
            }

            var usertxt = cmbUser.Text.Trim();
            var userid = usertxt.Substring(usertxt.IndexOf('(')+1, usertxt.LastIndexOf(')') - usertxt.IndexOf('(') -1);

            if(string.IsNullOrEmpty(userid))
            {
                FCOMMON.Util.MsgE("담당자가 선택되지 않았습니다");
                return;
            }
            //시작일 마감여부
            var smon = dateTimePicker1.Value.ToString("yyyy-MM");
            if(FCOMMON.DBM.GetMagamStatus(smon))
			{
                FCOMMON.Util.MsgE("시작일이 속한 월이 마감되었습니다\n마감된 월에는 [추가/변경/삭제] 를 할 수 없습니다");
                return;
			}
            var emon = dateTimePicker2.Value.ToString("yyyy-MM");
            if (FCOMMON.DBM.GetMagamStatus(emon))
            {
                FCOMMON.Util.MsgE("종료일이 속한 월이 마감되었습니다\n마감된 월에는 [추가/변경/삭제] 를 할 수 없습니다");
                return;
            }

            dr.uid = userid;// cmbType.SelectedValue.ToString();

            dr.sdate = dateTimePicker1.Value;
           // dr.edate = dr.sdate;
            dr.edate = dateTimePicker2.Value;
            dr.iwol = this.checkBox1.Checked;

            dr.cate = cmbType.Text;
            dr.contents = contentsTextBox.Text;

            dr.term = (double)this.nudCrDAy.Value;
            dr.CrTime = (double)this.nudCrHour.Value;

            dr.termDr = (double)this.nudDrDay.Value;
            dr.DrTime = (double)this.nudDrHour.Value;


            this.dr.EndEdit();

            DialogResult = DialogResult.OK;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (!bInit) return;


            //if(dateTimePicker1.Value > this.dateTimePicker2.Value)
            //    this.dateTimePicker2.Value = dateTimePicker1.Value;
            RefreshTerm();


        }

        void RefreshTerm()
        {
            //var ts = dateTimePicker2.Value - dateTimePicker1.Value;
            //var days = Math.Ceiling(ts.TotalDays) + 1;
            //this.numericUpDown1.Value = (decimal)days;
            //this.dr.term = days;
        }
        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            if (!bInit) return;
            RefreshTerm();
        }
    }
}
