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

namespace HSUCO_Cargo_Garage_Operation_Program.CustomUserControl
{
    public partial class ListCertificate : UserControl
    {
        public ListCertificate()
        {
            InitializeComponent();
        }

        private void metroButton_ExportExcel_Click(object sender, 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, "Certificate", saveFileDialog.FileName, "증명서 발급 대장", Environment.MachineName, DateTime.Now);
                if (result)
                {
                    MetroMessageBox.Show(this, "엑셀 파일 저장에 성공하였습니다.", "Excel Export", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MetroMessageBox.Show(this, "엑셀 파일 저장에 실패하였습니다.", "Excel Export", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void metroButton_Search_Click(object sender, EventArgs e)
        {
            List<CertificateInformation> certificateInformation = Database.GetLedgerCertificate(metroDateTime_StartDate.Value, metroDateTime_EndDate.Value);

            var dataTable = new DataTable();
            dataTable.Columns.Add("발급번호");
            dataTable.Columns.Add("발급일자");
            dataTable.Columns.Add("성명\r\n(법인명)");
            dataTable.Columns.Add("생년월일\r\n(법인등록번호)");
            dataTable.Columns.Add("주소");
            dataTable.Columns.Add("차량번호\r\n(화물)");
            dataTable.Columns.Add("이용시작일");
            dataTable.Columns.Add("이용종료일");
            dataTable.Columns.Add("이용금액");


            for (int i = 0; i < certificateInformation.Count; i++)
            {
                dataTable.Rows
                    .Add(

                        certificateInformation[i].IssueNumber,
                        certificateInformation[i].IssueDate.DateTimeDatabase(),
                        certificateInformation[i].Name,
                        certificateInformation[i].PersonalNumber,
                        certificateInformation[i].Address,
                        certificateInformation[i].CargoVehicleNumber,
                        certificateInformation[i].StartDate.ToString("yyyy-MM-dd"),
                        certificateInformation[i].EndDate.ToString("yyyy-MM-dd"),
                        certificateInformation[i].Amount

                    );
            }

            dataGridView_List.DataSource = dataTable;
            dataGridView_List.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView_List.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            /*
            dataGridView_List.Columns[0].MinimumWidth = 100;
            dataGridView_List.Columns[0].FillWeight = 100;


            dataGridView_List.Columns[1].MinimumWidth = 100;
            dataGridView_List.Columns[1].FillWeight = 100;
            dataGridView_List.Columns[2].MinimumWidth = 60;
            dataGridView_List.Columns[2].FillWeight = 60;
            */
        }

        private void metroButton_RePrint_Click(object sender, EventArgs e)
        {
            int count = dataGridView_List.SelectedRows.Count;
            if (count == 0)
            {
                MetroMessageBox.Show(this, "선택된 이용자가 없습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string issueNumber = dataGridView_List.SelectedRows[0].Cells[0].Value.ToString();
            CertificateInformation certificateInformation = Database.GetCertificate(issueNumber);

            var print = new PrintCertificate(new List<CertificateInformation>() { certificateInformation }, Global.GlobalSettings.GarageName);
            print.Print();

        }

        private void ListCertificate_Load(object sender, EventArgs e)
        {
            metroDateTime_StartDate.Value = metroDateTime_StartDate.Value.AddDays(1 - metroDateTime_StartDate.Value.Day);
            metroDateTime_EndDate.Value = metroDateTime_EndDate.Value.AddMonths(1).AddDays(-metroDateTime_EndDate.Value.Day);
        }
    }
}
