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_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 machineid = strmt[4].ToString();
                    string userid = strmt[5].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 in (select userid from Users Where usercode='{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 (check1_status='Y' or check2_status='Y') ");

                    }

                    string str_getinput = string.Format("select confirm{6}_status as cm_status,check{6}_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 result1 is NOT NULL or result1 = 'OK' then 'OK' else 'NG' end as resultstatus from InspectionInput Where equid in (select equid from EquipmentList Where linegroupid='{0}' and partnumber='{1}' and machineid='{2}') and inspectiontype='4'  and month(inspectiondate) = {3} and year(inspectiondate)={4} {5}  order by resultstatus desc", linegroup, partnumber, machineid, month, year, filter, level.ToString());

                    ds = fc.SQL_DataSet(str_getinput);


                    fc.closeconnection();

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


        }

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

            DataSet ds = new DataSet();
            string cmdhd = string.Empty;
            string status_confirm = string.Empty;
            using (FcSql.Function fc = new FcSql.Function())
            {

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


                    }

                    string str_getinput = string.Empty;
                    if (level == "1")
                    {
                        str_getinput = string.Format("select inspidid from InspectionInput Where  equid in (select equid from EquipmentList where linegroupid='{1}' and partnumber='{2}') and inspectiontype='4'  and confirm1_date between '{0}' and '{0} 23:59:59' and confirm1_status='Y' ", DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US")), linegroup, partnumber);
                        ds = fc.SQL_DataSet(str_getinput);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            status_confirm = "Y";

                        }

                    }
                    else if (level == "2")
                    {

                        str_getinput = string.Format("select inspidid from InspectionInput Where  equid in (select equid from EquipmentList where linegroupid='{1}' and partnumber='{2}') and inspectiontype='4'  and confirm2_date between '{0}' and '{0} 23:59:59' and confirm2_status='Y' ", DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US")), linegroup, partnumber);
                        ds = fc.SQL_DataSet(str_getinput);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            status_confirm = "Y";

                        }

                    }





                    fc.closeconnection();

                }
                catch { }
            }
            return status_confirm;


        }

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

            DataSet ds = new DataSet();
            string cmdhd = string.Empty;
            string status_confirm = string.Empty;
            using (FcSql.Function fc = new FcSql.Function())
            {

                try
                {


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


                    }



                    string str_getinput = string.Empty;
                    str_getinput = string.Format("select inspidid from InspectionInput Where equid in (select equid from EquipmentList where linegroupid='{1}' and partnumber='{2}') and  inspectiontype='4'  and (year(approve1_date)={0} or year(approve2_date)={0}) and (month(approve1_date)={3} or month(approve2_date)={3}) and (approve1_status='Y' or approve2_status='Y') ", DateTime.Now.Year.ToString(), linegroup, partnumber, DateTime.Now.Month.ToString());
                    ds = fc.SQL_DataSet(str_getinput);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        status_confirm = "Y";

                    }




                    fc.closeconnection();

                }
                catch { }
            }
            return status_confirm;


        }


        [System.Web.Services.WebMethod]
        public static string GetAllDay(string year, string month)
        {
            DataSet ds;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string query = string.Empty;
                query = string.Format(" select Day(listday) as listnumday, listday from dbo.listdate_of_year('{0}')	Where month(listday)={1} and year(listday)={2}", year + "0101", month, year);
                ds = fc.SQL_DataSet(query.ToString());
                fc.closeconnection();
            }
            return ds.GetXml();


        }

        [System.Web.Services.WebMethod]
        public static string GetDayOff(string year, string month)
        {
            DataSet ds;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string query = string.Empty;
                query = string.Format(" select Day(listday) as listnumday, listday from dbo.listweekend_of_year('{0}')	Where month(listday)={1} and year(listday)={2}", year + "0101", month, year);
                ds = fc.SQL_DataSet(query.ToString());
                fc.closeconnection();
            }
            return ds.GetXml();


        }


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


        }

      

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {

                txt_checkyear.Items.Add(DateTime.Now.AddYears(-1).Year.ToString());
                txt_checkyear.Items.Add(DateTime.Now.Year.ToString());
                txt_checkyear.SelectedIndex = 1;


                txt_checkmonth.Items.Add("1");
                txt_checkmonth.Items.Add("2");
                txt_checkmonth.Items.Add("3");
                txt_checkmonth.Items.Add("4");
                txt_checkmonth.Items.Add("5");
                txt_checkmonth.Items.Add("6");
                txt_checkmonth.Items.Add("7");
                txt_checkmonth.Items.Add("8");
                txt_checkmonth.Items.Add("9");
                txt_checkmonth.Items.Add("10");
                txt_checkmonth.Items.Add("11");
                txt_checkmonth.Items.Add("12");

                txt_checkmonth.SelectedValue = DateTime.Now.Month.ToString();

            }

        }

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

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

        protected void btnapprove_Click(object sender, EventArgs e)
        {
            Response.Redirect("4m1e_inspection_approval");
        }

        protected void btncsvdownload_Click(object sender, EventArgs e)
        {
            Response.Redirect("4m1e_inspection_csv_download");
        }

        protected void btncreatenextmonth_Click(object sender, EventArgs e)
        {
            Response.Redirect("4m1e_checklist_create");
        }
    }
}