using FCOMMON;
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FPJ0000.JobReport_
{
    public partial class rK5Dailyform : fBase
    {
        public rK5Dailyform()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void rJobReport_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            this.dtSD.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-01");
            this.dtED.Text = DateTime.Parse(dtSD.Text).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            this.Show();
            Application.DoEvents();



            //var dsinfo = new Microsoft.Reporting.WinForms.ReportDataSource();
            //dsinfo.Name = "DataSet1";
            //dsinfo.Value = this.dsReport.K5DailyForm;


            this.reportViewer1.LocalReport.ReportPath = "JobReport_\\rK5Dailyform.rdlc";
            this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.Normal);
            this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth;


            //makedata();

        }
        void makedata()
        {
            this.dsReport.K5DailyForm.Clear();
            this.progressBar1.Value = 0;


            //자료를 불러온다
            var sd = DateTime.Parse(dtSD.Text).ToString("yyyy-MM-dd");
            var ed = DateTime.Parse(dtED.Text).ToString("yyyy-MM-dd");
            var db = new dsReportTableAdapters.vJobReportForUserTableAdapter();// EEEntities();

            var gcode = "EET1P";
            if (String.IsNullOrEmpty(FCOMMON.info.Login.gcode) == false)
                gcode = FCOMMON.info.Login.gcode;

            //var ta = new dsReportTableAdapters.vJobReportForUserTableAdapter();
            //var rawdata0 = ta.GetData(FCOMMON.info.Login.gcode, sd, ed);
            var rawdata = db.GetByDate(FCOMMON.info.Login.gcode, sd, ed).GroupBy(t => t.pdate);//.vJobReportForUser.AsNoTracking()
                //.Where(t => t.gcode == gcode && t.pdate.CompareTo(sd) >= 0 && t.pdate.CompareTo(ed) <= 0)
                //.OrderBy(t => t.pdate).GroupBy(t => t.pdate);
            //ta.Fill(this.dsReport.vJobReportForUser, FCOMMON.info.Login.gcode, sd.ToString("yyyy-MM-dd"), ed.ToShortTimeString());

            //    var rawdata = rawdata0.GroupBy(t => t.pdate);

            this.progressBar1.Maximum = rawdata.Count();

            //날짜별로 묶음처리한다.
            //var grplist = rawdata.GroupBy(t => t.pdate);
            foreach (var item in rawdata)
            {
                this.progressBar1.Value += 1;
                var drDate = item.FirstOrDefault();

                if(drDate.ww == "19")
                    Console.WriteLine("asdf");

                //근무일원
                var 휴가인원 = item.Where(t => t.svalue == "휴가").Count();
                var 파견인원 = item.Where(t => t.svalue == "파견").Count();
                var 전체인원 = item.GroupBy(t => t.id).Count();
                var 근무인원 = 전체인원 - 휴가인원 - 파견인원;
                if (chkallto.Checked) 근무인원 = 전체인원;
                //var 근무인원 = 전체인원;

                //완료
                var newdr = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr.Grp = "1.Tech. Support Summary";
                newdr.Item = "1.Completed";
                newdr.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr.Format = "N0";
                newdr.Sign = string.Empty;
                newdr.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr.value = item.Where(t => t.svalue == "Technical Support" && t.status == "진행 완료").Count();
                newdr.graph = true;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr);

                //진행중
                var newdr2 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr2.Grp = "1.Tech. Support Summary";
                newdr2.Item = "2.On-going";
                newdr2.Format = "N0";
                newdr2.Sign = string.Empty;
                newdr2.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr2.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr2.value = item.Where(t => t.svalue == "Technical Support" && (t.status == "진행 중" || t.status.EndsWith("%"))).Count();
                newdr2.graph = true;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr2);

                //건수
                var newdr3 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr3.Grp = "1.Tech. Support Summary";
                newdr3.Item = "3.건수";
                newdr3.Format = "N0";
                newdr3.Sign = string.Empty;
                newdr3.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr3.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr3.value = item.Where(t => t.svalue == "Technical Support").Count();
                newdr3.graph = true;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr3);

                //hrs
                var newdr4 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr4.Grp = "1.Tech. Support Summary";
                newdr4.Item = "4.Hrs";
                newdr4.Format = "N0";
                newdr4.Sign = string.Empty;
                newdr4.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr4.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr4.value = (double)(item.Where(t => t.svalue == "Technical Support").Sum(t => t.hrs));
                newdr4.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr4);

                //요약
                var newdr4a = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr4a.Grp = "1.Tech. Support Summary";
                newdr4a.Item = "5.Technical Support";
                newdr4a.Format = "N1";
                newdr4a.Sign = "%";
                newdr4a.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr4a.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr4a.value = (newdr4.value / (근무인원 * 8.0)) * 100.0;
                newdr4a.graph = true;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr4a);

                //Other Job Portion
                var newdr5 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr5.Grp = "2.Other Job Portion";
                newdr5.Item = "1.Project";
                newdr5.Format = "N0";
                newdr5.Sign = string.Empty;
                newdr5.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr5.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr5.value = (double)(item.Where(t => t.svalue == "Project").Sum(t => t.hrs));
                newdr5.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr5);

                //Other Job Portion
                var newdr6 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr6.Grp = "2.Other Job Portion";
                newdr6.Item = "2.Training";
                newdr6.Format = "N0";
                newdr6.Sign = string.Empty;
                newdr6.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr6.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr6.value = (double)(item.Where(t => t.svalue == "Training").Sum(t => t.hrs));
                newdr6.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr6);

                //Overhaul
                var newdr7 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr7.Grp = "2.Other Job Portion";
                newdr7.Item = "3.Overhaul";
                newdr7.Format = "N0";
                newdr7.Sign = string.Empty;
                newdr7.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr7.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr7.value = (double)(item.Where(t => t.svalue == "Overhaul").Sum(t => t.hrs));
                newdr7.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr7);

                //Others
                var newdr8 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr8.Grp = "2.Other Job Portion";
                newdr8.Item = "4.Others";
                newdr8.Format = "N0";
                newdr8.Sign = string.Empty;
                newdr8.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr8.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr8.value = (double)(item.Where(t => (t.svalue == "Others" || t.svalue == "others")).Sum(t => t.hrs));
                newdr8.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr8);


                //Other Job Portion
                var newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "2.Other Job Portion";
                newdr10.Item = "5.Project";
                newdr10.Format = "N1";
                newdr10.Sign = "%";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = (newdr5.value / (근무인원 * 8)) * 100.0;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //Other Job Portion
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "2.Other Job Portion";
                newdr10.Item = "6.Training";
                newdr10.Format = "N1";
                newdr10.Sign = "%";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = (newdr6.value / (근무인원 * 8)) * 100.0;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //Overhaul
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "2.Other Job Portion";
                newdr10.Item = "7.Overhaul";
                newdr10.Format = "N1";
                newdr10.Sign = "%";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = (newdr7.value / (근무인원 * 8)) * 100.0;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //Others
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "2.Other Job Portion";
                newdr10.Item = "8.Others";
                newdr10.Format = "N1";
                newdr10.Sign = "%";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = (newdr8.value / (근무인원 * 8)) * 100.0;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //Others TTL
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "2.Other Job Portion";
                newdr10.Item = "9.Others TTL";
                newdr10.Format = "N0";
                newdr10.Sign = "";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = newdr5.value + newdr6.value + newdr7.value + newdr8.value;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //Others TTL (%)
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "2.Other Job Portion";
                newdr10.Item = "9.Others TTL(%)";
                newdr10.Format = "N1";
                newdr10.Sign = "%";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = (newdr5.value / (근무인원 * 8) + newdr6.value / (근무인원 * 8) + newdr7.value / (근무인원 * 8) + newdr8.value / (근무인원 * 8)) * 100.0;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //휴가
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "3.휴가";
                newdr10.Item = "1.(p.s)";
                newdr10.Format = "N0";
                newdr10.Sign = "";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = 휴가인원;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //파견
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "4.파견";
                newdr10.Item = "1.(p.s)";
                newdr10.Format = "N0";
                newdr10.Sign = "";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = 파견인원;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //3.근무인원
                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                if (chkallto.Checked)
                    newdr10.Grp = "5.근무인원(전체)";
                else
                    newdr10.Grp = "5.근무인원";

                newdr10.Item = "1.(8hrs/p.s)";
                newdr10.Format = "N0";
                newdr10.Sign = "";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = 근무인원;
                newdr10.graph = false;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

                //3.근무인원
                var v1 = (newdr4.value / (근무인원 * 8.0)) * 100.0;
                var v2 = (newdr5.value / (근무인원 * 8) + newdr6.value / (근무인원 * 8) + newdr7.value / (근무인원 * 8) + newdr8.value / (근무인원 * 8)) * 100.0;

                newdr10 = this.dsReport.K5DailyForm.NewK5DailyFormRow();
                newdr10.Grp = "6.TTL";
                newdr10.Item = "1.Working Utilization(%)";
                newdr10.Format = "N1";
                newdr10.Sign = "%";
                newdr10.ww = item.Key.Substring(2, 2) + "-" + int.Parse(drDate.ww).ToString("00");
                newdr10.pdate = int.Parse(item.Key.Substring(8, 2)).ToString("00");
                newdr10.value = v1 + v2;
                newdr10.graph = true;
                dsReport.K5DailyForm.AddK5DailyFormRow(newdr10);

            }
            this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.Normal);
            this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth;

            List<ReportParameter> parameters = new List<ReportParameter>();
            parameters.Add(new ReportParameter("sd", dtSD.Text));
            parameters.Add(new ReportParameter("ed", dtED.Text));
            reportViewer1.LocalReport.SetParameters(parameters);

            this.reportViewer1.RefreshReport();

        }
        private void btSearch_Click(object sender, EventArgs e)
        {

            makedata();



        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var fn = FCOMMON.Util.CurrentPath + "a.xml";
            this.dsReport.K5DailyForm.WriteXml(fn);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            var fn = FCOMMON.Util.CurrentPath + "a.xml";
            this.dsReport.K5DailyForm.Clear();
            this.dsReport.K5DailyForm.ReadXml(fn);
            this.dsReport.K5DailyForm.AcceptChanges();



            var ser1 = this.chart1.Series[0];// new System.Windows.Forms.DataVisualization.Charting.Series("Completed");
            var ser2 = this.chart1.Series[1];// new System.Windows.Forms.DataVisualization.Charting.Series("On-Going");
            var ser3 = this.chart1.Series[2];// new System.Windows.Forms.DataVisualization.Charting.Series("건수");
            var ser4 = this.chart1.Series[3];// new System.Windows.Forms.DataVisualization.Charting.Series("technical Support");
            var ser5 = this.chart1.Series[4];// new System.Windows.Forms.DataVisualization.Charting.Series("TTL Working");

            ser1.Points.Add(new System.Windows.Forms.DataVisualization.Charting.DataPoint(10, 100));
            this.chart1.Invalidate();
            var fni = FCOMMON.Util.CurrentPath + "a.jpg";
            this.chart1.SaveImage(fni, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Jpeg);

            ReportParameter parameter = new ReportParameter("ImagePath", fni);
            reportViewer1.LocalReport.SetParameters(parameter);


            //this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("chartfile", fni));
            this.reportViewer1.RefreshReport();
        }

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

        private void lbStt_Click(object sender, EventArgs e)
        {
            var f = new FCOMMON.fSelectDay(DateTime.Parse(dtSD.Text));
            if (f.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            var sdDate = f.dtPick.SelectionStart;//  DateTime.Parse(DateTime.Now.ToString("yyyy-") + f.selectmon.ToString() + "-01");
            dtSD.Text = sdDate.ToString("yyyy-MM-dd");
            if (f.dtPick.SelectionStart.ToString("yyyy-MM-dd") == f.dtPick.SelectionEnd.ToString("yyyy-MM-dd"))
                dtED.Text = sdDate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            else
                dtED.Text = f.dtPick.SelectionEnd.ToString("yyyy-MM-dd");
        }

        private void toolStripLabel4_Click(object sender, EventArgs e)
        {
            var f = new FCOMMON.fSelectDay(DateTime.Parse(dtED.Text));
            if (f.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            dtED.Text = f.dtPick.SelectionStart.ToString("yyyy-MM-dd");
        }

        private void chkallto_Click(object sender, EventArgs e)
        {

            //makedata();
        }
    }
}
