using Pms.DataLibrary.Models;
using Pms.DataLibrary.Order;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.WebApi;
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 HistoryTaskUI : UserControl, IDisplayUI
    {
        public HistoryTaskUI()
        {
            InitializeComponent();
        }

        public bool Init()
        {
            dateTimePicker1.Value = DateTime.Now.AddDays(-1);
            dateTimePicker2.Value = DateTime.Now;
            return true;
        }

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

            string taskId = dataGridUI1.GetCurrentColValue("TaskID");
            string taskId_StepList = dataGridUI2.GetCurrentColValue("TaskID");
            
            if(!string.IsNullOrEmpty(taskId_StepList)
                && taskId == taskId_StepList)
            {
                return;
            }

            if (string.IsNullOrEmpty(taskId))
            {
                dataGridUI2.ShowDataGrid<StepData>(null);
                return;
            }

            var stepInfoList = PmsApi.GetHistoryStepList(taskId);
            if (stepInfoList == null) return;

            dataGridUI2.ShowDataGrid(stepInfoList.OrderBy(d => d.TemplateStepID).ToList());
        }

        private List<TaskInfo> GetHistoryTaskList()
        {
            var startTime = dateTimePicker1.Value;
            var endTime = dateTimePicker2.Value;

            if (endTime <= startTime)
            {
                MessageBox.Show("结束时间必须晚于开始时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

            var taskDataList = PmsApi.GetHistoryTaskList(startTime, endTime);
            if (taskDataList == null) return null;

            foreach (var item in taskDataList)
            {
                item.AllWarehouse = item.AllWarehouse.ToShortPosString();
            }

            return taskDataList;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var taskDataList = GetHistoryTaskList();
                if (taskDataList == null) return;

                dataGridUI1.ShowDataGrid(taskDataList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                dialog.FileName = string.Format("历史数据_{0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmss"));
                dialog.Filter = "Excel文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
                dialog.ValidateNames = true;
                dialog.CheckPathExists = true;
                dialog.AddExtension = true;
                DialogResult dialogResult = dialog.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    try
                    {
                        var dataList = dataGridUI1.GetDataTable();

                        Common.Config.ExcelConfig.Instance.SaveExcel(dialog.FileName, dataList);
                        MessageBox.Show("导出成功！");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}
