<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    @include('layout.header')
    <link href="{{url(env("APP_URL").'storage/assets/libs/bootstrap-datepicker/css/bootstrap-datepicker.min.css')}}" rel="stylesheet" type="text/css" />
    <style>
         #modal-repayment .form-group label {
            line-height: 0.4rem !important;
        }
       #modal-repayment .select2-container .select2-selection--single, #modal-repayment .select2-selection--multiple {
            min-height: 30px !important;
        }
       #modal-repayment .select2-selection__arrow {
            height: 30px !important;
        }
       #modal-repayment .form-control, #modal-repayment .select2-container--default .select2-selection--single, #modal-repayment .select2-container--default .select2-selection--single .select2-search__field, .typeahead, .tt-query, .tt-hint {
            padding: 0.4rem 1.375rem;
        }
    </style>
</head>
<body>


<div class="container-scroller">
@include('layout.navbar')
<!-- partial -->
    @include('layout.breadscrum')
    <div class="container-fluid page-body-wrapper">

    @include('layout.sidebar')

    <!-- partial -->
        <div class="main-panel">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-md-12 grid-margin stretch-card">
                        <div class="card" style="min-height: 80vh">
                            <div class="card-body">
                                <h4 class="card-title"><i class="mdi mdi-dots-vertical menu-icon"></i> {{__('invoice.table_title')}}</h4>
                                <div class="row">
                                    <div class="col-sm-12">
                                        <div class="col-sm-12 inv-search">
                                            <form method="get" action="{{url('invoice')}}">
                                                <div class="row">
                                                    <div class="col-sm-12 col-md-6 col-lg-3 pr-0">
                                                        <div class="form-group mb-1">
                                                            <label for="start-invoice-date">{{__('invoice.table_date_filter')}}</label>
                                                            <div class="input-group">
                                                                <input id="start-invoice-date" value="{{isset($_GET['from_date']) ? $_GET['from_date'] : ''}}" name="from_date" class="form-control rounded-top-left-25 rounded-bottom-left-25 rounded-right-0" onfocus="(this.type='date')" onblur="(this.type='text')" placeholder="{{__('general.type_date_placeholder')}}">
                                                                <input id="to-invoice-date" value="{{isset($_GET['to_date']) ? $_GET['to_date'] : ''}}" name="to_date" class="form-control rounded-left-0 rounded-right-0" onfocus="(this.type='date')" onblur="(this.type='text')" placeholder="{{__('general.type_date_placeholder')}}">
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="col-sm-12 col-md-3 col-lg-2 pr-0">
                                                        <div class="form-group mb-1">
                                                            <label for="lab-name-en">{{__('report.doctor_physician_filter')}}</label>
                                                            <select class="form-control rounded-left-0 rounded-right-0" name="physician">
                                                                <option></option>
                                                                @foreach($physicians as $physician)
                                                                    <option {{$physician->id==@$_GET['physician'] ? 'selected':''}} value="{{$physician->id}}">{{$physician->name_en}}</option>
                                                                @endforeach
                                                            </select>
                                                        </div>
                                                    </div>

                                                    <div class="col-sm-12 col-md-6 col-lg-5">
                                                        <div class="form-group mb-1">
                                                            <label for="lab-name-en">{{__('invoice.table_textable_filter')}}</label>
                                                            <div class="input-group">
                                                                <input type="text" class="form-control rounded-right-0 rounded-left-0" value="{{isset($_GET['kword']) ? $_GET['kword'] : ''}}" name="kword" id="kword" placeholder="{{__('invoice.table_textable_filter_placeholder')}}">
                                                                <div class="input-group-append">
                                                                    <button class="btn btn-sm btn-inverse-secondary rounded-bottom-right-25 rounded-top-right-25 px-3" title="Search" type="submit"><i class="fa fa-search"></i> {{__('general.btn_search')}}</button>
                                                                    <!--@if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_invoice']))-->
                                                                    <!--<a href="{{url('invoice/new')}}" class="btn btn-info rounded-0 waves-effect px-3 ml-5"><i class="typcn typcn-plus"></i>{{__('general.btn_add_new')}}</a>-->
                                                                    <!--@endif-->
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                </div>
                                        </form> <hr class="my-3 d-none">
                                        </div>
                                        <div class="col-sm-12 p-0 mb-1 mt-3">
{{--                                            @if(!in_array(Session::get('base_lab_id'),[37,19]))--}}
{{--                                                <button type="button" onclick="paddAll()" class="btn btn-danger rounded-0 btn-xs" id="mark-as-paid" disabled>Mark as paid</button>--}}
{{--                                            @endif--}}
                                            <div class="table-responsive">
                                                <table class="table table-bordered table-hover" id="department_dt">
                                                    <thead>
                                                    <tr class="bg-gradient-light">
                                                        <th class="py-2 text-center" width="20px">{{__('invoice.table_no')}}</th>
                                                        <th class="py-2 text-center" width="80px">{{__('invoice.table_action')}}</th>
                                                        <th class="py-2">{{__('invoice.table_invoice_code')}}</th>
                                                        <th class="py-2" width="150px">{{__('invoice.table_patient_name')}}</th>
{{--                                                        <th class="py-2" width="120px">{{__('sample.physician_name')}}</th>--}}
                                                        <th class="py-2" width="100px">{{__('invoice.table_sample_number')}}</th>
                                                        <th class="py-2 text-center" width="120px">{{__('invoice.table_invoice_date')}}</th>
                                                        <th class="py-2 text-center" width="150px">{{__('invoice.table_total_cost')}}</th>
                                                        <th class="py-2" width="150px">{{__('invoice.table_discount')}}</th>
                                                        <th class="py-2 text-center" width="150px">{{__('invoice.table_net_cost')}}</th>
                                                        <th class="py-2 text-center" width="150px">{{__('invoice.form_paid_amount')}}</th>
                                                        <th class="py-2 text-center" width="150px">{{__('invoice.table_owe')}}</th>
                                                    </tr>
                                                    </thead>
                                                    <tbody>
                                                    @foreach($invoices as $k=>$invoice)
                                                        <tr>
                                                            <td class="py-1 text-center">{{($k+1) + (($_GET['page'] ?? 1) * 20) - 20}}</td>
                                                            <td class="py-1 text-right">
                                                                <div class="btn-group" role="group" aria-label="Basic example" >

                                                                    @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_repayment']) && round($invoice->total - ($invoice->total * ($invoice->discount/100)),0)> round($invoice->repayments->sum('cash_repayment_amount') + $invoice->repayments->sum('bank_repayment_amount') + $invoice->deposit + $invoice->bank_deposit,0))
                                                                        <button data-toggle="modal" data-action="new" data-invoice-id="{{$invoice->id}}" data-target="#modal-repayment" data-backdrop="static" type="button" style="padding: 0px 2px; font-size:10px;" class="btn btn-sm btn-inverse-success mr-2 py-1" title="Click here to paid"><i class="mdi mdi-currency-usd"></i></button>
                                                                    @endif

                                                                    @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['view_invoice']))
                                                                        <a href="{{url('invoice/edit/'.$invoice->id.'?print')}}" class="btn btn-sm btn-inverse-warning mr-2 py-1" title="Print"><i class="mdi mdi-printer"></i></a>
                                                                    @endif
                                                                    @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['update_invoice']))
                                                                        <a href="{{url('invoice/edit/'.$invoice->id)}}" class="btn btn-sm btn-inverse-info mr-2 py-1" title="{{__('general.btn_edit')}}"><i class="typcn typcn-edit"></i></a>
                                                                    @endif
                                                                    @if($invoice->record_status_id==0)
                                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['update_invoice','view_invoice']))
                                                                            <button type="button" class="btn btn-sm btn-inverse-primary" onclick="restoreData({{$invoice->id}})"><i class="typcn typcn-plus"></i></button>
                                                                        @endif
                                                                    @else
                                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['delete_invoice']))
                                                                            <button type="button" class="btn btn-sm btn-inverse-danger py-1" title="{{__('general.btn_delete')}}" onclick="deleteData({{$invoice->id}})"><i class="mdi mdi-close-circle"></i></button>
                                                                        @endif
                                                                    @endif



                                                                </div>
                                                            </td>
                                                             <td class="py-1">{{$invoice->invoice_code}}</td>
                                                             <td class="py-1">{{$invoice->sample->patient->name_en}}</td>
{{--                                                            <td class="py-1">{{$invoice->sample->physician->name_en}}</td>--}}
                                                            <td class="py-1">{{$invoice->sample->sample_number}}</td>

                                                            <td class="py-1 text-center">{{date('d-m-Y',strtotime($invoice->invoice_date))}}</td>
                                                            <td class="py-1 text-right">{{$invoice->total}}</td>
                                                            <td class="py-1 text-right">{{$invoice->discount}}</td>
                                                            <td class="py-1 text-right">{{$invoice->gross_total}}</td>
                                                            <td class="py-1 text-right">{{$invoice->deposit + $invoice->bank_deposit}}</td>
{{--                                                            <td class="py-1 text-right">{{$invoice->bank_deposit}}</td>--}}
                                                            <td class="py-1 text-right">
                                                                {{number_format($invoice->balance,2)}}
                                                            </td>
{{--                                                            <td class="py-1 text-right">--}}
{{--                                                                                                                             {{$invoice->repayments->sum('cash_repayment_amount') + $invoice->repayments->sum('bank_repayment_amount')}}--}}
{{--                                                            </td>--}}
                                                        </tr>
                                                    @endforeach
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>

                                        <div class="float-right mt-3">
                                            {!! $invoices->appends($_GET)->links('pagination.custom') !!}
                                        </div>

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

            @include('v_invoice.report_filter')

             @include('modal.repayment')

            @include('layout.footer')
        </div>
    </div>
</div>
@include('layout.common_script')

<script src="{{url(env("APP_URL").'storage/assets/libs/bootstrap-datepicker/js/bootstrap-datepicker.min.js')}}"></script>
<script>

    var base_url = "{{url('invoice')}}";
    let invoice_id = 0;
    let repayment_base_url = "{{url('repayment')}}";

    let report_type_tittle = "{{__('invoice.report_type')}}";
    let report_type_daily = "{{__('invoice.daily')}}";
    let report_type_monthly = "{{__('invoice.monthly')}}";
    let report_type_yearly = "{{__('invoice.yearly')}}";
    let repayment_modal_title = "{{__('invoice.repayment')}}";

    let currency = {{$labSettings->currency}};

    $(".datepicker").datepicker({
        format:'dd-mm-yyyy',
        autoclose: true
    });

    $(".monthpicker").datepicker({
        format:'mm-yyyy',
        autoclose: true
    });

    $(".yearpicker").datepicker({
        format:'yyyy',
        autoclose: true
    });

    var inv_ids = []

    function checkAll(parentId, targetClass) {
        var checkedState = $('#'+parentId).is(':checked');
        var checkboxes = document.getElementsByClassName(targetClass);
        inv_ids = []
        for (var i = 0; i < checkboxes.length; i++) {
            checkboxes[i].checked = checkedState;
            if(checkboxes[i].checked){
                inv_ids.push(checkboxes[i].getAttribute('value'))
            }
            else{
                inv_ids = []
            }
        }
        if(inv_ids.length>=1) $('#mark-as-paid').prop('disabled', false)
        else $('#mark-as-paid').prop('disabled', true)
    }

    function checkOne(parentId) {
        var checkedState = $('#'+parentId).is(':checked');
        if(checkedState){
            inv_ids.push($('#'+parentId).val())
        }
        else{
            inv_ids = inv_ids.filter(val => val!=$('#'+parentId).val());
        }
        if(inv_ids.length>=1) $('#mark-as-paid').prop('disabled', false)
        else $('#mark-as-paid').prop('disabled', true)
    }

    function paid(id){
        var success = false
        Swal.fire({
            text: 'Are you sure want to paid this invoice?',
            icon: 'question',
            showCancelButton: true,
            cancelButtonClass:'danger',
            confirmButtonText: 'Ok, Paid',
            cancelButtonText: 'Cancel'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: base_url + '/'+id+'/paid',
                    method:'POST',
                    dataType:"JSON",
                    data:{
                        "_token": csrf_token
                    },
                    success:function(response){
                        handleMessage(response.success, response.message)
                        setTimeout(function () {location.reload()}, messageDuration)
                        success = true
                    }
                })

            }
        });
        return success;
    }

</script>
<script src="{{url(env("APP_URL").'storage/assets/js/admin/invoice.js?time='.time())}}"></script>
<script src="{{url(env("APP_URL").'storage/assets/js/admin/repayment.js?time='.time())}}"></script>

</body>

</html>

