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


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

            string str_cmd = string.Empty;

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

                try
                {

                    string level = string.Empty;
                    string filteruser = string.Empty;
                    string username = string.Empty;
                    string seq = string.Empty;
                    string partnumber = string.Empty;
                    string str_user = string.Format(" select approve_userid,approve_level,approve_username,approve_seq,partnumber from mst_Approve  Where approve_userid='{0}' and inspectiontype='{1}' ", userid,insp_type);
                    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();
                        partnumber = dt_user.Rows[0]["partnumber"].ToString();
                    }
                    string query = string.Empty;
                    if (level == "1")
                    {

                        if (id.IndexOf("*") != -1)
                        {

                            string[] arr = id.Split('*');
                            foreach (string str in arr)
                            {
                                if(status=="Y")
                                {
                                    notice = "";

                                }
                                string str_confirm = string.Empty;
                                str_confirm = string.Format("update InspectionInput set check{6}_status='{0}',check{6}_notice='{1}',check{6}_date='{2}',check{6}_userid='{3}' Where inspectiontype='{5}' and confirm{6}_status='Y'  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, insp_type,seq);
                                str_cmd = fc.ExecSQLStatement(str_confirm);


                                if(insp_type=="3")
                                {

                                    
                                    str_confirm = string.Format("update InspectionInput set check{6}_status='{0}',check{6}_notice='{1}',check{6}_date='{2}',check{6}_userid='{3}' Where inspectiontype='{5}' and confirm{6}_status='Y'  and  jigid in (select jigid from EquipmentList Where  machineid='{4}') ", status, notice, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US")), userid, str, insp_type, seq);
                                    str_cmd = fc.ExecSQLStatement(str_confirm);

                                }

                               



                            }

                            if (status == "Y")
                            {

                                string userapprove = clsControlUI.getuserapprove("2", insp_type,partnumber);
                                clsControlUI.sendemail("2", "Check sheet document waiting for approval.", userapprove, insp_type);



                            }
                            else
                            {
                                string usercreate = string.Empty;
                                string str_usercreate = string.Format("select create_user,inspidid from InspectionInput Where check{2}_status='N' and approve{2}_status is NULL and inspectiontype='{0}'", insp_type, seq);
                                DataTable dt_usercreate = fc.SQL_DataTable(str_usercreate);
                                if (dt_usercreate.Rows.Count > 0)
                                {
                                    usercreate = dt_usercreate.Rows[0]["create_user"].ToString();

                                }
                                foreach (DataRow dr in dt_usercreate.Rows)
                                {
                                    string upstatus = string.Format("update InspectionInput SET insp_status='R' Where inspidid='{0}' ", dr["inspidid"].ToString());
                                    string str_cmdup = fc.ExecSQLStatement(upstatus);


                                }
                                clsControlUI.sendreturnemail("1", "Check sheet document have been Reject.", usercreate, insp_type, userid);


                            }


                        }

                    }
                    else if (level == "2")
                    {
                        if (id.IndexOf("*") != -1)
                        {

                            string[] arr = id.Split('*');
                            foreach (string str in arr)
                            {
                                if (status == "Y")
                                {
                                    notice = "";

                                }

                                string str_confirm = string.Empty;
                                str_confirm = string.Format("update InspectionInput set approve{6}_status='{0}',approve{6}_notice='{1}',approve{6}_date='{2}',approve{6}_userid='{3}' Where inspectiontype='{5}' and check{6}_status='Y'  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,insp_type,seq);
                                str_cmd = fc.ExecSQLStatement(str_confirm);

                                if(insp_type=="3")
                                {
                                    str_confirm = string.Format("update InspectionInput set approve{6}_status='{0}',approve{6}_notice='{1}',approve{6}_date='{2}',approve{6}_userid='{3}' Where inspectiontype='{5}' and check{6}_status='Y'  and  jigid in (select jigid from EquipmentList Where  machineid='{4}') ", status, notice, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US")), userid, str, insp_type, seq);
                                    str_cmd = fc.ExecSQLStatement(str_confirm);


                                }
                                

                               
                            }

                            if (status == "Y")
                            {

                                string usercreate = string.Empty;
                                string str_usercreate = string.Format("select inspidid,create_user from InspectionInput Where approve{1}_status='Y' and insp_status='W'  and inspectiontype='{0}'", insp_type, seq);
                                DataTable dt_usercreate = fc.SQL_DataTable(str_usercreate);
                                foreach (DataRow dr in dt_usercreate.Rows)
                                {
                                    string upstatus = string.Format("update InspectionInput SET insp_status='S' Where inspidid='{0}' ", dr["inspidid"].ToString());
                                    string str_cmdup = fc.ExecSQLStatement(upstatus);


                                }

                            }
                            else
                            {
                                string usercreate = string.Empty;
                                string str_usercreate = string.Format("select inspidid,create_user from InspectionInput Where  insp_status='W' and approve{1}_status='N' and confirm{1}_status is NOT NULL and inspectiontype='{0}'", insp_type, seq);
                                DataTable dt_usercreate = fc.SQL_DataTable(str_usercreate);
                                if (dt_usercreate.Rows.Count > 0)
                                {
                                    usercreate = dt_usercreate.Rows[0]["create_user"].ToString();

                                }
                                foreach (DataRow dr in dt_usercreate.Rows)
                                {
                                    string upstatus = string.Format("update InspectionInput SET insp_status='R' Where inspidid='{0}' ", dr["inspidid"].ToString());
                                    string str_cmdup = fc.ExecSQLStatement(upstatus);


                                }
                                clsControlUI.sendreturnemail("2", "Check sheet document have been Reject.", usercreate, insp_type, userid);


                            }


                        }

                    }




                    fc.closeconnection();

                }
                catch { }
            }
            return str_cmd;


        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}