using DbCommon.BusinessCore.BaseCore;
using ProjectManagementSystem.Common.Config;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.Logger;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectManagementSystem.UI
{
    public partial class PmsTaskInfoUI : UserControl, IDisplayUI
    {
        private PmsTaskInfoManager pmsTaskInfoManger = new PmsTaskInfoManager();
        public PmsTaskInfoUI()
        {
            InitializeComponent();
        }

        public bool Init()
        {
            dataGridUI1.dataGridView1.CellClick += DataGridView1_CellClick;
            return true;
        }

        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridUI1.GetCurrentColValue("TaskId");
        }

        public void UpdateDisplay()
        {
            if (!this.Visible) return;

            var dataList = pmsTaskInfoManger.GetList().OrderBy(d => d.CreateTime).ToList();
            dataGridUI1.ShowDataGrid(dataList);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string taskId = textBox1.Text;
                if (string.IsNullOrEmpty(taskId)) return;

                string text = $"确定要{button1.Text}任务号{taskId}？";
                var dialogResult = MessageBox.Show(text, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (dialogResult != DialogResult.OK)
                {
                    return;
                }

                bool result = pmsTaskInfoManger.Delete(taskId);
                CLog.Instance.TaskLog.WriteInfo($"手动删除预约任务{result.ToChineseString()} {taskId}");
                MessageBox.Show($"{button1.Text} 任务号{taskId} {result.ToChineseString()}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                CLog.Instance.SystemLog.WriteException("UI", ex);
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
    }
}
