using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
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 CallTaskUI : UserControl, IDisplayUI
    {
        private CallTaskManager callTaskManager = new CallTaskManager();
        public CallTaskUI()
        {
            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 = callTaskManager.GetList(d => d.TaskStatus < EnumTaskStatus.任务完成);
            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;
                }

                var data = callTaskManager.GetById(taskId);
                if(data != null)
                {
                    data.TaskStatus = EnumTaskStatus.任务手动删除;
                    bool result = callTaskManager.Update(data);
                    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);
            }
        }
    }
}
