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;

namespace FCM0000
{
    public partial class fMailform : FCOMMON.fBase
    {
        public fMailform()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            PredefinedButtonSets.SetupDefaultButtons(this.htmlEditor1);
            this.dsMSSQL.MailForm.TableNewRow += MailForm_TableNewRow;
        }

        void MailForm_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["subject"] = "메일제목";
            e.Row["title"] = "분류제목";
            e.Row["cate"] = "--";
            e.Row["gcode"] = FCOMMON.info.Login.gcode;
        }

        private void fMailform_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            refreshData();
        }
       
        void refreshData()
        {
            try
            {
                ta.Fill(this.dsMSSQL.MailForm, FCOMMON.info.Login.gcode);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

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

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
           
        }
        DSMail.MailFormRow dr = null;
        private void bs_CurrentChanged(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            dr = drv.Row as DSMail.MailFormRow;
            this.htmlEditor1.Html = dr.body;
            //this.htmlEditor2.Html = dr.tail;
        }


        private void htmlEditor1_Validated(object sender, EventArgs e)
        {
            if (dr != null)
            {
                dr.body = this.htmlEditor1.Html;
               // dr.tail = this.htmlEditor2.Html;
            }
        }

        private void mailFormDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            var dlg = FCOMMON.Util.MsgQ("선택된 자료를 삭제하시겠습니까?");
            if (dlg != System.Windows.Forms.DialogResult.Yes) return;
            bs.RemoveCurrent();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            var gcode = FCOMMON.info.Login.gcode;
            var uid = FCOMMON.info.Login.no;
            if (FCOMMON.Util.MsgQ("없는 목록을 기본값에서 생성 할까요?") != DialogResult.Yes) return;

            var sql = "insert into MailForm(gcode,cate,title,[subject],tail,body,selfcc,wuid,wdate)" +
                    $" select '{gcode}',cate,title,[subject],tail,body,selfcc,'{uid}',GETDATE() from MailForm" +
                    $" where gcode = 'EET1P'" +
                    $" and cate not in (select cate from MailForm where gcode = '{gcode}')";
            var cnt = FCOMMON.DBM.ExecuteNonQuery(sql);
            FCOMMON.Util.MsgI($"{cnt}건의 목록이 생성 되었습니다");
            refreshData();
        }

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