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

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

            string str_cmd = string.Empty;

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

                try
                {

                    string level = string.Empty;
                    string filteruser = string.Empty;
                    string seq = string.Empty;
                    string str_user = string.Format(" select approve_userid,approve_level,approve_seq from mst_Approve  Where  approve_userid in (select userid from Users Where usercode='{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();
                        seq = dt_user.Rows[0]["approve_seq"].ToString();

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

                        if (status == "Y")
                        {
                            notice = "";

                        }
                        string str_confirm = string.Format("update InspectionInput set confirm1_status='{0}',confirm1_notice='{1}',confirm1_date='{2}',confirm1_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);

                        if (status == "N")
                        {

                           
                            string usercreate = string.Empty;
                            string str_usercreate = string.Format("select create_user,inspidid from InspectionInput Where confirm1_status='N' and check2_status is NULL and inspectiontype='{0}'", insp_type);
                            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 (status == "Y")
                        {
                            notice = "";

                        }
                        string str_confirm = string.Format("update InspectionInput set confirm2_status='{0}',confirm2_notice='{1}',confirm2_date='{2}',confirm2_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);
                        if (status == "N")
                        {


                            string usercreate = string.Empty;
                            string str_usercreate = string.Format("select create_user,inspidid from InspectionInput Where confirm2_status='N' and check2_status is NULL and inspectiontype='{0}'", insp_type);
                            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)
        {

        }
    }
}