$(document).ready(function () {



});



function createuser(type) {

    let username = $("#txtUserName").val();
    let password = $("#txtPassword").val();
    let repssword = $("#txtRePassword").val();
    let name = $("#txtName").val();
    let surname = $("#txtSurname").val();
    let position = $("#txtPosition").val();
    let email = $("#txtEmail").val();
    let code = $("#txtCode").val();
    let userid = $("#lbluserid").html();
    let admintype="";
    if($("#admin").prop("checked")==true)
    {
        admintype="Y";

    }
    else
    {
        admintype="N";

    }

    if (code == "") {
        swal({
            title: "Warning",
            text: "Please input information!",
            type: 'warning'
        });

        $("#txtCode").focus();
        $("#txtCode").select();

    }
    else if (username == "" || password == "" || repssword == "") {

        swal({
            title: "Warning",
            text: "Please input information!",
            type: 'warning'
        });

        $("#txtUserName").focus();
        $("#txtUserName").select();
    }


    else if (password != repssword) {

        swal({
            title: "Warning",
            text: "Password is not match!",
            type: 'warning'
        });

        $("#txtRePassword").focus();
        $("#txtRePassword").select();

    }
    else {
        $.ajax({
            type: "POST",
            url: "CreateUser.aspx/saveuser",
            data: "{username:'" + username + "',password:'" + password + "',repassword:'" + repssword + "',name:'" + name + "',surname:'" + surname + "',position:'" + position + "',email:'" + email + "',code:'" + code + "',useridcreate:'" + userid + "',type:'"+type+"',admintype:'"+admintype+"'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {

                if (response.d == "") {


                    swal({
                        title: "Success",
                        type: 'success',
                        text: "Successfully created a new user.",
                        timer: 1000,
                        showConfirmButton: false,
                        timerProgressBar: true
                    })

  .then(() => {
      swal({ 
          title: 'Success!',
          type: 'info',
          timer: 1000,
          showConfirmButton: false
      })
  })




                    $("#txtUserName").val("");
                    $("#txtPassword").val("");
                    $("#txtRePassword").val("");
                    $("#txtName").val("");
                    $("#txtSurname").val("");
                    $("#txtUserName").focus();
                    $("#txtUserName").select();
                    $("#txtPosition").val("");
                    $("#txtEmail").val("");
                    $("#txtCode").val("");
                    $("#admin").prop("checked",false);



                }
                else {
                    swal({
                        title: "Error",
                        text: response.d,
                        type: "error"
                    });



                }

            },
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });

    }
}


function getdata()
{
  


        let empcode = $("#txtCode").val();        

        $.ajax({
            type: "POST",
            url: "CreateUser.aspx/getdata",
            data: '{code:"' + empcode + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var xmlDoc = $.parseXML(response.d);
                var xml = $(xmlDoc);
                var customers = xml.find("Table");
                if (customers.length == 0) {
                    $("#txtUserName").val("");
                    $("#txtPassword").val("");
                    $("#txtRePassword").val("");
                    $("#txtName").val("");
                    $("#txtSurname").val("");
                    $("#txtPosition").val("");
                    $("#txtEmail").val("");
                    $("#admin").prop("checked",false);


                }

                $.each(customers, function () {
                    var customer = $(this);
                                 
                    $("#txtUserName").val($(this).find("username").text());
                    $("#txtPassword").val($(this).find("password").text());
                    $("#txtRePassword").val($(this).find("password").text());
                    $("#txtName").val($(this).find("firstname").text());
                    $("#txtSurname").val($(this).find("lastname").text());
                    $("#txtPosition").val($(this).find("position").text());
                    $("#txtEmail").val($(this).find("emailaddress").text());
                    $("#txtCode").val($(this).find("usercode").text());
                    if($(this).find("usertype").text()=="A")
                    {
                        $("#admin").prop("checked",true);
                    }
                    else
                    {
                        $("#admin").prop("checked",false);

                    }
             


                });

           

            },
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });


    




}