using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

namespace CheckSheet.setting
{
    public partial class Jig_inspection_item_edit : System.Web.UI.Page
    {

        [System.Web.Services.WebMethod]
        public static string save(string parahd)
        {
            string cmdhd = string.Empty;
            string equid = string.Empty;
            string jigid = string.Empty;
            string userapprove = string.Empty;
            using (FcSql.Function fc = new FcSql.Function())
            {
                byte[] datahd = System.Convert.FromBase64String(parahd);
                string base64Decoded = System.Text.ASCIIEncoding.UTF8.GetString(datahd);
                string[] strmt = base64Decoded.Split('|');
                try
                {


                    string toolname = strmt[0].ToString();
                    string serialno = strmt[1].ToString();
                    string ddate = strmt[2].ToString();
                    string userid = strmt[3].ToString();
                    string linegroup = strmt[4].ToString();
                    string linename = strmt[5].ToString();
                    string partnumber = strmt[6].ToString();

                    if(ddate.IndexOf('/')!=-1)
                    {
                        string[] arrd = ddate.Split('/');
                        if(int.Parse(arrd[0])>2500)
                        {
                            string year = (int.Parse(arrd[0])-543).ToString();
                            ddate = year + "-" + arrd[1] + "-" + arrd[2];

                        }
                        else
                        {
                            ddate = arrd[0] + "-" + arrd[1] + "-" + arrd[2];

                        }

                    }


                    string chkid = string.Format("select jigid from JigMaster Where toolname='{0}' and serial='{1}'  ", toolname,serialno);
                    DataTable dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        jigid = dtchkid.Rows[0]["jigid"].ToString();

                    }




                    if (jigid.Length == 0)
                    {

                        jigid = clsControlUI.create_master_id("JigMaster", "jigid");           


                    }



                    string crdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US"));

                    string inst_mt = string.Format(" insert into JigMaster (jigid, toolname, serial, expire_date, flagdel, create_user, create_datetime) VALUES ('" + jigid + "','" + toolname + "','" + serialno + "','" + ddate + "',0,'" + userid + "','" + crdate + "')");
                    string up_mt = string.Format(" update JigMaster SET toolname= '" + toolname + "',expire_date='" + ddate + "', update_user='" + userid + "', update_datetime= '" + crdate + "' Where jigid= '" + jigid + "' and serial='"+serialno+"'");

                    cmdhd = fc.ExecSQLStatement(inst_mt.ToString());
                    if (cmdhd.ToLower().IndexOf("duplicate") != -1)
                    {
                        cmdhd = fc.ExecSQLStatement(up_mt.ToString());
                    }
                    equid = "";
                    chkid = string.Format("select equid,linegroupid,partnumber from EquipmentList Where jigid='{0}' ", jigid);
                    dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        equid = dtchkid.Rows[0]["equid"].ToString();

                    }




                    if (equid.Length == 0)
                    {
                        equid = clsControlUI.create_master_id("EquipmentList", "equid");
                        

                    }

                    
                    inst_mt = string.Format(" insert into EquipmentList (equid, partnumber, linegroupid, jigid, flagdel, create_user, create_datetime) VALUES ('" + equid + "','" + partnumber + "','" + linegroup + "','" + jigid + "',0,'" + userid + "','" + crdate + "')");
                    up_mt = string.Format(" update EquipmentList SET partnumber='" + partnumber + "', linegroupid= '" + linegroup + "', jigid= '" + jigid + "',Update_user= '" + userid + "',Update_Datetime= '" + crdate + "' Where equid= '" + equid + "' ");

                    cmdhd = fc.ExecSQLStatement(inst_mt.ToString());
                    if (cmdhd.ToLower().IndexOf("duplicate") != -1)
                    {
                        cmdhd = fc.ExecSQLStatement(up_mt.ToString());
                    }
                    fc.closeconnection();


                }
                catch { };

            }



            return cmdhd;


        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            Response.Redirect("RegisterEquipment");
        }
    }
}