@extends('layouts.masterBackend')

@section('title')
    {{$category}}
@endsection

@section('content')
    <!-- MAIN CONTENT -->
    <div class="container-fluid">
    	<div class="row">

        <div class="col-xs-12">
			    <div class="panel panel-primary">
			    	<div class="panel-heading">
              <h2>
                @lang('backend.products')
  			    		<button class="btn btn-success" data-toggle="modal" data-target="#addProduct">@lang('backend.new-product')</button>
                <button class="btn btn-warning" data-toggle="modal" data-target="#reorder">@lang('backend.arrange-order')</button>
              </h2>
			    	</div>
			    	<div class="panel-body">
				    	@if(Session::has('message-product'))
  							<div class="alert {{ Session::get('alert-class') }} alert-dismissible" role="alert">
  								<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  							 	<p>{{ Session::get('message-product') }}</p>
  							</div>
						  @endif
				    	<div class="table-responsive">
				    		<table id="productTable" class="table table-hover">
							    <thead>
								    <tr>
								    	<th class="text-center">#</th>
                      <th>@lang('backend.name')</th>
                      <th class="text-center">@lang('backend.image')</th>
								      <th class="text-center">@lang('backend.price')</th>
                      <th class="text-center">@lang('backend.featured')</th>
								      <th class="text-center">@lang('backend.options')</th>
								    </tr>
							    </thead>
							    <tbody>
                    @foreach ($products as $key => $val)
                      <tr>
                        <td class="text-center">{{$key+1}}</td>
                        <td>{{ $val->product_name }}</td>
                        <td class="text-center">
                          <img class="img img-responsive" alt="image" src="{{ asset($storage_url.'img/products/'.$val->product_code.'/'.$val->product_image) }}" style="max-height: 200px; height: 100%;margin: 0 auto; width: auto;">
                        </td>
                        @php
                          $currency = explode('|',$val->product_currency);
                        @endphp
                        @php
                          if( $masterCurrency == "USD" ){
                            $price = number_format($val->product_price,2,".",",");
                          }elseif( $masterCurrency == "EUR" ){
                            $price = number_format($val->product_price,2,",",".");
                          }else{
                            $price = number_format($val->product_price,0,",",".");
                          }
                        @endphp
                        <td class="text-center">{{$masterCurrency}} {{$price}}</td>
                        <td class="text-center">
                          <a href="{{ route('backendProductFeatured', $val->id) }}" class="btn btn-xs {{ $val->is_featured ? 'btn-warning' : 'btn-default' }}">
                            <i class="fa fa-star fa-fw"></i>
                          </a>
                        </td>
                        <td class="text-center">
                          <a href="{{route('beViewDetailProduct',['id_category'=>$val->product_category,'code'=>$val->product_code])}}" class="btn btn-warning" title="Edit product"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                          <button title="Delete product" class="btn btn-danger" data-toggle="modal" data-target="#deleteproduct{{ $val->id }}"><i class="fa fa-trash" aria-hidden="true"></i></button>
                          <div id="deleteproduct{{ $val->id }}" class="modal fade" role="dialog">
                            <div class="modal-dialog modal-sm">

                            {!! Form::open(['url' => route('beDeleteProduct',['id'=>$val->id]),'method'=>'delete']) !!}
                              {!! Form::hidden('id',$val->id) !!}
                              <div class="modal-content text-center">
                                <div class="modal-body">
                                  <h3>@lang('backend.delete') <b>'{{$val->product_name}}'<b> ?</h3>
                                  <img class="img img-responsive" alt="image" src="{{ asset($storage_url.'img/products/'.$val->product_code.'/'.$val->product_image) }}">
                                  <br>
                                  <button type="submit" class="btn btn-primary">@lang('backend.delete')</button>
                                <button class="btn btn-default" data-dismiss="modal">@lang('backend.cancel')</button>
                                </div>
                              </div>
                            {!! Form::close() !!}

                            </div>
                          </div>
                        </td>
                      </tr>
                    @endforeach

							    </tbody>
							  </table>
				    	</div>

              {{-- ADD NEW PRODUCT --}}
              <div id="addProduct" class="modal fade" role="dialog">
    						<div class="modal-dialog modal-lg">
    							<div class="modal-content">
    								<div class="modal-header">
    									<h2>@lang('backend.add-product')</h2>
    								</div>
    								{!! Form::open(['url' => route('beSaveProduct'),'method' => 'post','files'=>true, 'class' => 'image-form']) !!}
                    {!! Form::hidden('image') !!}
    								<div class="modal-body container-fluid">
    									@if(Session::has('message-addproduct'))
    										<div id="message-addproduct" class="alert {{ Session::get('alert-class') }} alert-dismissible" role="alert">
    											<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    										 	<p>{{ Session::get('message-addproduct') }}</p>
    										</div>
    									@endif

    									<div class="col-xs-12 col-sm-4">
                      {{-- Image upload --}}
                        <div class="" style="width: 250px; margin: 0 auto 20px auto;">
                          <div class="image-cropper" data-height="225" data-width="150" data-exportZoom="4">
                            <div class="cropit-preview">
                              <div class="caption">
                                <p>@lang('backend.minimum-resolution') 600x900 px</p>
                              </div>
                            </div>

                            <div class="controls-wrapper">
                              <div class="rotation-btns">
                                <a class="rotate-ccw-btn"><i class="fa fa-undo"></i></a>
                                <a class="rotate-cw-btn"><i class="fa fa-repeat"></i></a>
                              </div>
                              <div class="slider-wrapper">
                                <i class="fa fa-picture-o" aria-hidden="true"></i>
                                <input type="range" class="cropit-image-zoom-input">
                                <i class="fa fa-picture-o fa-2x" aria-hidden="true"></i>
                              </div>
                            </div>

                            <input type="file" class="cropit-image-input" />
                          </div>
                        </div>
                        {{-- /. Image upload --}}


                        <div class="form-group">
                          {!! Form::label('product_category_text',__('backend.category'),['class'=>'control-label']) !!}
                          {!! Form::text('product_category_text',$category,['class'=>'form-control','readonly'=>'readonly']) !!}
                          {!! Form::hidden('product_category',$category_id,['class'=>'form-control']) !!}
                        </div>

                        <div class="form-group">
                          <label for="price" class="control-label">@lang('backend.product-price')</label>
                          <div class="input-group">
                            <span class="input-group-addon">{{ $masterCurrency or '' }}</span>
                            <input type="number" class="form-control" name="product_price" value="0">
                          </div>
                          <div class="{{ $errors->has('product_price') ? ' has-error' : '' }}">
                            @if ($errors->has('product_price'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('product_price') }}</strong>
                                </span>
                            @endif
                          </div>
                        </div>

                        <div class="form-group">
                          <label for="weight" class="control-label">@lang('backend.product-weight')</label>
                          <div class="input-group">
                            <input type="number" class="form-control" name="product_weight" value="0">
                            <span class="input-group-addon">kg</span>
                          </div>
                          <div class="{{ $errors->has('product_weight') ? ' has-error' : '' }}">
                            @if ($errors->has('product_weight'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('product_weight') }}</strong>
                                </span>
                            @endif
                          </div>
                        </div>

                        <div class="form-group">
                          <label for="price" class="control-label">@lang('backend.average-production-time')</label>
                          <div class="input-group">
                            <input type="number" class="form-control" name="production_time" value="0">
                            <span class="input-group-addon">day(s)</span>
                          </div>
                          <div class="{{ $errors->has('production_time') ? ' has-error' : '' }}">
                            @if ($errors->has('production_time'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('production_time') }}</strong>
                                </span>
                            @endif
                          </div>
                        </div>

                        <div class="form-group">
                          <label for="price" class="control-label">@lang('backend.product-stock')</label>
                          <div class="input-group">
                            <input type="number" class="form-control" name="product_stock" value="0">
                            <span class="input-group-addon">pcs</span>
                          </div>
                          <div class="{{ $errors->has('product_stock') ? ' has-error' : '' }}">
                            @if ($errors->has('product_stock'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('product_stock') }}</strong>
                                </span>
                            @endif
                          </div>
                        </div>
                        
    									</div>
    									<div class="col-xs-12 col-sm-8">
    										<div class="row">
                          <div class="col-xs-12">
                            @foreach (config('translatable.locales') as $lang => $language)
                              @php
                                if(count(config('translatable.locales')) > 1){
                                  $lang_ext = $language;
                                }else{
                                  $lang_ext = "";
                                }
                              @endphp
                              <div class="col-xs-12">
                                <div class="panel panel-info">
                                  <div class="panel-heading">
                                    <h3>{{$lang_ext}}</h3>
                                  </div>
                                  <div class="panel-body">
                                    <div class="form-group">
                											{!! Form::label('product_name',__('backend.product-name'),['class'=>'control-label']) !!}
                											<div class="{{ $errors->has('product_name') ? ' has-error' : '' }}">
                												{!! Form::text('product_name['.$lang.']','',['class'=>'form-control']) !!}
                                        @if ($errors->has('product_name'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('product_name') }}</strong>
                                            </span>
                                        @endif
                											</div>
                										</div>
                                    <div class="form-group">
                                      {!! Form::label('product_alt',__('backend.seo'),['class'=>'control-label']) !!}
                                      <div class="{{ $errors->has('product_alt') ? ' has-error' : '' }}">
                                        {!! Form::text('product_alt['.$lang.']','',['class'=>'form-control']) !!}
                                        @if ($errors->has('product_alt'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('product_alt') }}</strong>
                                            </span>
                                        @endif
                                      </div>
                                    </div>
                                    <div class="form-group">
                											{!! Form::label('product_content',__('backend.description'),['class'=>'control-label']) !!}
                											<div class="{{ $errors->has('product_content') ? ' has-error' : '' }}">
                												{!! Form::textarea('product_content['.$lang.']','',['class'=>'form-control']) !!}
                                        @if ($errors->has('product_content'))
                                          <span class="help-block">
                                            <strong>{{ $errors->first('product_content') }}</strong>
                                          </span>
                                        @endif
                											</div>
                										</div>
                                  </div>
                                </div>
                              </div>

                            @endforeach
                          </div>
                        </div>
                      </div>
    								</div>
    								<div class="modal-footer">
    									<button type="submit" class="btn btn-primary btn-image-submit">@lang('backend.save')</button>
    									<button class="btn btn-default" data-dismiss="modal">@lang('backend.cancel')</button>
    								</div>
    								{!! Form::close() !!}
    							</div>
    						</div>
    					</div>

			    	</div>
			    </div>
		    </div>

    	</div>
    </div>

    {{-- reorder --}}
    <div id="reorder" class="modal fade" role="dialog">
      <div class="modal-dialog">

      {!! Form::open(['url' => route('beProductOrder'),'method'=>'post']) !!}
        <div class="modal-content">
          <div class="modal-header">
            <h2>@lang('backend.rearrange-order')</h2>
          </div>
          <div class="modal-body">
            <ul class="sortable media-list">
              @foreach ($products as $x => $vals)
                  @php
                    $get = $vals->locales()->where('locale',config('app.locale'))->first();
                  @endphp
                <li class="media">
                  {!! Form::hidden('reorder[]',$vals->id) !!}
                  <div class="media-left">
                    <a href="#">
                      <img class="media-object img-border" src="{{ asset($storage_url.'img/products/'.$vals->product_code.'/'.$vals->product_image) }}" alt="" height="64px">
                    </a>
                  </div>
                  <div class="media-body">
                    <h3 class="media-heading">{{ $get->product_name or $vals->product_name }}</h3>
                    @php
                      $currency = explode('|',$vals->product_currency);
                    @endphp
                    @php
                      if( $masterCurrency == "USD" ){
                        $price = number_format($vals->product_price,2,".",",");
                      }elseif( $masterCurrency == "EUR" ){
                        $price = number_format($vals->product_price,2,",",".");
                      }else{
                        $price = number_format($vals->product_price,0,",",".");
                      }
                    @endphp
                    <p>{{$masterCurrency}} {{$price}}</p>
                  </div>
                </li>
              @endforeach
            </ul>
          </div>
          <footer class="modal-footer">
            <button type="submit" class="btn btn-primary">@lang('backend.save')</button>
            <button class="btn btn-default" data-dismiss="modal">@lang('backend.cancel')</button>
          </footer>
        </div>
      {!! Form::close() !!}

      </div>
    </div>
@endsection

@section('script')
<script type="text/javascript">
  $("#productTable").DataTable();
  $(".sortable").sortable();
  @if ($errors->all())
      $("#addProduct").modal('show');
  @endif
</script>
@endsection
