<script>
import { ref } from 'vue'
import { createProduct } from "@/components/shop/Products.vue";
import Swal from "sweetalert2";

//

export const showCreateProductForm = async () => {
  await Swal.fire({
    title: 'Create Product',
    html: `
      <form id="createProductForm">
        <div class="field">
          <label class="label">Name</label>
          <div class="control">
            <input class="input" type="text" id="name" />
          </div>
        </div>
        <div class="field">
          <label class="label">Description</label>
          <div class="control">
            <input class="input" type="text" id="description" />
          </div>
        </div>
        <div class="field">
          <label class="label">Price</label>
          <div class="control">
            <input class="input" type="number" id="price" />
          </div>
        </div>
        <div class="field">
          <label class="label">Category</label>
          <div class="control">
            <input class="input" type="text" id="category" />
          </div>
        </div>
        <div class="field">
          <label class="label">Piktogram</label>
          <div class="control">
            <input class="input" type="text" id="piktogram" />
          </div>
        </div>
        <div class="field">
          <label class="label">Economic produkt id</label>
          <div class="control">
            <input class="input" type="number" id="economicProductId" />
          </div>
        </div>
      </form>
    `,
    showCancelButton: true,
    confirmButtonText: 'Create Product',
    preConfirm: () => {
      createProduct(document.getElementById('name').value, document.getElementById('description').value, document.getElementById('price').value, document.getElementById('category').value, document.getElementById('piktogram').value, document.getElementById('economicProductId').value)
          .then(() => {
            Swal.fire('Product created successfully');
          })
          .catch((e) => {
            Swal.fire('Error', e.response.data.error ?? e.response, 'error');
          });
    }
  });
}

</script>