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

namespace CheckSheet
{
    public partial class _4m1e_inspection_machine_confirm : System.Web.UI.Page
    {


        [System.Web.Services.WebMethod]
        public static string saveconfirm(string id, string status, string notice, string userid)
        {

            string str_cmd = string.Empty;

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

                try
                {

                    string str_confirm = string.Format("update InspectionInput set confirm_status='{0}',confirm_notice='{1}',confirm_date='{2}',confirm_userid='{3}' Where inspidid='{4}'", status, notice, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US")), userid, id);
                    str_cmd = fc.ExecSQLStatement(str_confirm);

                    fc.closeconnection();

                }
                catch { }
            }
            return str_cmd;


        }




        [System.Web.Services.WebMethod]
        public static string getinspection(string parahd, string currentpage, string dayvalue, string itemvalue, 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 filter = base64Decoded;

                    if (dayvalue.IndexOf("*") != -1)
                    {
                        string[] arrday = dayvalue.Split('*');

                        foreach (string str in arrday)
                        {
                            if (str.Length > 0)
                            {
                                if (filterselect.Length == 0)
                                {
                                    filterselect = string.Format(" inspectiondate between '{0}' and '{0} 23:59:59' ", year + "-" + int.Parse(month).ToString("00") + "-" + str);


                                }
                                else
                                {
                                    filterselect = filterselect + string.Format(" or inspectiondate between '{0}' and '{0} 23:59:59' ", year + "-" + int.Parse(month).ToString("00") + "-" + str);


                                }
                            }

                        }

                        filterselect = string.Format(" and ({0}) ", filterselect);


                    }
                    else if (itemvalue.IndexOf("*") != -1)
                    {
                        string[] arritem = itemvalue.Split('*');

                        foreach (string str in arritem)
                        {
                            if (str.Length > 0)
                            {
                                if (filterselect.Length == 0)
                                {
                                    filterselect = "'" + str + "'";
                                }
                                else
                                {
                                    filterselect = filterselect + ",'" + str + "'";
                                }

                            }
                        }

                        filterselect = string.Format(" and b.machineid in ({0}) ", filterselect);


                    }

                    if (filter.Length == 0 && filterselect.Length == 0)
                    {
                        filterselect = string.Format(" and month(inspectiondate)={0} and year(inspectiondate)={1} ", month, year);


                    }

                    string level = string.Empty;
                    string filteruser = 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();

                    }
                    string query = string.Empty;
                    if (level == "1")
                    {

                        filteruser = string.Format(" and insp_status='W'  and  confirm1_status is NULL  and  (check1_status is NULL or check2_status is NULL) ");
                        query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by inspectiondate,b.machineid)  as RowNum, inspidid,inspectid,itemno,itemname,inspectiondate, notice,'' as recdate,'' as noticeshort,confirm1_status as confirm_status,confirm1_notice as confirm_notice,'' as confirm_noticeshort,c.machinename from  InspectionInput 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  inspectiontype='4' {3} {2} {4} ) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize, filter, filterselect, filteruser);

                        ds = fc.SQL_DataSet(query.ToString());


                    }
                    else if (level == "2")
                    {
                        filteruser = string.Format(" and insp_status='W' and  confirm2_status is NULL  and  (check1_status='Y' or check2_status='Y') ");
                        query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by inspectiondate,b.machineid)  as RowNum, inspidid,inspectid,itemno,itemname,inspectiondate, notice,'' as recdate,'' as noticeshort,confirm2_status as confirm_status,confirm2_notice as confirm_notice,'' as confirm_noticeshort,c.machinename from  InspectionInput 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  inspectiontype='4' {3} {2} {4} ) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize, filter, filterselect, filteruser);
                        ds = fc.SQL_DataSet(query.ToString());

                    }


                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        dr["recdate"] = DateTime.Parse(dr["inspectiondate"].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_notice"].ToString().Length > 15)
                        {
                            dr["confirm_noticeshort"] = dr["confirm_notice"].ToString().Substring(0, 15);

                        }
                        else
                        {
                            dr["confirm_noticeshort"] = dr["confirm_notice"].ToString();

                        }
                    }


                    fc.closeconnection();

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


        }





   


        protected void Page_Load(object sender, EventArgs e)
        {

        }

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