using MetroFramework;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace HSUCO_Cargo_Garage_Operation_Program.CustomForm
{
    public partial class InsertResult : MetroForm
    {
        List<ResultSetUserListInformation> _resultSetUserListInformation;
        public InsertResult(List<ResultSetUserListInformation> ResultSetUserListInformation)
        {
            _resultSetUserListInformation = ResultSetUserListInformation;
            InitializeComponent();
            DataChange();

        }

        private void DataChange()
        {
            var dataTable = new DataTable();
            dataTable.Columns.Add("주차구역");


            dataTable.Columns.Add("이용자번호");
            dataTable.Columns.Add("접수번호");
            dataTable.Columns.Add("화물차량번호");

            for (int i = 0; i < _resultSetUserListInformation.Count; i++)
            {
                dataTable.Rows
                    .Add(
                    _resultSetUserListInformation[i].Area,
                    _resultSetUserListInformation[i].No,
                    _resultSetUserListInformation[i].ApplicantNo,
                    _resultSetUserListInformation[i].CargoVehicleNumber
                    
                    );
            }

            dataGridView_List.DataSource = dataTable;
            dataGridView_List.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
        private void metroButton_ExportExcel_Click(object sender, System.EventArgs e)
        {
            var excelWrapper = new ExcelWrapper();

            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "XLSX files (*.xlsx)|*.xlsx";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                var result = excelWrapper.WriteDataTableToExcel((DataTable)dataGridView_List.DataSource, "당첨 결과 리스트", saveFileDialog.FileName, "당첨 결과 리스트", Environment.MachineName, DateTime.Now);
                if (result)
                {
                    MetroMessageBox.Show(this, "엑셀 파일 저장에 성공하였습니다.", "Excel Export", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DataChange();
                }
                else
                {
                    MetroMessageBox.Show(this, "엑셀 파일 저장에 실패하였습니다.", "Excel Export", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void metroButton_Close_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
