using FCOMMON;
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 rJobReport : fBase
    {
        public rJobReport()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void rJobReport_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            this.tbProcess.Items.Clear();
            tbProcess.Items.Add("전체");
           // var taProcess = new dsReportTableAdapters.ProcessListTableAdapter();
            var dtProcessList = FCOMMON.DBM.GroupUserProcessList();// taProcess.GetData(FCOMMON.info.Login.gcode);
            foreach (var dr in dtProcessList)
                tbProcess.Items.Add(dr);

            this.tbProcess.Text = FCOMMON.info.Login.process;
            if (tbProcess.SelectedIndex < 0) tbProcess.SelectedIndex = 0;

            this.tbMon.Text = DateTime.Now.Year.ToString();
            var curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.jobreport));
            if (curLevel < 5) tbProcess.Enabled = false;
            refrehData();
        }

        void refrehData()
        {




            ////휴일데이터가 들어가 있음.
            //var taH = new dsReportTableAdapters.HolidayLIstTableAdapter();
            //taH.Fill(this.dsReport.HolidayLIst, tbMon.Text + "%");

            string prcname = tbProcess.SelectedIndex < 1 ? "%" : tbProcess.Text.Trim();
            this.ta.Fill(this.dsReport.jobReport, tbMon.Text, FCOMMON.info.Login.gcode, prcname);
            //this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            //this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth;

            //그리드뷰 생성
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.Rows.Clear();
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridView1.ColumnHeadersHeight = 35;
            this.dataGridView1.Columns.Add("이름", "이름");

            //이름/년도데이터추가
            var ymlist = dsReport.jobReport.OrderBy(t => t.yymm).GroupBy(t => t.yymm);
            foreach (var ym in ymlist)
            {
                var drYm = ym.FirstOrDefault();

                var basehr = int.Parse(drYm.yymm.Substring(drYm.yymm.IndexOf('(')).Replace("(", "").Replace(")", ""));

                this.dataGridView1.Columns.Add(drYm.yymm, drYm.yymm.Substring(0, 7) + "\r\n(" + basehr.ToString() + ")");
            }
            var curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.jobreport));
            //이름으로 정렬해서 데이터를 가져온다
            var namelist = this.dsReport.jobReport.OrderBy(t => t.uname).GroupBy(t => t.uname);
            foreach (var uname in namelist)
            {
                var drName = uname.FirstOrDefault();
                if (curLevel < 5 && drName.uid != FCOMMON.info.Login.no) continue;  //개인정보 251111

                List<string> rowdata = new List<string>();
                rowdata.Add(drName.uname + "(" + drName.uid + ")");

                List<Boolean> high = new List<bool>();
                List<Boolean> low = new List<bool>();
                for (int i = 1; i < this.dataGridView1.ColumnCount; i++)
                {
                    var coltitle = dataGridView1.Columns[i].HeaderText;
                    var basehr = int.Parse(coltitle.Split('\n')[1].Replace("(","").Replace(")",""));

                    var userDatas = dsReport.jobReport.Where(t => t.uid == drName.uid && t.yymm == dataGridView1.Columns[i].Name);
                    if (userDatas == null || userDatas.Count() == 0)
                    {
                        //이달에는 데이터가없다
                        rowdata.Add("--");
                        high.Add(false);
                        low.Add(false);
                    }
                    else
                    {
                        ////모든데이터를 확인해서 휴일이랑 데이터를 분리해야한다.
                        //var sumhr = 0f;
                        //var sumot = 0f;
                        //var sumholy = 0f;
                        //foreach(dsReport.jobReportRow item in userDatas)
                        //{
                        //    dsReport.HolidayLIst.Where(t => t.pdate == item.yymm);
                        //}

                        

                        var sumhr = userDatas.Sum(t => t.hrs);
                        var sumot = userDatas.Sum(t => t.ot);
                        var sumhl = userDatas.Sum(t => t.holyot);
                        if (sumot + sumhl == 0) rowdata.Add(string.Format("{0}", sumhr, sumot));

                        else rowdata.Add(string.Format("{0}+{1}(*{2})", sumhr, sumot,sumhl));

                        if (sumhr > basehr) high.Add(true);
                        else high.Add(false);

                        if (sumhr < basehr) low.Add(true);
                        else low.Add(false);
                    }
                }

                dataGridView1.Rows.Add(rowdata.ToArray());

                //이번에 추가한 줄의 셀 컬러를 지정한다
                var currentrow = dataGridView1.Rows.Count - 1;
                for (int i = 1; i < this.dataGridView1.ColumnCount; i++)
                {
                    if (high[i - 1]) dataGridView1.Rows[currentrow].Cells[i].Style.ForeColor = Color.Red;
                    else if (low[i - 1]) dataGridView1.Rows[currentrow].Cells[i].Style.ForeColor = Color.Blue;
                    else dataGridView1.Rows[currentrow].Cells[i].Style.ForeColor = Color.Black;
                }
            }
            this.dataGridView1.AutoResizeColumns();

        }

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

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

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

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tbProcess.SelectedIndex >= 0) refrehData();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            dataGridView1.ExportData(string.Empty);
        }

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

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            var sdo = DateTime.Parse(this.tbMon.Text + "-01-01");
            var sd = sdo.AddYears(-1);
            tbMon.Text = sd.ToString("yyyy");
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            var sdo = DateTime.Parse(this.tbMon.Text + "-01-01");
            var sd = sdo.AddYears(1);
            tbMon.Text = sd.ToString("yyyy");
        }
    }
}
