using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Data;

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 등록 상품 리스트 조회 후 상품 선택
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Win
{
    public partial class frmDiscountList : Form
    {

        #region 변수 선언

        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 IDataProcessUs m_cDataSrv = null;
        
        private bool bNotDoubleClik = false;                                        // 더블 클릭 방지용

        /// <summary>
        /// 리스트 데이터
        /// </summary>
        private DataTable m_dtGridList;

        #endregion 변수 선언

        #region 생성자 & 소멸자 & 폼초기화
        public frmDiscountList()
        {
            InitializeComponent();

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

            m_cPosStatus = (PosStatus)StateObject.POS;          // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;        // POS 거래정보 

            m_cDataSrv = (IDataProcessUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_SERVICE);            
        }

        private void frmDiscountList_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();
            InitializeGrid();
            DisplayGrid();
        }

        private void frmDiscountList_Activated(object sender, EventArgs e)
        {
            
        }

        private void frmDiscountList_Deactivate(object sender, EventArgs e)
        {
            
        }

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

        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            try
            {
                // 이미지 로딩 처리
                picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_S);
                
                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, true, m_cPosStatus.Sale.ScreenSizeUser);

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

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

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

                bNotDoubleClik = false;
            }
            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 InitializeGrid()
        {
            try
            {
                gridList.CsmGridColumnHeadersFont = new Font(m_cPosStatus.Base.FONT, 10, gridList.CsmGridColumnHeadersFont.Style);
               
                //DataGridView 자체의 컬럼 헤더 Visible 여부
                gridList.CsmGridColumnHeadersVisible = true;
                //DataGridView 자체의 컬럼 헤더 높이
                gridList.CsmGridColumnHeadersHeight = 37;
                gridList.CsmGridRowsHeight = 39;

                //그리드의 컬럼 수
                gridList.CsmGridColumnCount = 5;
                //그리드의 한 화면에 보이는 로우수
                gridList.CsmGridShowPageRowsCount = 7;

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

                //각 컬럼별 이름 지정
                gridList.CsmGridColumnName(0, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0015));
                gridList.CsmGridColumnName(1, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0017));
                gridList.CsmGridColumnName(2, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0018));
                gridList.CsmGridColumnName(3, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0019));
                gridList.CsmGridColumnName(4, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0020));

                //컬럼넓이 지정(406)
                gridList.CsmGridSetColumnWidth(new int[] {250, 70, 90, 100, 200});

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

                m_dtGridList = new DataTable("GRID_LIST");
                m_dtGridList.Columns.Add(new DataColumn("PLU_NM", typeof(string)));
                m_dtGridList.Columns.Add(new DataColumn("PLU_QTY", typeof(string)));
                m_dtGridList.Columns.Add(new DataColumn("PLU_DIS", typeof(string)));
                m_dtGridList.Columns.Add(new DataColumn("PLU_AMT", typeof(string)));
                m_dtGridList.Columns.Add(new DataColumn("PLU_ETC", typeof(string)));
                m_dtGridList.Clear();
            }
            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 DisplayGrid()
        {
            try
            {
                ICampaignUs cCampaingMain = (ICampaignUs)sManager.InitServiceInstance(ServiceLists.ASV_CAMPAIGN.DLL, ServiceLists.ASV_CAMPAIGN.CAMPAIGN_MAIN);

                m_dtGridList.Clear();

                ArrayList alPluItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);
                for (int i = 0; i < alPluItem.Count; i++)
                {
                    Column.TR_PLU.DATA cPluItem = (Column.TR_PLU.DATA)alPluItem[i];

                    if (cPluItem.CANCEL_DIV == "1" || cPluItem.CANCEL_DIV_MAIN == "1") continue;

                    string sEtcName = "";
                    if (cPluItem.TOT_DC_AMT == 0) continue;
                    
                    if (cPluItem.SET_DC_AMT > 0 ) 
                        sEtcName = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0830);
                    if (cPluItem.ITEM_DC_AMT > 0 || cPluItem.SUM_DC_AMT > 0)
                        sEtcName = PosMstManager.GetMstPayDc(cPluItem.DC_DIV.Substring(0, 2), cPluItem.DC_DIV.Substring(2, 2), PosMst.MST_PAY_DC.DATA.PAY_DC_NM);
                    if (cPluItem.COOP_CARD_DC_AMT > 0)
                        sEtcName = PosMstManager.GetMstPayDc(cPluItem.COOP_CARD_DC_DIV.Substring(0, 2), cPluItem.COOP_CARD_DC_DIV.Substring(2, 2), PosMst.MST_PAY_DC.DATA.PAY_DC_NM);
                    if (cPluItem.CPN_DC_AMT > 0)
                        sEtcName = PosMstManager.GetMstPayDc(cPluItem.CPN_DC_DIV.Substring(0, 2), cPluItem.CPN_DC_DIV.Substring(2, 2), PosMst.MST_PAY_DC.DATA.PAY_DC_NM);
                    if (cPluItem.EMP_DC_AMT > 0)
                        sEtcName = PosMstManager.GetMstPayDc(cPluItem.EMP_DC_DIV.Substring(0, 2), cPluItem.EMP_DC_DIV.Substring(2, 2), PosMst.MST_PAY_DC.DATA.PAY_DC_NM);
                    if (cPluItem.CPI_DC_AMT > 0)
                    {
                        // 캠페인명 설정
                        if (cPluItem.alTrSaleCpi != null)
                        {
                            foreach (Column.TR_SALE_CPI_RSLT.DATA cTrPluCpi in cPluItem.alTrSaleCpi)
                            {
                                if (cTrPluCpi.DC_SUM_AMT > 0) sEtcName = cCampaingMain.GetCampaignName(cTrPluCpi.CPI_CD, 0);
                            }
                        }
                    }
                    if (cPluItem.EXCEP_DC_AMT > 0)
                    {
                        sEtcName = (sEtcName.Trim() != "" ? sEtcName = sEtcName + "\n" : "");
                        sEtcName += PosMstManager.GetMstPayDc(cPluItem.EXCEP_DC_DIV.Substring(0, 2), cPluItem.EXCEP_DC_DIV.Substring(2, 2), PosMst.MST_PAY_DC.DATA.PAY_DC_NM);
                    }
                    if (cPluItem.ETC_DC_AMT > 0)
                    {
                        sEtcName = (sEtcName.Trim() != "" ? sEtcName = sEtcName + "\n" : "");
                        sEtcName += PosMstManager.GetMstPayDc(cPluItem.ETC_DC_DIV.Substring(0, 2), cPluItem.ETC_DC_DIV.Substring(2, 2), PosMst.MST_PAY_DC.DATA.PAY_DC_NM);
                    }
                    DataRow drNewRow = m_dtGridList.NewRow();
                    drNewRow["PLU_NM"] = cPluItem.ITEM_NAME;
                    drNewRow["PLU_QTY"] = cPluItem.SALE_QTY;
                    drNewRow["PLU_DIS"] = m_cPosStatus.Global.NumericTOCurrency(cPluItem.TOT_DC_AMT);
                    drNewRow["PLU_AMT"] = m_cPosStatus.Global.NumericTOCurrency(cPluItem.BILL_AMT);
                    drNewRow["PLU_ETC"] = sEtcName;

                    m_dtGridList.Rows.Add(drNewRow);
                }
                gridList.CsmGridDataSource = m_dtGridList;
                gridList.CsmGridSelectRow(0); 
            }
            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 버튼 입력 처리
        /// <summary>
        /// 버튼 입력 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProc_Click(object sender, EventArgs e)
        {
            try
            {
                if (bNotDoubleClik) return;
                bNotDoubleClik = true;

                if (((Cosmos.UI.CsmButton)sender) == btnUp)
                {
                    gridList.CsmGridScroll("UP");
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnDown)
                {
                    gridList.CsmGridScroll("DOWN");
                }
            }
            catch (Exception ex)
            {
               WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
               bNotDoubleClik = false;
            }
            bNotDoubleClik = false;
        }

        /// <summary>
        /// 닫기
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                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);
            }
        }
        #endregion
    }
}
