

<?php $__env->startSection('title', 'Case Search'); ?>

<?php $__env->startSection('content'); ?>

<div class="container-fluid">

    <!-- Page Header -->

    <div class="mb-4">

        <h3 class="fw-bold mb-1">
            <i class="bi bi-search"></i>
            Case Status Search
        </h3>

        <p class="text-muted mb-0">
            Search case records using different search criteria
        </p>

    </div>


    <!-- Search Card -->

    <div class="card border-0 shadow-sm mb-4">

        <!-- Card Header -->

        <div class="card-header bg-dark text-white py-3">

            <h5 class="mb-0">

                <i class="bi bi-search me-2"></i>

                Search Case

            </h5>

        </div>


        <div class="card-body">


            <!-- Search Tabs -->

            <ul class="nav nav-tabs mb-4" id="searchTabs">

                <li class="nav-item">

                    <button
                        type="button"
                        class="nav-link active"
                        data-type="case_number"
                    >

                        <i class="bi bi-file-earmark-text me-1"></i>

                        Case Number

                    </button>

                </li>


                <li class="nav-item">

                    <button
                        type="button"
                        class="nav-link"
                        data-type="party_name"
                    >

                        <i class="bi bi-person me-1"></i>

                        Party Name

                    </button>

                </li>


                <li class="nav-item">

                    <button
                        type="button"
                        class="nav-link"
                        data-type="lawyer_name"
                    >

                        <i class="bi bi-person-badge me-1"></i>

                        Lawyer Name

                    </button>

                </li>


                <li class="nav-item">

                    <button
                        type="button"
                        class="nav-link"
                        data-type="crime"
                    >

                        <i class="bi bi-shield-exclamation me-1"></i>

                        Crime No./Year

                    </button>

                </li>


                <li class="nav-item">

                    <button
                        type="button"
                        class="nav-link"
                        data-type="district"
                    >

                        <i class="bi bi-geo-alt me-1"></i>

                        District Court

                    </button>

                </li>

            </ul>


            <!-- Search Form -->

            <form
                method="GET"
                action="<?php echo e(route('cases.search')); ?>"
            >

                <input
                    type="hidden"
                    name="search_type"
                    id="search_type"
                    value="<?php echo e($searchType); ?>"
                >


                <!-- CASE NUMBER -->

                <div
                    id="case_number"
                    class="search-section"
                >

                    <div class="row g-3">

                        <!-- Case Type -->

                        <div class="col-md-4">

                            <label class="form-label fw-semibold">

                                Case Type

                            </label>

                            <select
                                name="case_type"
                                class="form-select"
                            >

                                <option value="">

                                    -- Select Case Type --

                                </option>

                                <?php $__currentLoopData = $caseTypes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $type): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                    <option
                                        value="<?php echo e($type->id); ?>"
                                        <?php echo e(request('case_type') == $type->id ? 'selected' : ''); ?>

                                    >

                                        <?php echo e(trim($type->casename)); ?>


                                    </option>

                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                            </select>

                        </div>


                        <!-- Case Number -->

                        <div class="col-md-4">

                            <label class="form-label fw-semibold">

                                Case Number

                            </label>

                            <input
                                type="number"
                                name="case_number"
                                class="form-control"
                                value="<?php echo e(request('case_number')); ?>"
                                placeholder="e.g. 4966"
                            >

                        </div>


                        <!-- Year -->

                        <div class="col-md-4">

                            <label class="form-label fw-semibold">

                                Year

                            </label>

                            <select
                                name="year"
                                class="form-select"
                            >

                                <option value="">

                                    -- Select Year --

                                </option>

                                <?php $__currentLoopData = $years; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $year): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                    <option
                                        value="<?php echo e($year); ?>"
                                        <?php echo e(request('year') == $year ? 'selected' : ''); ?>

                                    >

                                        <?php echo e($year); ?>


                                    </option>

                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                            </select>

                        </div>

                    </div>

                </div>


                
                <!-- PARTY NAME -->

<div
    id="party_name"
    class="search-section d-none"
>

    <label class="form-label fw-semibold">
        Party Name
    </label>

    <div class="party-search-wrapper position-relative">

        <input
            type="text"
            name="party_name"
            id="partyNameSearch"
            class="form-control"
            value="<?php echo e(request('party_name')); ?>"
            placeholder="Enter Petitioner or Respondent Name"
            autocomplete="off"
        >

        <div
            id="partySuggestions"
            class="party-suggestions"
        ></div>

    </div>

    <small class="text-muted">
        Search will check both Petitioner and Respondent.
    </small>

</div>


<script>

document.addEventListener('DOMContentLoaded', function () {

    const input = document.getElementById('partyNameSearch');
    const box = document.getElementById('partySuggestions');

    console.log('Party Search Loaded');
    console.log('Input:', input);
    console.log('Suggestions Box:', box);


    if (!input || !box) {

        console.error('Party search elements not found');

        return;
    }


    let timer = null;


    /*
    |--------------------------------------------------------------------------
    | LIVE SEARCH
    |--------------------------------------------------------------------------
    */

    input.addEventListener('input', function () {

        const term = this.value.trim();

        clearTimeout(timer);


        /*
        |--------------------------------------------------------------------------
        | Empty
        |--------------------------------------------------------------------------
        */

        if (term.length === 0) {

            box.innerHTML = '';

            box.style.display = 'none';

            return;
        }


        /*
        |--------------------------------------------------------------------------
        | Minimum 1 Character
        |--------------------------------------------------------------------------
        */

        box.style.display = 'block';

        box.innerHTML = `
            <div class="party-suggestion-loading">
                Searching...
            </div>
        `;


        /*
        |--------------------------------------------------------------------------
        | AJAX
        |--------------------------------------------------------------------------
        */

        timer = setTimeout(function () {

            fetch(
                "<?php echo e(route('cases.party.suggestions')); ?>?term="
                + encodeURIComponent(term),
                {
                    method: 'GET',

                    headers: {
                        'Accept': 'application/json',
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                }
            )

            .then(function (response) {

                if (!response.ok) {

                    throw new Error(
                        'HTTP Error: ' + response.status
                    );

                }

                return response.json();

            })

            .then(function (data) {

                console.log('Party Suggestions:', data);


                box.innerHTML = '';


                /*
                |--------------------------------------------------------------------------
                | No Result
                |--------------------------------------------------------------------------
                */

                if (!data || data.length === 0) {

                    box.innerHTML = `
                        <div class="party-suggestion-empty">
                            No matching party found
                        </div>
                    `;

                    box.style.display = 'block';

                    return;
                }


                /*
                |--------------------------------------------------------------------------
                | Results
                |--------------------------------------------------------------------------
                */

                data.forEach(function (item) {

                    const row = document.createElement('div');

                    row.className =
                        'party-suggestion-item';


                    row.innerHTML = `

                        <div class="party-name">
                            ${escapeHtml(item.pet_name || '-')}
                        </div>

                        <div class="party-vs">
                            Vs
                        </div>

                        <div class="party-name">
                            ${escapeHtml(item.res_name || '-')}
                        </div>

                        <div class="party-case">

                            Case No:
                            ${escapeHtml(item.caseno || '-')}

                            /

                            ${escapeHtml(item.caseyear || '-')}

                        </div>

                    `;


                    /*
                    |--------------------------------------------------------------------------
                    | CLICK RESULT
                    |--------------------------------------------------------------------------
                    */

                    row.addEventListener(
                        'click',
                        function () {

                            input.value =
                                item.pet_name ||
                                item.res_name ||
                                '';

                            box.innerHTML = '';

                            box.style.display = 'none';

                        }
                    );


                    box.appendChild(row);

                });


                box.style.display = 'block';

            })


            /*
            |--------------------------------------------------------------------------
            | ERROR
            |--------------------------------------------------------------------------
            */

            .catch(function (error) {

                console.error(
                    'Party Live Search Error:',
                    error
                );

                box.innerHTML = `
                    <div class="party-suggestion-empty">
                        Unable to load suggestions
                    </div>
                `;

                box.style.display = 'block';

            });

        }, 250);

    });


    /*
    |--------------------------------------------------------------------------
    | CLOSE DROPDOWN
    |--------------------------------------------------------------------------
    */

    document.addEventListener('click', function (event) {

        if (
            !input.contains(event.target) &&
            !box.contains(event.target)
        ) {

            box.style.display = 'none';

        }

    });


    /*
    |--------------------------------------------------------------------------
    | ESCAPE HTML
    |--------------------------------------------------------------------------
    */

    function escapeHtml(value) {

        return String(value)

            .replace(/&/g, '&amp;')

            .replace(/</g, '&lt;')

            .replace(/>/g, '&gt;')

            .replace(/"/g, '&quot;')

            .replace(/'/g, '&#039;');

    }

});

</script>


<style>

.party-search-wrapper {

    position: relative;

}


.party-suggestions {

    position: absolute;

    top: 100%;

    left: 0;

    right: 0;

    background: #ffffff;

    border: 1px solid #dee2e6;

    border-radius: 0 0 6px 6px;

    box-shadow: 0 5px 15px rgba(0, 0, 0, .15);

    z-index: 99999;

    max-height: 400px;

    overflow-y: auto;

    display: none;

}


.party-suggestion-item {

    padding: 10px 15px;

    border-bottom: 1px solid #eeeeee;

    cursor: pointer;

}


.party-suggestion-item:hover {

    background: #f5f6f8;

}


.party-name {

    font-weight: 600;

    color: #212529;

}


.party-vs {

    font-size: 11px;

    color: #6c757d;

    margin: 2px 0;

}


.party-case {

    font-size: 12px;

    color: #6c757d;

    margin-top: 3px;

}


.party-suggestion-loading,
.party-suggestion-empty {

    padding: 12px 15px;

    color: #6c757d;

}

</style>





                <!-- LAWYER NAME -->

                <div
                    id="lawyer_name"
                    class="search-section d-none"
                >

                    <label class="form-label fw-semibold">

                        Lawyer Name

                    </label>

                    <input
                        type="text"
                        name="lawyer_name"
                        class="form-control"
                        value="<?php echo e(request('lawyer_name')); ?>"
                        placeholder="Enter Advocate Name"
                    >

                    <small class="text-muted">

                        Search will check both Petitioner and Respondent Advocate.

                    </small>

                </div>


                <!-- CRIME -->

                <div
                    id="crime"
                    class="search-section d-none"
                >

                    <div class="row g-3">

                        <div class="col-md-6">

                            <label class="form-label fw-semibold">

                                Crime Number

                            </label>

                            <input
                                type="number"
                                name="crime_number"
                                class="form-control"
                                value="<?php echo e(request('crime_number')); ?>"
                                placeholder="Enter Crime Number"
                            >

                        </div>


                        <div class="col-md-6">

                            <label class="form-label fw-semibold">

                                Year

                            </label>

                            <select
                                name="crime_year"
                                class="form-select"
                            >

                                <option value="">

                                    -- Select Year --

                                </option>

                                <?php $__currentLoopData = $years; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $year): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                    <option
                                        value="<?php echo e($year); ?>"
                                        <?php echo e(request('crime_year') == $year ? 'selected' : ''); ?>

                                    >

                                        <?php echo e($year); ?>


                                    </option>

                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                            </select>

                        </div>

                    </div>

                </div>


                <!-- DISTRICT -->

                <div
                    id="district"
                    class="search-section d-none"
                >

                    <label class="form-label fw-semibold">

                        District

                    </label>

                    <input
                        type="number"
                        name="district"
                        class="form-control"
                        value="<?php echo e(request('district')); ?>"
                        placeholder="Enter District ID"
                    >

                    <small class="text-muted">

                        District master 

                    </small>

                </div>


                <!-- Buttons -->

                <div class="mt-4 d-flex gap-2">

                    <button
                        type="submit"
                        class="btn btn-dark px-4"
                    >

                        <i class="bi bi-search me-1"></i>

                        Search

                    </button>


                    <a
                        href="<?php echo e(route('cases.search')); ?>"
                        class="btn btn-outline-secondary px-4"
                    >

                        <i class="bi bi-arrow-clockwise me-1"></i>

                        Reset

                    </a>

                </div>

            </form>

        </div>

    </div>


    <!-- SEARCH RESULT -->

   







<?php if(request()->hasAny([
    'case_type',
    'case_number',
    'year',
    'party_name',
    'lawyer_name',
    'crime_number',
    'crime_year',
    'district'
])): ?>

    <?php if($cases->count() > 0): ?>

        

        <div class="card border-0 shadow-sm mb-4">

            <div class="card-body p-4">

                

                <div class="result-header mb-3">

                    <div>

                        <h5 class="fw-bold mb-1">
                            Search Results
                        </h5>

                        <div class="text-muted small">

                            Showing
                            <strong><?php echo e($cases->firstItem()); ?></strong>

                            to

                            <strong><?php echo e($cases->lastItem()); ?></strong>

                            of

                            <strong><?php echo e($cases->total()); ?></strong>

                            entries

                        </div>

                    </div>

                    <div class="result-count">
                        <?php echo e($cases->total()); ?> Cases
                    </div>

                </div>


                

                <div class="row mb-3">

                    <div class="col-md-5 ms-auto">

                        <input
                            type="text"
                            id="resultSearch"
                            class="form-control"
                            placeholder="Search in results..."
                        >

                    </div>

                </div>


                

                <div class="table-responsive">

                    <table
                        class="table table-bordered case-result-table align-middle"
                        id="caseResultTable"
                    >

                        <thead>

                            <tr>

                                <th class="text-center">
                                    S.No.
                                </th>

                                <th>
                                    Case / Reg. Date
                                </th>

                                <th>
                                    Petitioner Name<br>
                                    Respondent Name
                                </th>

                                <th>
                                    Petitioner's Advocate<br>
                                    Respondent's Advocate
                                </th>

                                <th class="text-center">
                                    Status
                                </th>

                                <th class="text-center">
                                    Action
                                </th>

                            </tr>

                        </thead>


                        <tbody>

                            <?php $__currentLoopData = $cases; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $case): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                <tr class="case-row">

                                    

                                    <td class="text-center fw-semibold">

                                        <?php echo e($cases->firstItem() + $index); ?>


                                    </td>


                                    

                                    <td>

                                        <a
                                            href="javascript:void(0)"
                                            class="case-link"
                                            data-bs-toggle="modal"
                                            data-bs-target="#caseModal<?php echo e($case->cino); ?>"
                                        >

                                            <?php echo e(trim($case->caseType->skey ?? '')); ?>

                                            -
                                            <?php echo e($case->caseno); ?>

                                            -
                                            <?php echo e($case->caseyear); ?>


                                        </a>


                                        <div class="case-date">

                                            <?php if($case->fil_dt): ?>

                                                <?php echo e(\Carbon\Carbon::parse($case->fil_dt)->format('Y-m-d H:i:s')); ?>


                                            <?php else: ?>

                                                -

                                            <?php endif; ?>

                                        </div>

                                    </td>


                                    

                                    <td>

                                        <div class="party-result-name">
                                            <?php echo e($case->pet_name ?? '-'); ?>

                                        </div>

                                        <div class="vs-small">
                                            Vs
                                        </div>

                                        <div class="party-result-name">
                                            <?php echo e($case->res_name ?? '-'); ?>

                                        </div>

                                    </td>


                                    

                                    <td>

                                        <div>
                                            <?php echo e($case->pet_adv ?? '-'); ?>

                                        </div>

                                        <div class="vs-small">
                                            Vs
                                        </div>

                                        <div>
                                            <?php echo e($case->res_adv ?? '-'); ?>

                                        </div>

                                    </td>


                                    

                                    <td class="text-center">

                                        <?php if($case->c_status === 'P'): ?>

                                            <span class="case-status pending">
                                                Pending
                                            </span>

                                        <?php elseif($case->c_status === 'D'): ?>

                                            <span class="case-status disposed">
                                                Disposed
                                            </span>

                                        <?php else: ?>

                                            <span class="case-status other">
                                                <?php echo e($case->c_status ?? 'N/A'); ?>

                                            </span>

                                        <?php endif; ?>

                                    </td>


                                    

                                    <td class="text-center">

                                        <button
                                            type="button"
                                            class="btn btn-view-detail"
                                            data-bs-toggle="modal"
                                            data-bs-target="#caseModal<?php echo e($case->cino); ?>"
                                        >

                                            <i class="bi bi-eye me-1"></i>

                                            View Full Detail

                                        </button>

                                    </td>

                                </tr>


                                

                                <?php echo $__env->make(
                                    'cases.partials.case-detail-modal',
                                    ['case' => $case]
                                , array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        </tbody>

                    </table>

                </div>


                

                <?php if($cases->hasPages()): ?>

                    <div class="mt-4 d-flex justify-content-center">

                        <?php echo e($cases->withQueryString()->links()); ?>


                    </div>

                <?php endif; ?>

            </div>

        </div>


    <?php else: ?>

        

        <div class="card border-0 shadow-sm">

            <div class="card-body text-center py-5">

                <i class="bi bi-search no-result-icon"></i>

                <h4 class="mt-3">
                    No Case Found
                </h4>

                <p class="text-muted mb-0">
                    No Matching Case Record Found             
                   </p>

            </div>

        </div>

    <?php endif; ?>

<?php endif; ?>







</div>


<script>

document.addEventListener('DOMContentLoaded', function () {

    const input = document.getElementById('partyNameSearch');
    const suggestionsBox = document.getElementById('partySuggestions');

    if (!input || !suggestionsBox) {
        return;
    }

    let timer = null;

    input.addEventListener('input', function () {

        const term = this.value.trim();

        clearTimeout(timer);

        if (term.length < 2) {

            suggestionsBox.innerHTML = '';
            suggestionsBox.classList.add('d-none');

            return;
        }

        timer = setTimeout(function () {

            fetch(
                "<?php echo e(route('cases.party.suggestions')); ?>?term="
                + encodeURIComponent(term),
                {
                    headers: {
                        'Accept': 'application/json',
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                }
            )
            .then(response => {

                if (!response.ok) {
                    throw new Error('Network response was not OK');
                }

                return response.json();

            })
            .then(data => {

                suggestionsBox.innerHTML = '';

                if (!data.length) {

                    suggestionsBox.innerHTML = `
                        <div class="party-no-result">
                            No matching party found
                        </div>
                    `;

                    suggestionsBox.classList.remove('d-none');

                    return;
                }


                data.forEach(item => {

                    const div = document.createElement('div');

                    div.className = 'party-suggestion-item';

                    div.innerHTML = `
                        <div class="party-suggestion-name">
                            ${highlightText(item.pet_name || item.res_name || '', term)}
                        </div>

                        <div class="party-suggestion-case">
                            Case No:
                            ${item.caseno || '-'}
                            /
                            ${item.caseyear || '-'}
                        </div>
                    `;

                    div.addEventListener('click', function () {

                        input.value =
                            item.pet_name ||
                            item.res_name ||
                            '';

                        suggestionsBox.innerHTML = '';
                        suggestionsBox.classList.add('d-none');

                    });

                    suggestionsBox.appendChild(div);

                });

                suggestionsBox.classList.remove('d-none');

            })
            .catch(error => {

                console.error('Party live search error:', error);

                suggestionsBox.innerHTML = '';
                suggestionsBox.classList.add('d-none');

            });

        }, 250);

    });


    /*
    |--------------------------------------------------------------------------
    | Highlight Search Text
    |--------------------------------------------------------------------------
    */

    function highlightText(text, search) {

        const escaped = search.replace(
            /[.*+?^${}()|[\]\\]/g,
            '\\$&'
        );

        return text.replace(
            new RegExp(`(${escaped})`, 'gi'),
            '<strong>$1</strong>'
        );

    }


    /*
    |--------------------------------------------------------------------------
    | Hide Suggestions When Clicking Outside
    |--------------------------------------------------------------------------
    */

    document.addEventListener('click', function (event) {

        if (
            !input.contains(event.target) &&
            !suggestionsBox.contains(event.target)
        ) {

            suggestionsBox.classList.add('d-none');

        }

    });

});

</script>

<style>

.party-search-wrapper {
    position: relative;
}

.party-suggestions {

    position: absolute;

    top: 100%;
    left: 0;
    right: 0;

    background: #ffffff;

    border: 1px solid #dee2e6;

    border-top: none;

    border-radius: 0 0 6px 6px;

    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);

    max-height: 350px;

    overflow-y: auto;

    z-index: 9999;

}

.party-suggestion-item {

    padding: 10px 14px;

    border-bottom: 1px solid #eeeeee;

    cursor: pointer;

    background: #ffffff;

}

.party-suggestion-item:hover {

    background: #f5f6f7;

}

.party-suggestion-item:last-child {

    border-bottom: none;

}

.party-suggestion-name {

    font-size: 14px;

    font-weight: 500;

    color: #212529;

}

.party-suggestion-name strong {

    color: #000000;

    font-weight: 700;

}

.party-suggestion-case {

    margin-top: 3px;

    font-size: 12px;

    color: #6c757d;

}

.party-no-result {

    padding: 12px 14px;

    color: #6c757d;

    font-size: 14px;

}

</style>


<script>
document.addEventListener('DOMContentLoaded', function () {

    const input = document.getElementById('party_name_input');
    const box = document.getElementById('partySuggestions');

    if (!input || !box) {
        console.log('Party search elements not found');
        return;
    }

    input.addEventListener('input', function () {

        const term = this.value.trim();

        console.log('Searching:', term);

        if (term.length < 1) {

            box.innerHTML = '';
            box.style.display = 'none';

            return;
        }

        fetch('/case-search/party-suggestions?term=' + encodeURIComponent(term))
            .then(response => {

                console.log('Response status:', response.status);

                return response.json();

            })
            .then(data => {

                console.log('Live search data:', data);

                box.innerHTML = '';

                if (!data.length) {

                    box.innerHTML = `
                        <div class="p-3 text-muted">
                            No matching party found
                        </div>
                    `;

                    box.style.display = 'block';

                    return;
                }

                data.forEach(item => {

                    const row = document.createElement('div');

                    row.className = 'party-suggestion-item';

                    row.innerHTML = `
                        <div>
                            <strong>
                                ${item.pet_name ?? '-'}
                            </strong>
                            <span class="text-muted"> Vs </span>
                            <strong>
                                ${item.res_name ?? '-'}
                            </strong>
                        </div>

                        <small class="text-muted">
                            Case No: ${item.caseno ?? '-'}
                            /
                            ${item.caseyear ?? '-'}
                        </small>
                    `;

                    row.addEventListener('click', function () {

                        input.value =
                            item.pet_name ||
                            item.res_name ||
                            '';

                        box.innerHTML = '';
                        box.style.display = 'none';

                    });

                    box.appendChild(row);

                });

                box.style.display = 'block';

            })
            .catch(error => {

                console.error(
                    'Live Search Error:',
                    error
                );

            });

    });

});
</script>


<style>

#partySuggestions {
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;

    background: #fff;

    border: 1px solid #ddd;

    border-radius: 0 0 6px 6px;

    box-shadow: 0 5px 15px rgba(0,0,0,.15);

    z-index: 99999;

    max-height: 350px;

    overflow-y: auto;

    display: none;
}

.party-suggestion-item {

    padding: 10px 14px;

    border-bottom: 1px solid #eee;

    cursor: pointer;

}

.party-suggestion-item:hover {

    background: #f5f6f8;

}

</style>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\abhay\erp-system\resources\views/cases/search.blade.php ENDPATH**/ ?>