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 fWorkTableUser : FCOMMON.fBase
    {
        public fWorkTableUser()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            this.dsMSSQL.EETGW_WorkTableUser.TableNewRow += Holyday_TableNewRow;
            //this.dv1.CellFormatting += dv1_CellFormatting;
        }

        void UpdateGrpList()
        {
            //udpate user list
            var ta = new dsMSSQLTableAdapters.WorkTableGrpTableAdapter();
            var userList = ta.GetData(FCOMMON.info.Login.gcode);// FCOMMON.DBM.getUserList();
            this.cmbGrp.Items.Clear();
            foreach (var user in userList)
            {
                this.cmbGrp.Items.Add(string.Format("{0}", user.grp));//
            }
           
        }
        private void __Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            UpdateGrpList();
            if (cmbGrp.Items.Count > 0) cmbGrp.SelectedIndex = 0;
            //일반사용자의경우에는 상태를 변경하지 못한다.
            int curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.holyday));
            if (curLevel >= 5)
            {
                //권한이 잇으므로 모든 사용자로 한다.
            }
            else
            {
               // this.cmbGrp.Enabled = false;   //사용자를 고칠수 없게 한다.
            }

            RefreshData();
        }
       
        void Holyday_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["gcode"] = FCOMMON.info.Login.gcode;
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["grp"] = cmbGrp.Text;
        }

        string GetUIDValue()
        {
            string uid = "%";
            if (cmbGrp.SelectedIndex > 0)
            {
                uid = cmbGrp.Text.Substring(1, cmbGrp.Text.IndexOf(']') - 1);
            }
            return uid;
        }

        void RefreshData()
        {
            //update data
          
            this.ta.Fill(this.dsMSSQL.EETGW_WorkTableUser, FCOMMON.info.Login.gcode,cmbGrp.Text);


            //색상변경


           // this.dv1.AutoResizeColumns();
        }
   
        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 toolStripButton1_Click(object sender, EventArgs e)
        {
            RefreshData();
        }


        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)
        {
            this.Validate();
            this.bs.EndEdit();
            try
            {
                var cnt = this.ta.Update(this.dsMSSQL.EETGW_WorkTableUser);
                this.dsMSSQL.EETGW_WorkTableUser.AcceptChanges();
                FCOMMON.Util.MsgI(cnt.ToString() + "건의 자료가 저장됨");
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            var newdr = this.dsMSSQL.Holyday.NewHolydayRow();
            newdr.uid = FCOMMON.info.Login.no;
            newdr.gcode = FCOMMON.info.Login.gcode;
            newdr.sdate = DateTime.Now;
            newdr.edate = DateTime.Now;
            newdr.contents = string.Empty;
            newdr.wuid = FCOMMON.info.Login.no;
            newdr.wdate = DateTime.Now;
            this.dsMSSQL.Holyday.AddHolydayRow(newdr);
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            this.bs.RemoveCurrent();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            this.bs.AddNew();
            this.bs.Position = this.bs.Count - 1;
        }

        private void cmbGrp_Click(object sender, EventArgs e)
        {
            //리스트업데이트
            UpdateGrpList();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
