using ClientLib;
using ClientLib.CommonService;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using PublicLib;
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 HANMI_STD
{
    public partial class WorkdayManager : PublicLib.CommonMDI
    {
        private bool m_Loading;

        public WorkdayManager()
        {
            InitializeComponent();

            m_Loading = true;

            comboBoxEdit_YY.Properties.Items.Clear();
            comboBoxEdit_YY.Properties.DropDownRows = 25;
            int yy = 2009;
            for (int i = 0; i < 100; i++)
            {
                yy++;
                comboBoxEdit_YY.Properties.Items.Add(yy.ToString());
            }

            lookUpEdit_COMP_CD.EditValueChanged += (sender, e) =>
            {
                searchProc();
            };
            comboBoxEdit_YY.SelectedIndexChanged += (sender, e) =>
            {
                searchProc();
            };

            simpleButton_Next.Click += (sender, e) =>
            {
                if (comboBoxEdit_YY.SelectedIndex < comboBoxEdit_YY.Properties.Items.Count - 1)
                {
                    comboBoxEdit_YY.SelectedIndex++;
                }
            };

            simpleButton_Prev.Click += (sender, e) =>
            {
                if (comboBoxEdit_YY.SelectedIndex > 0)
                {
                    comboBoxEdit_YY.SelectedIndex--;
                }
            };

            simpleButton_Create.Click += (sender, e) =>
            {
                if (MessageBox.Show("작업달력을 생성하시겠습니까?", "생성", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    return;
                }

                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    SerializedSqlParam[] aParam = new SerializedSqlParam[]
                    {
                        ClientClass.CreateSqlParameter("COMP_CD", SqlDbType.NVarChar, lookUpEdit_COMP_CD.EditValue),
                        ClientClass.CreateSqlParameter("YY", SqlDbType.NVarChar, comboBoxEdit_YY.Text),
                        ClientClass.CreateSqlParameter("S_1", SqlDbType.NVarChar, checkedListBoxControl_Saturday.Items[0].CheckState == CheckState.Checked ? "Y" : "N"),
                        ClientClass.CreateSqlParameter("S_2", SqlDbType.NVarChar, checkedListBoxControl_Saturday.Items[1].CheckState == CheckState.Checked ? "Y" : "N"),
                        ClientClass.CreateSqlParameter("S_3", SqlDbType.NVarChar, checkedListBoxControl_Saturday.Items[2].CheckState == CheckState.Checked ? "Y" : "N"),
                        ClientClass.CreateSqlParameter("S_4", SqlDbType.NVarChar, checkedListBoxControl_Saturday.Items[3].CheckState == CheckState.Checked ? "Y" : "N"),
                        ClientClass.CreateSqlParameter("S_5", SqlDbType.NVarChar, checkedListBoxControl_Saturday.Items[4].CheckState == CheckState.Checked ? "Y" : "N"),
                        ClientClass.CreateSqlParameter("REG_ID", SqlDbType.NVarChar, ConstClass._USR_ID)
                    };
                    ResultData result = ClientClass.SetData("CreateWorkday", aParam);
                    if (result.isError)
                    {
                        throw new Exception(result.ResultValue);
                    }

                    this.Cursor = Cursors.Arrow;
                    MessageBox.Show("생성 완료");
                    searchProc();
                }
                catch (Exception ex)
                {
                    this.Cursor = Cursors.Arrow;
                    MessageBox.Show(ex.Message);
                }
            };

            bandedGridView_Main.FormatConditions.Clear();
            StyleFormatCondition sfc;

            sfc = new StyleFormatCondition();
            sfc.Appearance.ForeColor = System.Drawing.Color.Red;
            sfc.Appearance.Options.UseForeColor = true;
            sfc.Column = bandedGridView_Main.Columns["WORK_DAY"];
            sfc.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
            sfc.Expression = "[HOL_YN]  == 'Y'";
            sfc.Appearance.Font = new Font(this.Font, FontStyle.Bold);
            sfc.Appearance.Options.UseFont = true;
            bandedGridView_Main.FormatConditions.Add(sfc);

            sfc = new StyleFormatCondition();
            sfc.Appearance.ForeColor = System.Drawing.Color.Red;
            sfc.Appearance.Options.UseForeColor = true;
            sfc.Column = bandedGridView_Main.Columns["DAY_WEEK"];
            sfc.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
            sfc.Expression = "[HOL_YN]  == 'Y'";
            sfc.Appearance.Font = new Font(this.Font, FontStyle.Bold);
            sfc.Appearance.Options.UseFont = true;
            bandedGridView_Main.FormatConditions.Add(sfc);

            this.Shown += (sender, e) =>
            {
                try
                {
                    SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                        ClientClass.CreateSqlParameter("MENU_ID", SqlDbType.NVarChar, this.m_MenuID),
                        ClientClass.CreateSqlParameter("USR_ID", SqlDbType.NVarChar, ConstClass._USR_ID)
                    };
                    ResultData resultComp = ClientClass.GetData("GetCompanyListByMenuUser", aParam);
                    if (resultComp.isError)
                    {
                        throw new Exception(resultComp.ResultValue);
                    }

                    UtilClass.SetLookup(lookUpEdit_COMP_CD, resultComp.TableData, "COMP_CD", "COMP_NM", true);

                    m_Loading = false;
                    comboBoxEdit_YY.SelectedIndex = comboBoxEdit_YY.Properties.Items.IndexOf(DateTime.Now.ToString("yyyy"));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Close();
                }
            };
        }

        public override void searchProc()
        {
            try
            {
                if (m_Loading)
                {
                    return;
                }
                if (UtilClass.isNull(lookUpEdit_COMP_CD.EditValue) || UtilClass.isNull(comboBoxEdit_YY.Text))
                {
                    return;
                }

                this.Cursor = Cursors.WaitCursor;

                gridControl_Main.DataSource = null;

                SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                    ClientClass.CreateSqlParameter("COMP_CD", SqlDbType.NVarChar, lookUpEdit_COMP_CD.EditValue),
                    ClientClass.CreateSqlParameter("YY", SqlDbType.NVarChar, comboBoxEdit_YY.Text)
                };
                ResultData resultData = ClientClass.GetData("GetWorkdayManager", aParam);
                if (resultData.isError)
                {
                    throw new Exception(resultData.ResultValue);
                }

                gridControl_Main.DataSource = resultData.TableData;

                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show(ex.Message);
            }
        }

        public override void excelProc()
        {
            this.ExportExcelGrid(gridControl_Main);
        }

        public override void saveProc()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                bandedGridView_Main.PostEditor();
                bandedGridView_Main.UpdateCurrentRow();
                Application.DoEvents();

                DataTable aData = gridControl_Main.DataSource as DataTable;
                if (aData.GetChanges() == null)
                {
                    throw new Exception("변경된 내용이 없습니다.");
                }

                DataTable srcData = UtilClass.GetDataTable(aData, true, "COMP_CD", "WORK_DAY", "HOL_YN", "HOL_NM");

                srcData.TableName = "ADATA";
                ResultData data1 = new ResultData();
                data1.TableData = srcData;
                ResultData[] dataList = new ResultData[] { data1 };

                SerializedSqlParam[] aParam = new SerializedSqlParam[]
                {
                    ClientClass.CreateSqlParameter("REG_ID", SqlDbType.NVarChar, ConstClass._USR_ID)
                };

                ResultData result = ClientClass.SetTableData("SaveWorkdayManager", dataList, aParam);
                if (result.isError)
                {
                    throw new Exception(result.ResultValue);
                }

                this.Cursor = Cursors.Arrow;
                MessageBox.Show("저장 완료");
                searchProc();
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show(ex.Message);
            }
        }
    }
}