using AR;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Project
{
    public partial class FMain
    {
        delegate void savedataeehandler(Class.JobData item, string Loc, string inboundok, string reason);

        public void SaveData_EE(Class.JobData item, string Loc, string inboundok, string reason)
        {
            if (this.InvokeRequired)
            {
                //이자료를 복사해서 데이터를 넘겨줘야한다.
                var a = new Class.JobData(999);
                item.CopyTo(ref a);
                this.BeginInvoke(new savedataeehandler(SaveData_EE), a, Loc, inboundok, reason);
                return;
            }

            var retval = false;
            if (AR.SETTING.Data.OnlineMode == false)
            {
                PUB.log.AddAT($"[SAVE-EE] Not saving in offline mode");
                return;
            }


            DateTime savestart = DateTime.Now;
            try
            {
                var save_month = DateTime.Now.ToString("YY") + DateTime.Now.Month.ToString("X");
                var save_sn = string.Empty;
                if (item.VisionData.RID.Length == 15)
                {
                    save_month = item.VisionData.RID.Substring(8, 3); //21A 식으로 년도와 월이 나온다
                    save_sn = item.VisionData.RID.Substring(item.VisionData.RID.Length - 4); //뒷에서 4자리 끊는다
                }
                //var Jobtype = VAR.STR[eVarString.JOB_TYPE];
                //var ByPassMode = Jobtype == "BP";
                var CVMode = VAR.BOOL[eVarBool.Use_Conveyor];
                //option check
                //var OPT_PrinterOff = PUB.OPT_PRINTEROFF(target);
                //var OPT_CameraOff = PUB.OPT_CAMERA(target);
                var OPT_BYPASS = SETTING.Data.SystemBypass;// PUB.OPT_BYPASS(target);


                using (var taResult = new DataSet1TableAdapters.K4EE_Component_Reel_ResultTableAdapter())
                {
                    //QTY 0값은 없을 수도있다(해당 값은 서버조회여부에다라 다르다)
                    int? qtyorg = null;
                    if (int.TryParse(item.VisionData.QTY0, out int vqty0))
                        qtyorg = vqty0;
                    string remark = string.Empty;
                    if (item.VisionData.ConfirmAuto) remark = "[자동]" + remark; //자동확인문구
                    else if (item.VisionData.ConfirmUser) remark = "[확인]" + remark;  //사용자 확인데이터 비고에 추가
                    if (OPT_BYPASS) remark = "(BYPASS)" + remark;

                    DataSet1.K4EE_Component_Reel_ResultRow newdr = this.dataSet1.K4EE_Component_Reel_Result.Where(t => t.JGUID.Equals(item.guid)).FirstOrDefault();
                    if (newdr == null) newdr = this.dataSet1.K4EE_Component_Reel_Result.NewK4EE_Component_Reel_ResultRow();
                    //else newdr = dt.Rows[0] as DataSet1.Component_Reel_ResultRow;

                    if (item.JobStart.Year != 1982) newdr.STIME = item.JobStart;
                    else newdr.STIME = DateTime.Now;

                    if (item.JobEnd.Year != 1982) newdr.ETIME = item.JobEnd;
                    if (item.PrintTime.Year != 1982) newdr.PTIME = item.PrintTime;

                    newdr.ATIME = item.Attachtime;
                    newdr.PDATE = save_month;
                    newdr.JTYPE = PUB.Result.vModel.Title;// PUB.Result.JobType2;
                    if (newdr.JTYPE.Length > 10) newdr.JTYPE = newdr.JTYPE.Substring(0, 9);

                    newdr.SID = item.VisionData.SID;
                    newdr.SID0 = item.VisionData.SID0;
                    newdr.RID = item.VisionData.RID;
                    newdr.RID0 = item.VisionData.RID0;
                    newdr.RSN = save_sn;
                    newdr.QR = item.VisionData.PrintQRData;
                    newdr.ZPL = item.VisionData.ZPL;
                    newdr.POS = item.VisionData.PrintPositionData;
                    newdr.LOC = Loc;
                    newdr.ANGLE = item.VisionData.ApplyAngle;
                    if (int.TryParse(item.VisionData.QTY, out int oqty))
                        newdr.QTY = oqty;
                    else
                        newdr.QTY = 0;

                    newdr.MFGDATE = item.VisionData.MFGDATE;//210326 날짜도 추가함
                    if (qtyorg != null) newdr.QTY0 = (int)qtyorg;
                    newdr.VNAME = item.VisionData.VNAME;
                    newdr.VLOT = item.VisionData.VLOT; ///210326
					newdr.PRNATTACH = item.PrintAttach;
                    newdr.PRNVALID = item.PrintQRValid;
                    newdr.REMARK = remark;
                    if (inboundok.isEmpty() == false) newdr.iNBOUND = inboundok;//230622		
                    newdr.MC = AR.SETTING.Data.McName;
                    newdr.PARTNO = item.VisionData.PARTNO;
                    newdr.CUSTCODE = item.VisionData.CUSTCODE;
                    newdr.MCN = item.VisionData.MCN;
                    newdr.target = item.VisionData.Target;

                    //220921 - batch qtymax
                    newdr.BATCH = item.VisionData.BATCH;
                    if (int.TryParse(item.VisionData.QTYMAX, out int qtymax))
                        newdr.qtymax = qtymax;
                    else newdr.qtymax = -1;

                    

                    //new mode
                    if (newdr.RowState == System.Data.DataRowState.Detached)
                    {
                        newdr.GUID = PUB.Result.guid;   //220921
                        newdr.JGUID = item.guid;
                        newdr.wdate = DateTime.Now;
                        this.dataSet1.K4EE_Component_Reel_Result.AddK4EE_Component_Reel_ResultRow(newdr);
                    }
                    else newdr.EndEdit();

                    var ta = new DataSet1TableAdapters.K4EE_Component_Reel_ResultTableAdapter();
                    var cnt = ta.Update(newdr);
                    newdr.AcceptChanges();
                    if (cnt == 0) PUB.log.AddE("Save Error");
                }

                retval = true;
            }
            catch (Exception ex)
            {
                PUB.log.AddE($"[EE-SAVE] {ex.Message}");
                retval = false;
            }

            var ts = DateTime.Now - savestart;
            PUB.log.AddI($"data save time : {ts.TotalSeconds:N2}");

            ListFormmatData();


        }
    }
}