using System;
using System.Collections;
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.CommonManager;
using Cosmos.ServiceProvider;
using Cosmos.Common;

namespace Cosmos.Win
{
    public partial class frmSchKtchOrder : Form
    {
        #region 변수 선언
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private IDataServiceUs m_cSchKtchOrder = null;                          // 본사반품조회
        #endregion 변수 선언

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

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

            m_cPosStatus = (PosStatus)StateObject.POS;             // POS 기본정보
            m_cSchKtchOrder = (IDataServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_BASIC.DLL, ServiceLists.BSV_BASIC.SCH_KTCH_ORDER);  // 본사방품내역조회
        }

        private void frmSchKtchOrder_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();
        }

        private void frmSchKtchOrder_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 InitControl()
        {
            try
            {
                if (m_cPosStatus.Sale.ScreenSizeUser == 1024)
                {
                    // 이미지 로딩 처리
                    picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_FULL_SIZE);
                    //this.Size = new Size(1024, 696);
                    //this.Location = new Point(0, 50);
                    FormManager.SetControlSize(picBack, 0, 0, 1024, 696);
                    FormManager.SetControlSize(this, 0, 50, picBack.Size.Width, picBack.Size.Height);

                    FormManager.SetControlSize(this.lblTitle, 16, 1, 950, 50);
                    FormManager.SetControlSize(this.btnExit, 965, 1, 58, 50);
                    FormManager.SetControlSize(this.dgvData, 16, 161, 964, 518);
                    FormManager.SetControlSize(this.label01, 22, 67, 683, 2);
                    FormManager.SetControlSize(this.lblSearchDate, 22, 68, 122, 40);
                    FormManager.SetControlSize(this.btnDw, 979, 420, 29, 259);
                    FormManager.SetControlSize(this.btnUp, 979, 161, 29, 260);
                    FormManager.SetControlSize(this.btnSearch, 908, 107, 100, 40);
                    FormManager.SetControlSize(this.lblTableNo, 363, 68, 122, 40);
                    FormManager.SetControlSize(this.lblTradeNo, 363, 107, 122, 40);
                    FormManager.SetControlSize(this.lblOrderTime, 22, 107, 122, 40);
                    FormManager.SetControlSize(this.txtTableNo, 500, 75, 189, 26);
                    FormManager.SetControlSize(this.txtTradeNo, 501, 114, 189, 26);
                    FormManager.SetControlSize(this.txtOrderTime, 159, 114, 189, 26);
                    FormManager.SetControlSize(this.btnDate, 322, 75, 26, 26);
                    FormManager.SetControlSize(this.txtDate, 160, 75, 161, 26);
                    FormManager.SetControlSize(this.lblBox4, 484, 107, 221, 40);
                    FormManager.SetControlSize(this.lblBox3, 484, 68, 221, 40);
                    FormManager.SetControlSize(this.lblBox2, 143, 107, 221, 40);
                    FormManager.SetControlSize(this.lblBox1, 138, 68, 226, 40);
                    FormManager.SetControlSize(this.label1, 16, 160, 992, 2);
                }
                else
                {
                    // 이미지 로딩 처리
                    picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_800X600);
                    FormManager.SetControlSize(picBack, 0, 0, 800, 600);
                    FormManager.SetControlSize(this, 0, 0, picBack.Size.Width, picBack.Size.Height);

                    FormManager.SetControlSize(this.lblTitle, 16, 1, 728, 50);
                    FormManager.SetControlSize(this.btnExit, 741, 1, 58, 50);
                    FormManager.SetControlSize(this.dgvData, 22, 161, 729, 416);
                    FormManager.SetControlSize(this.label01, 22, 67, 643, 2);
                    FormManager.SetControlSize(this.lblSearchDate, 22, 68, 122, 40);
                    FormManager.SetControlSize(this.btnDw, 750, 369, 29, 208);
                    FormManager.SetControlSize(this.btnUp, 750, 161, 29, 209);
                    FormManager.SetControlSize(this.btnSearch, 680, 107, 100, 40);
                    FormManager.SetControlSize(this.lblTableNo, 343, 68, 122, 40);
                    FormManager.SetControlSize(this.lblTradeNo, 343, 107, 122, 40);
                    FormManager.SetControlSize(this.lblOrderTime, 22, 107, 122, 40);
                    FormManager.SetControlSize(this.txtTableNo, 480, 75, 169, 26);
                    FormManager.SetControlSize(this.txtTradeNo, 481, 114, 169, 26);
                    FormManager.SetControlSize(this.txtOrderTime, 159, 114, 169, 26);
                    FormManager.SetControlSize(this.btnDate, 302, 75, 26, 26);
                    FormManager.SetControlSize(this.txtDate, 160, 75, 141, 26);
                    FormManager.SetControlSize(this.lblBox4, 464, 107, 201, 40);
                    FormManager.SetControlSize(this.lblBox3, 464, 68, 201, 40);
                    FormManager.SetControlSize(this.lblBox2, 143, 107, 201, 40);
                    FormManager.SetControlSize(this.lblBox1, 143, 68, 201, 40);
                    FormManager.SetControlSize(this.label1, 22, 160, 757, 2);
                }

                //this.Size = new Size(800, 600);
                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);
                FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);

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

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

                btnUp.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.NAVIBTN_14X9_UP1_BASIC);
                if (btnUp.Image != null) btnUp.Text = "";
                btnDw.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.NAVIBTN_14X9_DOWN1_BASIC);
                if (btnDw.Image != null) btnDw.Text = "";


                txtDate.InputTypeFormat = m_cPosStatus.Global.m_stCultureMaster.strDatePattern;
                txtDate.Text = m_cPosStatus.Global.DateToCulture(System.DateTime.Now.ToString("yyyyMMdd"));

                txtTableNo.Text = "";
                txtTradeNo.Text = "";
                txtOrderTime.Text = "";

                // 그리드 설정
                SetListColumn();
            }
            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>
        /// 고객 불편사항 정보에 대한 DataGridView 초기화
        /// </summary>
        private void SetListColumn()
        {
            try
            {
                int[] arColumnsWidth = { 120, 90, 68, 100, 68, 370, 80, 68 };
                int[] arColumnsAlignment = { 1, 1, 1, 1, 1, 1, 1, 1 };

                if (m_cPosStatus.Sale.ScreenSizeUser != 1024)    // 729
                {
                    arColumnsWidth[0] = 100;
                    arColumnsWidth[1] = 80;
                    arColumnsWidth[2] = 63;
                    arColumnsWidth[3] = 100;
                    arColumnsWidth[4] = 63;
                    arColumnsWidth[5] = 185;
                    arColumnsWidth[6] = 75;
                    arColumnsWidth[7] = 63;
                }

                dgvData.Columns.Clear();
                dgvData.Rows.Clear();

                dgvData.Columns.Add("SALE_DT", "영업일자");
                dgvData.Columns.Add("TRADE_NO", "거래번호");
                dgvData.Columns.Add("SEQ", "SEQ");
                dgvData.Columns.Add("ORD_TIME", "거래시간");
                dgvData.Columns.Add("TABLE_NO", "TABLE");
                dgvData.Columns.Add("ITEM_NAME", "아이템명");
                dgvData.Columns.Add("ITEM_DIV", "세트구분");
                dgvData.Columns.Add("SALE_QTY", "수량");
                // dgvData.Columns.Add("NO", MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0014));
                // 위에를 주석과 같이 바꿔야 함.. pss

                dgvData.ColumnHeadersHeight = 38;
                dgvData.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
                dgvData.RowHeadersVisible = false;
                dgvData.AllowUserToAddRows = false;

                //dgvData.Font = new Font(m_cPosStatus.Base.FONT, 12);
                dgvData.Font = new Font(m_cPosStatus.Base.FONT, m_cPosStatus.Sale.ScreenSizeUser == 1024 ? 12 : 10);

                // 헤더 값 정렬
                for (int nLoop = 0; nLoop < dgvData.Columns.Count; nLoop++)
                {
                    dgvData.Columns[nLoop].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgvData.Columns[nLoop].Width = arColumnsWidth[nLoop];
                    dgvData.Columns[nLoop].Resizable = DataGridViewTriState.False;

                    switch (arColumnsAlignment[nLoop])
                    {
                        case 0:
                            dgvData.Columns[nLoop].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                            break;
                        case 1:
                            dgvData.Columns[nLoop].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                            break;
                        default:
                            dgvData.Columns[nLoop].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                            break;
                    }

                    dgvData.Columns[nLoop].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
            }
            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>
        /// 계정선택 DataGridView와 Data 연결
        /// </summary>
        private void GetKtchOrderInfo()
        {
            string sRetData = null;

            string[] sCol = null;
            string[] sRow = null;

            string sSale_Dt = string.Empty;
            string sTrade_No = string.Empty;

            try
            {
                DataTable dtData = new DataTable();

                dgvData.Rows.Clear();

                string sDate = m_cPosStatus.Global.CultureToDate(txtDate.Text);
                dtData = (DataTable)m_cSchKtchOrder.GetData(new string[] { sDate, txtTableNo.Text, txtTradeNo.Text, txtOrderTime.Text });

                if (dtData != null && dtData.Rows.Count > 0)
                {
                    dgvData.Rows.Clear();
                    for (int nLoop = 0; nLoop < dtData.Rows.Count; nLoop++)
                    {
                        DataGridViewRow dgvrItem = new DataGridViewRow();
                        dgvrItem.Height = 30;
                        dgvrItem.Resizable = DataGridViewTriState.False;

                        // 거래일자가 다를 경우에는 화면에 표시해주기 위한 것임
                        if (sTrade_No != CmUtil.GetDataRowStr(dtData.Rows[nLoop], Column.TRAN_HEADER.DB_DATA.TRADE_NO))
                        {
                            sSale_Dt = "";
                        }

                        // 영업일자
                        string sSale_Dt_Format = CmUtil.GetDataRowStr(dtData.Rows[nLoop], Column.TRAN_HEADER.DB_DATA.SALE_DT);
                        sSale_Dt_Format = sSale_Dt_Format.Substring(0, 4) + "-" + sSale_Dt_Format.Substring(4, 2) + "-" + sSale_Dt_Format.Substring(6, 2);

                        //영업시간
                        string sOrderTime = CmUtil.GetDataRowStr(dtData.Rows[nLoop], PosTr.TR_SALE_DETAIL.DATA.ORD_TIME);
                        if (sOrderTime.Length == 6)
                        {
                            sOrderTime = sOrderTime.Substring(0, 2) + ":" + sOrderTime.Substring(2, 2) + ":" + sOrderTime.Substring(4, 2);
                        }

                        // 일자
                        DataGridViewCell dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgvcItem.Style.BackColor = Color.White;
                        dgvcItem.Value = (sSale_Dt == CmUtil.GetDataRowStr(dtData.Rows[nLoop], Column.TRAN_HEADER.DB_DATA.SALE_DT)) ? "" : sSale_Dt_Format;
                        dgvrItem.Cells.Add(dgvcItem);
                        // 거래번호
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgvcItem.Style.BackColor = Color.White;
                        dgvcItem.Value = (sTrade_No == CmUtil.GetDataRowStr(dtData.Rows[nLoop], Column.TRAN_HEADER.DB_DATA.TRADE_NO)) ? "" : CmUtil.GetDataRowStr(dtData.Rows[nLoop], Column.TRAN_HEADER.DB_DATA.TRADE_NO);
                        dgvrItem.Cells.Add(dgvcItem);
                        // SEQ
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgvcItem.Value = CmUtil.GetDataRowStr(dtData.Rows[nLoop], "SEQ");
                        dgvrItem.Cells.Add(dgvcItem);
                        // 거래시간
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgvcItem.Value = sOrderTime;
                        dgvrItem.Cells.Add(dgvcItem);
                        // 테이블번호
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgvcItem.Value = CmUtil.GetDataRowStr(dtData.Rows[nLoop], PosTr.TR_SALE_DETAIL.DATA.TBL_NO);
                        dgvrItem.Cells.Add(dgvcItem);
                        // 아이템명
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                        dgvcItem.Value = CmUtil.GetDataRowStr(dtData.Rows[nLoop], PosMst.MST_ITEM.DATA.SHTCUT_ITEMNM);
                        dgvrItem.Cells.Add(dgvcItem);
                        // 세트구분
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgvcItem.Value = SetDiv(CmUtil.GetDataRowStr(dtData.Rows[nLoop], PosTr.TR_SALE_DETAIL.DATA.ITEM_DIV));
                        dgvrItem.Cells.Add(dgvcItem);
                        // 수량
                        dgvcItem = new DataGridViewTextBoxCell();
                        dgvcItem.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgvcItem.Value = CmUtil.GetDataRowStr(dtData.Rows[nLoop], PosTr.TR_SALE_DETAIL.DATA.SALE_QTY);
                        dgvrItem.Cells.Add(dgvcItem);

                        dgvData.Rows.Add(dgvrItem);

                        sTrade_No = CmUtil.GetDataRowStr(dtData.Rows[nLoop], Column.TRAN_HEADER.DB_DATA.TRADE_NO);
                        sSale_Dt = CmUtil.GetDataRowStr(dtData.Rows[nLoop], Column.TRAN_HEADER.DB_DATA.SALE_DT);

                    }
                }
                else
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0302);
                }
            }
            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);
            }
        }
        #endregion 폼 컨트롤 초기화


        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            GetKtchOrderInfo();
        }

        #region 업/다운 클릭 이벤트
        /// <summary>
        /// 조회 그리드 업/다운 클릭 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelProc_Click(object sender, EventArgs e)
        {

            if ((UI.CsmButton)sender == btnUp)                               // 상단 그리드 Up
            {
                if (dgvData.Rows.Count == 0) return;

                if (dgvData.CurrentRow.Index > 0)
                {
                    for (int iLoop = dgvData.CurrentRow.Index - 1; iLoop >= 0; iLoop--)
                    {
                        if (dgvData.Rows[iLoop].Visible == false) continue;

                        dgvData.Rows[iLoop].Selected = true;
                        dgvData.Rows[iLoop].Cells[dgvData.CurrentCell.ColumnIndex].Selected = true;
                        break;
                    }
                }
            }
            else if ((UI.CsmButton)sender == btnDw)                          // 상단 그리드 Down
            {
                if (dgvData.Rows.Count == 0) return;

                if (dgvData.CurrentRow.Index < dgvData.Rows.Count - 1)
                {
                    for (int iLoop = dgvData.CurrentRow.Index + 1; iLoop <= dgvData.Rows.Count - 1; iLoop++)
                    {
                        if (dgvData.Rows[iLoop].Visible == false) continue;

                        dgvData.Rows[iLoop].Selected = true;
                        dgvData.Rows[iLoop].Cells[dgvData.CurrentCell.ColumnIndex].Selected = true;
                        break;
                    }
                }

            }
        }
        #endregion 업/다운 클릭 이벤트

        private string SetDiv(string sSetFlag)
        {
            string sRet = "";

            try
            {
                switch (sSetFlag)
                {
                    case ItemConst.PLU_ITEM_DIV.NORMAL:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0910);
                        break;
                    case ItemConst.PLU_ITEM_DIV.SET_MAIN:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0911);
                        break;
                    case ItemConst.PLU_ITEM_DIV.SET_DETL:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0912);
                        break;
                    case ItemConst.PLU_ITEM_DIV.OPT_MENU:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0913);
                        break;
                    case ItemConst.PLU_ITEM_DIV.SET_DIS:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0914);
                        break;
                    case ItemConst.PLU_ITEM_DIV.GIFT:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0915);
                        break;
                    case ItemConst.PLU_ITEM_DIV.PREPAID:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0305);
                        break;
                    default:
                        sRet = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0007);
                        break;
                }
                return sRet;
            }
            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);
            }
            return sRet;
        }

        private void btnDate_Click(object sender, EventArgs e)
        {
            try
            {
                string sDate = WinBasic.ShowForm(new string[] { FormManager.FORM_CALENDAR });
                if (sDate != "") txtDate.Text = m_cPosStatus.Global.DateToCulture(sDate);
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
    }
}
