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 fHolydayJobReport : FCOMMON.fBase
    {
        string p_uid = string.Empty;
        public fHolydayJobReport(string sd,string ed   ,string uid  )
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            this.tbSD.Text = sd;
            this.tbED.Text = ed;
            p_uid = uid; 
        }
   
        private void __Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            //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 == p_uid) 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)
            {
                //권한이 잇으므로 모든 사용자로 한다.
                if (cmbUser.Items.Count > 0)
                    cmbUser.SelectedIndex = 0;
              
            }
            else
            {
                this.cmbUser.Enabled = false;   //사용자를 고칠수 없게 한다.
            }

            RefreshData();
        }

       


        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 sumdrD = this.dsMSSQL.vHoliday_uselist.Sum(t => t.term);    //전체발생수량
           
            sbUse.Text = string.Format("{0}day,{1}hour", sumdrD/8.0f,sumdrD);  //사용량
          
            //색상변경


            this.dv1.AutoResizeColumns();
        }
    

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

        private void dv1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        
        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);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            FCOMMON.Util.MsgI("준비중");
        }

    }
}
