@extends('layouts.app')

<?php
    $request = request();
    $victim = new App\Models\Victim;
    $tab = ""; $successmessage = $failureurl ="";
    $rawCaseRef = $request->id;
    $id = null;
    $caseRef = $rawCaseRef;
    if (!empty($rawCaseRef)) {
        $case = null;
        if (\Illuminate\Support\Str::isUuid((string) $rawCaseRef) && \Illuminate\Support\Facades\Schema::hasColumn((new App\Models\Victim())->getTable(), 'uuid')) {
            $case = App\Models\Victim::where('uuid', $rawCaseRef)->first();
        } else {
            $decoded = base64_decode($rawCaseRef, true);
            $numericId = ($decoded !== false && is_numeric($decoded)) ? $decoded : (is_numeric($rawCaseRef) ? $rawCaseRef : null);
            if ($numericId) {
                $case = App\Models\Victim::find($numericId);
            }
        }
        if ($case) {
            $id = $case->id;
            $caseRef = !empty($case->uuid) ? $case->uuid : $case->id;
        }
    }
    $tab = $request->tab; $numbercount = $request->defaultValue;

    if (empty($numbercount)){
        $numbercount = 1;
    }
    $buttontext = "Save And Continue";
    $skipbutton = "Skip"; $skipurl = "#";
    if(Empty($tab)){
        $tab = 'overview';
    }
    if($tab == 'overview'){
        $successurl = url("dataentry/index?tab=incident&stp=new");
        $successurlnxt = url('dataentry/index?tab='.$tab);
        $gotosuccess = true;
        $posturl = url("dataentry/processvictimdata");
    }
    if($tab == 'incidents'){
        $successurl = url("dataentry/index?tab=perpetrator&stp=new");
        $successurlnxt = url('dataentry/index?tab='.$tab);
        $gotosuccess = true;
        $posturl = url("dataentry/processincidentdata");
    }
    if($tab == 'perpetrator'){
        $defaultvalue = 1;

        if(isset($request->defaultValue) ){
            $defaultValue = $request->defaultValue;
        }
        $skipurl = url("dataentry/index?id=".$request->id."&tab=witness");
        $successurl = url("dataentry/index?tab=witness&stp=new");
        $successurlnxt = url('dataentry/index?tab='.$tab);
        $gotosuccess = true;
        $posturl = url("dataentry/processperpetrator");
    }
    if($tab == 'witness'){
        $skipurl = url("dataentry/index?id=".$request->id."&tab=action");
        $successurl = url("dataentry/index?tab=action&stp=new");
        $successurlnxt = url('dataentry/index?tab='.$tab);
        $gotosuccess = true;
        $posturl = url("dataentry/processwitness");
    }
    if($tab == 'action'){
        $skipurl = url("dataentry/index?id=".$request->id."&tab=status");
        $successurl = url("dataentry/index?tab=incident&stp=new");
        $gotosuccess = true;
        $posturl = url("dataentry/processaction");
    }
    if($tab == 'status'){
        $successurl = url("dataentry/index?tab=incident&stp=new");
        $gotosuccess = true;
        $posturl = url("dataentry/processstatus");
        $buttontext = "Save And Submit";
    }
    $gotosuccess = false;
    if(!Empty($request->successurl)){
        $successurl = $request->successurl;
        $gotosuccess = true;
    }
?>
@section('content')
<div class="pcoded-content ngbvd-data-entry-shell">
    <div class="pcoded-inner-content">
        <div class="main-body">
            <div class="page-wrapper">
                <!-- Page-header start -->
                <div class="page-header card page-body ngbvd-card">
                    <div class="row align-items-end">
                        <div class="col-lg-8">
                            <div class="page-header-title">
                                <i class="icofont icofont icofont icofont-file-document bg-c-pink"></i>
                                <div class="d-inline">
                                    <h4 class="ngbvd-page-title">Incident Data</h4>
                                    <span class="ngbvd-page-subtitle">Capture survivor, incident, referral, witness and case-status information</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-4">
                            <div class="page-header-breadcrumb">
                                <ul class="breadcrumb-title">
                                    <li class="breadcrumb-item">
                                        <a href="index.html">
                                            <i class="icofont icofont-home"></i>
                                        </a>
                                    </li>
                                    <li class="breadcrumb-item"><a href="#!">Incident Data</a>
                                    </li>
                                    <li class="breadcrumb-item"><a href="#!">Data Entry</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                        <hr>
                        <div class="row">
                            <div class="col-sm-12">
                                <?php //if(!Empty($id)){ ?>
                                <ul class="nav nav-tabs tabs ngbvd-step-tabs ngbvd-tabs" role="tablist">
                                    <li class="nav-item">
                                        <a class="nav-link <?php echo $tab == 'overview' ? 'active' : ''; ?>" href="<?php echo url('dataentry/index?id='.$caseRef.'&tab=overview'); ?>" role="tab">General Information</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link <?php echo $tab == 'incidents' ? 'active' : ''; ?>" href="<?php echo url('dataentry/index?id='.$caseRef.'&tab=incidents'); ?>" role="tab">Incident Data</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link <?php echo $tab == 'perpetrator' ? 'active' : ''; ?>" href="<?php echo url('dataentry/index?id='.$caseRef.'&tab=perpetrator'); ?>" role="tab">Perpetrator Information</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link <?php echo $tab == 'witness' ? 'active' : ''; ?>" href="<?php echo url('dataentry/index?id='.$caseRef.'&tab=witness'); ?>" role="tab">Witnesses</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link <?php echo $tab == 'action' ? 'active' : ''; ?>" href="<?php echo url('dataentry/index?id='.$caseRef.'&tab=action'); ?>" role="tab">Action Taken</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link <?php echo $tab == 'status' ? 'active' : ''; ?>" href="<?php echo url('dataentry/index?id='.$caseRef.'&tab=status'); ?>" role="tab">Case Status</a>
                                    </li>
                                </ul>

                                <?php //} ?>
                                <form id="indexform" class="form-horizontal ngbvd-form-section" method="POST" action="<?php echo $posturl; ?>">
                                    @csrf
                                    <input name="entityname" id="entityname" type="hidden" value="Incident" />
                                    <input type="hidden" id="action" name="action"value="" />
                                    <input type="hidden" id="id" name="id" value="<?php echo $caseRef; ?>" />
                                    <input name="successmessage" id="successmessage" type="hidden" value="<?php echo $successmessage; ?>" />
                                    <input type="hidden" id="<?php echo "SUCCESS_MESSAGE"; ?>" name="<?php echo "SUCCESS_MESSAGE"; ?>" value="<?php echo $successmessage; ?>" />
                                    <input type="hidden" id="<?php echo "URL_SUCCESS"; ?>" name="URL_SUCCESS" value="<?php echo base64_encode($successurl); ?>" />
                                    <input type="hidden" id="<?php echo "URL_FAILURE"; ?>" name="URL_FAILURE" value="<?php echo base64_encode($failureurl); ?>" />
                                    <input type="hidden" id="status" name="status" value="" />
                                    <input type="hidden" id="companyid" name="companyid" value="" />
                                    <input type="hidden" id="work_company_id" name="work_company_id" value="" />
                                    <input type="hidden" name="location" id="location" value="<?php echo $tab; ?>" />
                                    <div class="tab-pane active" id="general" role="tabpanel">
                                        <?php if($tab == 'overview'){ ?>
                                            @include('dataentry.index_survivor')
                                        <?php } ?>
                                    </div>
                                    <div class="tab-pane active" id="incidents" role="tabpanel">
                                        <?php if($tab == 'incidents'){ ?>
                                            @include('dataentry.index_incidents')
                                        <?php } ?>
                                    </div>
                                    <div class="tab-pane active" id="perpetrator" role="tabpanel">
                                        <?php if($tab == 'perpetrator'){ ?>
                                            @include('dataentry.index_perpetrator')
                                        <?php } ?>
                                    </div>
                                    <div class="tab-pane active" id="witness" role="tabpanel">
                                        <?php if($tab == 'witness'){ ?>
                                            @include('dataentry.index_witness')
                                        <?php } ?>
                                    </div>
                                    <div class="tab-pane active" id="action" role="tabpanel">
                                        <?php if($tab == 'action'){ ?>
                                            @include('dataentry.index_action')
                                        <?php } ?>
                                    </div>
                                    <div class="tab-pane active" id="status" role="tabpanel">
                                        <?php if($tab == 'status'){ ?>
                                            @include('dataentry.index_status')
                                        <?php } ?>
                                    </div>
                                    <hr>
                                    <div class="text-md-right testborder xhide">
                                        @if($tab != "overview" && $tab != "status" && $tab != "incidents")
                                            <a class="btn btn-warning" href="<?= $skipurl ?>" type="button"> <?= $skipbutton ?></a>
                                        @endif
                                        <button class="btn btn-primary btn-green changesuccessurl savebutton savecontinue" type="submit" successurl="" pretext="Save and Continue"><?= $buttontext ?></button>
                                    </div>
                                </form>
                            </div>
                        </div>

                </div>
                <!-- Page-header end -->
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script src="{{ asset('/assets/js/ngbvd-dataentry-final-fix.js') }}?v=20260604e"></script>
<script src="{{ asset('/assets/js/ngbvd-repeatable-sections.js') }}?v=20260604e"></script>
@endpush

@endsection
