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 XR_inspection_approval : 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
                {
                    if (id.IndexOf("*") != -1)
                    {

                        string[] arr = id.Split('*');
                        foreach (string str in arr)
                        {
                            string str_confirm = string.Format("update InspectionInput set approve_status='{0}',approve_notice='{1}',approve_date='{2}',approve_userid='{3}' Where inspectiontype='2' and confirm_status='Y' and approve_status = 'W' and  equid in (select equid from EquipmentList Where  machineid='{4}') ", status, notice, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US")), userid, str);
                            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 username = string.Empty;
                    string seq = string.Empty;
                    string str_user = string.Format(" select approve_userid,approve_level,approve_username,approve_seq from mst_Approve  Where approve_userid='{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();
                        username = dt_user.Rows[0]["approve_username"].ToString();
                        seq = dt_user.Rows[0]["approve_seq"].ToString();

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

                        filteruser = string.Format(" and insp_status='W'  and  (check1_status is NULL or check2_status is NULL) ");
                        query = string.Format("select * from (select ROW_NUMBER() OVER (order by machineid)  as RowNum,* from  (Select  distinct '{6}' as approve_seq, '1' as level, '' as recdate1,'' as recdate2,'' as recdate3,'' as recdate4,'' as noticeshort1,'' as noticeshort2,'' as noticeshort3,'' as noticeshort4,check1_status, check1_notice,'' as check1_noticeshort,check1_date,check1_userid,approve1_status,approve1_notice,'' as approve1_noticeshort,approve1_date,approve1_userid,c.machinename,b.machineid,check2_status, check2_notice,'' as check2_noticeshort,check2_date,check2_userid,approve2_status,approve2_notice,'' as approve2_noticeshort,approve2_date,approve2_userid 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='2' and confirm1_status='Y'  {3} {2} {4}  ) as subtemp) as temp  WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize, filter, filterselect, filteruser, username, seq);
                        ds = fc.SQL_DataSet(query.ToString());


                    }
                    else if (level == "2")
                    {
                        filteruser = string.Format(" and insp_status in ('W','S')  and (approve1_status is NULL and approve2_status is NULL) and (check1_status='Y' or check2_status='Y') ");
                        query = string.Format("select * from (select ROW_NUMBER() OVER (order by machineid)  as RowNum,* from  (Select  distinct '{6}' as approve_seq,'2' as level,'' as recdate1,'' as recdate2,'' as recdate3,'' as recdate4,'' as noticeshort1,'' as noticeshort2,'' as noticeshort3,'' as noticeshort4,check1_status, check1_notice,'' as check1_noticeshort,check1_date,check1_userid,approve1_status,approve1_notice,'' as approve1_noticeshort,approve1_date,approve1_userid,c.machinename,b.machineid,check2_status, check2_notice,'' as check2_noticeshort,check2_date,check2_userid,approve2_status,approve2_notice,'' as approve2_noticeshort,approve2_date,approve2_userid 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='2' and confirm2_status='Y'  {3} {2} {4}  ) as subtemp) as temp  WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize, filter, filterselect, filteruser, username, seq);
                        ds = fc.SQL_DataSet(query.ToString());

                    }


                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        try
                        {
                            dr["recdate1"] = DateTime.Parse(dr["check1_date"].ToString()).ToString("dd/MM/yy", new CultureInfo("en-US"));
                        }
                        catch { }
                        try
                        {
                            dr["recdate2"] = DateTime.Parse(dr["approve1_date"].ToString()).ToString("dd/MM/yy", new CultureInfo("en-US"));
                        }
                        catch { }
                        try
                        {
                            dr["recdate3"] = DateTime.Parse(dr["check2_date"].ToString()).ToString("dd/MM/yy", new CultureInfo("en-US"));
                        }
                        catch { }
                        try
                        {
                            dr["recdate4"] = DateTime.Parse(dr["approve2_date"].ToString()).ToString("dd/MM/yy", new CultureInfo("en-US"));
                        }
                        catch { }

                        dr["check1_userid"] = dr["check1_userid"].ToString().Length == 0 ? "" : dr["check1_userid"].ToString() + "/" + clsControlUI.getEmpName(dr["check1_userid"].ToString());
                        dr["check2_userid"] = dr["check2_userid"].ToString().Length == 0 ? "" : dr["check2_userid"].ToString() + "/" + clsControlUI.getEmpName(dr["check2_userid"].ToString());
                        dr["approve1_userid"] = dr["approve1_userid"].ToString().Length == 0 ? "" : dr["approve1_userid"].ToString() + "/" + clsControlUI.getEmpName(dr["approve1_userid"].ToString());
                        dr["approve2_userid"] = dr["approve2_userid"].ToString().Length == 0 ? "" : dr["approve2_userid"].ToString() + "/" + clsControlUI.getEmpName(dr["approve2_userid"].ToString());


                        if (dr["approve1_notice"].ToString().Length > 15)
                        {
                            dr["approve1_noticeshort"] = dr["approve1_notice"].ToString().Substring(0, 15);

                        }
                        else
                        {
                            dr["approve1_noticeshort"] = dr["approve1_notice"].ToString();

                        }

                        if (dr["check1_notice"].ToString().Length > 15)
                        {
                            dr["check1_noticeshort"] = dr["check1_notice"].ToString().Substring(0, 15);

                        }
                        else
                        {
                            dr["check1_noticeshort"] = dr["check1_notice"].ToString();

                        }

                        if (dr["approve2_notice"].ToString().Length > 15)
                        {
                            dr["approve2_noticeshort"] = dr["approve2_notice"].ToString().Substring(0, 15);

                        }
                        else
                        {
                            dr["approve2_noticeshort"] = dr["approve2_notice"].ToString();

                        }

                        if (dr["check2_notice"].ToString().Length > 15)
                        {
                            dr["check2_noticeshort"] = dr["check2_notice"].ToString().Substring(0, 15);

                        }
                        else
                        {
                            dr["check2_noticeshort"] = dr["check2_notice"].ToString();

                        }

                    }


                    fc.closeconnection();

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


        }


        [System.Web.Services.WebMethod]
        public static string GetRoot(string equid)
        {
            DataSet ds;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string query = string.Empty;
                query = string.Format(" select emp_approve_status,emp_approve_seqno,emp_approve_status+''+emp_approve_seqno as approveid from RootApproval where flagdel=0", equid);
                ds = fc.SQL_DataSet(query.ToString());
                fc.closeconnection();
            }
            return ds.GetXml();


        }
        [System.Web.Services.WebMethod]
        public static string GetMachineMaster()
        {
            DataSet ds;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string query = string.Empty;
                query = string.Format("select machineid,machinename from  MachineMaster Where flagdel=0 order by convert(int,machineid)");
                ds = fc.SQL_DataSet(query.ToString());
                fc.closeconnection();
            }
            return ds.GetXml();


        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

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