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;
using NetOffice;
using Outlook = NetOffice.OutlookApi;
using NetOffice.OutlookApi.Enums;

namespace FBS0000
{
    public partial class fHolyday : FCOMMON.fBase
    {
        public fHolyday()
        {
            InitializeComponent();
            this.dsMSSQL.vHoliday_uselist.TableNewRow += Holyday_TableNewRow;
            this.dv1.CellFormatting += dv1_CellFormatting;
        }

        void dv1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
            var col = this.dv1.Columns[e.ColumnIndex];
            var cate = dv1.Rows[e.RowIndex].Cells["dvc_cate"].Value.ToString();
            if (cate == "0") dv1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Blue;// = Color.Gray;
            else dv1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
        }

        private void __Load(object sender, EventArgs e)
        {

            tbSD.Text = DateTime.Now.Year.ToString("0000") + "-01-01";
            tbED.Text = DateTime.Now.AddDays(10).ToShortDateString();

            //var userlist = FCOMMON.DBM.getUserTable();
            //dvc_user.DataSource = userlist;
            //dvc_user.ValueMember = "Key";
            //dvc_user.DisplayMember = "Value";

            //var cateList = FCOMMON.DBM.getCodeTable("02");
            //this.dvc_cate.DataSource = cateList;
            //dvc_cate.ValueMember = "Key";
            //dvc_cate.DisplayMember = "Value";

            //var rrltList = FCOMMON.DBM.getCodeTable("03");
            //this.dvc_result.DataSource = rrltList;
            //dvc_result.ValueMember = "Key";
            //dvc_result.DisplayMember = "Value";

            //udpate user list
            var userList = FCOMMON.DBM.getUserList();
            this.cmbUser.Items.Clear();
            this.cmbUser.Items.Add("-- ALL --");
            int curuserindex = -1;
            foreach (var user in userList)
            {
                this.cmbUser.Items.Add(string.Format("[{0}] {1}", user.Key, user.Value));
                if (user.Key == FCOMMON.info.Login.no) curuserindex = cmbUser.Items.Count - 1;
            }
            this.cmbUser.SelectedIndex = curuserindex;

            //일반사용자의경우에는 상태를 변경하지 못한다.
            int curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.holyday));
            if (curLevel >= 5)
            {
                //권한이 잇으므로 모든 사용자로 한다.
            }
            else
            {
                this.cmbUser.Enabled = false;   //사용자를 고칠수 없게 한다.
            }

            RefreshData();
        }

        void Holyday_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["uid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["cate"] = "0"; //기본 신청
            e.Row["pdate"] = DateTime.Now;
            e.Row["termDr"] = 1.0f;
            e.Row["contents"] = string.Empty;
        }


        void RefreshData()
        {
            //update data
            string uid = "%";
            if (cmbUser.SelectedIndex > 0)
            {
                uid = cmbUser.Text.Substring(1, cmbUser.Text.IndexOf(']') - 1);
            }
            var sd = DateTime.Parse(tbSD.Text + " 00:00:00");
            var ed = DateTime.Parse(tbED.Text + " 23:59:59");
            this.ta.Fill(this.dsMSSQL.vHoliday_uselist,uid,tbSD.Text,tbED.Text,FCOMMON.info.Login.gcode);


            //잔량계산
            double sumdr = this.dsMSSQL.vHoliday_uselist.Sum(t => t.termDr);
            double sumcr = this.dsMSSQL.vHoliday_uselist.Sum(t => t.term);
            double jan = sumdr - sumcr;
            sbGen.Text = sumdr.ToString("N0");
            sbUse.Text = sumcr.ToString("N0");
            sbJan.Text = jan.ToString("N0");
            if (jan < 0) sbJan.ForeColor = Color.Red;
            else sbJan.ForeColor = Color.Blue;

            //색상변경


            this.dv1.AutoResizeColumns();
        }
        private void boardBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            var dlg = FCOMMON.Util.MsgQ("변경된 내용을 저장하시겠습니까?");
            if (dlg != System.Windows.Forms.DialogResult.Yes) return;
            try
            {
               
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            
            var f = new fHolyday_Add();
            f.TopMost = true;
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                RefreshData();
            }
        }

        private void autoResizeComlumnsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dv1.AutoResizeColumns();
        }

        private void dv1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dv1_DoubleClick(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as dsMSSQL.vHoliday_uselistRow;
            if(dr.cate == "0")
            {
                var f = new fHolyday_Add(dr.idx);
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    RefreshData();
                }
            }
            else
            {
                FCOMMON.Util.MsgI("이 데이터는 업무일지의 데이터가 입니다.\n\n" +
                    "변경하려면 업무일지의 자료를 수정하세요" );
            }
            
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void tbSD_KeyDown(object sender, KeyEventArgs e)
        {
            var tb = sender as ToolStripTextBox;
            if (e.KeyCode == Keys.Enter)
            {
                string datestring;
                if (FCOMMON.Util.MakeDateString(tb.Text, out datestring))
                {
                    tb.Text = datestring;
                    SendKeys.Send("{TAB}");
                }
            }
        }

        private void sendToMailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as dsMSSQL.HolydayRow;

            //string to = dr.tolist;
            //string body = "<html><body>test 2<strong>sfd</strong>nice to meet</body></html>";
            //string bcc = string.Empty;
            //string cc = string.Empty;
            //string subject = "etst mail";

            Outlook.Application outlookApplication = new Outlook.Application();
            Outlook.MailItem newTask = outlookApplication.CreateItem(OlItemType.olMailItem) as Outlook.MailItem;
            newTask.Subject = "mail test";
            newTask.To = "Chikyun.Kim@amkor.co.kr";
            newTask.HTMLBody = "<i><b>this is test mail</b></i>";
            newTask.BodyFormat = OlBodyFormat.olFormatHTML;
            newTask.Display();

            //FCOMMON.Util.RunDefaultMail(to, subject, body, cc, bcc);
        }

    }
}
