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



        [System.Web.Services.WebMethod]
        public static string getinspection(string parahd)
        {

            DataSet ds = new DataSet();
            string cmdhd = string.Empty;
            string inspidid = 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 partnumber = strmt[1].ToString();
                    string month = strmt[2].ToString();
                    string year = strmt[3].ToString();
                    string machinename = strmt[4].ToString();
                    string itemno = strmt[5].ToString();
                    string userid = strmt[6].ToString();


                    string level = string.Empty;
                    string filter = string.Empty;
                    string str_user = string.Format(" select approve_userid,approve_level from mst_Approve  Where approve_userid='{0}' and inspectiontype='4' ", userid);
                    DataTable dt_user = fc.SQL_DataTable(str_user);
                    if (dt_user.Rows.Count > 0)
                    {
                        level = dt_user.Rows[0]["approve_level"].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 insp_status='W' and  (check1_status='Y' or check2_status='Y') ");

                    }
                 
                    string str_getinput = string.Format("select day(inspectiondate) as daytocheck,case when resultvalue is NOT NULL  then convert(varchar(10),resultvalue) when resultstatus='OK' then 'OK'  else 'NG' end  as resultstatus from InspectionInput Where equid in (select equid from EquipmentList as a inner join MachineMaster as b on a.machineid=b.machineid Where linegroupid='{0}' and partnumber='{1}' and b.machinename='{2}') and inspectiontype='4'  and month(inspectiondate) = {3} and year(inspectiondate)={4} and inspectid='{5}' {6} ", linegroup, partnumber, machinename, month, year, itemno, filter);
                    ds = fc.SQL_DataSet(str_getinput);




                    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)
        {

        }
    }
}