@extends('layouts.app')

@section('title', __('clinicCore::lang.duplicates'))

@section('content')
<section class="content-header">
    <h1><i class="fas fa-clone"></i> @lang('clinicCore::lang.duplicate_patients')</h1>
    <ol class="breadcrumb">
        <li><a href="{{ route('clinic.patients.index') }}">@lang('clinicCore::lang.pets')</a></li>
        <li class="active">@lang('clinicCore::lang.duplicates')</li>
    </ol>
</section>

<section class="content">
    <div class="alert alert-info" style="border-radius:8px;">
        <i class="fa fa-info-circle"></i>
        @lang('clinicCore::lang.duplicates_intro')
    </div>

    @component('components.widget', ['class' => 'box-primary', 'title' => __('clinicCore::lang.candidate_pairs') . ' (' . count($pairs) . ')'])
        @if(empty($pairs))
            <div style="text-align:center;padding:40px 20px;color:#94a3b8;">
                <i class="fa fa-check-circle" style="font-size:36px;display:block;margin-bottom:10px;color:#22c55e;"></i>
                @lang('clinicCore::lang.no_duplicates')
            </div>
        @else
        <div class="table-responsive">
            <table class="table table-bordered table-hover" style="font-size:13px;">
                <thead>
                    <tr>
                        <th style="width:90px;">@lang('clinicCore::lang.confidence')</th>
                        <th>@lang('clinicCore::lang.suggested_master')</th>
                        <th>@lang('clinicCore::lang.duplicate')</th>
                        <th>@lang('clinicCore::lang.match_reasons')</th>
                        <th style="width:170px;">@lang('clinicCore::lang.actions')</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($pairs as $pair)
                    @php
                        $conf = (float) $pair['confidence'];
                        $confColor = $conf >= 0.9 ? '#e74c3c' : ($conf >= 0.75 ? '#f39c12' : '#3498db');
                    @endphp
                    <tr class="clc-dup-row"
                        data-master="{{ $pair['master']->id }}"
                        data-duplicate="{{ $pair['duplicate']->id }}">
                        <td>
                            <span class="label" style="background:{{ $confColor }};font-size:12px;">
                                {{ round($conf * 100) }}%
                            </span>
                        </td>
                        <td>
                            <a href="{{ route('clinic.patients.show', $pair['master']->id) }}" target="_blank" style="font-weight:700;">
                                {{ $pair['master']->patient_name }}
                            </a>
                            <small class="text-muted" style="display:block;">
                                #{{ $pair['master']->id }}
                                @if($pair['master']->dob) · {{ $pair['master']->dob->format('d-m-Y') }} @endif
                                @if(optional($pair['master']->owner)->mobile) · {{ $pair['master']->owner->mobile }} @endif
                            </small>
                        </td>
                        <td>
                            <a href="{{ route('clinic.patients.show', $pair['duplicate']->id) }}" target="_blank">
                                {{ $pair['duplicate']->patient_name }}
                            </a>
                            <small class="text-muted" style="display:block;">
                                #{{ $pair['duplicate']->id }}
                                @if($pair['duplicate']->dob) · {{ $pair['duplicate']->dob->format('d-m-Y') }} @endif
                                @if(optional($pair['duplicate']->owner)->mobile) · {{ $pair['duplicate']->owner->mobile }} @endif
                            </small>
                        </td>
                        <td>
                            @foreach($pair['reasons'] as $reason)
                                <span class="label label-default" style="font-weight:500;">{{ $reason }}</span>
                            @endforeach
                        </td>
                        <td>
                            <button class="btn btn-xs btn-warning clc-dup-merge"
                                data-master="{{ $pair['master']->id }}"
                                data-master-name="{{ e($pair['master']->patient_name) }}"
                                data-duplicate="{{ $pair['duplicate']->id }}"
                                data-duplicate-name="{{ e($pair['duplicate']->patient_name) }}">
                                <i class="fa fa-compress-arrows-alt"></i> @lang('clinicCore::lang.review_merge')
                            </button>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @endif
    @endcomponent
</section>

{{-- Merge confirmation modal --}}
<div class="modal fade" id="clcMergeModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
    <div class="modal-header" style="background:#e67e22;color:#fff;border-radius:4px 4px 0 0;">
        <button type="button" class="close" data-dismiss="modal" style="color:#fff;opacity:1;">&times;</button>
        <h4 class="modal-title"><i class="fa fa-compress-arrows-alt"></i> @lang('clinicCore::lang.confirm_merge')</h4>
    </div>
    <div class="modal-body">
        <p>@lang('clinicCore::lang.merge_warning')</p>
        <ul style="font-size:13px;">
            <li>@lang('clinicCore::lang.merge_master'): <strong id="clc_merge_master_name"></strong></li>
            <li>@lang('clinicCore::lang.merge_duplicate'): <strong id="clc_merge_dup_name"></strong></li>
        </ul>
        <p class="text-danger" style="font-size:12px;"><i class="fa fa-exclamation-triangle"></i> @lang('clinicCore::lang.merge_irreversible')</p>
        <input type="hidden" id="clc_merge_master">
        <input type="hidden" id="clc_merge_dup">
    </div>
    <div class="modal-footer">
        <button class="btn btn-default btn-sm" data-dismiss="modal">@lang('clinicCore::lang.cancel')</button>
        <button class="btn btn-danger btn-sm" id="clc_merge_confirm"><i class="fa fa-check"></i> @lang('clinicCore::lang.merge_now')</button>
    </div>
</div></div></div>
@endsection

@section('javascript')
<script>
$(function () {
    var token = $('meta[name="csrf-token"]').attr('content') || '{{ csrf_token() }}';

    $(document).on('click', '.clc-dup-merge', function () {
        var b = $(this);
        $('#clc_merge_master').val(b.data('master'));
        $('#clc_merge_dup').val(b.data('duplicate'));
        $('#clc_merge_master_name').text(b.data('master-name'));
        $('#clc_merge_dup_name').text(b.data('duplicate-name'));
        $('#clcMergeModal').modal('show');
    });

    $('#clc_merge_confirm').on('click', function () {
        var $btn = $(this).prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i>');
        $.ajax({
            url: '{{ route('clinic.patients.merge') }}',
            method: 'POST',
            data: {
                _token: token,
                master_id: $('#clc_merge_master').val(),
                duplicate_id: $('#clc_merge_dup').val()
            }
        }).done(function (res) {
            if (res.success) {
                if (window.toastr) toastr.success(res.msg);
                setTimeout(function () { location.reload(); }, 700);
            } else {
                if (window.toastr) toastr.error(res.msg || 'Merge failed.');
                $btn.prop('disabled', false).html('<i class="fa fa-check"></i> {{ __('clinicCore::lang.merge_now') }}');
            }
        }).fail(function (xhr) {
            var msg = (xhr.responseJSON && xhr.responseJSON.msg) ? xhr.responseJSON.msg : 'Merge failed.';
            if (window.toastr) toastr.error(msg);
            $btn.prop('disabled', false).html('<i class="fa fa-check"></i> {{ __('clinicCore::lang.merge_now') }}');
        });
    });
});
</script>
@endsection
