using FCOMMON;
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;

namespace FCM0000.Mail
{
    public partial class fAutoSendSetting : fBase
    {
        string fn_fpcolsize = "";
        public fAutoSendSetting()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;

            this.FormClosed += fAutoSendSetting_FormClosed;
            fn_fpcolsize = System.IO.Path.Combine(FCOMMON.Util.CurrentPath, "formSetting", "fp_" + this.Name + ".ini");
        }

        void fAutoSendSetting_FormClosed(object sender, FormClosedEventArgs e)
        {
            FCOMMON.Util.FPColsizeSave(this.fpSpread1, fn_fpcolsize);
        }
       
        private void __Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            refreshData();
        }
        private void refreshData()
        {
            try
            {
                this.ta.Fill(this.dSMail.MailAuto, FCOMMON.info.Login.gcode);
                this.dSMail.MailAuto.AcceptChanges();
            }
            catch (System.Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
            FCOMMON.Util.FPColSizeLoad(ref this.fpSpread1, fn_fpcolsize);
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            try
            {
                var dlg = FCOMMON.Util.MsgQ("저장하시겠습니까?");
                if (dlg == System.Windows.Forms.DialogResult.Yes)
                {
                    var cnt = this.ta.Update(this.dSMail.MailAuto);
                    if (cnt < 1) FCOMMON.Util.MsgE("저장된 자료가 없습니다");
                    else FCOMMON.Util.MsgI(string.Format("{0}건의 자료가 저장 되었습니다", cnt));
                    this.dSMail.MailAuto.AcceptChanges();
                }
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE("Save error\n" + ex.Message);
            }
        }

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

            var dlg = FCOMMON.Util.MsgQ("현재 선택된 자료를 삭제 하시겠습니까?");
            if(dlg != System.Windows.Forms.DialogResult.Yes) return;
            bs.RemoveCurrent();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            var newdr = this.dSMail.MailAuto.NewMailAutoRow();
            newdr.gcode = FCOMMON.info.Login.gcode;
            newdr.wdate = DateTime.Now;
            newdr.sdate = DateTime.Now.ToString("yyyy-MM-dd");
            newdr.edate = "2099-12-31";
            newdr.sday = new byte[] { 0x00,0x01}; //매월 1일에
            newdr.wuid = FCOMMON.info.Login.no;
            newdr.wdate = DateTime.Now;
            newdr.enable = false;
            newdr.stime = "01:00"; //1시0분에 트리거
            newdr.fidx = -1; //기본폼이 선택되지 않게

            var f = new fMailAutoAdd(newdr);
            if(f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.dSMail.MailAuto.AddMailAutoRow(newdr);
                newdr.EndEdit();
            }
            else
            {
                newdr.Delete(); //제거함
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var newdr = drv.Row as DSMail.MailAutoRow;
            var f = new fMailAutoAdd(newdr);
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                newdr.EndEdit();
            }
            else
            {
                newdr.RejectChanges();
            }
        }

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

        private void btRefresh_Click(object sender, EventArgs e)
        {

        }

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

    }
}
