using FCOMMON;
using NetOffice.OutlookApi;
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 rJobReportType : fBase
    {
        Boolean binit = false;
        public rJobReportType()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            this.dts.Value = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01"));
            this.dte.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1);

        }

        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;

            //사용자 목록을 선택한다
            UpdateUserList();

            this.Show();
            System.Windows.Forms.Application.DoEvents();

            //this.tbMon.Text = DateTime.Now.ToString("yyyy-MM");
            //refrehData();
            binit = true;

            tbProcess.SelectedIndexChanged += tbProcess_SelectedIndexChanged;
            var curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.jobreport));
            if(curLevel < 5)
            {
                tbProcess.Enabled = false;
                cmbUser.Enabled = false;
            }
        }
        void UpdateUserList()
        {
            var ta = new dsPRJTableAdapters.vJobReportForUserListTableAdapter();

            cmbUser.Items.Clear();
            cmbUser.Items.Add("--전체--");
            //일반사용자 목록 가져온다
            dsPRJ.vJobReportForUserListDataTable userlist;

            if (tbProcess.SelectedIndex <= 0)
            {
                //공정구분없이 전체사용자를 가져온다
                userlist = ta.GetData(FCOMMON.info.Login.gcode, "%");
                //  db.vJobReportForUser.Where(t => t.gcode == FCOMMON.info.Login.gcode).OrderBy(t => t.name).GroupBy(t => t.name);
            }
            else
            {
                userlist = ta.GetData(FCOMMON.info.Login.gcode, tbProcess.Text);
                //db.vJobReportForUser.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.userProcess == tbProcess.Text).OrderBy(t => t.name).GroupBy(t => t.name);
            }

            //if (tbProcess.SelectedIndex != 0) userlist = userlist.Where(t => t.processs == tbProcess.Text); //해당 공정의 인원만 처리한다
            foreach (var dr in userlist)
            {
                //var dr = item.First();
                cmbUser.Items.Add(String.Format("[{0}] {1}", dr.id, dr.name));
            }

            if (tbProcess.SelectedIndex > 0)
                cmbUser.Text = string.Format("[{0}] {1}", FCOMMON.info.Login.no, FCOMMON.info.Login.nameK);

            if (cmbUser.SelectedIndex < 0) cmbUser.SelectedIndex = 0;  //기본전체로 선택해준다.
        }

        void refrehData()
        {

            //근문일수,시간
            var vSD = dts.Value.ToString("yyyy-MM-dd");
            var vED = dte.Value.ToString("yyyy-MM-dd");

            //string prcname = tbProcess.SelectedIndex < 1 ? "%" : tbProcess.Text.Trim();
            var taH = new dsReportTableAdapters.vJobReportForUserTableAdapter();

            var username = cmbUser.Text.Trim();
            var uid = "%";
            if (username.isEmpty() == false && username.StartsWith("--") == false)
                uid = username.Substring(1, username.IndexOf("]") - 1);// "395552";

            if (tbProcess.SelectedIndex == 0)
            {
                if(cmbUser.SelectedIndex == 0)
                {
                    //전체공정-전체인원
                    taH.FillNew(this.dsReport.vJobReportForUser, FCOMMON.info.Login.gcode, vSD, vED, uid);
                }
                else
                {
                    //전체공정-특정인원
                    taH.FillNew(this.dsReport.vJobReportForUser, FCOMMON.info.Login.gcode, vSD, vED, uid);
                }
            }
            else
            {
                if (cmbUser.SelectedIndex == 0)
                {
                    //특정공정-전체인원
                    taH.FillByProcess_New(this.dsReport.vJobReportForUser, FCOMMON.info.Login.gcode, vSD, vED, tbProcess.Text);
                }
                else
                {
                    //특정공정-특정인원
                    taH.FillNew(this.dsReport.vJobReportForUser, FCOMMON.info.Login.gcode, vSD, vED, uid);
                }
                    
                    
            }
            //this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            //this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth;
            //this.reportViewer1.RefreshReport();

            if (this.checkBox1.Checked)
                this.reportViewer1.LocalReport.ReportEmbeddedResource = "FPJ0000.JobReport_.rJobReportTypeU.rdlc";
            else
                this.reportViewer1.LocalReport.ReportEmbeddedResource = "FPJ0000.JobReport_.rJobReportType.rdlc";

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

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

        private void tbProcess_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (binit == false) return;
            UpdateUserList();
        }

        private void linkLabel7_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void linkLabel13_Click(object sender, EventArgs e)
        {
            var lnk = sender as LinkLabel;
            if (lnk.Text.Equals("상반기"))
            {
                dts.Value = DateTime.Parse(DateTime.Now.Year.ToString("0000") + "-01-01");
                dte.Value = DateTime.Parse(DateTime.Now.Year.ToString("0000") + "-06-30");
            }
            else if (lnk.Text.Equals("하반기"))
            {
                dts.Value = DateTime.Parse(DateTime.Now.Year.ToString("0000") + "-07-01");
                dte.Value = DateTime.Parse(DateTime.Now.Year.ToString("0000") + "-12-31");
            }
            else
            {
                if (int.TryParse(lnk.Text.Substring(0, 2), out int mon) == false) return;
                dts.Value = DateTime.Parse(DateTime.Now.Year.ToString("0000") + "-" + mon.ToString("00") + "-01");
                dte.Value = dts.Value.AddMonths(1).AddDays(-1);
            }
        }

        private void linkLabel14_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }
    }
}
