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
{
    public partial class RegisterMachine : System.Web.UI.Page
    {

        [System.Web.Services.WebMethod]
        public static string save(string parahd)
        {
            string cmdhd = string.Empty;
            string equid = 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 lamp = strmt[0].ToString();
                    string linename = strmt[1].ToString();
                    string model = strmt[2].ToString();
                    string partnumber = strmt[3].ToString();
                    string machinename = strmt[4].ToString();
                    machinename = machinename.Replace("&amp;", "&");
                    string userid = strmt[5].ToString();
                    string piz = strmt[6].ToString();

                    string _idmachine = string.Empty;
                    string linegroupcode = string.Empty;
                    string machineno = string.Empty;

                    string chkmc = string.Format("select a.machineid,b.linegroupid,b.linegroupcode from MachineMaster as a inner join LineGroupMaster as b on a.linegroupcode=b.linegroupcode Where a.machinename='{0}' ", machinename);
                    DataTable dtmc = fc.SQL_DataTable(chkmc);
                    if(dtmc.Rows.Count>0)
                    {
                        _idmachine = dtmc.Rows[0]["machineid"].ToString();
                        linegroupcode = dtmc.Rows[0]["linegroupcode"].ToString();
                        chk_inst_update = "U";
                        machineno =  _idmachine;
                    }


                    if (_idmachine.Length == 0)
                    {

                        string _qmachine = string.Format("select max(num) as numid,linegroupcode from (select Row_Number() over (Order by machineid) as num,linegroupcode from MachineMaster Where linegroupcode in (select linegroupcode from LineGroupMaster Where model='{0}' and lamp='{1}')) as temp group by linegroupcode ", model, lamp);
                        DataTable dtgenidmachine = fc.SQL_DataTable(_qmachine.ToString());
                        if (dtgenidmachine.Rows.Count == 0)
                        {
                            _idmachine = "1";
                        }
                        else
                        {
                            foreach (DataRow drmachine in dtgenidmachine.Rows)
                            {
                                if (drmachine[0].ToString() == "")
                                {
                                    _idmachine = "1";
                                }
                                else
                                {
                                    _idmachine = (int.Parse(drmachine[0].ToString()) + 1).ToString();
                                }
                            }
                        }

                        if (dtgenidmachine.Rows.Count > 0)
                        {
                            linegroupcode = dtgenidmachine.Rows[0]["linegroupcode"].ToString();

                        }
                        machineno = linegroupcode + "" + _idmachine;
                        chk_inst_update = "I";

                    }





                   
                    string linegroupid = string.Empty;

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

                    string inst_mt = string.Format(" insert into MachineMaster (machineid, machinename, flagdel, create_user, create_datetime,machine_piz,linegroupcode) VALUES ('" + machineno + "','" + machinename + "',0,'" + userid + "','" + crdate + "','" + piz + "','"+linegroupcode+"')");
                    string up_mt = string.Format(" update MachineMaster SET machinename= '" + machinename + "',update_user='" + userid + "', update_datetime= '" + crdate + "',machine_piz='" + piz + "',linegroupcode='"+linegroupcode+"'  Where machineid= '" + machineno + "' ");
                    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 chkid = string.Format("select linegroupid from LineGroupMaster Where model='{0}' and lamp='{1}' ", model, lamp);
                    DataTable dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        linegroupid = dtchkid.Rows[0]["linegroupid"].ToString();


                    }



                    chkid = string.Format("select equid,linegroupid,partnumber from EquipmentList Where machineid='{0}' and linegroupid='{1}' and partnumber='{2}' ", machineno, linegroupid, partnumber);
                    dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        equid = dtchkid.Rows[0]["equid"].ToString();

                    }




                    if (equid.Length == 0)
                    {

                        string _pv = string.Empty;
                        string _id = string.Empty;
                        string _q = string.Format(" select max(convert(real,equid)) as id from EquipmentList ");
                        DataTable dtgenid = fc.SQL_DataTable(_q.ToString());
                        if (dtgenid.Rows.Count == 0)
                        {
                            _id = "1";
                        }
                        else
                        {
                            foreach (DataRow dr in dtgenid.Rows)
                            {
                                if (dr[0].ToString() == "")
                                {
                                    _id = "1";
                                }
                                else
                                {
                                    _id = (int.Parse(dr[0].ToString()) + 1).ToString();
                                }
                            }
                        }

                        equid = _id;


                    }




                    inst_mt = string.Format(" insert into EquipmentList (equid, partnumber, linegroupid, machineid, flagdel, create_user, create_datetime) VALUES ('" + equid + "','" + partnumber + "','" + linegroupid + "','" + machineno + "',0,'" + userid + "','" + crdate + "')");
                    up_mt = string.Format(" update EquipmentList SET partnumber='" + partnumber + "', linegroupid= '" + linegroupid + "', machineid= '" + machineno + "',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("Menusetting");
        }
    }
}