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;
            string chk_inst_update = 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();
                    string lamp = strmt[7].ToString();
                    string model = strmt[8].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();
                        chk_inst_update = "U";

                    }




                    if (jigid.Length == 0)
                    {

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

                    }



                    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,lamp,linename,model,partnumber) VALUES ('" + jigid + "','" + toolname + "','" + serialno + "','" + ddate + "',0,'" + userid + "','" + crdate + "','"+lamp+"','"+linename+"','"+model+"','"+partnumber+"')");
                    string up_mt = string.Format(" update JigMaster SET toolname= '" + toolname + "',expire_date='" + ddate + "', update_user='" + userid + "', update_datetime= '" + crdate + "',lamp='" + lamp + "',linename='" + linename + "',model='" + model + "',partnumber='" + partnumber + "' Where jigid= '" + jigid + "' and serial='" + serialno + "'");

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

                    }
                    else
                    {
                        cmdhd = fc.ExecSQLStatement(up_mt.ToString());

                    }



                    string equjig = string.Format(" select * from EquipmentList as a inner join LineGroupMaster as b on a.linegroupid=b.linegroupid and a.partnumber=b.partnumber Where model='{0}' and lamp='{1}' and linename='{2}'", model, lamp, linename);
                    DataTable dt_equ_jig = fc.SQL_DataTable(equjig);
                    if(dt_equ_jig.Rows.Count>0)
                    {
                        foreach (DataRow dr in dt_equ_jig.Rows)
                        {
                            up_mt = string.Format(" update EquipmentList SET jigid='" + jigid + "',Update_user= '" + userid + "',Update_Datetime= '" + crdate + "' Where equid= '" + dr["equid"].ToString() + "' ");
                            cmdhd = fc.ExecSQLStatement(up_mt.ToString());
                        }

                        chk_inst_update = "U";


                    }
                    else
                    {

                        equid = clsControlUI.create_master_id("EquipmentList", "equid");
                        chk_inst_update = "I";

                        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 + "' ");

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

                        }
                        else
                        {

                            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");
        }
    }
}