@extends('layouts.masterBackend')

@section('title')
    <a href="{{route('backendOrder')}}" class="btn btn-primary">@lang('backend.back')</a>
    {{ ucfirst('order') }} [ #{{$order_id}} ]
@endsection

@section('content')
  <!-- MAIN CONTENT -->
  <div class="container-fluid">
  	<div class="row">
  		<div class="col-xs-12">
        @if(Session::has('message-status'))
          <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-status') }}</p>
          </div>
        @endif
        <div class="row">
          <div class="col-xs-12 col-sm-6">
            <div class="panel panel-primary">
              <header class="panel-heading">
                <h2><i class="fa fa-user-circle-o" aria-hidden="true"></i> @lang('backend.checkout-data')</h2>
              </header>
              <div class="table-responsive panel-body">
                <table id="tableCheckout" class="table table-striped table-hover">
                  <tbody>
                    <tr>
                      <th width="30%">@lang('backend.order-id')</th><td>{{ $checkout->order_id }}</td>
                    </tr>
                    <tr>
                      <th>Paypal Payment ID</th><td>{{ $checkout->paypal_payment_id }}</td>
                    </tr>
                    <tr>
                      <th>Name</th><td>{{ $checkout->checkout_fname }} {{ $checkout->checkout_lname }}</td>
                    </tr>
                    <tr>
                      <th>Email</th><td><a href="mailto:{{ $checkout->checkout_email }}">{{ $checkout->checkout_email }}</a></td>
                    </tr>
                    <tr>
                      <th>Phone</th><td>{{ $checkout->checkout_phone }}</td>
                    </tr>
                    <tr>
                      <th>Address</th><td>{{ $checkout->checkout_address }}</td>
                    </tr>
                    <tr>
                      <th>Payment Date</th><td>{{ date('d M Y H:i:s', strtotime($checkout->created_at)) }}</td>
                    </tr>
                    <tr>
                      <th>Payment Method</th>
                      <td>
                        @if($checkout->paypal_payment_id == "-")
                          Manual
                        @else
                          Online
                        @endif
                      </td>
                    </tr>
                    <tr>
                      <th>Order Status</th>
                      <td>
                        @if($checkout->checkout_status == "paid")
                          <span class="label label-primary">{{ title_case($checkout->checkout_status) }}</span>
                        @elseif ($checkout->checkout_status == "process")
                          <span class="label label-info">{{ title_case($checkout->checkout_status) }}</span>
                        @elseif ($checkout->checkout_status == "not paid yet")
                          <span class="label label-default">{{ title_case($checkout->checkout_status) }}</span>
                        @else
                          <span class="label label-success">{{ title_case($checkout->checkout_status) }}</span>
                        @endif
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
              <div class="panel-footer">
                <div class="row">
                  <header class="col-xs-12 col-sm-6">
                    <h2>Total</h2>
                  </header>
                  <div class="col-xs-12 col-sm-6">
                    @php
											if( $masterCurrency == "USD" ){
												$total = number_format($checkout->checkout_total,2,".",",");
											}elseif( $masterCurrency == "EUR" ){
												$total = number_format($checkout->checkout_total,2,",",".");
											}else{
												$total = number_format($checkout->checkout_total,0,",",".");
											}
										@endphp
      							<h2>{{$masterCurrency}} {{ $total }}</h2>
										@if( $masterCurrency == "IDR" )
      								<span class="label label-default label-lg">USD {{$checkout->checkout_total_paypal}}</span>
										@endif
                  </div>
                </div>
              </div>
            </div>

            <aside class="row text-center">
              {!! Form::open(['url'=>route("changeOrderStatus",['order_id'=>$order_id]),'method'=>'put','class'=>'form-inline']) !!}
                <div class="form-group">
                  {!! Form::label('status','Status',['class'=>'control-label']) !!}
                  {!! Form::select('status',[
                    'not paid yet'=>'Not Paid Yet',
                    'paid'=>'Paid',
                    'process'=> 'Process',
                    'deliver'=>'Deliver'
                  ],$checkout->checkout_status,['class'=>'form-control']) !!}
                  <button type="submit" class="btn btn-primary">Update</button>
                </div>
              {!! Form::close() !!}
            </aside>
            <br/>

          </div>
          <div class="col-xs-12 col-sm-6">
            <div class="panel panel-primary">
                <header class="panel-heading">
                  <h2><i class="fa fa-asterisk" aria-hidden="true"></i> Order Detail</h2>
                </header>
                <div class="table-responsive panel-body">
                  <table id="tableCart" class="table table-striped table-hover">
                    <thead>
                      <tr class="">
                        <th>Name</th>
                        <th>Notes</th>
                        <th>Qty</th>
                        <th>Price</th>
                        <th>Subtotal</th>
                      </tr>
                    </thead>

                    <tbody>
                      @foreach($cart as $x => $row)

                        <tr>
                          <td width="25%">
                            {{ $row->name }}
                          </td>
                          <td width="30%">
                            {{ ($row->options->notes!="") ? $row->options->notes : '-' }}
                          </td>
                          <td width="5%">{{ $row->qty }}</td>
                          @php
														if( $masterCurrency == "USD" ){
															$price = number_format($row->price,2,".",",");
															$total = number_format($row->total,2,".",",");
														}elseif( $masterCurrency == "EUR" ){
															$price = number_format($row->price,2,",",".");
															$total = number_format($row->total,2,",",".");
														}else{
															$price = number_format($row->price,0,",",".");
															$total = number_format($row->total,0,",",".");
														}
													@endphp
													<td width="20%">{{$masterCurrency}} {{ $price or 0 }}</td>
													<td width="20%">{{$masterCurrency}} {{ $total or 0 }}</td>
                        </tr>

                      @endforeach
                      @php
                        $shipmentCost = 0;
                        $shipmentDestination = null;
                        if ($cart->count()) {
                          $shipmentCost = $cart->first()->options->shipment_cost ? $cart->first()->options->shipment_cost : 0;
                          $shipmentDestination = $cart->first()->options->shipment_destination ? $cart->first()->options->shipment_destination : 0;        
                        }
                        $shipmentCost = str_replace([',', '.'], "", $shipmentCost);
                        $cartTotal = str_replace([',', '.'], "", Cart::total());
                      @endphp
                      <tr>
                        @if ($shipment->option == 'delivery')
                          <td colspan="4" style="text-align: left">
                            Shipment {{ $shipmentDestination ? 'to '.$shipmentDestination : '' }}
                          </td>
                          <td>{{ $masterCurrency }} {{ number_format($shipmentCost, 0, ".", ".") }}</td>
                        @elseif($shipment->option == 'flat')
                          @php
                            $shipmentCost = $shipment->amount;
                          @endphp
                          <td colspan="4" style="text-align: left">
                            Shipment(Flat)
                          </td>
                          <td>{{ $masterCurrency }} {{ number_format($shipment->amount, 0, ".", ".") }}</td>
                        @else
                          @php
                            $shipmentCost = 0;
                          @endphp
                          <td colspan="4" style="text-align: left">
                            Shipment(Free)
                          </td>
                          <td>{{ $masterCurrency }} {{ number_format(0, 0, ".", ".") }}</td>
                        @endif
                      </tr>
                    </tbody>
                  </table>
                </div>
            </div>
          </div>
        </div>
		  </div>
  	</div>
  </div>
@endsection
