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 FrmToDo_Log : Form
    {
        public FrmToDo_Log()
        {
            InitializeComponent();
            OlvTime.AspectGetter = delegate (object x) { return ((LogInfo)x).LogTime.ToString("yyyy-MM-dd dddd HH:mm:ss"); };
            OlvLogText.AspectGetter = delegate (object x) { return ((LogInfo)x).LogText; };
        }
        public void LoadDb(int id)
        {
            LvLogs.ClearObjects();
            DataProvider mydb = new DataProvider();
            IDbInterface db = TimeClock.Itrycn_Db.CreateDataProvider(TimeClock.Itrycn_Db.dataType);
            if (db.ConnDb(TimeClock.Itrycn_Db.SQLConnStr) == 1)
            {
                RyQuickSQL mySQL = new RyQuickSQL("ToDo_logs");
                mySQL.AddParameter("todo_id", id);
                DataSet ds = db.ReadData("select * from ToDo_logs where todo_id=@todo_id order by addTime desc", mySQL);
                List<LogInfo> logList = new List<LogInfo>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow row = ds.Tables[0].Rows[i];
                    logList.Add(new LogInfo() { LogTime = row["addTime"].ToDateTime(), LogText = row["log_text"].ToString() });
                }
                DataSet ds_todo = db.ReadData("select * from ToDo where id=" + id);
                if (mydb.HaveData(ds_todo))
                {
                    var row = mydb.GetData(ds_todo);
                    Text = "待办操作记录[" + row["name"].ToString() + "]";
                    logList.Add(new LogInfo() { LogTime = row["addTime"].ToDateTime(), LogText = "创建该待办" });
                }
                LvLogs.AddObjects(logList);
                OlvTime.Text = "时间("+ logList.Count+")";
                db.Free();
            }
        }
        private void FrmToDo_Log_Load(object sender, EventArgs e)
        {

        }


        private void LvLogs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LvLogs.SelectedObject == null) { richTextBox1.Text = ""; return; }
            richTextBox1.Text = ((LogInfo)LvLogs.SelectedObject).LogText;
        }
    }
    /// <summary>
    /// 日志信息
    /// </summary>
    public class LogInfo
    {
        /// <summary>
        /// 日志时间
        /// </summary>
        public DateTime LogTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 日志内容
        /// </summary>
        public string LogText { get; set; }
        /// <summary>
        /// 日志文字颜色
        /// </summary>
        public Color Color { get; set; } = Color.Black;
    }
}
