using FarPoint.Excel.EntityClassLibrary.SpreadsheetML;
using FCOMMON;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Windows.Forms;

namespace FCM0000.Item
{
    public partial class fItemAdd : FCOMMON.fBase
    {
        public int newIDX = -1;
        public string newSID = string.Empty;
        public fItemAdd(string sidname)
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            this.ControlBox = true;
            this.MaximizeBox = true;
            this.MinimizeBox = false;
            tbSid.Text = sidname;
            foreach (Control ctl in this.Controls)
            {
                if (ctl.GetType() == typeof(TextBox) || ctl.GetType() == typeof(ComboBox))
                {
                    ctl.KeyDown += ctl_KeyDown;
                }
            }
        }

        void ctl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Control ctl = sender as Control;
                string nm = ctl.Name.ToLower();
                string search = ctl.Text.Trim();
                SendKeys.Send("{TAB}");
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            EnsureVisibleAndUsableSize();
            //Application.DoEvents();

            if (tbSid.Text.isEmpty()) tbSid.Focus();
            else tbName.Focus();
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Trim() == "")
            {
                FCOMMON.Util.MsgE("품명은 필수 입니다");
                tbName.Focus();
                return;
            }

            //if (tbModel.Text.Trim() == "")
            //{
            //    FCOMMON.Util.MsgE("모델은 필수 입니다");
            //    tbModel.Focus();
            //    return;
            //}

            if (tbSid.Text.StartsWith("신규")) tbSid.Text = "신규";
            else
            {
                //이떄는 sid가 정상이어야 한다.
                var siddata = tbSid.Text.Split('-')[0].Trim();
                if (siddata.StartsWith("10") == false || siddata.Length != 9)
                {
                    Util.MsgE("SID는 9자리 숫자이며 10으로 시작 합니다\n입력값을 확인 하세요");
                    tbSid.Focus();
                    tbSid.SelectAll();
                    return;
                }

                if (ta.ExistSID(FCOMMON.info.Login.gcode, tbSid.Text) > 0)
                {
                    var dlg = Util.MsgQ("동일한 SID정보가 있습니다\n강제입력을 진행할까요?\n입력값을 다시 확인하세요.\n품목정보에서 해당 SID정보를 검색해보세요");
                    if(dlg != DialogResult.Yes)
                    {
                        
                        return;
                    }
                }
            }

            var pumName = tbName.Text.Trim();
            var pumModel = tbModel.Text.Trim();
            var searchKey = pumName + pumModel;
            var cnt = (int)(ta.ExistCheck(FCOMMON.info.Login.gcode, searchKey));
            if (cnt > 0)
            {
                FCOMMON.Util.MsgE("동일 품명 모델의 정보가 존재 합니다");
                return;
            }




            if (tbSupplyIdx.Text == "") tbSupplyIdx.Text = "-1";
            var newdr = this.dsMSSQL.Items.NewItemsRow();
            newdr.gcode = FCOMMON.info.Login.gcode;
            newdr.wdate = DateTime.Now;
            newdr.wuid = FCOMMON.info.Login.no;
            newdr.name = tbName.Text.Trim();
            newdr.sid = tbSid.Text.Trim();
            newdr.model = tbModel.Text.Trim();
            newdr.manu = tbManu.Text.Trim();
            newdr.supply = tbSupply.Text.Trim();
            newdr.supplyidx = int.Parse(tbSupplyIdx.Text.Trim());
            newdr.memo = tbMemo.Text.Trim();
            newdr.cate = tbCate.Text.Trim();
            newdr.Storage = tbStorage.Text.Trim();
            newdr.barcode = tbBarcode.Text.Trim();

            //21711
            //if (tbPriceD.Text.isEmpty() == false)
            //{
            //    if (decimal.TryParse(tbPriceD.Text, out decimal priced))
            //    {
            //        newdr.priceD = priced;
            //        if (decimal.TryParse(tbPrice.Text, out decimal priceWon) == false)
            //        {
            //            var ConvertedPrice = (decimal)((double)priced * FCOMMON.info.dollertowon);
            //            tbPrice.Text = ConvertedPrice.ToString();
            //        }
            //    }
            //    else
            //    {
            //        FCOMMON.Util.MsgE("입력값이 숫자가 아닙니다. 단가($)를 사용할 수 없습니다");
            //    }
            //}


            int price;
            int.TryParse(tbPrice.Text, out price);
            newdr.price = price;

            newdr.scale = 1;
            newdr.unit = tbUnit.Text.Trim();
            if (newdr.unit == "") newdr.unit = "EA";
            newdr.disable = false;
            newdr.import = "";
            this.dsMSSQL.Items.AddItemsRow(newdr);
            try
            {
                var cnt2 = ta.Update(dsMSSQL.Items);
                if (cnt2 == 0)
                {
                    FCOMMON.Util.MsgE("추가 실패");
                }
                else
                {
                    newIDX = (int)(ta.ReadIDX(FCOMMON.info.Login.gcode, pumName, pumModel));
                    if (tbSid.Text.Length == 9 && tbSid.Text.StartsWith("10"))
                        newSID = tbSid.Text.Trim();
                    else newSID = string.Empty;

                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            catch (Exception eX)
            {
                FCOMMON.Util.MsgE(eX.Message);
                newIDX = -1;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            tbSid.Enabled = !checkBox1.Checked;
            if (checkBox1.Checked) tbSid.Text = "신규";
            else if (tbSid.Text == "신규") tbSid.Text = "";
        }

        private void fItemAdd_Load(object sender, EventArgs e)
        {

        }
    }
}
