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




        [System.Web.Services.WebMethod]
        public static string getinspection(string parahd, string currentpage, string linegroup, string partnumber,string machinename, string month, string year,string userid)
        {

            DataSet ds = new DataSet();
            int pagesize = 10;
            string cmdhd = string.Empty;
            string inspidid = string.Empty;
            string userapprove = string.Empty;
            string filterselect = 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 level = string.Empty;
                    string filter = string.Empty;
                    string seq = string.Empty;

                    string str_user = string.Format(" select approve_userid,approve_level,approve_seq from mst_Approve  Where  approve_userid in (select userid from Users Where usercode='{0}') and inspectiontype='3' ", userid);
                    DataTable dt_user = fc.SQL_DataTable(str_user);
                    if (dt_user.Rows.Count > 0)
                    {
                        level = dt_user.Rows[0]["approve_level"].ToString();
                        seq = dt_user.Rows[0]["approve_seq"].ToString();

                    }

                    if (level == "1")
                    {

                        filter = string.Format(" and insp_status='W' and  (check1_status is NULL or check2_status is NULL) ");


                    }
                    else if (level == "2")
                    {
                        filter = string.Format(" and (check1_status='Y' or check2_status='Y') ");

                    }
                    machinename = machinename.Replace("&amp;", "&");
                    filterselect = string.Format(" and month(inspectiondate)={0} and year(inspectiondate)={1} and a.jigid in (select jigid from EquipmentList as a inner join MachineMaster as b on a.machineid=b.machineid Where a.linegroupid='{2}' and a.partnumber='{3}' and b.machinename='{4}' ) ", month, year, linegroup, partnumber, machinename);


                    string query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by inspectiondate,a.jigid)  as RowNum, inspidid,inspectid,itemno,itemname,inspectiondate, notice,'' as recdate,'' as noticeshort,confirm{4}_status,confirm{4}_notice,'' as confirm_noticeshort,c.machinename,a.jigid,a.toolname,a.serialnumber,d.expire_date from  InspectionInput as a inner join EquipmentList as b on a.jigid=b.jigid inner join JigMaster as d on b.jigid=d.jigid and a.serialnumber=d.serial inner join MachineMaster as c on b.machineid=c.machineid Where a.flagdel=0 and  inspectiontype='3'  {3} {2} ) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize, filter, filterselect, seq);
                    ds = fc.SQL_DataSet(query.ToString());
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        dr["recdate"] = DateTime.Parse(dr["expire_date"].ToString()).ToString("dd/MM/yy", new CultureInfo("en-US"));
                        if (dr["notice"].ToString().Length > 15)
                        {
                            dr["noticeshort"] = dr["notice"].ToString().Substring(0, 15);

                        }
                        else
                        {
                            dr["noticeshort"] = dr["notice"].ToString();

                        }

                        if (dr["confirm" + seq +"_notice"].ToString().Length > 15)
                        {
                            dr["confirm_noticeshort"] = dr["confirm" + seq + "_notice"].ToString().Substring(0, 15);

                        }
                        else
                        {
                            dr["confirm_noticeshort"] = dr["confirm" + seq + "_notice"].ToString();

                        }
                    }


                    fc.closeconnection();

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


        }



        [System.Web.Services.WebMethod]
        public static string GetInspectionMaster(string currentpage, string linegroup, string partnumber, string machinename)
        {
            DataSet ds;
            int pagesize = 10;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string query = string.Empty;
                query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by convert(int,inspectid))  as RowNum, inspectid,itemname from  InspectionItem as a inner join EquipmentList as b on a.equid=b.equid inner join MachineMaster as c on b.machineid=c.machineid Where a.flagdel=0 and b.partnumber='{2}' and b.linegroupid='{3}' and c.machinename='{4}' ) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize, partnumber, linegroup, machinename);
                ds = fc.SQL_DataSet(query.ToString());
                fc.closeconnection();
            }
            return ds.GetXml();


        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}