@extends('backend.app')
@section('title','Alert Action')
@push('styles')
    <style>
        .action-view {
            height: 402px;
            overflow-y: scroll;
            padding: 0px;
        }

        .card-header {
            background: #EAECF0;
            align-items: center;
            height: 60px;
        }

        table tr td a {
            color: #1C274D;
        }

        table th, table td {
            border-color: #EAECF0;
        }

        .action-view {
            scrollbar-width: auto;
            scrollbar-color: #fff #CCCED2;
        }

        .action-view::-webkit-scrollbar {
            width: 8px;
        }

        .action-view::-webkit-scrollbar-track {
            background: #CCCED2;
        }

        .action-view::-webkit-scrollbar-thumb {
            background-color: #fff;
            border-radius: 5px;
            border: 2px solid #CCCED2;
        }

        .card-body table tr td, .card-body table tr th {
            font-size: 0.8vw;
        }

        .view_details {
            cursor: pointer;
        }
    </style>
@endpush
@section('content')
    <h2 class="mb-3 card-title">{{__('messages.alert_action')}} {{__('messages.list')}}</h2>
    <div class="card-body">
        <div class="row">
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name"> {{__('messages.first_heat_age_employee')}} ({{config('perameters.first_heat_age')}}
                            {{__('messages.months')}})</h5>
                        <button class="btn btn-light border" onclick="printDiv('firstHeatPdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="firstHeatPdf">
                        <table class="table table-bordered w-100">
                            @foreach ($firstHeatAgeEmployee as $item)
                                <tr>
                                    <td>
                                        <a data-url="{{ route('ai.create',["tag"=>$item->id])}}" class="view_details">
                                            {{ $item->tag->uid }}
                                        </a>
                                    </td>
                                    <td>
                                        <a href="javascript: void(0);">
                                            {{ $item->expected_ai_date }}
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name"> {{__('messages.fertility_problem_employee')}} ( {{config('perameters.fertility_problem') }}
                            {{__('messages.fails')}})</h5>
                        <button class="btn btn-light border" onclick="printDiv('fertilityPdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="fertilityPdf">
                        <table class="table table-bordered w-100">
                            @foreach ($fertilityProblemEmployee as $item)
                                @if($item->hasFertilityProblem)
                                    <tr>
                                        <td>
                                            <a href="#">
                                                {{ $item->tag->uid }}
                                            </a>
                                        </td>
                                    </tr>
                                @endif
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name">{{__('messages.low_milk_yield_employee')}} (< {{config('perameters.milk_yield') }}L
                            {{ __('messages.in').' 100'. __('messages.days')}})</h5>
                        <button class="btn btn-light border" onclick="printDiv('milkYieldPdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="milkYieldPdf">
                        <table class="table table-bordered w-100">
                            @foreach ($milkYieldEmployees as $item)
                                @php $milkYeild = config('perameters.milk_yield');  @endphp
                                @if($item['grand_milk'] < $milkYeild)
                                    <tr>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{ $item['tag']['uid'] }}
                                            </a>
                                        </td>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{ $item['grand_milk'] }}L
                                            </a>
                                        </td>
                                    </tr>
                                @endif
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name">{{__('messages.low_milk_yield_employee')}}( < {{config('perameters.lactation_yield')}}L {{ __('messages.in').' ' .__('messages.a').' '. __('messages.calving_period')}})</h5>
                        <button class="btn btn-light border" onclick="printDiv('lacYieldPdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="lacYieldPdf">
                        <table class="table table-bordered w-100">
                            @foreach ($lowLactionEmployees as $item)
                                @if($item->totalMilkInPeriod < config('perameters.lactation_yield'))
                                    <tr>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{ $item->tag->uid }}
                                            </a>
                                        </td>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{ $item->totalMilkInPeriod }}L
                                            </a>
                                        </td>
                                    </tr>
                                @endif
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name">{{__('messages.low_lactation_length_employee')}}( < {{config('perameters.lactation_length')}} {{__('messages.days_in_a_calving_period')}})</h5>
                        <button class="btn btn-light border" onclick="printDiv('lacLengthPdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="lacLengthPdf">
                        <table class="table table-bordered w-100">
                            @foreach ($lowLactationLengthEmployee as $item)
                                @if($item->calving_interval < config('perameters.lactation_length'))
                                    <tr>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{$item->tag->uid}}
                                            </a>
                                        </td>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{ $item->calving_interval }} {{__('messages.days')}}
                                            </a>
                                        </td>
                                    </tr>
                                @endif
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name">{{__('messages.amr_alert')}} ({{config('perameters.amr_period')}} {{__('messages.days_after_treatment')}})</h5>
                        <button class="btn btn-light border" onclick="printDiv('amrPdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="amrPdf">
                        <table class="table table-bordered w-100">
                            @foreach ($amrEmployeeAlertData as $item)
                                <tr>
                                    <td>
                                        <a href="Javascript:void(0)">
                                            {{ $item->employee->tag->uid }}
                                        </a>
                                    </td>
                                    <td>
                                        <a href="javascript: void(0);">
                                            {{ $item->alert_expire_date }}
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name">{{__('messages.calving_preparation')}} ({{config('perameters.calving_preparation')}} {{__('messages.days').' '.__('messages.before_calving')}})</h5>
                        <button class="btn btn-light border" onclick="printDiv('calvingPdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="calvingPdf">
                        <table class="table table-bordered w-100">
                            @foreach ($aiEmployeeCalvingData as $item)
                                <tr>
                                    <td>
                                        <a href="#">
                                            {{ $item['employee']['tag']['uid'] }}
                                        </a>
                                    </td>
                                    <td>
                                        <a href="javascript: void(0);">
                                            {{ $item['expected_calving_date'] }}
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mt-4 ">
                <div class="card  pb-3">
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="title_name">{{__('messages.sire_performance')}} (< 60%)</h5>
                        <button class="btn btn-light border" onclick="printDiv('sirePdf')" type="button"><i
                                class="print-ico"></i></button>
                    </div>
                    <div class="card-body action-view " id="sirePdf">
                        <table class="table table-bordered w-100">
                            @foreach ($sirePerformance as $item)
                                @if($item['sirePregnantRate'] < 60)
                                    <tr>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{$item['code']}}
                                            </a>
                                        </td>
                                        <td>
                                            <a href="javascript: void(0);">
                                                {{ number_format($item['sirePregnantRate'], 2) }}%
                                            </a>
                                        </td>
                                    </tr>
                                @endif
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="statusUpdate" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
         role="dialog" aria-labelledby="statusLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
            <div class="modal-content">
                <form method="POST" action="{{ route('alert.status.update') }}">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title" id="statusLabel">{{__('messages.alert_status')}}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" id="alertid" name="alertId"/>
                        <h4 class="text-info">{{__('messages.are_you_remove')}}</h4>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">{{__('messages.cancel')}}</button>
                        <button type="submit" class="btn btn-success">{{__('messages.ok')}}</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="modal fade" id="alertaction_modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content p-5">
                <div class="modal-body p-0 mt-3 editModal">

                </div>
            </div>
        </div>
    </div>
@endsection
@push('scripts')
    <script type="text/javascript">
        function printDiv(divName) {
            var printContents = document.getElementById(divName).innerHTML;
            var printStyle = '<style>@page {size: auto;margin: 0mm;}</style>';
            var printDiv = '<div id="order-details-wrapper" style="padding: 20px;">' + printStyle + printContents + '</div>';
            document.body.innerHTML = printDiv;
            var originalContents = document.body.innerHTML;
            window.print();
            document.body.innerHTML = originalContents;
            location.reload();
        }

        $(document).on('click', '.statusUpdate', function () {
            var id = $(this).data('id');
            var uid = $(this).data('uid-id');
            $('#alertid').val(id);
            $('#statusLabel').text(uid);
        });
        $('.view_details').click(function () {
            let id = $(this).attr('data-url');
            $('.editModal').empty();
            axios.get(id, {}).then(function (response) {
                $('#alertaction_modal').modal('show');
                $('.editModal').html(response.data);

            }).catch(function (error) {
                console.log(error);
            });
        });
    </script>
@endpush
