@extends('backend.layouts.master')

@section('main-content')

<div class="card">
    <h5 class="card-header">Create Subscription Package</h5>

    <div class="card-body">

          <form method="POST" action="{{ route('subscription.store') }}">
            @csrf

            {{-- PACKAGE DETAILS --}}
            <div class="row">
                <div class="col-md-6 form-group">
                    <label>Package Name</label>
                    <input type="text" name="package_name" class="form-control" required>
                </div>

                <div class="col-md-3 form-group">
                    <label>Package Price</label>
                    <input type="number" step="0.01" name="price" class="form-control" required>
                </div>

                <div class="col-md-3 form-group">
                    <label>Duration (Days)</label>
                    <input type="number" name="duration_days" class="form-control" required>
                </div>

                <div class="col-md-6 ">
                    <label>package description</label>
                    <input type="text" name="package_description" class="form-control" required>
                </div>
            </div>

            <hr>

            {{-- PRODUCTS SECTION --}}
            <h6>Subscription Products</h6>

            <div id="product-wrapper">

                <div class="product-item border p-3 mb-3">

                    <div class="row">

                        <div class="col-md-3 form-group">
                            <label>Product Name</label>
                            <input type="text" name="products[0][product_name]" class="form-control" required>
                        </div>

                        <div class="col-md-3 form-group">
                            <label>Category</label>
                            <input type="text"
                                   name="products[0][category]"
                                   class="form-control category-field"
                                   >
                        </div>

                        <div class="col-md-2 form-group">
                            <label>Quantity</label>
                            <input type="number" name="products[0][quantity]" value="1" class="form-control" required>
                        </div>

                        <div class="col-md-2 form-group">
                            <label>Price</label>
                            <input type="number" step="0.01" name="products[0][price]" class="form-control" required>
                        </div>

                        <div class="col-md-2 form-group d-flex align-items-end">
                            <button type="button" class="btn btn-danger remove-product">X</button>
                        </div>

                        <!-- <div class="col-md-12 form-group mt-2">
                            <label>Description</label>
                            <textarea name="products[0][product_description]" class="form-control"></textarea>
                        </div> -->

                    </div>
                </div>

            </div>

            <button type="button" class="btn btn-success mb-3" id="add-product">
                + Add Product
            </button>

            <br>

            <button type="submit" class="btn btn-primary">
                Save Subscription Package
            </button>

        </form>

    </div>
</div>

@endsection

@section('scripts')

<script>
let productIndex = 1;

document.getElementById('add-product').addEventListener('click', function () {

    let wrapper = document.getElementById('product-wrapper');

    let html = `
    <div class="product-item border p-3 mb-3">
        <div class="row">

            <div class="col-md-3 form-group">
                <label>Product Name</label>
                <input type="text"
                       name="products[${productIndex}][product_name]"
                       class="form-control"
                       required>
            </div>

            <div class="col-md-3 form-group">
                <label>Category</label>
                <input type="text"
                       name="products[${productIndex}][category]"
                       class="form-control">
            </div>

            <div class="col-md-2 form-group">
                <label>Quantity</label>
                <input type="number"
                       name="products[${productIndex}][quantity]"
                       value="1"
                       class="form-control"
                       required>
            </div>

            <div class="col-md-2 form-group">
                <label>Price</label>
                <input type="number"
                       step="0.01"
                       name="products[${productIndex}][price]"
                       class="form-control"
                       required>
            </div>

            <div class="col-md-2 form-group d-flex align-items-end">
                <button type="button"
                        class="btn btn-danger remove-product">
                    X
                </button>
            </div>

        </div>
    </div>
    `;

    wrapper.insertAdjacentHTML('beforeend', html);
    productIndex++;
});

document.addEventListener('click', function (e) {
    if (e.target.classList.contains('remove-product')) {
        e.target.closest('.product-item').remove();
    }
});
</script>

@endsection