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 fMailAutoAdd : FCOMMON.fBase
    {
        DSMail.MailAutoRow dr = null;
        public fMailAutoAdd(DSMail.MailAutoRow dr_)
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;

            PredefinedButtonSets.SetupDefaultButtons(this.tbBody);
            this.dr = dr_;
        }
        private void fMailform_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            //시작일을 오늘로
            this.dtSd.Value = DateTime.Now;

            cmbDay.Items.Clear();
            foreach (var i in Enumerable.Range(1, 31))
                cmbDay.Items.Add(i.ToString("00"));

            cmbHour.Items.Clear();
            foreach (var i in Enumerable.Range(1, 24))
                cmbHour.Items.Add(i.ToString("00"));

            this.cmbMinute.Items.Clear();
            foreach (var i in Enumerable.Range(0, 59))
                cmbMinute.Items.Add(i.ToString("00"));

            //메일양식
            this.taf.Fill(this.dsMSSQL.MailForm, FCOMMON.info.Login.gcode);

            dtSd.Value = DateTime.Parse(dr.sdate);
            dtEd.Value = DateTime.Parse(dr.edate);
            chkEnb.Checked = dr.enable;
            if (dr.sday == null || dr.sday.Length < 2)
            {
                //아무것도 없다 매주 월요일로 한다
                radWeek.Checked = true;
                this.chkMon.Checked = true;
            }
            else
            {
                if (dr.sday[0] == 1)
                {
                    radWeek.Checked = true;
                    var bitString = Convert.ToString(dr.sday[1], 2).PadLeft(8, '0').ToArray();
                    this.chkSun.Checked = bitString[1] == '1';
                    this.chkMon.Checked = bitString[2] == '1';
                    this.chkTue.Checked = bitString[3] == '1';
                    this.chkWend.Checked = bitString[4] == '1';
                    this.chkThjur.Checked = bitString[5] == '1';
                    this.chkFri.Checked = bitString[6] == '1';
                    this.chkSat.Checked = bitString[7] == '1';
                }
                else
                {
                    radMon.Checked = true;
                    cmbDay.Text = dr.sday[1].ToString("00");
                }

            }

            tbTo.Text = dr.tolist;
            tbBcc.Text = dr.bcc;
            tbCC.Text = dr.cc;
            tbFrom.Text = dr.fromlist;
            tbSubject.Text = dr.subject;
            tbBody.Html = dr.body;

            //전송시간
            var buf = dr.stime.Split(':');
            cmbHour.Text = buf[0];
            cmbMinute.Text = buf[1];

            //선택된 자료갸ㅏ 있다면 선택해준다.
           // if (dr.fidx != -1) this.cmbMailForm.SelectedValue = dr.fidx;
           // cmbMailForm_SelectedIndexChanged(null, null);
        }

        private void cmbMailForm_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //메일폼이 선택되어이 있어야 함
            if (chkEnb.Checked)
            {
                if (this.tbBody.Html=="")
                {
                    FCOMMON.Util.MsgE("메일 내용을 입력하세요");
                    this.tbBody.Focus();
                    return;
                }
                if (tbTo.Text.Trim() == "")
                {
                    FCOMMON.Util.MsgE("수신목록을 입력하세요");
                    this.tbBody.Focus();
                    return;
                }
                if (tbSubject.Text.Trim() == "")
                {
                    FCOMMON.Util.MsgE("제목을 입력하세요");
                    this.tbBody.Focus();
                    return;
                }
            }

            if(radMon.Checked && cmbDay.SelectedIndex < 0)
            {
                FCOMMON.Util.MsgE("전송간격의 [일]을 선택하세요");
                cmbDay.Focus();
                return;
            }
            if (tbFrom.Text.Trim() == "")
            {
                FCOMMON.Util.MsgE("발신 주소가 없습니다");
                tbFrom.Focus();
                return;
            }
            if(tbTo.Text.Trim() == "")
            {
                FCOMMON.Util.MsgE("수신 주소가 없습니다");
                tbTo.Focus();
                return;
            }
            if(tbSubject.Text.Trim() == "")
            {
                FCOMMON.Util.MsgE("메일 제목이 없습니다");
                tbSubject.Focus();
                return;
            }
            if(tbBody.Html.Trim() == "")
            {
                FCOMMON.Util.MsgE("메일 본문이 없습니다");
                tbBody.Focus();
                return;
            }

            this.dr.fromlist = tbFrom.Text.Trim();
            this.dr.enable = chkEnb.Checked;
            this.dr.sdate = dtSd.Value.ToString("yyyy-MM-dd");
            this.dr.edate = dtEd.Value.ToString("yyyy-MM-dd");
            this.dr.stime = string.Format("{0}:{1}",cmbHour.Text,cmbMinute.Text);
            this.dr.tolist = tbTo.Text.Trim();
            this.dr.bcc = tbBcc.Text.Trim();
            this.dr.cc = tbCC.Text.Trim();
            this.dr.subject = tbSubject.Text.Trim();
            this.dr.body = tbBody.Html;
            //this.dr.fidx = (int)(cmbMailForm.SelectedValue);

            byte sendtype = 0;
            if (radWeek.Checked) sendtype = 1;

            byte sendvalue = 0;
            if (radMon.Checked) sendvalue = byte.Parse(cmbDay.Text);
            else
            {
                //체크박스를 비트로 처리한다.
                var bitString = "0";
                bitString += chkSun.Checked ? "1" : "0";
                bitString += chkMon.Checked ? "1" : "0";
                bitString += chkTue.Checked ? "1" : "0";
                bitString += chkWend.Checked ? "1" : "0";
                bitString += chkThjur.Checked ? "1" : "0";
                bitString += chkFri.Checked ? "1" : "0";
                bitString += chkSat.Checked ? "1" : "0";
                sendvalue = Convert.ToByte(bitString, 2);
            }
            this.dr.sday = new byte[] { sendtype, sendvalue };
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            dtEd.Value = dtSd.Value;
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FCOMMON.Util.MsgE("양식 선택 준비중");
            ////메일폼이 선택되면 그 자료를 불러온다
            ////var drv = cmbMailForm.SelectedValue as DataRowView;
            //if (cmbMailForm.SelectedIndex < 0)
            //{
            //    tbSubject.Text = "";
            //    tbBody.ResetText();
            //    return;
            //}
            //var idx = (int)cmbMailForm.SelectedValue;// int.Parse(drv["idx"].ToString());
            //var data = taf.GetByIdx(FCOMMON.info.Login.gcode, idx);
            //if (data != null && data.Rows.Count > 0)
            //{
            //    var dr = data.Rows[0] as DSMail.MailFormRow;
            //    tbSubject.Text = dr.subject;
            //    tbBody.Html = dr.body;
            //    if (tbTo.Text.Trim() == "") tbTo.Text = dr.tolist;
            //    if (tbBcc.Text.Trim() == "") tbBcc.Text = dr.bcc;
            //    if (tbCC.Text.Trim() == "") tbCC.Text = dr.cc;
            //}
            //else
            //{
            //    tbSubject.Text = "";
            //    tbBody.ResetText();

            //}
        }

    }
}
