@include('admin/include/header')
<link rel="stylesheet" href="{{asset('assets/dropzone/dropzone.min.css')}}">
<link rel="stylesheet" href="{{asset('assets/modules/summernote/summernote-bs4.css')}}">
<!-- Main Content -->

<div class="main-content">
    <section class="section">
      <div class="section-header">
        <h1>Product</h1>
        <div class="section-header-breadcrumb">
          <div class="breadcrumb-item active"><a href="{{ url('admin/') }}">Dashboard</a></div>
          <div class="breadcrumb-item"><a href="javascript:void (0)">Product</a></div>
          <div class="breadcrumb-item">Add Product</div>
        </div>
      </div>

      <div class="section-body">
        <div class="row">
          <div class="col-12 col-md-12 col-lg-12">
            <div class="card">
              <form id="editProduct">
                <div class="card-header">
                  <h4>Edit Product</h4>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-12">
                            <div class="form-group">
                                <input type="hidden" name="id" value="{{$products->id}}">
                                <label>Variant</label>
                                <select value="{{$products->variant_id}}" id="variant_id" name="variant_id" class="form-control select2">
                                    @if(count($category) != 0)
                                    <option value="">Select Variant</option>
                                    @foreach ($category as $type)
                                    <option value="{{ $type->variant_id }}">{{ $type->title }}</option>
                                    @endforeach
                                    @else
                                    <option value="">No Variant Found</option>
                                    @endif
                                </select>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-group">
                                <label>Title</label>
                                <input type="text" id="title" name="title" value="{{$products->title}}" class="form-control" />
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-12">
                            <div class="form-group">
                                <label class="col-form-label text-md-right">Description</label>
                                <textarea name="description" class="summernote" id="description"></textarea>
                            </div>
                        </div>
                        <div class="col-1">
                            <div class="form-group">
                                <div class="control-label">Is Featured</div>
                                <label class="custom-switch mt-2">
                                    <input id="is_featured" type="checkbox" name="is_featured" class="custom-switch-input">
                                    <span class="custom-switch-indicator"></span>
                                </label>
                            </div>
                        </div>
                        <div class="col-1">
                            <div class="form-group">
                                <div class="control-label">On Sale</div>
                                <label class="custom-switch mt-2">
                                    <input id="on_sale" type="checkbox" name="on_sale" class="custom-switch-input">
                                    <span class="custom-switch-indicator"></span>
                                </label>
                            </div>
                        </div>
                        <div class="col-2">
                            <div class="form-group">
                                <label>Display Price</label>
                                <input id="display_price" type="number" name="display_price" value="{{$products->display_price}}" class="form-control" />
                            </div>
                        </div>
                        <div class="col-2">
                            <div class="form-group">
                                <label>Selling Price</label>
                                <input id="selling_price" type="number" name="selling_price" value="{{$products->selling_price}}" class="form-control" />
                            </div>
                        </div>
                        <div class="col-1">
                            <div class="form-group">
                                <label>GST (%)</label>
                                <input id="gst" type="number" name="gst" value="{{$products->gst}}" class="form-control" />
                            </div>
                        </div>
                        <div class="col-2">
                            <div class="form-group">
                                <label>Product Discount (%)</label>
                                <input id="discount" type="number" name="discount" value="{{$products->discount}}" class="form-control" />
                            </div>
                        </div>
                        <div class="col-2">
                            <div class="form-group">
                                <label>Shipping Charges (₹)</label>
                                <input id="shipping_charges" type="number" name="shipping_charges" value="{{$products->shipping_charges}}" class="form-control" />
                            </div>
                        </div>
                        <div class="col-1">
                            <div class="form-group">
                                <label>Total Qty</label>
                                <input id="total_qty" type="number" name="total_qty" value="{{$products->total_qty}}" class="form-control" />
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Vendor</label>
                                <select id="vendor_id" name="vendor_id" class="form-control select2">
                                    @if(count($vendors) != 0)
                                    <option value="">Select Vendor</option>
                                    @foreach ($vendors as $vendor)
                                    <option value="{{ $vendor->id }}">{{ $vendor->name }}</option>
                                    @endforeach
                                    @else
                                    <option value="">No Vendor Found</option>
                                    @endif
                                </select>
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Category</label>
                                <select value="{{$products->category_id}}" id="addProductCategory" name="category_id" class="form-control select2">
                                    @if(count($category) != 0)
                                    <option value="">Select Category</option>
                                    @foreach ($category as $parent)
                                    <option data-data="{{$parent}}" value="{{ $parent->id }}">{{ $parent->title }}</option>
                                    @endforeach
                                    @else
                                    <option value="">No Category Found</option>
                                    @endif
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="variant d-none"></div>
                    <div class="row">
                        <div class="col-6">
                            <div class="form-group">
                                <label>Product Image (max-size: 1mb)</label>
                                <div class="custom-file mb-4">
                                    <input type="file" class="custom-file-input" id="customFile" name="thumbnail">
                                    <label class="custom-file-label" for="customFile">Choose file</label>
                                </div>
                            </div>
                            </div>
                            <div class="col-6">
                                <div class="gallery gallery-md" id="gallery-thumbnail">
                                    <img class="gallery-item" src="{{$products->thumbnail}}" />
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group">
                                    <label>Product Multiple Image</label>
                                    <div class="custom-file mb-4">
                                        <input type="file" class="custom-file-input" id="multipleImage" multiple name="multi_img[]">
                                        <label class="custom-file-label" for="multipleImage">Choose file</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="gallery gallery-md position-relative" id="gallery-multithumbnail">
                                    @foreach(json_decode($products->multi_img) as $key => $image)
                                        <i class="fas fa-trash text-danger position-absolute" onclick="deleteImage('{{$products->id}}','{{$key}}')" style="z-index: 5; right: 10;"></i>
                                        <img class="gallery-item" src="{{$image}}" />
                                    @endforeach
                                </div>
                            </div>
                        </div>
                        <div class="col-1">
                            <div class="form-group">
                                <div class="control-label">Status</div>
                                <label class="custom-switch mt-2">
                                    <input id="product_status" type="checkbox" name="status" class="custom-switch-input">
                                    <span class="custom-switch-indicator"></span>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card-footer text-right">
                  <button type="submit" class="btn btn-primary submit">Update Product</button>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>

    </section>
  </div>
@include('admin/include/footer')

<script src="{{asset('assets/modules/summernote/summernote-bs4.js')}}"></script>
<script>
    $( document ).ready(function() {
        $("#description").summernote('code', "<?=$products->description?>");
        if (<?=$products->is_featured?>) {
            document.getElementById("is_featured").checked = true;
        }
        if (<?=$products->on_sale?>) {
            document.getElementById("on_sale").checked = true;
        }
        var status = '<?= $products->status ?>';
        if(status && status == 1){
            document.getElementById("product_status").checked = true;
        }
        $('#addProductCategory').val(<?= $products->category_id ?>);
        $('#addProductCategory').trigger('change');
        $('#vendor_id').val(<?= $products->vendor_id ?>);
        $('#vendor_id').trigger('change');
        showHideDiv('.variant', 'show');
        var variant = JSON.parse('<?=$products->details?>');
        var html = generateHtmlWithValue(variant)
        $('.variant').html(html);
    });
</script>
