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

//

export const showEditDepartmentForm = async (id, name, description, economic_department_id = null) => {
  await Swal.fire({
    title: 'Edit Department',
    html: `
      <form id="editDepartmentForm">
        <div class="field">
          <label class="label">Name</label>
          <div class="control">
            <input class="input" type="text" id="name" value="${name}" />
          </div>
        </div>
        <div class="field">
          <label class="label">Description</label>
          <div class="control">
            <input class="input" type="text" id="description" value="${description}" />
          </div>
        </div>
        <div class="field">
          <label class="label">Economic Department ID</label>
          <div class="control">
            <input class="input" type="number" id="economic_department_id" value="${economic_department_id}" />
          </div>
        </div>
      </form>
    `,
    showCancelButton: true,
    confirmButtonText: 'Edit Department',
    preConfirm: () => {
      editDepartment(id, document.getElementById('name').value, document.getElementById('description').value, document.getElementById('economic_department_id').value)
          .then(() => {
            Swal.fire('Department edited successfully');
          })
          .catch((e) => {
            Swal.fire('Error', e.response.data.error ?? e.response, 'error');
          });
    }
  });
}

</script>