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

//

export const showCreateUserDiscountForm = async () => {
  await Swal.fire({
    title: 'Create user discount',
    html: `
      <form id="createUserDiscountForm">
        <div class="field">
          <label class="label">User ID</label>
          <div class="control">
            <input class="input" type="number" id="userId" />
          </div>
        </div>
        <div class="field">
          <label class="label">Object ID</label>
          <div class="control">
            <input class="input" type="number" id="objectId" />
          </div>
        </div>
        <div class="field">
          <label class="label">Discount</label>
          <div class="control">
            <input class="input" type="number" id="discount" />
          </div>
        </div>
        <div class="field">
          <label class="label">Is category ?</label>
          <div class="control">
            <input class="checkbox" type="checkbox" id="isCategory" />
          </div>
        </div>
      </form>
    `,
    showCancelButton: true,
    confirmButtonText: 'Create Discount',
    preConfirm: () => {
      createUserDiscount(document.getElementById('userId').value, document.getElementById('objectId').value, document.getElementById('discount').value, document.getElementById('isCategory').checked)
          .then(() => {
            Swal.fire('Discount created', '', 'success');
          })
          .catch((e) => {
            Swal.fire('Error', e.response.data.error ?? e.response, 'error');
          });
    }
  });
}

</script>