using System;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;
using AR;

namespace Project.Dialog
{
    public partial class fFinishJob : Form
    {
        public string Command = string.Empty;

        public fFinishJob()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == Keys.Escape)
                {
                    Close();
                }
            };
            if (System.Diagnostics.Debugger.IsAttached == true)
            {
                this.TopMost = false;
            }
            this.FormClosed += fFinishJob_FormClosed;
        }



        void fFinishJob_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Comm.WebService.Message -= WebService_Message;
        }

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

        private void __LoaD(object sender, EventArgs e)
        {
            this.Text = string.Format("Today's work list ({0})", DateTime.Now.ToShortDateString());


            if (AR.SETTING.Data.OnlineMode)
            {
                RefreshData();
            }

            //lbCntL.Text = cntl.ToString();
            //lbCntR.Text = cntr.ToString();
            //lbSumQty.Text = qty.ToString();
        }

        void RefreshData()
        {
            var sd = DateTime.Parse(DateTime.Now.ToShortDateString() + " 00:00:00");
            var ed = DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59");

            if (PUB.Result.JobStartTime.Year != 1982) sd = PUB.Result.JobStartTime;
            if (PUB.Result.JobEndTime.Year != 1982) ed = PUB.Result.JobEndTime;

            label2.Text = string.Format("Current work information ({0}~{1})", sd.ToString("HH:mm:ss"), ed.ToString("HH:mm:ss"));

            //현작업
            var ta = new DataSet1TableAdapters.K4EE_Component_Reel_ResultTableAdapter();
            var list = ta.GetByValidGuid(sd, ed, AR.SETTING.Data.McName, PUB.Result.guid);
            //var list = db.Component_Reel_Result.Where(t => t.STIME >= sd && t.STIME <= ed && t.PRNVALID == true && t.PRNATTACH == true && t.MC == AR.SETTING.Data.McName);
            UpdateList(list, dataGridView1, dataGridView2);

            //당일작업
            sd = DateTime.Parse(sd.ToShortDateString() + " 00:00:00");
            ed = DateTime.Parse(sd.ToShortDateString() + " 23:59:59");
            label3.Text = $"Daily ({sd.ToShortDateString()}) work information";
            var listday = ta.GetByValid(sd, ed, AR.SETTING.Data.McName);
            UpdateList(listday, dataGridView3, dataGridView4);
        }

        void UpdateList(DataSet1.K4EE_Component_Reel_ResultDataTable list, DataGridView dvS, DataGridView dvB)
        {
            dvS.Rows.Clear();
            dvB.Rows.Clear();

            int sum_reel = 0;
            int sum_cntl = 0;
            int sum_cntr = 0;
            double sum_kpc = 0;

            if (list != null && list.Count() > 0)
            {
                //SID별
                sum_reel = 0;
                sum_cntl = 0;
                sum_cntr = 0;
                sum_kpc = 0;

                var grplist = list.OrderBy(t => t.SID).GroupBy(t => t.SID);
                foreach (var row in grplist)
                {
                    var sid = row.Key;
                    var reel = row.Count();
                    var cntl = row.Where(t => t.LOC == "L")?.Count() ?? 0;
                    var cntr = row.Where(t => t.LOC == "R")?.Count() ?? 0;
                    double kpc = row.Sum(t => t.QTY);
                    if (kpc > 0) kpc = kpc / 1000.0;

                    var lim = -1;
                    dvS.Rows.Add(new object[] { sid, reel, cntl, cntr, kpc, lim });

                    sum_reel += reel;
                    sum_cntl += cntl;
                    sum_cntr += cntr;
                    sum_kpc += kpc;
                }
                dvS.Rows.Add(new object[] { "TOTAL", sum_reel, sum_cntl, sum_cntr, sum_kpc, null });
                dvS.Rows[dvS.RowCount - 1].DefaultCellStyle.BackColor = Color.DimGray;


                //배치별
                sum_reel = 0;
                sum_cntl = 0;
                sum_cntr = 0;
                sum_kpc = 0;

                var grpbatch = list.OrderBy(t => t.BATCH).GroupBy(t => t.BATCH);
                foreach (var row in grpbatch)
                {
                    var sid = row.Key;
                    if (sid.isEmpty()) sid = "(none)";
                    var reel = row.Count();
                    var cntl = row.Where(t => t.LOC == "L")?.Count() ?? 0;
                    var cntr = row.Where(t => t.LOC == "R")?.Count() ?? 0;
                    double kpc = row.Sum(t => t.QTY);
                    if (kpc > 0) kpc = kpc / 1000.0;

                    var lim = -1;
                    dvB.Rows.Add(new object[] { sid, reel, cntl, cntr, kpc, lim });

                    sum_reel += reel;
                    sum_cntl += cntl;
                    sum_cntr += cntr;
                    sum_kpc += kpc;
                }
                dvB.Rows.Add(new object[] { "TOTAL", sum_reel, sum_cntl, sum_cntr, sum_kpc, null });
                dvB.Rows[dvB.RowCount - 1].DefaultCellStyle.BackColor = Color.DimGray;

            }
            dvS.Invalidate();
            dvB.Invalidate();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            if (AR.SETTING.Data.OnlineMode)
                RefreshData();
        }
    }
}
