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 ListProceeds : UserControl
    {
        public ListProceeds()
        {
            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, "Proceeds", 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<ProceedsData> proceedsDatas = Database.GetLedgerProceeds(metroDateTime_StartDate.Value, metroDateTime_EndDate.Value);

            var dataTable = new DataTable();

            dataTable.Columns.Add("차량번호\r\n(화물)");
            dataTable.Columns.Add("차량번호\r\n(승용)");
            dataTable.Columns.Add("성명\r\n(법인명)");
            dataTable.Columns.Add("수익금 종류");
            dataTable.Columns.Add("금액");
            dataTable.Columns.Add("일자");

            for (int i = 0; i < proceedsDatas.Count; i++)
            {
                string typeString = string.Empty;
                switch (proceedsDatas[i].Type)
                {
                    case EProceedsType.Fees:
                        typeString = "이용요금";
                        break;
                    case EProceedsType.ExtensionFees:
                        typeString = "연장이용요금";
                        break;
                    case EProceedsType.Certificate:
                        typeString = "인증서 발급비용";
                        break;

                    case EProceedsType.Refunds:
                        typeString = "환불";
                        break;
                    default:
                        break;
                }

                dataTable.Rows
                    .Add(

                        proceedsDatas[i].CargoVehicleNumber,
                        proceedsDatas[i].PassengerVehicleNumber,
                        proceedsDatas[i].Name,
                        typeString,
                        String.Format("{0:n0}", proceedsDatas[i].Amount),
                        proceedsDatas[i].Date.DateTimeDatabase()

                    );
            }

            dataGridView_List.DataSource = dataTable;
            dataGridView_List.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView_List.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView_List.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            /*
            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 ListProceeds_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);
        }
    }
}
