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 FBS0000
{
    public partial class fHolyDayDataSum : Form
    {
        bool binit = false;
        public fHolyDayDataSum(string sdate, string uid)
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            this.WindowState = FormWindowState.Maximized;

            //var dtUser = FCOMMON.DBM.getUserTable();
            //this.cmbUser.Items.Clear();
            //this.cmbUser.Items.Add("-- ALL --");
            //foreach (DataRow dr in dtUser.Rows)
            //{
            //    this.cmbUser.Items.Add(dr["dispname"].ToString());
            //}
            //cmbUser.Text = uid;

            //if (cmbUser.SelectedIndex < 0)
            //    cmbUser.SelectedIndex = 0;

            this.tbMon.Text = DateTime.Now.ToString("yyyy-MM-dd");
            cmbType.SelectedIndex = 0; //일기준으로한다

        }

        private void rJobReport_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            refreshData();
            binit = true;

        }
        private void EnsureVisibleAndUsableSize()
        {
            var visibleBounds = Screen.GetWorkingArea(this);
            var isOutOfView = (Left > visibleBounds.Right) || (Top > visibleBounds.Bottom) ||
                (Right < visibleBounds.Left) || (Bottom < visibleBounds.Top);
            if (isOutOfView)
            {
                CenterToScreen();
            }
            var minW = Math.Max(320, this.MinimumSize.Width);
            var minH = Math.Max(240, this.MinimumSize.Height);

            if (Width < minW || Height < minH)
            {
                this.Size = new Size(minW, minH);
            }
        }

        //string GetUIDValue()
        //{
        //    string uid = "%";
        //    if (cmbUser.SelectedIndex > 0)
        //    {
        //        var si = cmbUser.Text.IndexOf("(");
        //        var ei = cmbUser.Text.LastIndexOf(")");
        //        uid = cmbUser.Text.Substring(si + 1, ei - si - 1);
        //    }
        //    return uid;
        //}

        void refreshData()
        {

            var ed = DateTime.Parse(this.tbMon.Text);
            var sd = DateTime.Parse(ed.ToString("yyyy") + "-01-01");

            int curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.holyday));
            var uid = curLevel >= 5 ? "%" : FCOMMON.info.Login.no; // GetUIDValue();
            if (cmbType.SelectedIndex == 0)
            {
                taDay.Fill(this.dsReport.Holydata_Day, FCOMMON.info.Login.gcode, uid, sd.ToString("yyyy-MM-dd"), ed.ToString("yyyy-MM-dd"));
                //this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", bsDay));
            }
            else if(cmbType.SelectedIndex == 1)
            {
                taTime.Fill(this.dsReport.Holydata_Time, FCOMMON.info.Login.gcode, uid, sd.ToString("yyyy-MM-dd"), ed.ToString("yyyy-MM-dd"));
                dsReport.Holydata_Day.Clear();
                dsReport.Holydata_Day.Merge(dsReport.Holydata_Time);
                dsReport.Holydata_Day.AcceptChanges();
                //this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", bsTime));
            }
            else //pms
            {
                var taPMS = new dsReportTableAdapters.Holydata_TimePMSTableAdapter();
                taPMS.Fill(this.dsReport.Holydata_TimePMS, FCOMMON.info.Login.gcode, uid, sd.ToString("yyyy-MM-dd"), ed.ToString("yyyy-MM-dd"));
                dsReport.Holydata_Day.Clear();
                dsReport.Holydata_Day.Merge(dsReport.Holydata_TimePMS);
                dsReport.Holydata_Day.AcceptChanges();
                //this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", bsTime));
            }

            this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
            this.reportViewer1.PageCountMode = Microsoft.Reporting.WinForms.PageCountMode.Actual;
            this.reportViewer1.RefreshReport();



        }

        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)
        {
            refreshData();
        }

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

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

        private void reportViewer1_Load(object sender, EventArgs e)
        {

        }

        private void lbStt_Click(object sender, EventArgs e)
        {

        }

        private void cmbType_Click(object sender, EventArgs e)
        {

        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(binit)
            refreshData();
        }
    }
}
