<script>
import { ref } from 'vue'
import { editUser } from '@/components/session/Session.vue'
import Swal from "sweetalert2";

//

export const showEditUserForm = async (id, customer_number, display_name, role) => {
  await Swal.fire({
    title: 'Edit User',
    html: `
      <form id="editNumberPlateScannerForm">
        <div class="field">
          <label class="label">Customer Number</label>
          <div class="control">
            <input class="input" type="text" id="customer_number" value="${customer_number}" />
          </div>
        </div>
        <div class="field">
          <label class="label">Display Name</label>
          <div class="control">
            <input class="input" type="text" id="display_name" value="${display_name}" />
          </div>
        </div>
        <div class="field">
          <label class="label">Role</label>
          <div class="control">
            <input class="input" type="text" id="role" value="${role}" />
          </div>
        </div>
        <div class="field">
          <label class="label">Password</label>
          <div class="control">
            <input class="input" type="password" id="password" />
          </div>
        </div>
      </form>
    `,
    showCancelButton: true,
    confirmButtonText: 'Edit User',
    preConfirm: () => {
      editUser(id, document.getElementById('customer_number').value, document.getElementById('display_name').value, document.getElementById('role').value, document.getElementById('password').value)
          .then(() => {
            Swal.fire('User edited successfully');
          })
          .catch((e) => {
            Swal.fire('Error', e.response.data.error ?? e.response, 'error');
          });
    }
  });
}

</script>