using FarPoint.Excel.EntityClassLibrary.DrawingEx;
using FCOMMON;
using NetOffice.OutlookApi;
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 fItemEdit : FCOMMON.fBase
    {
        public int newIDX = -1;
        public string newSID = string.Empty;
        FCM0000.dsMSSQL.ItemsRow dr;

        public fItemEdit(int pumidx)
        {
            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;
                }
            }
            dr = ta.GetbyIDX(pumidx).FirstOrDefault();
        }

        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}");
            }
        }
      
        private void fItemAdd_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
          

            tbSid.Text = dr.IssidNull() == false ? dr.sid : string.Empty;
            tbName.Text = dr.IsnameNull() == false ? dr.name : string.Empty;
            tbModel.Text = dr.IsmodelNull() == false ? dr.model : string.Empty;
            tbUnit.Text = dr.IsunitNull() == false ? dr.unit : string.Empty;
            tbManu.Text = dr.IsmanuNull() == false ? dr.manu : string.Empty;
            tbPrice.Text = dr.IspriceNull() == false ? dr.price.ToString() : string.Empty;
            tbPriceD.Text = dr.IspriceDNull() == false ? dr.priceD.ToString() : string.Empty;
            tbStorage.Text = dr.IsStorageNull() == false ? dr.Storage : string.Empty;
            tbCate.Text = dr.IscateNull() == false ? dr.cate : string.Empty;
            tbSupply.Text = dr.IssupplyNull() == false ? dr.supply : string.Empty;
            tbSupplyIdx.Text = dr.IssupplyidxNull() == false ? dr.supplyidx.ToString() : string.Empty;
            tbBarcode.Text = dr.IsbarcodeNull() == false ? dr.barcode : string.Empty;
            tbMemo.Text = dr.IsmemoNull() == false ? dr.memo : string.Empty;
            this.Show();
            //Application.DoEvents();

        }

        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가 정상이어야 한다.
            //    if (tbSid.Text.StartsWith("10") == false || tbSid.TextLength != 9)
            //    {
            //        Util.MsgE("SID는 9자리 숫자이며 10으로 시작 합니다\n입력값을 확인 하세요");
            //        tbSid.Focus();
            //        tbSid.SelectAll();
            //        return;
            //    }

            //    if (ta.ExistSID(FCOMMON.info.Login.gcode, tbSid.Text) > 0)
            //    {
            //        Util.MsgE("동일한 SID정보가 있습니다\n화면을 닫은 후 SID로 검색하세요");
            //        this.Close();
            //        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 (int.TryParse(tbSupplyIdx.Text, out int supidx) == false)
            {
                dr.SetsupplyidxNull();
            }
            
            dr.wdate = DateTime.Now;
            dr.wuid = FCOMMON.info.Login.no;
            dr.name = tbName.Text.Trim();
            dr.sid = tbSid.Text.Trim();
            dr.model = tbModel.Text.Trim();
            dr.manu = tbManu.Text.Trim();
            dr.supply = tbSupply.Text.Trim();
            dr.memo = tbMemo.Text.Trim();
            dr.cate = tbCate.Text.Trim();
            dr.Storage = tbStorage.Text.Trim();
            dr.barcode = tbBarcode.Text.Trim();

            ////21711
            //if (tbPriceD.Text.isEmpty() == false)
            //{
            //    if (decimal.TryParse(tbPriceD.Text, out decimal priced))
            //    {
            //        dr.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);
            dr.price = price;

            if(dr.IscateNull() || dr.scale == 0) dr.scale = 1;
            dr.unit = tbUnit.Text.Trim();
            if ( dr.IsunitNull() || dr.unit == "") dr.unit = "EA";
            dr.EndEdit();

            try
            {
                var cnt2 = ta.Update(dr);
                if (cnt2 == 0)
                {
                    FCOMMON.Util.MsgE("변경 실패");
                }
                else
                {
                    this.newIDX = dr.idx;
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            catch (System.Exception eX)
            {
                FCOMMON.Util.MsgE(eX.Message);
                newIDX = -1;
            }
        }

    }
}
