using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;

namespace Cosmos.Win
{
    public partial class frmHappyOrderBarcodeRegMain : Form
    {
        private SManager sManager = new SManager();                                 //이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   //StateObject : StateServer Object(객체)
        private PosStatus m_cPosStatus;                                             //거래정보 참조

        /// <summary>
        /// Grid 연동 DataTable
        /// </summary>
        private DataTable m_dtGrid;

        public frmHappyOrderBarcodeRegMain()
        {
            try
            {
                InitializeComponent();

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

                m_cPosStatus = (PosStatus)StateObject.POS;      //POS 기본정보
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void frmHappyOrderBarcodeRegMain_Load(object sender, EventArgs e)
        {
            try
            {
                UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");
                InitControls();

                RegisterItemOnGrid();
                ReadyToBarcode();
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void frmHappyOrderBarcodeRegMain_FormClosed(object sender, FormClosedEventArgs 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);
        }

        private void RegisterItemOnGrid()
        {
            try
            {
                ArrayList aSaleItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);

                m_dtGrid.Clear();
                int nCnt = 0;
                foreach (Column.TR_PLU.DATA cPluItem in aSaleItem)
                {
                    if(cPluItem.FLAVOUR_DIV == ItemConst.PLU_FLAVOUR_DIV.FLAVOUR)
                    {
                        continue;
                    }
                    DataRow drNew = m_dtGrid.NewRow();
                    drNew["NO"] = Convert.ToString(++nCnt);
                    drNew["ITEM_NM"] = cPluItem.ITEM_NAME;
                    drNew["QTY"] = Convert.ToString(cPluItem.SALE_QTY);
                    drNew["PRICE"] = m_cPosStatus.Global.NumericTOCurrency(cPluItem.BILL_AMT); ;
                    drNew["BARCODE"] = cPluItem.ITEM_BAR_CD;
                    drNew["BLANK"] = string.Empty;
                    m_dtGrid.Rows.Add(drNew);
                }
                gridHappyOrderList.CsmGridDataSource = m_dtGrid;
                gridHappyOrderList.CsmGridSelectRow(0);
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void InitControls()
        {
            try
            {
                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);

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

                lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0483);

                InitializeGrid();

                btnGridUp.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BUTTON_GRID_UP_ARROW);
                btnGridUp.ImageAlign = ContentAlignment.MiddleCenter;
                btnGridUp.BorderStyle = BorderStyle.None;

                btnGridDown.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BUTTON_GRID_DOWN_ARROW);
                btnGridUp.ImageAlign = ContentAlignment.MiddleCenter;
                btnGridDown.BorderStyle = BorderStyle.None;

                btnDel.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0482);
                btnReg.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0126);

                tbBarcodeNum.Text = string.Empty;
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        /// <summary>
        /// Grid 초기화
        /// </summary>
        private void InitializeGrid()
        {
            try
            {
                //DataGridView 자체의 컬럼 헤더 Visible 여부
                gridHappyOrderList.CsmGridColumnHeadersVisible = true;
                //DataGridView 자체의 컬럼 헤더 높이
                gridHappyOrderList.CsmGridColumnHeadersHeight = 40;
                //DataGridView 컬럼 폰트 설정
                gridHappyOrderList.CsmGridColumnHeadersFont = new Font(m_cPosStatus.Base.FONT, 11, FontStyle.Bold);

                //DataGridView 컬럼 수
                gridHappyOrderList.CsmGridColumnCount = 6;

                gridHappyOrderList.CsmGridSetHeaderBackColor(236, 238, 239);                             //그리드 헤더 배경색
                gridHappyOrderList.CsmGridDefaultRowBackColor = Color.FromArgb(255, 255, 255);           //그리드 홀수(Default)행의 배경색 지정
                gridHappyOrderList.CsmGridAlternateRowBackColor = Color.FromArgb(251, 253, 255);         //그리드 짝수(Alternate)행의 배경색 지정
                gridHappyOrderList.CsmGridBackGroundColor = Color.FromArgb(251, 253, 255);               //기본 백그라운드 컬러
                gridHappyOrderList.CsmGridHighlightColor = Color.FromArgb(255, 251, 211);                //그리드 선택 백 컬러
                gridHappyOrderList.CsmGridHighlightTextColor = Color.Black;                              //그리드 선택 글자 컬러

                //각 컬럼별 이름 설정
                gridHappyOrderList.CsmGridColumnName(0, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0014));    // NO
                gridHappyOrderList.CsmGridColumnName(1, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0015));    // 상품명
                gridHappyOrderList.CsmGridColumnName(2, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0017));    // 수량
                gridHappyOrderList.CsmGridColumnName(3, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0019));    // 금액
                gridHappyOrderList.CsmGridColumnName(4, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0480));    // 바코드
                gridHappyOrderList.CsmGridColumnName(5, string.Empty);                                                  // Blank

                //컬럼별 정렬 기준 설정(0:왼쪽, 1:가운데, 2:오른쪽)
                gridHappyOrderList.CsmGridAlignment(new int[] { 1, 0, 1, 2, 2, 0 });

                //DataGridView 한 화면에 보이는 Row 수
                gridHappyOrderList.CsmGridShowPageRowsCount = 6;
                //DataGridView Row의 헤더 높이
                gridHappyOrderList.CsmGridRowsHeight = 36;
                //컬럼 넓이 지정  // 전체너비 708->744 변경되어 수정
                gridHappyOrderList.CsmGridSetColumnWidth(new int[] { 46, 256, 56, 116, 238, 32 });
                
                m_dtGrid = new DataTable();
                m_dtGrid.Columns.Add(new DataColumn("NO", typeof(string)));
                m_dtGrid.Columns.Add(new DataColumn("ITEM_NM", typeof(string)));
                m_dtGrid.Columns.Add(new DataColumn("QTY", typeof(string)));
                m_dtGrid.Columns.Add(new DataColumn("PRICE", typeof(string)));
                m_dtGrid.Columns.Add(new DataColumn("BARCODE", typeof(string)));
                m_dtGrid.Columns.Add(new DataColumn("BLANK", typeof(string)));
                m_dtGrid.Clear();

                gridHappyOrderList.CsmGridDataSource = m_dtGrid;
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        /// <summary>
        /// 텍스트박스에 포커스를 고정
        /// </summary>
        private void ReadyToBarcode()
        {
            try
            {
                tbBarcodeNum.Select();
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void ControlOnFormClick(object sender, EventArgs e)
        {
            try
            {
                ReadyToBarcode();
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void btnProc_Click(object sender, EventArgs e)
        {
            try
            {
                if ((Cosmos.UI.CsmButton)sender == btnDel)           // 다시
                {
                    for(int nLoop = 0;nLoop < m_dtGrid.Rows.Count;nLoop++)
                    {
                        m_dtGrid.Rows[nLoop]["BARCODE"] = string.Empty;
                    }
                    gridHappyOrderList.Refresh();
                    ReadyToBarcode();
                }
                else if ((Cosmos.UI.CsmButton)sender == btnReg)      // 등록
                {
                    ArrayList aSaleItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);

                    int nCnt = 0;
                    foreach (Column.TR_PLU.DATA cPluItem in aSaleItem)
                    {
                        if (cPluItem.FLAVOUR_DIV == ItemConst.PLU_FLAVOUR_DIV.FLAVOUR)
                        {
                            continue;
                        }

                        cPluItem.ITEM_BAR_CD = m_dtGrid.Rows[nCnt++]["BARCODE"].ToString();
                    }

                    this.DialogResult = DialogResult.OK;
                    this.Close();                    
                }
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void tbBarcodeNum_KeyDown(object sender, KeyEventArgs e)
        {
            string sInputData = string.Empty;
            try
            {
                if(e.KeyCode == Keys.Enter)
                {
                    sInputData = tbBarcodeNum.Text;
                    tbBarcodeNum.Text = string.Empty;

                    m_dtGrid.Rows[gridHappyOrderList.CsmGridSelectedRowIndex]["BARCODE"] = sInputData;

                    int iCurrentIndex = gridHappyOrderList.CsmGridSelectedRowIndex;
                    if(m_dtGrid.Rows.Count >= (iCurrentIndex + 1) + 1)
                    {
                        gridHappyOrderList.CsmGridSelectRow(iCurrentIndex + 1);
                    }

                    gridHappyOrderList.Refresh();
                    ReadyToBarcode();
                }
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void btnGrid_Click(object sender, EventArgs e)
        {
            try
            {
                if ((Cosmos.UI.CsmButton)sender == btnGridUp)
                {
                    gridHappyOrderList.CsmGridScroll("UP");
                }
                else if ((Cosmos.UI.CsmButton)sender == btnGridDown)
                {
                    gridHappyOrderList.CsmGridScroll("DOWN");
                }
                ReadyToBarcode();
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void btnGrid_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                ((Cosmos.UI.CsmButton)sender).BorderStyle = BorderStyle.FixedSingle;
                ReadyToBarcode();
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void btnGrid_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                ((Cosmos.UI.CsmButton)sender).BorderStyle = BorderStyle.None;
                ReadyToBarcode();
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }

        private void gridHappyOrderList_GridClickEvent(object sender, string rowIndex)
        {
            ReadyToBarcode();
        }

        
    }
}
