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.setting
{
    public partial class Register_operator : System.Web.UI.Page
    {


        [System.Web.Services.WebMethod]
        public static string save(string parahd)
        {
            string cmdhd = string.Empty;
            string approvelogid = 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 inspectionid = strmt[0].ToString();
                    string inspectionname = strmt[1].ToString();
                    string linegroupid = strmt[2].ToString();
                    string linename = strmt[3].ToString();
                    string partnumber = strmt[4].ToString();
                    string empid = strmt[5].ToString();
                    string empname = strmt[6].ToString();
                    string approveid = strmt[7].ToString();
                    string approvename = strmt[8].ToString();
                    string userid = strmt[9].ToString();

                    string position = string.Empty;
                    string emailaddress = string.Empty;
                    string approvelevel = string.Empty;
                    string approve_seq = string.Empty;

                    string str_seq = string.Format("select approve_seq FROM  mst_Approve where linegroupid='{0}' and partnumber='{1}' and inspectiontype='{2}' and approve_userid<>'{3}' ",linegroupid,partnumber,inspectionid,empid);
                    DataTable dt_seq = fc.SQL_DataTable(str_seq.ToString());
                    if (dt_seq.Rows.Count > 0)
                    {
                        approve_seq = "2";

                    }
                    else
                    {
                        approve_seq = "1";

                    }



                    string str_emp = string.Format("select userid, usercode, position, emailaddress  FROM   Users where usercode='{0}'", empid);
                    DataTable dt_emp = fc.SQL_DataTable(str_emp.ToString());
                    if(dt_emp.Rows.Count>0)
                    {
                        position = dt_emp.Rows[0]["position"].ToString();
                        emailaddress = dt_emp.Rows[0]["emailaddress"].ToString();

                    }



                    string chkid = string.Format("select approve_userid,approve_id from mst_Approve Where approve_userid='{0}' ", empid);
                    DataTable dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        empid = dtchkid.Rows[0]["approve_userid"].ToString();
                        approvelogid = dtchkid.Rows[0]["approve_id"].ToString();

                    }

                    string str_approvelevel = string.Format("select approvallevel from mst_Approval_Level Where approvalid='{0}'", approveid);
                    DataTable dt_approvelevel = fc.SQL_DataTable(str_approvelevel);
                    if(dt_approvelevel.Rows.Count>0)
                    {
                        approvelevel = dt_approvelevel.Rows[0]["approvallevel"].ToString();

                    }
                   



                    if (approvelogid.Length == 0)
                    {

                        approvelogid = clsControlUI.create_master_id("mst_Approve", "approve_id");


                    }



                    string crdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US"));

                    string inst_mt = string.Format(" insert into mst_Approve (approve_id, approve_userid, approve_username, approve_position, approve_level, approve_email, approve_seq, inspectiontype, flagdel, create_user, create_datetime,linegroupid,linename,partnumber,approve_positionid) VALUES ('" + approvelogid + "','" + empid + "','" + empname + "','" + position + "'," + approvelevel + ",'" + emailaddress + "'," + approve_seq + ",'" + inspectionid + "',0,'" + userid + "','" + crdate + "','" + linegroupid + "','" + linename + "','" + partnumber + "','"+approveid+"')");
                    string up_mt = string.Format(" update mst_Approve SET approve_userid='" + empid + "', approve_username='" + empname + "', approve_position='" + approvename + "', approve_level=" + approvelevel + ", approve_email='" + emailaddress + "', approve_seq=" + approve_seq + ", inspectiontype='" + inspectionid + "', flagdel=0,update_user='" + userid + "', update_datetime= '" + crdate + "',linegroupid='" + linegroupid + "',linename='" + linename + "',partnumber='" + partnumber + "',approve_positionid='"+approveid+"'  Where approve_id= '" + approvelogid + "' ");

                    cmdhd = fc.ExecSQLStatement(inst_mt.ToString());
                    if (cmdhd.ToLower().IndexOf("duplicate") != -1)
                    {
                        cmdhd = fc.ExecSQLStatement(up_mt.ToString());
                    }

                    fc.closeconnection();


                }
                catch { };

            }



            return cmdhd;


        }


        [System.Web.Services.WebMethod]
        public static string getdataedit(string approvelogid)
        {

            DataSet ds = new DataSet();
            using (FcSql.Function fc = new FcSql.Function())
            {


                try
                {


                    string query = string.Format("select c.inspectionid,c.inspectionname,a.linegroupid,a.linename,a.partnumber,approve_userid as empid,approve_username as empname,approve_positionid as approveid,approve_position as approvename from mst_Approve as a inner join mst_Approval_Level as b on a.approve_positionid=b.approvalid inner join mst_Inspection as c on a.inspectiontype=c.inspectionid Where a.approve_id='{0}'", approvelogid);
                    ds = fc.SQL_DataSet(query.ToString());



                    fc.closeconnection();

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


        }       



        [System.Web.Services.WebMethod]
        public static string getdata(string parahd, string currentpage)
        {

            DataSet ds = new DataSet();
            int pagesize = 10;
            string cmdhd = string.Empty;
            string inspidid = string.Empty;
            string userapprove = string.Empty;
            string filter = 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 query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by create_datetime desc)  as RowNum,approve_id, approve_userid, approve_username, approve_position from mst_Approve  Where flagdel=0) as temp WHERE RowNum BETWEEN ({0} - 1) * {1} + 1 AND {0} * {1} ", currentpage, pagesize);
                    ds = fc.SQL_DataSet(query.ToString());



                    fc.closeconnection();

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


        }



        [System.Web.Services.WebMethod]
        public static string DeleteRow(string approvelogid)
        {
            string cmdhd = string.Empty;

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

                    string del_mt = string.Format("update mst_Approve set flagdel=1 Where approve_id='{0}' ", approvelogid);

                    cmdhd = fc.ExecSQLStatement(del_mt.ToString());

                    fc.closeconnection();


                }
                catch { };

            }



            return cmdhd;


        }



        protected void Page_Load(object sender, EventArgs e)
        {

        }

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