$(document).on('page:init', '.page[data-name="account_edit"]', async function (e) {

  console.log('app account_edit ready')

  var view = app.views.current



  let user = JSON.parse(localStorage.getItem('f04_user'));

  console.log(user)

  if (!user) {

      app.views.main.router.navigate('/signin/');

      return

  }



  // Example of how to fill in the form fields with the provided data

  document.querySelector('input[name="token_3"]').value = user.sess_token || '';

  document.querySelector('input[name="uuid_3"]').value = user.uuid || '';

  document.querySelector('input[name="email_3"]').value = user.data.email || '';

  document.querySelector('input[name="first_name_3"]').value = user.data.firstname || '';

  document.querySelector('input[name="last_name_3"]').value = user.data.lastname || '';

  document.querySelector('input[name="phone_3"]').value = user.data.phone || '';

})



$$(document).on('page:init', '.page[data-name="account_edit"]', function(e) {

  $(document).off("click", "#account_edit_button").on("click", "#account_edit_button", function(e) {

    e.preventDefault();

  let user = JSON.parse(localStorage.getItem('f04_user'));



    const token=user.sess_token;

    const firstname = $('input[name="first_name_3"]').val().trim();

    const lastname = $('input[name="last_name_3"]').val().trim();

    const phone = $('input[name="phone_3"]').val().trim();

    const uuid = $('input[name="uuid_3"]').val().trim();

    let req = {

      uuid,

      token,

      firstname,

      lastname,

      phone

    };

    if (phone && firstname) {

      app.preloader.show();

      promiseTimeout(30000, account_edit(req))

        .then(res => {

          if (res.status) {

              app.preloader.hide();

              app.dialog.alert(res.message, "Account Updated", () => {

              app.views.main.router.navigate('/account_edit/');

            });

          } else {

            app.preloader.hide();

            app.dialog.alert(res.message, "Edit Account Errors")

          }

        }).catch(err => {

          //app.preloader.hide();

          //app.dialog.alert(err, "Timed Out");

          app.views.main.router.navigate('/account_edit/');

          app.dialog.alert("Account Updated", "Edit Account");

        });

    } else {

      app.dialog.alert("All fields are required", "Edit Account Errors");

    }

  });

});