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

namespace FCM0000
{
    public partial class fSendMail : FCOMMON.fBase
    {
       

        public fSendMail()
        {
            InitializeComponent();
            PredefinedButtonSets.SetupDefaultButtons(this.tbBody);
            this.dsMSSQL.MailData.TableNewRow += MailForm_TableNewRow;
        }

        void MailForm_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["cate"] = "NR";
        }

        private void fMailform_Load(object sender, EventArgs e)
        {
            this.bs.AddNew();
            LoadNRData();
        }

        void LoadNRData()
        {
            //자로에서 불러와서 그 값을 가져온다.
            DSMailTableAdapters.MailFormTableAdapter taMF = new DSMailTableAdapters.MailFormTableAdapter();
            var data = taMF.GetByCate(FCOMMON.info.Login.gcode, "NR");
            if (data != null && data.Rows.Count > 0)
            {
                var drForm = data.Rows[0] as DSMail.MailFormRow;
                this.tbSubject.Text = drForm.subject;
                this.tbBody.Html = drForm.body;
                this.tbTo.Text = drForm.tolist;
                this.tbBCC.Text = drForm.bcc;
                this.tbCC.Text = drForm.cc;
            }
        }

        private void mailFormBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            var drv = this.bs.Current as DataRowView;
            drv["body"] = this.tbBody.Html;
            drv.EndEdit();
            this.bs.EndEdit();
            this.tam.UpdateAll(this.dsMSSQL);
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;

            var dr = drv.Row as DSMail.MailFormRow;
            var tolist = new string[] { "Chikyun.kim@amkor.co.kr" }; //dr.tolist.Split(',');
            Outlook.Application outlookApplication = new Outlook.Application();
            foreach (var to in tolist)
            {
                if (to.isEmpty()) continue;
                var newMail = outlookApplication.CreateItem(OlItemType.olMailItem) as Outlook.MailItem;
                newMail.Display();
                newMail.Subject = this.tbSubject.Text.Trim(); // dr.title;
                newMail.To = to;
                newMail.CC = tbCC.Text.Trim();
                newMail.BCC = tbBCC.Text.Trim();
               // newMail.BodyFormat = OlBodyFormat.olFormatHTML;
                newMail.HTMLBody = this.tbBody.Html
                     .Replace("{USER}", FCOMMON.info.Login.nameK)
                     .Replace("{EUSER}", FCOMMON.info.Login.nameE)
                     .Replace("{EMAIL}", FCOMMON.info.Login.email)
                     .Replace("%7BEMAIL%7D", FCOMMON.info.Login.email)
                     .Replace("{HP}", FCOMMON.info.Login.hp)
                     .Replace("{TEL}", FCOMMON.info.Login.tel)
                     .Replace("{ITEM}", tbSubject.Text) + newMail.HTMLBody;

              
               
            }
        }
    }
}
