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 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;
            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 linegroup = strmt[0].ToString();
                    string linename = strmt[1].ToString();
                    string partnumber = strmt[2].ToString();
                    string userid = strmt[3].ToString();
                    string piz = strmt[4].ToString();


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

                        }
                        else
                        {

                            linegroupid = linegroup;
                        }
                    



                    if (linegroupid.Length == 0)
                    {

                        linegroup = clsControlUI.create_master_id("LineGroupMaster", "linegroupid");


                    }



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

                    string inst_mt = string.Format(" insert into LineGroupMaster (linegroupid, linename, flagdel, create_user, create_datetime,partnumber,linegroup_piz) VALUES ('" + linegroup + "','" + linename + "',0,'" + userid + "','" + crdate + "','" + partnumber + "','" + piz + "')");
                    string up_mt = string.Format(" update LineGroupMaster SET linename= '" + linename + "',update_user='" + userid + "', update_datetime= '" + crdate + "',partnumber='" + partnumber + "',linegroup_piz='" + piz + "'  Where linegroupid= '" + linegroup + "' ");

                    cmdhd = fc.ExecSQLStatement(inst_mt.ToString());
                    if (cmdhd.ToLower().IndexOf("duplicate") != -1)
                    {
                        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");
        }
       

    }
}