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

        [System.Web.Services.WebMethod]
        public static string saveuser(string username, string password, string repassword, string name, string surname,string position,string email,string code,string useridcreate,string type) 
        {

            string cmd = string.Empty;
            //string strEncriptPassword = clsControlUI.encriptpassword(username, password);

            string userid = string.Empty;

            using (FcSql.Function fc = new FcSql.Function())
            {
                string chkid = string.Format("select userid from Users Where username='{0}' or usercode='{1}'  ", username,code);
                DataTable dtchkid = fc.SQL_DataTable(chkid.ToString());
                if (dtchkid.Rows.Count > 0)
                {
                    userid = dtchkid.Rows[0]["userid"].ToString();

                }
                             


                string crdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US"));
                if(userid.Length==0)
                {
                    string inst_user = string.Format("Insert into Users (usercode, username, password, firstname, lastname, position, emailaddress, flagdel, create_user, create_datetime) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}',0,'{7}','{8}')", code, username, password, name, surname, position, email, useridcreate, crdate);
                    cmd = fc.ExecSQLStatement(inst_user.ToString());
                   

                }
                else
                {
                    string update_user = string.Format("update Users set usercode='" + code + "', username='" + username + "', password='" + password + "', firstname='" + name + "', lastname='" + surname + "', position='" + position + "', emailaddress='" + email + "', flagdel=" + type + ", update_user='" + useridcreate + "', create_datetime='" + crdate + "' Where userid=" + userid);
                    cmd = fc.ExecSQLStatement(update_user.ToString());
                    

                }



               
                    

                fc.closeconnection();
            }



            return cmd;


        }


        [System.Web.Services.WebMethod]
        public static string getdata(string code)
        {

            DataSet ds = new DataSet();

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

                try
                {

                    string query = string.Format("SELECT userid, usercode, username, password, firstname, lastname, position, emailaddress, flagdel  FROM   Users Where usercode='{0}' and flagdel=0 ", code);
                    ds = fc.SQL_DataSet(query.ToString());



                    fc.closeconnection();

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


        }




        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {

        }

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