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 FCM0000 
{
    public partial class fRequestItem : FCOMMON.fBase
    {
        public fRequestItem()
        {
            InitializeComponent();
            this.dsMSSQL.RequestItem.TableNewRow += RequestItem_TableNewRow;
        }

        void RequestItem_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["pdate"] = DateTime.Now.ToShortDateString();
            e.Row["qty"] = 1;
        }

        private void fRequestItem_Load(object sender, EventArgs e)
        {
            this.dtSD.Text = DateTime.Now.AddDays(-15).ToShortDateString();
            this.dtED.Text = DateTime.Now.ToShortDateString();
            refreshData();
        }

        private void boardBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            this.tam.UpdateAll(this.dsMSSQL);

        }

        private void fillToolStripButton_Click(object sender, EventArgs e)
        {
           

        }

        void refreshData()
        {
            try
            {
                this.ta.Fill(this.dsMSSQL.RequestItem, dtSD.Text,  dtED.Text);
                this.dv1.AutoResizeColumns();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            refreshData();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            var newdr = this.dsMSSQL.RequestItem.NewRequestItemRow();
            fRequestItem_Add f = new fRequestItem_Add(newdr);
            if (f.ShowDialog() != System.Windows.Forms.DialogResult.OK) newdr.Delete();
            else this.dsMSSQL.RequestItem.AddRequestItemRow(newdr);
        }

        private void label1_Click(object sender, EventArgs e)
        {
            if (label1.Text != "")
                FCOMMON.Util.RunExplorer(label1.Text);
        }

        private void boardDataGridView_DoubleClick(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as dsMSSQL.RequestItemRow;
            var f = new fRequestItem_Add(dr);
            if (f.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                dr.RejectChanges();
        }

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


            //자로에서 불러와서 그 값을 가져온다.s
            string subject = "[AMKOR-K4] 견적문의";
            var taMF = new DSMailTableAdapters.MailFormTableAdapter();
            var data = taMF.GetByCate(FCOMMON.info.Login.gcode, "BY");
            if (data != null && data.Rows.Count > 0)
            {
                var drForm = data.Rows[0] as DSMail.MailFormRow;
                subject = drForm.subject;
            }


            var tolist = 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.Subject = subject; // dr.title;
                newMail.To = to;
                newMail.CC = dr.cclist;
                newMail.BCC = dr.bcclist;
                newMail.HTMLBody = dr.remark
                    .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}", dr.title)
                    .Replace("{QTY}",dr.qty.ToString())
                    .Replace("{MODEL}", dr.model.ToString())
                    .Replace("%7BURL%7D", dr.url.ToString())
                    .Replace("{URL}",dr.url);

                newMail.BodyFormat = OlBodyFormat.olFormatHTML;
                newMail.Display();
                break;
            }
        }

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

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

            //자로에서 불러와서 그 값을 가져온다.s
            string subject = "[AMKOR-K4] 견적문의";
            var taMF = new DSMailTableAdapters.MailFormTableAdapter();
            var data = taMF.GetByCate(FCOMMON.info.Login.gcode, "BY");
            if (data != null && data.Rows.Count > 0)
            {
                var drForm = data.Rows[0] as DSMail.MailFormRow;
                subject = drForm.subject;
            }

            var tolist = 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.Subject = subject; // dr.title;
                newMail.To = to;
                newMail.CC = dr.cclist;
                newMail.BCC = dr.bcclist;
                newMail.HTMLBody = dr.remark
                     .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}", dr.title)
                     .Replace("{QTY}", dr.qty.ToString())
                     .Replace("{MODEL}", dr.model.ToString())
                     .Replace("%7BURL%7D", dr.url.ToString())
                     .Replace("{URL}", dr.url);

                newMail.BodyFormat = OlBodyFormat.olFormatHTML;
                newMail.Display();
            }
        }

        private void dv1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
    }
}
