using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CheckSheet.approve
{
    public partial class XR_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();
                    machinename = machinename.Replace("&amp;", "&");

                    string level = string.Empty;
                    string filter = string.Empty;
                    string str_user = string.Format(" select approve_userid,approve_level from mst_Approve  Where  approve_userid in (select userid from Users Where usercode='{0}') and inspectiontype='2' ", 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  (check1_status='Y' or check2_status='Y') ");

                    }

                    string str_getinput = string.Format("select confirm{7}_status as cm_status,check{7}_status as chk_status,case when (approve1_status='Y' or approve2_status='Y') and insp_status<>'W' then 'Y' else '' end as app_status,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='2'  and month(inspectiondate) = {3} and year(inspectiondate)={4} and inspectid='{5}' {6} ", linegroup, partnumber, machinename, month, year, itemno, filter, level.ToString());
                    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;
                machinename = machinename.Replace("&amp;", "&");
                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)
        {

        }

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

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