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

        [System.Web.Services.WebMethod]
        public static string getdata_equipment(string parahd, string currentpage, string linegroup, string partnumber, string userid)
        {

            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
                {
                    if(linegroup.Length>0)
                    {
                        filter = filter + string.Format(" and b.linename='{0}'", linegroup);

                    }
                    if(partnumber.Length>0)
                    {
                        filter = filter + string.Format(" and a.partnumber='{0}'", partnumber);

                    }



                    string query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by b.linegroupid,c.machineid)  as RowNum, b.linegroupid,b.linename,c.machineid,c.machinename,a.partnumber,b.model,b.lamp from EquipmentList as a inner join LineGroupMaster as b on a.linegroupid=b.linegroupid inner join MachineMaster as c on a.machineid=c.machineid Where 1=1 and a.flagdel=0  {2}) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize,filter);
                    ds = fc.SQL_DataSet(query.ToString());
                   


                    fc.closeconnection();

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


        }


        [System.Web.Services.WebMethod]
        public static string getdata_equipment_jig(string parahd, string currentpage, string linegroup, string partnumber, string userid)
        {

            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
                {
                    if (linegroup.Length > 0)
                    {
                        filter = filter + string.Format(" and b.linename='{0}'", linegroup);

                    }
                    if (partnumber.Length > 0)
                    {
                        filter = filter + string.Format(" and a.partnumber='{0}'", partnumber);

                    }



                    string query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by linegroupid,jigid)  as RowNum,* from (select distinct '' as expdate, b.linegroupid,b.linename,c.toolname,c.serial,a.partnumber,c.expire_date,b.model,b.lamp,c.jigid from EquipmentList as a inner join LineGroupMaster as b on a.linegroupid=b.linegroupid inner join JigMaster as c on b.lamp=c.lamp and b.linename=c.linename and b.model=c.model and b.partnumber=c.partnumber Where c.flagdel=0 )as tmp) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize, filter);
                    ds = fc.SQL_DataSet(query.ToString());
                    foreach(DataRow dr in ds.Tables[0].Rows)
                    {
                        try
                        {
                            dr["expdate"] = DateTime.Parse(dr["expire_date"].ToString()).ToString("dd/MM/yyyy", new CultureInfo("en-US"));
                        }
                        catch { }

                    }



                    fc.closeconnection();

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


        }


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

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

                    string del_mt = string.Format("update JigMaster set flagdel=1 Where toolname='{0}' and serial='{1}'", toolname, serial);
                    cmdhd = fc.ExecSQLStatement(del_mt.ToString());


                    //del_mt = string.Format("update EquipmentList set flagdel=1 Where linegroupid='{0}' and partnumber='{1}' and jigid in (select jigid from JigMaster Where toolname='{2}' and serial='{3}')", linegroupid, partnumber,toolname,serial);
                    //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");
        }

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

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

        protected void btninspection_Click(object sender, EventArgs e)
        {
            Response.Redirect("RegisterMachine");

        }


     
        
    }
}