<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="icon" type="image/png" href="{{url('public/logo', $general_setting->site_logo)}}" />
  <title>{{$general_setting->site_title}}</title>

  <style>
    body {
      font-family: 'Helvetica', 'Arial', sans-serif;
      font-size: 12px;
      margin: 20mm;
      color: #000;
    }

    .invoice-container {
      max-width: 100%;
      margin: 0;
    }

    header {
      text-align: center;
      border-bottom: 2px solid #000;
      padding-bottom: 2px;
      margin-bottom: 5px;
    }

    header h2 {
      margin: 2px 0;
      font-size: 16px;
      text-transform: uppercase;
    }

    .company-info {
      font-size: 13px;
      line-height: 18px;
    }

    table {
      width: 100%;
      border-collapse: collapse;
      margin-bottom: 5px;
    }

    table th, table td {
      border: 1px solid #ccc;
      padding: 2px;
      text-align: left;
    }

    table th {
      background-color: #f0f0f0;
    }

    .totals td {
      font-weight: bold;
    }

    .text-right {
      text-align: right;
    }

    .center {
      text-align: center;
    }

    .footer {
      text-align: center;
      margin-top: 2px;
      font-size: 10px;
    }

    @media print {
      @page {
        size: A4 landscape;
        margin: 12mm;
      }
      .hidden-print {
        display: none !important;
      }
    }
  </style>
</head>

<body>

@php
  $isCashCustomer = strtolower(trim($lims_customer_data->name)) === 'cash customer';
@endphp

<div class="invoice-container">

  <!-- Buttons -->
  <div class="hidden-print" style="margin-bottom:10px;">
    <button onclick="window.history.back();" style="padding:6px 10px;">← Back</button>
    <button onclick="window.print();" style="padding:6px 12px; background:#6449e7; color:#fff;">🖨 Print</button>
  </div>

  <!-- HEADER -->
  <header>
    <h2>{{$lims_biller_data->company_name}}</h2>
    <p class="company-info">
      {{trans('file.Address')}}: {{$lims_warehouse_data->address}}<br>
      {{trans('file.Phone Number')}}: {{$lims_warehouse_data->phone}}
    </p>
    <h2>INVOICE</h2>
  </header>

  <!-- INVOICE DETAILS -->
  <table>
    <tr>
      <td>{{trans('file.Date')}}: {{$lims_sale_data->created_at}}</td>
      <td>{{trans('Customer')}}: {{$lims_customer_data->name}}</td>
    </tr>
    <tr>
      <td>{{trans('Invoice No')}}: {{$lims_sale_data->id}}</td>
      <td>Customer VAT No: {{$lims_customer_data->tax_no}}</td>
    </tr>
    <tr>
      <td>{{trans('Reference No')}}: {{$lims_sale_data->reference_no}}</td>
      <td>Company VAT No: 687310152-7000</td>
    </tr>
  </table>

  <!-- ITEM TABLE -->
  <table>
    <thead>
      <tr>
        <th width="5%">#</th>

        <th width="{{ $isCashCustomer ? '65%' : '40%' }}">
          {{trans('file.Product Name')}}
        </th>

        <th width="10%">{{trans('file.Qty')}}</th>

        @if(!$isCashCustomer)
          <th width="15%">{{trans('file.Unit Price')}}</th>
          <th width="10%">{{trans('Discount Amount')}}</th>
        @endif

        <th width="{{ $isCashCustomer ? '20%' : '20%' }}" class="text-right">
          {{trans('file.Total')}}
        </th>
      </tr>
    </thead>

    <tbody>
      @foreach($lims_product_sale_data as $key => $product_sale_data)
        @php
          $product = \App\Product::find($product_sale_data->product_id);

          if($product_sale_data->variant_id) {
            $variant = \App\Variant::find($product_sale_data->variant_id);
            $product_name = $product->name.' ['.$variant->name.']';
          } elseif($product_sale_data->product_batch_id) {
            $batch = \App\ProductBatch::select('batch_no')->find($product_sale_data->product_batch_id);
            $product_name = $product->name.' [Batch:'.$batch->batch_no.']';
          } else {
            $product_name = $product->name;
          }
        @endphp

        <tr>
          <td>{{$key + 1}}</td>
          <td>{{$product_name}}</td>
          <td>{{$product_sale_data->qty}}</td>

          @if(!$isCashCustomer)
            <td>{{number_format($product->price, 2)}}</td>
            <td>{{number_format($product_sale_data->discount, 2)}}</td>
          @endif

          <td class="text-right">{{number_format($product_sale_data->total, 2)}}</td>
        </tr>
      @endforeach
    </tbody>
  </table>

  <!-- TOTALS -->
  <table class="totals">
    <tr>
      <td class="text-right">{{trans('file.Total')}} Rs:</td>
      <td width="20%" class="text-right">{{number_format($lims_sale_data->total_price, 2)}}</td>
    </tr>

    @if($lims_sale_data->order_discount)
      <tr>
        <td class="text-right">{{trans('file.Order Discount')}} Rs:</td>
        <td class="text-right">{{number_format($lims_sale_data->order_discount, 2)}}</td>
      </tr>
    @endif

    @if($lims_sale_data->shipping_cost)
      <tr>
        <td class="text-right">{{trans('file.Shipping Cost')}} Rs:</td>
        <td class="text-right">{{number_format($lims_sale_data->shipping_cost, 2)}}</td>
      </tr>
    @endif

    @if($lims_sale_data->order_tax)
      <tr>
        <td class="text-right">{{trans('Order VAT')}} Rs:</td>
        <td class="text-right">{{number_format($lims_sale_data->order_tax, 2)}}</td>
      </tr>
    @endif

    <tr>
      <td class="text-right">{{trans('Grand Total')}} Rs:</td>
      <td class="text-right">{{number_format($lims_sale_data->grand_total, 2)}}</td>
    </tr>
  </table>

  <!-- FOOTER -->
  <div class="footer">
    <table>
      <tr>
        <td><br>_____________________________<br>Manager Signature</td>
        <td><br>_____________________________<br>Customer Signature</td>
      </tr>
    </table>

    <p>{{trans('Thank you for shopping with us!')}}</p>

  <!--  <div class="center">
      {!! '<img src="data:image/png;base64,'.DNS1D::getBarcodePNG($lims_sale_data->reference_no,'C128').'" width="100" />' !!}
      <br>
      {!! '<img src="data:image/png;base64,'.DNS2D::getBarcodePNG($lims_sale_data->reference_no,'QRCODE').'" width="50" />' !!}
      <br>
    
    </div> -->
  </div>

</div>

<script>
  localStorage.clear();
</script>

</body>
</html>