using ClientLib;
using ClientLib.CommonService;
using DevExpress.XtraEditors;
using PublicLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace HANMI_SALE
{
    partial class SelectOutPosForm : XtraForm
    {
        private string m_CUST_CD;
        private string m_CUST_NN;
        public string m_LOCATION_NM;

        public SelectOutPosForm(string cust_cd, string cust_nm)
        {
            InitializeComponent();

            m_CUST_CD = cust_cd;
            m_CUST_NN = cust_nm;

            textEdit_CUST_CD.Text = cust_cd;
            textEdit_CUST_NM.Text = cust_nm;

            this.Shown += (sender, e) =>
            {
                searchProc();
            };

            textEdit_LOCATION_NM.KeyPress += (sender, e) =>
            {
                if (e.KeyChar == '\r')
                {
                    searchProc();
                }
            };

            simpleButton_Text_Clear.Click += (sender, e) =>
            {
                textEdit_LOCATION_NM.Text = "";
            };

            simpleButton_Search.Click += (sender, e) =>
            {
                searchProc();
            };

            simpleButton_SAVE.Click += (sender, e) =>
            {
                if (UtilClass.isNull(textEdit_LOCATION_NM.Text))
                {
                    return;
                }
                saveProc();
            };

            simpleButton_RELOAD.Click += (sender, e) =>
            {
                textEdit_LOCATION_NM.Text = "";
                searchProc();
            };

            simpleButton_DELETE.Click += (sender, e) =>
            {
                if (XtraMessageBox.Show("삭제하시겠습니까?", "삭제", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }

                deleteProc();
            };

            gridView_Main.FocusedRowChanged += (sender, e) =>
            {
                DataRow row = gridView_Main.GetFocusedDataRow();
                if (row == null)
                {
                    return;
                }
                textEdit_LOCATION_NM.Text = UtilClass.toStr(row["LOCATION_NM"]);
            };

            gridView_Main.KeyPress += (sender, e) =>
            {
                if (e.KeyChar == '\r')
                {
                    DataRow row = gridView_Main.GetFocusedDataRow();
                    if (row != null)
                    {
                        m_LOCATION_NM = UtilClass.toStr(row["LOCATION_NM"]);
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            };

            gridView_Main.DoubleClick += (sender, e) =>
            {
                DataRow row = gridView_Main.GetFocusedDataRow();
                if (row != null)
                {
                    m_LOCATION_NM = UtilClass.toStr(row["LOCATION_NM"]); 
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            };

            simpleButton_SELECT.Click += (sender, e) =>
            {
                DataRow row = gridView_Main.GetFocusedDataRow();
                if (row != null)
                {
                    m_LOCATION_NM = UtilClass.toStr(row["LOCATION_NM"]);
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            };


            simpleButton_Cancel.Click += (sender, e) =>
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            };
        }

        public void searchProc()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                gridControl_Main.DataSource = null;

                SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                    ClientClass.CreateSqlParameter("COMP_CD", SqlDbType.NVarChar, ConstClass._COMP_CD),
                    ClientClass.CreateSqlParameter("CUST_CD", SqlDbType.NVarChar, textEdit_CUST_CD.Text),
                    ClientClass.CreateSqlParameter("LOCATION_NM", SqlDbType.NVarChar, textEdit_LOCATION_NM.Text)
                };
                ResultData resultData = ClientClass.GetData("GetCustLocationSelectPopup", aParam);
                if (resultData.isError)
                {
                    throw new Exception(resultData.ResultValue);
                }

                gridControl_Main.DataSource = resultData.TableData;

                gridView_Main.Focus();

                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                XtraMessageBox.Show(ex.Message);
            }
        }

        private void saveProc()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                gridControl_Main.DataSource = null;

                SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                    ClientClass.CreateSqlParameter("MODE", SqlDbType.NVarChar, "I"),
                    ClientClass.CreateSqlParameter("COMP_CD", SqlDbType.NVarChar, ConstClass._COMP_CD),
                    ClientClass.CreateSqlParameter("CUST_CD", SqlDbType.NVarChar, textEdit_CUST_CD.Text),
                    ClientClass.CreateSqlParameter("LOCATION_NM", SqlDbType.NVarChar, textEdit_LOCATION_NM.Text),
                    ClientClass.CreateSqlParameter("REG_ID", SqlDbType.NVarChar, ConstClass._USR_ID),
                };
                ResultData resultData = ClientClass.SetData("SaveCustLocationSelectPopup", aParam);
                if (resultData.isError)
                {
                    throw new Exception(resultData.ResultValue);
                }

                searchProc();

                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                XtraMessageBox.Show(ex.Message);
            }
        }

        private void deleteProc()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                gridControl_Main.DataSource = null;

                SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                    ClientClass.CreateSqlParameter("MODE", SqlDbType.NVarChar, "D"),
                    ClientClass.CreateSqlParameter("COMP_CD", SqlDbType.NVarChar, ConstClass._COMP_CD),
                    ClientClass.CreateSqlParameter("CUST_CD", SqlDbType.NVarChar, textEdit_CUST_CD.Text),
                    ClientClass.CreateSqlParameter("LOCATION_NM", SqlDbType.NVarChar, textEdit_LOCATION_NM.Text),
                    ClientClass.CreateSqlParameter("REG_ID", SqlDbType.NVarChar, ConstClass._USR_ID),
                };
                ResultData resultData = ClientClass.SetData("SaveCustLocationSelectPopup", aParam);
                if (resultData.isError)
                {
                    throw new Exception(resultData.ResultValue);
                }

                searchProc();

                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                XtraMessageBox.Show(ex.Message);
            }

        }

    }
}