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 RegisterLine : System.Web.UI.Page
    {

        [System.Web.Services.WebMethod]
        public static string getdata(string parahd, string currentpage)
        {

            DataSet ds = new DataSet();
            int pagesize = 10;
            string cmdhd = string.Empty;
            string inspidid = string.Empty;
            string userapprove = string.Empty;
            string filter = string.Empty;
            using (FcSql.Function fc = new FcSql.Function())
            {
                byte[] datahd = System.Convert.FromBase64String(parahd);
                string base64Decoded = System.Text.ASCIIEncoding.UTF8.GetString(datahd);

                try
                {




                    string query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by create_datetime desc)  as RowNum, linegroupid,linename,partnumber,model,linegroupcode,lamp,lampid from LineGroupMaster  Where flagdel=0) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize);
                    ds = fc.SQL_DataSet(query.ToString());



                    fc.closeconnection();

                }
                catch { }
            }
            return ds.GetXml();


        }



        [System.Web.Services.WebMethod]
        public static string save(string parahd)
        {
            string cmdhd = string.Empty;
            string linegroupid = string.Empty;
            string userapprove = string.Empty;
            string chk_insert_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 userid = strmt[4].ToString();
                    string piz = strmt[5].ToString();
                    string linegroup = string.Empty;
                    string linegroupcode = string.Empty;
                    string lampid = string.Empty;
                    

                    if (model.IndexOf(' ') != -1)
                    {
                        try
                        {
                            string[] arr = model.Split(' ');
                            linegroupcode = arr[1].ToString();
                        }
                        catch { };

                    }


                    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();
                        chk_insert_update = "U";

                    }
                    else
                    {
                        chk_insert_update = "I";
                        linegroupid = clsControlUI.create_master_id("LineGroupMaster", "linegroupid");
                    }




                  


                    chkid = string.Format("select lampid from LineGroupMaster Where lamp='{0}' ", lamp);
                    dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        lampid= dtchkid.Rows[0]["lampid"].ToString();

                    }
                    else
                    {
                        lampid = clsControlUI.create_master_id("LineGroupMaster", "lampid");
                    }



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

                    string inst_mt = string.Format(" insert into LineGroupMaster (linegroupid,linegroupcode, linename,model,partnumber, flagdel, create_user, create_datetime,linegroup_piz,lampid,lamp) VALUES ('" + linegroupid + "','" + linegroupcode + "','" + linename + "','" + model + "','" + partnumber + "',0,'" + userid + "','" + crdate + "','" + piz + "','" + lampid + "','" + lamp + "')");
                    string up_mt = string.Format(" update LineGroupMaster SET linegroupcode='" + linegroupcode + "', linename= '" + linename + "',model='" + model + "',partnumber='" + partnumber + "',  update_user='" + userid + "', update_datetime= '" + crdate + "',linegroup_piz='" + piz + "',lampid='" + lampid + "',lamp='" + lamp + "'  Where linegroupid= '" + linegroupid + "' ");
                    if (chk_insert_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;


        }


        [System.Web.Services.WebMethod]
        public static string DeleteRow(string linegroupid,string partnumber)
        {
            string cmdhd = string.Empty;

            using (FcSql.Function fc = new FcSql.Function())
            {
                try
                {

                    string del_mt = string.Format("update LineGroupMaster set flagdel=1 Where linegroupid='{0}' and partnumber='{1}'", linegroupid, partnumber);

                    cmdhd = fc.ExecSQLStatement(del_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");
        }
       

    }
}