{{define "theme-mdui/viewpassword"}}
<!doctype html>
<html lang="{{.Conf.Language}}">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title>{{.Title}}</title>

  <!-- MDUI CSS -->
  <link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/mdui/1.0.2/css/mdui.min.css"/>
  <link rel="stylesheet" href="/static/theme-mdui/mdui.css" type="text/css">
  {{if ts .CustomCode}}
  {{.CustomCode|safe}}
  {{end}}
</head>

<body>
  <div class="mdui-container" id="container">
    <div class="mdui-dialog mdui-dialog-prompt mdui-dialog-open">
      <div class="mdui-dialog-title">{{tr "VerifyPassword"}}</div>

      <div class="mdui-dialog-content">
        <div class="mdui-textfield">
          <i class="mdui-icon material-icons">lock</i>
          <label class="mdui-textfield-label">{{tr "EnterPassword"}}</label>
          <input class="mdui-textfield-input" type="password" id="password">
        </div>
      </div>

      <div class="mdui-dialog-actions">
        <button class="mdui-btn mdui-ripple mdui-text-color-primary" id="confimBtn">{{tr "Confirm"}}</button>
      </div>
    </div>
  </div>

  <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/mdui/1.0.2/js/mdui.min.js"></script>
  <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/jquery/3.6.0/jquery.min.js"></script>
  <script>
    var $input = mdui.$('#container').find('.mdui-textfield-input');
    var $dialog = new mdui.Dialog(mdui.$('.mdui-dialog'));
    var onConfirm = async function() {
      var pwd = $input.val();
      const res = await fetch("/view-password", {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        redirect: 'follow',
        body: JSON.stringify({ Password: pwd }),
      });
      if (res.ok) {
        if (res.redirected) {
          window.location.href = res.url;
        } else {
          mdui.snackbar({
           message: '{{tr "WrongPassword"}}',
           position: 'top',
           timeout: 2000,
         });
          mdui.mutation();
        }
      }
    };

    mdui.updateTextFields($input);
    $input.on('keydown', function (event) {
      if (event.keyCode === 13) {
        var value = $input.val();
        onConfirm(value);
        return false;
      }
      return;
    });
    document.getElementById('confimBtn').addEventListener('click', onConfirm);

    $dialog.open();
    $input[0].focus();
    mdui.mutation();
  </script>
</body>
</html>
{{end}}
