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;

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

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

        protected IDataServiceUs m_cTranResend = null;                              // 매출 재전송 서비스
        private IDataServiceUs m_cResendTranToOutside = null;                       // 도로공사 매출 재전송 서비스

        private string m_sPosMenuKey;                                               // 입력코드
        public string SetPosMenuKey { set { this.m_sPosMenuKey = value; } get { return m_sPosMenuKey; } }

        private string m_sInputSaleDt = "";
        #endregion

        public frmTranResend()
        {
            InitializeComponent();

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

            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cTranResend = (IDataServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_ADMIN.DLL, ServiceLists.BSV_ADMIN.TRAN_RESEND);
            m_cResendTranToOutside = (IDataServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_ADMIN.DLL, ServiceLists.BSV_ADMIN.RESEND_TRAN_TO_OUTSIDE);
        }

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

                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 = "";

                // 타이틀
                if (m_sPosMenuKey == PosKey.MENU_KEY.EX_RESEND_SALE) lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0874);
                else lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0596);
                lblTitle1.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0597);
                lblTitle2.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0598);
                lblTitle3.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0599);

                btnEnter.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0600);

                lblSaleDt.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0242);//2017.12.20;다국어 처리;girak.kim;   

                txtDate.InputTypeFormat = m_cPosStatus.Global.m_stCultureMaster.strDatePattern;
                txtDate.Text = m_cPosStatus.Global.DateToCulture(m_cPosStatus.Base.SaleDate);

                lblTotTranCnt.Text = "0";
                lblOkCnt.Text = "0";
                lblFailCnt.Text = "0";

                tmrRefresh.Enabled = false;
                pgbMarquee.Visible = 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);
            }
        }

        private void frmTranResend_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 frmTranResend_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 + "()", this.Text);
        }

        private int CheckValidateSaleDt(string sInputSaleDt)
        {
            int iRet = UserCom.NG;
            try
            {
                string sMaxDt = m_cPosStatus.Base.SaleDate;
                string sMinDt = DateTime.Now.AddMonths(-3).ToString("yyyyMMdd");

                if(long.Parse(sInputSaleDt) > long.Parse(sMaxDt))
                {
                    iRet = UserCom.NG1;
                }
                else if(long.Parse(sInputSaleDt) < long.Parse(sMinDt))
                {
                    iRet = UserCom.NG2;
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }

            return iRet;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                tmrRefresh.Enabled = false;

                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);
            }
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            try
            {
                if (pgbMarquee.Visible == true) return;

                m_sInputSaleDt = m_cPosStatus.Global.CultureToDate(txtDate.Text);
                string sRet = string.Empty;
                if (m_sPosMenuKey == PosKey.MENU_KEY.EX_RESEND_SALE)
                {
                    sRet = m_cResendTranToOutside.Execute(new string[] { m_sPosMenuKey, m_sInputSaleDt, m_sInputSaleDt });
                }
                else
                {
                    sRet = m_cTranResend.Execute(new string[] { m_cPosStatus.Base.CmpCd, m_sInputSaleDt, m_cPosStatus.Base.StoreNo });
                }
                if(sRet == UserCom.RST_ERR)
                {
                    WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0374));
                    return;
                }
                else
                {
                    pgbMarquee.Visible = true;
                    tmrRefresh.Enabled = 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);
            }
        }

        private void dtpSaleDate_CloseUp(object sender, EventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        private void tmrRefresh_Tick(object sender, EventArgs e)
        {
            try
            {
                //WinManager.ShowSearchMessage(string.Format(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0231), ""), this);
                
                DataTable dtData = null;
                if (m_sPosMenuKey == PosKey.MENU_KEY.EX_RESEND_SALE)
                {
                    dtData = (DataTable)m_cResendTranToOutside.GetData(new string[] { m_cPosStatus.Base.CmpCd, m_sInputSaleDt, m_sInputSaleDt, m_cPosStatus.Base.StoreNo });
                }
                else
                {
                    dtData = (DataTable)m_cTranResend.GetData(new string[] { m_cPosStatus.Base.CmpCd, m_sInputSaleDt, m_cPosStatus.Base.StoreNo });
                }

                int iTotCnt = CmUtil.GetDataRowInt(dtData.Rows[0], "TOTAL_CNT");
                int iOkCnt = CmUtil.GetDataRowInt(dtData.Rows[0], "OK_CNT");
                int iFailCnt = CmUtil.GetDataRowInt(dtData.Rows[0], "FAIL_CNT");

                lblTotTranCnt.Text = iTotCnt.ToString();
                lblOkCnt.Text = iOkCnt.ToString();
                lblFailCnt.Text = iFailCnt.ToString();

                lblTotTranCnt.Refresh();
                lblOkCnt.Refresh();
                lblFailCnt.Refresh();

                //WinManager.HideSearchMessage(this);

                if(iTotCnt == iOkCnt + iFailCnt)
                {
                    pgbMarquee.Visible = false;
                    tmrRefresh.Enabled = 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);
            }
        }

        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);
            }
        }

        
    }
}
