using Model;
using PlcUiForm;
using Sunny.UI;
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 YangjieTester.机台日志表现
{
    [FormDescriptionAttribute("批次报告画面")]
    public partial class BatchListForm : PlcBaseForm
    {
        public BatchListForm()
        {
            InitializeComponent();
        }

        private void BatchListForm_Load(object sender, EventArgs e)
        {
            ShowBatchList();
        }
        void ShowBatchList()
        {
            var batches = BatchService.GetFinishedBatches(DateTime.MinValue, DateTime.Now, null, null);

            listBox1.DisplayMember = "BatchID"; // 显示 BatchID
            //listBox1.ValueMember = "BatchID";   // （可选）
            listBox1.DataSource = batches;      // 直接绑定 List<Batch>
        }


        private void txt批次查询输入_TextChanged(object sender, EventArgs e)
        {
            var batches = BatchService.GetFinishedBatches(DateTime.MinValue, DateTime.Now, txt批次查询输入.Text, null);
            listBox1.DisplayMember = "BatchID"; // 显示 BatchID
            //listBox1.ValueMember = "BatchID";   // （可选）
            listBox1.DataSource = batches;      // 直接绑定 List<Batch>
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem is Batch selectedBatch)
            {
                txtBatchReport.Text = BatchReport.GenerateBatchReport(selectedBatch);
            }
        }

        private void btn删除选中批次_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem is Batch selectedBatch)
            {
                var dr=MessageBox.Show($"确认删除批次{selectedBatch.BatchID}？此操作不可恢复！","确认删除",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
                if(dr!= DialogResult.Yes)
                {
                    return;
                }
                if (!BatchService.DeleteBatchByBatchId(selectedBatch.BatchID,out string err))
                {
                    MessageBox.Show($"删除批次{selectedBatch.BatchID}失败：{err}");
                    return;
                }
                ShowBatchList();
            }
        }

        private void btn导出选中_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem is Batch selectedBatch)
            {
                using (var saveFileDialog = new SaveFileDialog())
                {
                    saveFileDialog.Filter = "txt 文件 (*.txt)|*.txt";
                    saveFileDialog.FileName = $"{selectedBatch.BatchID}.txt";

                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            ExportData.ExportDataToTxt(BatchReport.GenerateBatchReport(selectedBatch), saveFileDialog.FileName);
                            MessageBox.Show("导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show($"导出失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
    }
}
