@extends('layouts.dashboard')

@section('page_title')
	Update Invoice
@endsection

@section('page_header_title')
Update Invoice
@endsection



@section('page_body')
{{ Breadcrumbs::render('invoice') }}

@php ($uniqid = uniqid())

{{ Form::open(array('route' => 'invoice.update', 'class' => 'cls_from ', 'enctype' => 'multipart/form-data', 'id' => 'form_update_invoice')) }}
<div class="content">
    <div class="container-xl">
        <div class="card">
            <div class="card-header d-print-none">
                <h3 class="card-title">Update Purchase Invoice</h3>
                <div class="card-options">
                    <!-- <button type="button" class="btn btn-primary" onclick="javascript:window.print();"><i
                        class="si si-printer"></i> Print Invoice
                        </button> -->
                </div>
            </div>
            <div class="card-body">
              <div class="row">

 <!-- LEFT SIDE -->
                <div class="col-lg-12">
                    <div class="row">
                        <div class="col-lg-3">
                            <div class="form-group">
                                <label class="label-color"> Project: <i style = "color:red;">*</i></label>
                                <small class="form-errors pull-right req" value="*"></small>
                                <select name="branch" class="form-control fstdropdown-select form-control-sm cls_required">
                                <option value="{{Crypt::encrypt(0)}}">Select Project</option>
                                    @foreach($branch_list as $branch)
                                        <option value="{{Crypt::encrypt($branch->id)}}" @if($branch->id == $invoice->branch_id ) selected @endif>{{$branch->name}} </option>
                                    @endforeach

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

                        <div class="col-lg-3">
                            <div class="form-group">
                                <label class="label-color"> Vendor: <i style = "color:red;">*</i></label>
                                <small class="form-errors pull-right req" value="*"></small>
                               <select name="user" class="form-control fstdropdown-select form-control-sm cls_required vendor" onChange="getVendor(event,this)">
                                    <option value="{{Crypt::encrypt(0)}}">Select Vendor </option>
                                    @foreach($vendor_list as $vendor)
                                    <option value="{{Crypt::encrypt($vendor->id)}}" @if($vendor->id == $invoice->user_id ) selected @endif>{{$vendor->name}} </option>
                                    @endforeach

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

                        <div class="col-lg-3">
                            <div class="form-group">
                                <label class="form-label">Date <i style = "color:red;">*</i></label>
                                <small class="form-errors pull-right req" value="*"></small>

                                <input type="date" name="date" value="{{EF::getdateByFormat('Y-m-d',$invoice->created_at)}}" class=" form-control ">

                                <input style="display: none;"  type="text" name="invoice_no" class=" form-control  " placeholder="Invoice No">
                            </div>
                        </div>

                        <div class="col-lg-3">
                            <div class="form-group">
                                <label class="form-label"> Due Date<i style = "color:red;">*</i></label>
                                <small class="form-errors pull-right req" value="*"></small>

                                <input required="" type="date" value="{{EF::getdateByFormat('Y-m-d',$invoice->due_date)}}" name="due_date" class=" form-control " >
                            </div>
                        </div>


                        <div class="col-lg-12">
                            <label class="form-label name">{{$invoice->user->name}}</label>
                        </div>

                        <div class="col-lg-12">
                            <label class="form-label cnic">{{$invoice->user->cnic}}</label>
                        </div>

                        <div class="col-lg-12">
                            <label class="form-label contact">{{$invoice->user->contact}}</label>
                        </div>

                        <div class="col-lg-12">
                            <label class="form-label address">{{$invoice->user->temp_address}}</label>
                        </div>


                    </div>
                </div>

                <!-- OLD CODE -->
        {{--
                <div class="col-3 text-left">
                    <div class="input-group mb-3">
                        <select name="branch" class="form-control fstdropdown-select form-control-sm cls_required">
                            <option value="{{Crypt::encrypt(0)}}">Select Project</option>
                                @foreach($branch_list as $rows)
                                    <option value="{{Crypt::encrypt($rows->id)}}" @if($rows->id == $invoice->branch_id ) selected @endif>{{$rows->name}} </option>
                                @endforeach


                        </select>
                    </div>
                    <div class="input-group mb-3">
                        <select name="user" class="form-control fstdropdown-select form-control-sm cls_required vendor" onChange="getVendor(event,this)">
                            <option value="{{Crypt::encrypt(0)}}">Select Vendor</option>
                            @foreach($vendor_list as $rows)
                            <option value="{{Crypt::encrypt($rows->id)}}" @if($rows->id == $invoice->user_id ) selected @endif>{{$rows->name}} </option>
                            @endforeach

                        </select>
                    </div>
                    <div class="row text-left">
                    <div class="col-md-6 ml-1 col-sm-6">
                        <label class="form-label name">{{$invoice->user->name}}</label>
                    </div>
                    
                    </div>

                    <div class="row text-left">
                    <div class="col-md-6 ml-1 col-sm-6">
                        <label class="form-label cnic">{{$invoice->user->cnic}}</label>
                    </div>
                    </div>

                    <div class="row text-left">
                    <div class="col-md-6 ml-1 col-sm-6">
                        <label class="form-label contact">{{$invoice->user->contact}}</label>
                    </div>
                    </div>

                    <div class="row text-left">
                    <div class="col-md-6 col-sm-6 ml-1">
                        <label class="form-label address">{{$invoice->user->temp_address}}</label>
                    </div>
                    
                    </div>
                </div>

                    <div class="col-3 offset-6 text-right"> 
                        <div class="row text-right">
                        <div class="col-md-5 cls_invoicealignment col-sm-5 mb-2">
                            <label class="form-label">Date</label>
                        </div>
                        <div class="col-md-7  cls_invoicealignment col-sm-7 text-left">
                            <input type="date" name="date" value="{{EF::getdateByFormat('Y-m-d',$invoice->created_at)}}" class=" form-control form-control-sm">
                        </div>
                        </div>

                        <!-- <div class="row text-right">
                        <div class="col-md-6 cls_invoicealignment col-sm-6">
                            <label class="form-label">Vendor Id: </label>
                        </div>
                        <div class="col-md-6  cls_invoicealignment col-sm-6 text-left">
                            <input type="text" name="name" class=" form-control form-control-sm" readonly>
                        </div>
                        </div> -->

                        <div class="row text-right" style="display: none;">
                        <div class="col-md-5 cls_invoicealignment col-sm-5 mb-2">
                            <label class="form-label">Invoice No: </label>
                        </div>
                        <div class="col-md-7 cls_invoicealignment col-sm-7 text-left">
                            <input type="text" name="invoice_no" value="{{$invoice->invoice_no}}" class=" form-control form-control-sm" placeholder="Invoice No">
                        </div>
                        </div>

                        <div class="row text-right">
                        <div class="col-md-5 cls_invoicealignment col-sm-5 mb-2">
                            <label class="form-label"> Due Date</label>
                        </div>
                        <div class="col-md-7  cls_invoicealignment col-sm-7 text-left">
                            <input required="" type="date" name="due_date" value="{{EF::getdateByFormat('Y-m-d',$invoice->due_date)}}" class=" form-control form-control-sm" >
                        </div>
                    </div>
                </div>
                --}}
                <!-- OLD CODE END -->
                <!-- <div class="col-12 my-5">
                  <h3>Invoice No: </h3>
                </div> -->
              </div>
  
    
              <div class="table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th class="text-center"style="width: 10%;">Item Code <i style = "color:red;">*</i></th>
                      <th class="text-center" style="width: 31%;">Description <i style = "color:red;">*</i></th>
                      <th class="text-center">Unit Price <i style = "color:red;">*</i></th>
                      <th class="text-center">Discount</th>
                      <th class="text-center">Quantity <i style = "color:red;">*</i></th>
                      <th class="text-center">GST %</th>
                      <th class="text-center">GST Value</th>
                      <th class="text-center">Amount</th>

                    </tr>
                  </thead>
                
                <tbody id="invoice_table">
                @php($count = 1)
                
                 @foreach($invoice->purchaseItems as $rows)
                 @php($single_amount = ($rows->unit_price * $rows->quantity) -$rows->discount)
                 @php($gst_percentage = ($rows->gst_value/$single_amount) * 100)

                  <input type="hidden" name="purchase_item[]" value="{{Crypt::encrypt($rows->id)}}">

                  <tr>
                    <td><input required="" type="text" name= "item_code[]" class="form-control form-control-sm item_code "  value="{{$rows->item_code}}" placeholder="Code"/></td>

                    <td><input required="" class = "form-control form-control-sm cls_alpha_numeric description" id="description_{{$count}}" value="{{$rows->description}}" placeholder="Description" type="text" name="description[]"/></td>
                    
                    <td><input required=""  class = "form-control form-control-sm cls_alpha_numeric" id="unit_price_{{$count}}" value="{{$rows->unit_price}}" placeholder="Unit Price" onkeyup="create_PurchaseItem(event,{{$count}})"  type="text" name="unit_price[]"/></td>
                    
                    <td><input  class = "form-control form-control-sm cls_alpha_numeric" id="discount_{{$count}}"  onkeyup="create_PurchaseItem(event,{{$count}})" value="{{$rows->discount}}" placeholder="Discount" type="text" name="discount[]"/></td>
                    
                    <td><input required=""  class = "form-control form-control-sm cls_alpha_numeric" id="quantity_{{$count}}" placeholder="Quantity"  value="{{$rows->quantity}}" onkeyup="create_PurchaseItem(event,{{$count}})" type="text" name="quantity[]"/></td>
                    
                    <td><input class = "form-control form-control-sm cls_alpha_numeric" id="gst_percentage_{{$count}}" value="{{round($gst_percentage)}}" placeholder="GST %"  onkeyup="create_PurchaseItem(event,{{$count}})" type="text" name="gst_percentage[]"/></td>

                    <td><input class = "form-control form-control-sm cls_alpha_numeric" id="gst_value_{{$count}}" value="{{$rows->gst_value}}" placeholder="GST Value"  type="text" name="gst_value[]" readonly/></td>
                    
                    <td><input class = "form-control form-control-sm cls_alpha_numeric" id="amount_{{$count}}" placeholder="Amount"  value="{{$rows->amount}}" type="text" name="amount[]" readonly/></td>
                    
                  </tr>
                  @php($count++)
                @endforeach

                </div>
                </table>
              </div>
            </div>
            <div class="card-footer">
                <input type="hidden" name="invoice" value="{{Crypt::encrypt($invoice->id)}}">
               
                <button onclick="updateInvoice(event,this)" type="submit" class="btn btn-primary float-right purchase_details" content="">Update</button>

            </div>
        </div>

    </div>
    {{Form::close()}}



@endsection

@section('page_modal')

@endsection

@section('page_script')
@include('scripts.invoice') 
<script type="text/javascript">
        $(function(){

        showAutoCompleteItemCodes('{{$uniqid}}');
});
</script>
@endsection

