@extends ('layouts.patient')
@section('inner-content')
<div id="company-client-{{ $patient->uid }}">
    @yield('company-client-content')
</div>

<script>
    (function () {
        function init() {
            initStagSuggest();
            let parentSegment = $('body');
            parentSegment.find('input[stag-suggest][target-key][target-field]')
                .off('stag-suggest-selected')
                .on('stag-suggest-selected', (_e, _input, _data) => {
                    _input = $(_input);
                    _input.closest('form').find('input[name="' + _input.attr('target-field') + '"]').val(_data[_input.attr('target-key')]);
                });
            parentSegment.find('.copy-target')
                .off('click.copy-target')
                .on('click.copy-target', function() {
                    copyTextToClipboard($(this).attr('data-target'));
                    return false;
                });

            parentSegment.find('.generate-and-visit-ic-portal')
                .off('click.generate-and-visit-ic-portal')
                .on('click.generate-and-visit-ic-portal', function() {
                    $.post('/api/session/proLogInAsCustomer', {
                        customerUid: $(this).attr('data-uid')
                    }, _data => {
                        if(!hasResponseError(_data)) {
                            window.location.href = '/ic/home/' + _data.data;
                            return false;
                        }
                    });
                    return false;
                });

            parentSegment.find('.generate-and-copy-ic-portal-url')
                .off('click.generate-and-copy-ic-portal-url')
                .on('click.generate-and-copy-ic-portal-url', function() {
                    $.post('/api/session/proLogInAsCustomer', {
                        customerUid: $(this).attr('data-uid')
                    }, _data => {
                        if(!hasResponseError(_data)) {
                            copyTextToClipboard('{{config('app.url')}}/ic/home/' + _data.data);
                            return false;
                        }
                    });
                    return false;
                });

            parentSegment.find('.generate-and-visit-ic-pay')
                .off('click.generate-and-visit-ic-pay')
                .on('click.generate-and-visit-ic-pay', function() {
                    $.post('/api/session/proLogInAsCustomer', {
                        customerUid: $(this).attr('data-uid')
                    }, _data => {
                        if(!hasResponseError(_data)) {
                            window.location.href = '/ic/pay/' + $(this).attr('data-invoice-uid') + '/' + _data.data;
                            return false;
                        }
                    });
                    return false;
                });

            parentSegment.find('.generate-and-copy-ic-pay-url')
                .off('click.generate-and-copy-ic-pay-url')
                .on('click.generate-and-copy-ic-pay-url', function() {
                    $.post('/api/session/proLogInAsCustomer', {
                        customerUid: $(this).attr('data-uid')
                    }, _data => {
                        if(!hasResponseError(_data)) {
                            copyTextToClipboard('{{config('app.url')}}/ic/pay/' + $(this).attr('data-invoice-uid') + '/' + _data.data);
                            return false;
                        }
                    });
                    return false;
                });

                parentSegment.find('.generate-and-copy-ic-payment-method-url')
                .off('click.generate-and-copy-ic-payment-method-url')
                .on('click.generate-and-copy-ic-payment-method-url', function() {
                    $.post('/api/session/proLogInAsCustomer', {
                        customerUid: $(this).attr('data-uid')
                    }, _data => {
                        if (!hasResponseError(_data)) {
                            copyTextToClipboard('{{ config('app.url') }}/ic/payment-methods/' + _data.data);
                            return false;
                        }
                    });
                    return false;
                });
        }
        addMCInitializer('company-client-{{$patient->uid}}', init, '#company-client-{{$patient->uid}}')
    }).call(window);
</script>


@endsection