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_STD
{
    partial class InsertGroupForm : XtraForm
    {
        string m_CATE_CD = "";
        string m_RowStatus = "ADD";

        public InsertGroupForm(string cate_cd)
        {
            InitializeComponent();

            m_CATE_CD = cate_cd;

            simpleButton_Save.Click += (sender, e) =>
            {
                saveProc(false);
            };

            simpleButton_SaveClose.Click += (sender, e) =>
            {
                saveProc(true);
            };

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

        public void setData(DataRow row)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                m_RowStatus = "MOD";
                textEdit_GRP_CD.ReadOnly = true;
                m_CATE_CD = UtilClass.toStr(row["CATE_CD"]);
                textEdit_GRP_CD.Text = UtilClass.toStr(row["GRP_CD"]);
                textEdit_GRP_CD_NM.Text = UtilClass.toStr(row["GRP_CD_NM"]);
                textEdit_PRNNO.Text = UtilClass.toStr(row["PRNNO"]);

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


        private void saveProc(bool candClose)
        {
            if (UtilClass.isNull(textEdit_GRP_CD.Text))
            {
                textEdit_GRP_CD.Focus();
                throw new Exception("그룹코드를 입력하십시오.");
            }

            if (UtilClass.isNull(textEdit_GRP_CD_NM.Text))
            {
                textEdit_GRP_CD_NM.Focus();
                throw new Exception("그룹명 입력하십시오.");
            }


            try
            {
                this.Cursor = Cursors.WaitCursor;

                SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                    ClientClass.CreateSqlParameter("ROW_STATUS", SqlDbType.NVarChar, m_RowStatus),
                    ClientClass.CreateSqlParameter("CATE_CD", SqlDbType.NVarChar, m_CATE_CD),
                    ClientClass.CreateSqlParameter("GRP_CD", SqlDbType.NVarChar, textEdit_GRP_CD.Text),
                    ClientClass.CreateSqlParameter("GRP_CD_NM", SqlDbType.NVarChar, textEdit_GRP_CD_NM.Text),
                    ClientClass.CreateSqlParameter("PRNNO", SqlDbType.Int, UtilClass.toInt(textEdit_PRNNO.Text)),
                    ClientClass.CreateSqlParameter("RESULT_STR", SqlDbType.NVarChar, "", ParameterDirection.Output, 1000)
                };
                ResultData result = ClientClass.SetData("SaveCodeGroupInfo", aParam);
                if (result.isError)
                {
                    throw new Exception(result.ResultValue);
                }

                textEdit_GRP_CD.Text = "";
                textEdit_GRP_CD_NM.Text = "";
                textEdit_PRNNO.Text = "";

                if (candClose)
                {
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }

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


        }
    }
}