using ryCommon;
using ryCommonDb;
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 TimeClock.DbOp
{
    public partial class FrmAddTodo : Form
    {
        public ModalForm mr = null;
        public string tableName = "ToDo";
        public int isAdd = 1;
        public string selectId = "-1";
        public FrmAddTodo()
        {
            InitializeComponent();
            CbbSType.Items.AddRange(new StateInfo[] {
                new StateInfo { Text = "未完成",id=0 },
                new StateInfo { Text = "已完成",id=100 },
                new StateInfo { Text = "任务作废",id=-1 }
            });
            CbbSType.SelectedIndex = 0;
            dateTimePicker1.Value = DateTime.Now.Date;
        }
        DataRow base_info;
        #region 需要修改
        public void GetInfo(string id)
        {
            selectId = id;
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.SQLConnStr) == 1)
            {
                DataSet ds = db.ReadData(tableName, id);
                if (mydb.HaveData(ds))
                {
                    DataRow reader = ds.Tables[0].Rows[0];
                    base_info = reader;
                    #region 读取信息               
                    txtName.Text = reader["Name"].ToString();
                    NumSorts.Value = reader["Sorts"].ToInt();
                    txtContent.Rtf = reader["des_rtf"].ToString();
                    var startTime= reader["startTime"].ToInt64();
                    var endTime = reader["endTime"].ToInt64();
                    dateTimePicker1.Checked = startTime != 0;
                    dateTimePicker2.Checked = endTime != 0;
                    var dt_base = new DateTime(1970, 1, 1);
                    dateTimePicker1.Value = dt_base.AddSeconds(startTime);
                    dateTimePicker2.Value = dt_base.AddSeconds(endTime);
                    if(startTime==0 && endTime==0)
                    {
                        ChkToDoTime.Checked = false;                       
                    }
                    else
                    {
                        ChkToDoTime.Checked = true;
                    }
                    var state = reader["state"].ToInt();
                    for (int i = 0; i < CbbSType.Items.Count; i++)
                    {
                        StateInfo item = (StateInfo)CbbSType.Items[i];
                        if (item.id == state)
                        {
                            CbbSType.SelectedIndex = i;
                            break;
                        }
                    }
                    #endregion
                }
            }
            db.Free();
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("待办名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            RyQuickSQL mySQL = new RyQuickSQL(tableName);
            mySQL.AddField("Name", txtName.Text);
            mySQL.AddField("Sorts", NumSorts.Value);
            var dt_base = new DateTime(1970, 1, 1);
            var startTime = (dateTimePicker1.Value- dt_base).TotalSeconds.ToInt64();
            var endTime = (dateTimePicker2.Value - dt_base).TotalSeconds.ToInt64();
            if (!dateTimePicker1.Checked) { startTime = 0; };
            if (!dateTimePicker2.Checked) { endTime = 0; };
            if (!ChkToDoTime.Checked) { startTime = 0; endTime = 0; }
            mySQL.AddField("startTime", startTime);
            mySQL.AddField("endTime", endTime);
            var state = ((StateInfo)CbbSType.SelectedItem).id;
            mySQL.AddField("state", state);
            mySQL.AddField("Des", txtContent.Text);
            mySQL.AddField("Des_rtf", txtContent.Rtf);
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.SQLConnStr) == 1)
            {
                if (isAdd >= 1)
                {
                    mySQL.AddField("addTime", DateTime.Now);
                    mySQL.AddField("editTime", DateTime.Now);
                    db.ExecuteNonQuery(mySQL.GetInsertSQL(), db.GetParameter(mySQL));
                }
                else
                {
                    mySQL.AddField("editTime", DateTime.Now);
                    db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + selectId, db.GetParameter(mySQL));
                    string log_text = "";
                    #region 设置变化日志
                    if (txtName.Text != base_info["Name"].ToString())
                    {
                        log_text += "标题从[" + base_info["Name"].ToString() + "]改为[" + txtName.Text + "],";
                    }
                    if (NumSorts.Value.ToInt() != base_info["Sorts"].ToInt())
                    {
                        log_text += "优先级从" + base_info["Sorts"].ToInt() + "调整为" + NumSorts.Value.ToInt() + ",";
                    }
                    if (txtContent.Rtf != base_info["des_rtf"].ToString())
                    {
                        log_text += "内容,";
                    }
                    #region 判断时间有没有变化
                    var _base_startTime = base_info["startTime"].ToInt64();
                    var _base_endTime = base_info["endTime"].ToInt64();
                    if (startTime != _base_startTime || endTime != _base_endTime)
                    {
                        log_text += "待办时间从["+GetTimeStr(_base_startTime, _base_endTime) +"]调整为["+ GetTimeStr(startTime, endTime) + "],";
                    }
                    string GetTimeStr(Int64 _startTime,Int64 _endTime)
                    {
                        if (_startTime == 0 && _endTime == 0)
                        {
                            return "无限期";
                        }
                        else
                        {
                            string text = "";
                            if (_startTime == 0) { text= "立即开始"; }
                            else { text= dt_base.AddSeconds(_startTime).ToString("yyyy-MM-dd dddd HH:mm:ss"); }
                            if (_endTime == 0) { text+= "->无限期"; }
                            else { return text += "->"+dt_base.AddSeconds(_endTime).ToString("yyyy -MM-dd dddd HH:mm:ss"); }
                            return text;
                        }
                    }
                    #endregion
                    if (state != base_info["state"].ToInt())
                    {
                        TipInfo tip = new TipInfo { state = state };
                        TipInfo base_tip = new TipInfo { state = base_info["state"].ToInt() };
                        log_text += "状态从[" + base_tip.State_Str + "]调整为[" + tip.State_Str + "],";
                    }
                    #endregion
                    Itrycn_Db.AddTodo_Log(db,selectId.ToInt(), "编辑待办", "编辑" + log_text.Trim(','));
                }
            }
            db.Free();
            ModalForm.SetDialogResult(this, mr, DialogResult.OK);
        } 
        #endregion

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            ModalForm.SetDialogResult(this, mr, DialogResult.Cancel);
        }

        private void ChkToDoTime_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Enabled = ChkToDoTime.Checked;
            dateTimePicker2.Enabled = ChkToDoTime.Checked;
            panel1.Enabled = ChkToDoTime.Checked;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            dateTimePicker2.Value = DateTime.Now.Date.AddDays(1).AddMilliseconds(-1);
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            dateTimePicker2.Value = DateTime.Now.Date.AddDays(2).AddMilliseconds(-1);
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            dateTimePicker2.Value = DateTime.Now.Date.AddDays(4).AddMilliseconds(-1);
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            dateTimePicker2.Value = DateTime.Now.Date.AddDays(8).AddMilliseconds(-1);
        }

        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Undo();
        }

        private void 重做ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Redo();
        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Cut();
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Copy();
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Paste();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectedText = "";
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectAll();
        }

        private void MenuNote_Opening(object sender, CancelEventArgs e)
        {
            撤销ToolStripMenuItem.Enabled = txtContent.CanUndo;
            重做ToolStripMenuItem.Enabled = txtContent.CanRedo;
            剪切ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
            复制ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
            粘贴ToolStripMenuItem.Enabled = Clipboard.GetText() != "";
            删除ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
        }

        private void 标为重点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectionBackColor = Color.Yellow;
        }

        private void 标为常规ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectionBackColor = Color.Transparent;
        }
        

        private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = txtContent.SelectionColor;
            if (colorDialog1.ShowDialog()==DialogResult.OK)
            {
                txtContent.SelectionColor = colorDialog1.Color;
            }
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1.Font = txtContent.SelectionFont;
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                txtContent.SelectionFont = fontDialog1.Font;
            }
        }

        private void 插入时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectedText = DateTime.Now.ToDateTimeStr();
        }

        private void FrmAddTodo_Load(object sender, EventArgs e)
        {

        }
    }
}
