using System;
using System.Collections;
using System.Windows.Forms;

using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;
using System.Drawing;

namespace Cosmos.Win
{
    public partial class frmRegGuest : Form
    {
        #region Variable

        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private TranStatus m_cTrnStatus = new TranStatus();                         // 거래정보 참조
        private DeviceStatus m_cDevStatus = new DeviceStatus();                     // 디바이스 관리

        private string m_sPosMenuKeyIn;
        public string PosMenuKeyIn { set { m_sPosMenuKeyIn = value; } get { return m_sPosMenuKeyIn; } }
        private string m_sPosMenuKeyOut;
        public string PosMenuKeyOut { set { m_sPosMenuKeyOut = value; } get { return m_sPosMenuKeyOut; } }

        #endregion

        #region 생성자 & 소멸자
        public frmRegGuest()
        {
            InitializeComponent();

            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            //this.UpdateStyles();

            m_cPosStatus = (PosStatus)StateObject.POS;      
            m_cTrnStatus = (TranStatus)StateObject.TRAN;
            m_cDevStatus = (DeviceStatus)StateObject.DEVICE;

            SetLabel();

        }
        /// <summary>
        /// 폼로드
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmRegGuest_Load(object sender, EventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");
            InitControl();

        }
        /// <summary>
        /// 폼 클로즈
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmRegGuest_FormClosing(object sender, FormClosingEventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", lblTitle.Text);
        }
        #endregion

        #region 폼 컨트롤 초기화
        /// <summary>
        /// 라벨 초기화
        /// </summary>
        private void SetLabel()
        {
            try
            {

                this.Size = new Size(788, 402);
                FormManager.MovePopUpForm(this, true, m_cPosStatus.Sale.ScreenSizeUser);

                picBack.Image = ImageManager.GetImage(Cosmos.BaseFrame.BaseCom.NxImgPath, ImageManager.POP_SIZE_S);

                btnExit.Image = ImageManager.GetImage(Cosmos.BaseFrame.BaseCom.NxImgPath, ImageManager.BTN_CLOSE);
                if (btnExit.Image != null) btnExit.Text = "";

                FormManager.SetFormAllControlFont(this, m_cPosStatus.Base.FONT);
                FormManager.SetTextBoxGlobalInfo(this, m_cPosStatus.Global.m_stCultureMaster.nGroupingDigits, m_cPosStatus.Global.m_stCultureMaster.strGroupingSymbol
                    , m_cPosStatus.Global.m_stCultureMaster.nDecimalDigits, m_cPosStatus.Global.m_stCultureMaster.strDecimalSymbol);

                //테마색상 적용!
                btnSave.BackColor = CmUtil.GetColorToString(m_cPosStatus.ScnMst.ThemeColor);
                padInPut.ThemeColor = m_cPosStatus.ScnMst.ThemeColor;
                padInPut.ClearBtnText = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0817);
                padInPut.SearchBtnText = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_1018);

                lblTitleL.Text = string.Format("{0}{1}{2}", "[[", MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0100), "]]");     
                lblTitleR.Text = string.Format("{0}{1}{2}", "[[", MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0099), "]]");
                
                lbl50R.Text = lbl50L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0101);
                lbl40R.Text = lbl40L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0102);
                lbl30R.Text = lbl30L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0103);
                lbl20R.Text = lbl20L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0104);
                lbl10R.Text = lbl10L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0105);
                lblTotal.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0107);

                btn60R.Text = btn60L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0129);
                btn50R.Text = btn50L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0101);
                btn40R.Text = btn40L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0102);
                btn30R.Text = btn30L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0103);
                btn20R.Text = btn20L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0104);
                btn10R.Text = btn10L.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0105);

                btn60L.Tag = "F60";
                btn50L.Tag = "F50";
                btn40L.Tag = "F40";
                btn30L.Tag = "F30";
                btn20L.Tag = "F20";
                btn10L.Tag = "F10";

                btn60R.Tag = "M60";
                btn50R.Tag = "M50";
                btn40R.Tag = "M40";
                btn30R.Tag = "M30";
                btn20R.Tag = "M20";
                btn10R.Tag = "M10";

                // 기능 버튼 설정 체크
                FormManager.SetbtnMenu(null, m_cDevStatus.ICReader.UseYn, btnMenu1, btnMenu2, btnMenu3, btnMenu4);

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
                
            }
        }
        
        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            try
            {
                if (PosMstManager.GetPosOption(POS_OPTION.OPT004) == "2")
                    FormManager.MovePopUpForm(this, true, m_cPosStatus.Sale.ScreenSizeUser);
                else
                    FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);

                ControlVisible(m_sPosMenuKeyIn == "PAY" ? false : true);
                
                if (m_sPosMenuKeyIn == "PAY")
                {
                    //결제 정보일때 처리 없음!
                    lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0130);
                }
                else
                {
                    lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0106);

                    ArrayList alCuestItem = (ArrayList)StateObject.GetItemObject(Column.TR_GUEST.ITEM);

                    if (alCuestItem.Count > 0)
                    {
                        Column.TR_GUEST.DATA clCuestItem = (Column.TR_GUEST.DATA)alCuestItem[0];

                        txt50L.Text = clCuestItem.FMALE_50L.ToString();
                        txt40L.Text = clCuestItem.FMALE_40L.ToString();
                        txt30L.Text = clCuestItem.FMALE_30L.ToString();
                        txt20L.Text = clCuestItem.FMALE_20L.ToString();
                        txt10L.Text = clCuestItem.FMALE_10L.ToString();

                        txt50R.Text = clCuestItem.MALE_50L.ToString();
                        txt40R.Text = clCuestItem.MALE_40L.ToString();
                        txt30R.Text = clCuestItem.MALE_30L.ToString();
                        txt20R.Text = clCuestItem.MALE_20L.ToString();
                        txt10R.Text = clCuestItem.MALE_10L.ToString();

                        TotalSum();
                    }
                    else
                    {
                        txt50L.Text = "";
                        txt40L.Text = "";
                        txt30L.Text = "";
                        txt20L.Text = "";
                        txt10L.Text = "";

                        txt50R.Text = "";
                        txt40R.Text = "";
                        txt30R.Text = "";
                        txt20R.Text = "";
                        txt10R.Text = "";

                        txtTotal.Text = "0";

                        txt50L.Select();
                    }
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion

        #region Control Event

        /// <summary>
        /// 입력박스 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="sFuncValue"></param>
        private void txtEditBox_EditBoxKeyDownEvent(object sender, string sFuncValue)
        {
            try
            {
                if (sFuncValue == PosKey.MENU_KEY.ENTER)
                {
                    if (((Cosmos.UI.CsmPosEditBox)sender) == txt10R)
                    {
                        txt50L.SelectText();
                    }
                    else
                    {
                        SendKeys.Send("{TAB}");
                    }

                    TotalSum();

                }
                else if (sFuncValue == PosKey.MENU_KEY.CLEAR)
                {
                    if (((Cosmos.UI.CsmPosEditBox)sender).ReadOnly == false) ((Cosmos.UI.CsmPosEditBox)sender).Text = "";
                }
                else if (sFuncValue == PosKey.MENU_KEY.ESC_PREVIOUS)
                {

                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        /// <summary>
        /// 입력박스 엔터 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtEditBox_EnterEvent(object sender, EventArgs e)
        {
            try
            {
                
                padInPut.SetActiveControl = (Control)sender;

                TotalSum();
                
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        /// <summary>
        /// 종료
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 저장
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ArrayList alCuestItem = (ArrayList)StateObject.GetItemObject(Column.TR_GUEST.ITEM);

                if (alCuestItem.Count == 0)
                {
                    Column.TR_GUEST.DATA clCuestItem = new Column.TR_GUEST.DATA();

                    clCuestItem.FMALE_50L = CmUtil.IntParse(txt50L.Value);
                    clCuestItem.FMALE_40L = CmUtil.IntParse(txt40L.Value);
                    clCuestItem.FMALE_30L = CmUtil.IntParse(txt30L.Value);
                    clCuestItem.FMALE_20L = CmUtil.IntParse(txt20L.Value);
                    clCuestItem.FMALE_10L = CmUtil.IntParse(txt10L.Value);

                    clCuestItem.MALE_50L = CmUtil.IntParse(txt50R.Value);
                    clCuestItem.MALE_40L = CmUtil.IntParse(txt40R.Value);
                    clCuestItem.MALE_30L = CmUtil.IntParse(txt30R.Value);
                    clCuestItem.MALE_20L = CmUtil.IntParse(txt20R.Value);
                    clCuestItem.MALE_10L = CmUtil.IntParse(txt10R.Value);

                    alCuestItem.Add(clCuestItem);

                }
                else
                {
                    Column.TR_GUEST.DATA clCuestItem = (Column.TR_GUEST.DATA)alCuestItem[0];

                    clCuestItem.FMALE_50L = CmUtil.IntParse(txt50L.Value);
                    clCuestItem.FMALE_40L = CmUtil.IntParse(txt40L.Value);
                    clCuestItem.FMALE_30L = CmUtil.IntParse(txt30L.Value);
                    clCuestItem.FMALE_20L = CmUtil.IntParse(txt20L.Value);
                    clCuestItem.FMALE_10L = CmUtil.IntParse(txt10L.Value);

                    clCuestItem.MALE_50L = CmUtil.IntParse(txt50R.Value);
                    clCuestItem.MALE_40L = CmUtil.IntParse(txt40R.Value);
                    clCuestItem.MALE_30L = CmUtil.IntParse(txt30R.Value);
                    clCuestItem.MALE_20L = CmUtil.IntParse(txt20R.Value);
                    clCuestItem.MALE_10L = CmUtil.IntParse(txt10R.Value);

                }

                // 총 고객수
                int iGuestCnt = CmUtil.IntParse(txt50L.Text) + CmUtil.IntParse(txt40L.Text) + CmUtil.IntParse(txt30L.Text) + CmUtil.IntParse(txt20L.Text) + CmUtil.IntParse(txt10L.Text) 
                              + CmUtil.IntParse(txt50R.Text) + CmUtil.IntParse(txt40R.Text) + CmUtil.IntParse(txt30R.Text) + CmUtil.IntParse(txt20R.Text) + CmUtil.IntParse(txt10R.Text);
                m_cTrnStatus.Head.CustCnt = iGuestCnt;

                // 여자동반자수 
                iGuestCnt = CmUtil.IntParse(txt50L.Text) + CmUtil.IntParse(txt40L.Text) + CmUtil.IntParse(txt30L.Text) + CmUtil.IntParse(txt20L.Text) + CmUtil.IntParse(txt10L.Text);
                m_cTrnStatus.Head.WomanCmpnpsnCnt = iGuestCnt;

                // 남자동반자수 
                iGuestCnt = CmUtil.IntParse(txt50R.Text) + CmUtil.IntParse(txt40R.Text) + CmUtil.IntParse(txt30R.Text) + CmUtil.IntParse(txt20R.Text) + CmUtil.IntParse(txt10R.Text);
                m_cTrnStatus.Head.ManCmpnpsnCnt = iGuestCnt;

                this.DialogResult = DialogResult.OK;
                this.Close();

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);

            }
        }
        /// <summary>
        /// 소수점 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditBoxTextChangedEvnet(object sender, EventArgs e)
        {

            try
            {
                //string stmp = ((Cosmos.UI.CsmPosEditBox)sender).Value;

                //if (stmp.IndexOf(".") != -1 )
                //    ((Cosmos.UI.CsmPosEditBox)sender).Value = Math.Truncate(CmUtil.DoubleParse(stmp)).ToString();

            }
            catch (Exception ex)
            {

                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }


        }

        /// <summary>
        /// 객층 코드
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProc_Click(object sender, EventArgs e)
        {
            try
            {
                m_sPosMenuKeyOut = ((UI.CsmButton)sender).Tag.ToString();

                this.DialogResult = DialogResult.OK;
                this.Close();

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        
        #endregion
        
        #region User Method
        /// <summary>
        /// 고객 합계 
        /// </summary>
        private void TotalSum()
        {
            try 
            {
                int iGuestCnt = 0;
                
                iGuestCnt = CmUtil.IntParse(txt50L.Text) + CmUtil.IntParse(txt40L.Text) + CmUtil.IntParse(txt30L.Text) + CmUtil.IntParse(txt20L.Text) + CmUtil.IntParse(txt10L.Text) + CmUtil.IntParse(txt50R.Text) + CmUtil.IntParse(txt40R.Text) + CmUtil.IntParse(txt30R.Text) + CmUtil.IntParse(txt20R.Text) + CmUtil.IntParse(txt10R.Text);

                txtTotal.Text = iGuestCnt.ToString();

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        /// <summary>
        /// 고객 정보 활성/비활성
        /// </summary>
        /// <param name="bValue"></param>
        private void ControlVisible(bool bValue)
        {
            try
            {
                lbl50R.Visible = bValue;
                lbl50L.Visible = bValue;
                lbl40R.Visible = bValue; 
                lbl40L.Visible = bValue;
                lbl30R.Visible = bValue; 
                lbl30L.Visible = bValue;
                lbl20R.Visible = bValue; 
                lbl20L.Visible = bValue;
                lbl10R.Visible = bValue; 
                lbl10L.Visible = bValue;
                lblTotal.Visible = bValue;

                txt50L.Visible = bValue;
                txt40L.Visible = bValue;
                txt30L.Visible = bValue;
                txt20L.Visible = bValue;
                txt10L.Visible = bValue;
                txt50R.Visible = bValue;
                txt40R.Visible = bValue;
                txt30R.Visible = bValue;
                txt20R.Visible = bValue;
                txt10R.Visible = bValue;
                txtTotal.Visible = bValue;

                btnSave.Visible = bValue;

                PayControlVisible(bValue == true ? false : true);

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }

        }
        /// <summary>
        /// 결제 버튼 활성/비활성
        /// </summary>
        /// <param name="bValue"></param>
        private void PayControlVisible(bool bValue)
        {
            try
            {
                btn60L.Visible = bValue;
                btn60R.Visible = bValue;
                btn50R.Visible = bValue;
                btn50L.Visible = bValue;
                btn40R.Visible = bValue;
                btn40L.Visible = bValue;
                btn30R.Visible = bValue;
                btn30L.Visible = bValue;
                btn20R.Visible = bValue;
                btn20L.Visible = bValue;
                btn10R.Visible = bValue;
                btn10L.Visible = bValue;
                
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }

        }


        #endregion

        


    }
}
