@extends('adminlte::page') @section('content')
<?php Fun::lang() ?>
<div class="title-page">
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="#">Construction</a></li>
            <li><a href="#">Set Up Information</a></li>
            <li><a href="#">Product List</a></li>
            <li><a href="#">Edit</a></li>
        </ul>
    </div>
</div>
<div class="page">
    <div class="top-content">
        <div class="row">
            <div class="col-sm-12 col-xs-12">
                <div class="title-page">
                    <div class="pull-left">
                        <h3>Edit Product</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 col-xs-12">

            <div class="form-wrapper">
                {!! Form::model($product, ['method' => 'PATCH','route' => ['construction-product.update', $product->id],'enctype'=>'multipart/form-data']) !!}
                <div class="row">
                    <div class="form-group col-sm-4 col-xs-12">
                        <label>Code</label>
                        {!! Form::text('code', null, array('placeholder' => 'Code','class' => 'form-control')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Name</label>
                        {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control','required')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label>Model</label>
                        {!! Form::text('model', null, array('placeholder' => 'Model','class' => 'form-control')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Category</label>
                        {!! Form::select('category_id', $categories, null, ['class' => 'form-control select2','required','id'=>'category-id']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Sub Category</label>
                        {!! Form::select('brand_id', $brands, null, ['class' => 'form-control select2','required','id'=>'sub-category-id']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label>Unit Price($)</label>
                        {!! Form::text('current_price', null, array('placeholder' => 'Unit Price','class' => 'form-control number-only')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Unit</label>
                        {!! Form::select('unit_id',$units, null, array('class' => 'form-control select2','required')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label>Description</label>
                        {!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')) !!}
                    </div>
                    <div class="col-sm-12 col-xs-12">
                        <label for="required">Feature Image ( <a href="javascript:void(0)" onclick="add_more_picture(this)"  class="image-a" data-count = '{{ count($product_image) }}'>Add more</a> )</label>
                                <p class="text-danger"></p>
                        <div class="form-group panel panel-default append-image">
                            @foreach($product_image as $k=>$value)
                                @if($k==0)
                                <div style="float: left;">
                                    <input type="hidden" name="image_old[]" value='{{$value->image}}'>
                                    <img src='/assets/images/resize/768x768/{{$value->image}}' width="100px" class="hide_img">
                                    <div class="imageupload{{ $k }}">
                                        <div class="file-tab panel-body">
                                            <label class="btn button_warning btn-file">
                                                <span>Browse</span>
                                                <input type="file" name="image[]">
                                            </label>
                                            <button type="button" class="btn button_danger">Remove</button>
                                        </div>
                                    </div>
                                </div>
                                @else
                                <div style="float: left;">
                                    <input type="hidden" name="image_old[]" value='{{$value->image}}'>
                                    <img src='/assets/images/resize/768x768/{{$value->image}}' width="100px" class="hide_img">
                                    <div class="imageupload{{ $k }}">
                                        <div class="file-tab panel-body">
                                            <label class="btn button_warning btn-file">
                                                <span>Browse</span>
                                                <input type="file" name="image[]">
                                            </label>
                                            <button type="button" class="btn button_danger">Remove</button>
                                        </div>
                                    </div>
                                </div>
                                @endif
                                
                            @endforeach
                        </div>
                    </div>
                    
                    <div class="col-sm-12 col-xs-12">
                        <div class="form-group">
                            <button type="submit" class="button_sm button_primary pull-right"><span class="fa fa-save"></span> Save</button>
                            <button type="button" onClick="javascript:history.go(-1)" class="button_sm button_warning pull-right">
                                <span class=" fa fa-arrow-circle-left"></span> Back</button>
                        </div>
                    </div>
                </div>

                {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>
@endsection @section('js')
<script type="text/javascript">
$(document).ready(function(){
    var count = $(".image-a").attr('data-count');
    for(var i = 0 ;i<count;i++){
        $(".imageupload"+i).imageupload();
    }
    
});
function add_more_picture(obj){
    let count = $(obj).attr('data-count');
    $(obj).attr('data-count',Number(count+1));
    $('.append-image').append(`
                    <div class="imageupload${count}" style="float: left;">
                        <img src="{{asset('assets/images/default.png')}}" class="hide_img" width="100px">
                        <div class="file-tab panel-body">
                            <label class="btn button_warning btn-file">
                                <span>Browse</span>
                                <input type="file" name="image[]">
                            </label>
                            <button type="button" class="btn button_danger">Remove</button>
                        </div>
                    </div>`);
                    $(".imageupload"+count).imageupload();
}
    $(document).on("change","#category-id",function(){
        $("#main-spinner").show();
        var route = "{{route('get_sub_cagegory_product',':id')}}";
        route = route.replace(":id",$(this).val());
        $.ajax({
            url:route,
            type:"get",
            dataType:"JSON",
            success:function(data){
                $("#main-spinner").hide();
                $("#sub-category-id").empty();
                $("#sub-category-id").append("<option value=''>Please Select</option>");
                $.each(data,function(index,value)
                {
                    $("#sub-category-id").append(`<option value='${value.id}'>${value.name}</option>`);
                });
            },
            complete:function(){
                $("#main-spinner").hide();
            }
        });
    });
</script>
@endsection