<?php echo $this->extend('template/admin/main') ?>

<?php echo $this->section('content') ?>
<?php echo $this->include('common/message') ?>

<style>
    .trip-group {
        display: flex;
        align-items: center;
        font-weight: bold;
        padding: 8px;
        background-color: #f8f9fa;
        border-radius: 5px;
        margin-bottom: 5px;
    }

    .subtrip-container {
        margin-left: 20px;
        padding-left: 10px;
        border-left: 2px solid #ccc;
        max-height: 150px;
        overflow-y: auto;
    }

    #tripContainer {
        max-height: 300px;
        overflow-y: auto;
        padding: 10px;
        border: 1px solid #ddd;
        border-radius: 5px;
        background-color: #fff;
    }

    input[type="checkbox"] {
        margin-right: 8px;
        transform: scale(1.2);
        cursor: pointer;
    }
</style>

<?php
$selectedSubTripsJson = '[]';
if (!empty($selectedSubTrips)) {
    if (is_array($selectedSubTrips)) {
        $selectedSubTripsJson = json_encode(array_map('strval', $selectedSubTrips));
    } elseif (is_string($selectedSubTrips)) {
        $decoded = json_decode($selectedSubTrips, true);
        $selectedSubTripsJson = json_encode(is_array($decoded) ? array_map('strval', $decoded) : []);
    }
}

$ticketType = $ticketType ?? 'normal';
$colspanFooter = ($role_id == 1) ? 8 : 7;
?>

<div class="card mb-4">
    <div class="card-body">
        <div class="sp3 mb-5">
            <div class="card">
                <div class="card-body">
                    <form action="<?php echo base_url(route_to('datatickesell-report')) ?>" id="ticketsoldform" method="post">
                        <?php echo $this->include('common/security') ?>
                        <input type="hidden" id="selectedSubTrips" value='<?php echo esc($selectedSubTripsJson, 'attr'); ?>'>

                        <div class="row mb-3">
                            <?php if ($role_id == 1) : ?>
                                <div class="col-12 col-md-6 col-lg-3">
                                    <label for="report_company_id" class="form-label"><?php echo lang("Localize.company") ?></label>
                                    <select id="report_company_id" name="company_id" class="form-select">
                                        <option <?php if (isset($s_company_id) && $s_company_id == "all") { echo "selected"; } ?> value="all"><?php echo strtoupper(lang("Localize.all")) ?></option>
                                        <?php foreach ($companies as $company) : ?>
                                            <?php $select = (isset($s_company_id) && $s_company_id == $company->id) ? "selected" : ""; ?>
                                            <option <?php echo $select; ?> value="<?php echo $company->id; ?>">
                                                <?php echo $company->name; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            <?php else : ?>
                                <input type="hidden" id="report_company_id" name="company_id" value="<?php echo esc($logged_company_id ?? session()->get('company_id')); ?>">
                            <?php endif; ?>

                            <div class="col-12 col-md-6 col-lg-3">
                                <label for="type" class="form-label"><?php echo lang("Localize.ticket") ?> <?php echo lang("Localize.type") ?></label>
                                <select id="type" name="type" class="form-select">
                                    <option value="normal" <?php echo $ticketType === 'normal' ? 'selected' : ''; ?>>Normal</option>
                                    <option value="refund" <?php echo $ticketType === 'refund' ? 'selected' : ''; ?>><?php echo lang("Localize.refund") ?></option>
                                    <option value="cancel" <?php echo $ticketType === 'cancel' ? 'selected' : ''; ?>><?php echo lang("Localize.cancel") ?></option>
                                </select>
                            </div>

                            <div class="col-12 col-md-6 col-lg-3">
                                <label for="start_date" class="form-label"><?php echo lang("Localize.from") ?></label>
                                <div class="input-append date datepicker" id="start_date" data-date-format="yyyy-mm-dd">
                                    <input size="16" type="text" name="start_date" class="form-control" value="<?php if (isset($s_startdate) && $s_startdate != "") { echo date("Y-m-d", strtotime($s_startdate)); } else { echo date('Y-m-01'); } ?>" required readonly>
                                    <span class="add-on"><i class="icon-th"></i></span>
                                </div>
                            </div>

                            <div class="col-12 col-md-6 col-lg-3">
                                <label for="end_date" class="form-label"><?php echo lang("Localize.to") ?></label>
                                <div class="input-append date datepicker" id="end_date" data-date-format="yyyy-mm-dd">
                                    <input size="16" type="text" name="end_date" class="form-control" value="<?php if (isset($s_enddate) && $s_enddate != "") { echo date("Y-m-d", strtotime($s_enddate)); } else { echo date('Y-m-d'); } ?>" required readonly>
                                    <span class="add-on"><i class="icon-th"></i></span>
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-12">
                                <label><?php echo lang("Localize.trip") ?></label>
                                <div id="tripContainer"></div>
                            </div>
                        </div>

                        <button type="submit" class="btn btn-success"><?php echo lang("Localize.submit") ?></button>
                    </form>
                </div>
            </div>
        </div>

        <div class="table-responsive mt-3">
            <table class="table display table-bordered basic" id="ticketsoldreport">
                <thead>
                    <tr>
                        <th scope="col">#</th>
                        <?php if ($role_id == 1) : ?>
                            <th scope="col"><?php echo lang("Localize.company") ?></th>
                        <?php endif; ?>
                        <th scope="col"><?php echo lang("Localize.booking") ?> <?php echo lang("Localize.id") ?></th>
                        <th scope="col"><?php echo lang("Localize.booking") ?> <?php echo lang("Localize.date") ?></th>
                        <th scope="col"><?php echo lang("Localize.journey") ?> <?php echo lang("Localize.date") ?></th>
                        <th scope="col"><?php echo lang("Localize.main") ?> <?php echo lang("Localize.trip") ?></th>
                        <th scope="col"><?php echo lang("Localize.sub") ?> <?php echo lang("Localize.trip") ?></th>
                        <th scope="col"><?php echo lang("Localize.total") ?> <?php echo lang("Localize.seat") ?></th>
                        <th scope="col"><?php echo lang("Localize.price") ?></th>
                        <th scope="col"><?php echo lang("Localize.discount") ?></th>
                        <th scope="col"><?php echo lang("Localize.tax") ?></th>
                        <th scope="col"><?php echo lang("Localize.paid") ?> <?php echo lang("Localize.amount") ?></th>
                        <th scope="col"><?php echo lang("Localize.book") ?> <?php echo lang("Localize.by") ?></th>
                        <th scope="col"><?php echo lang("Localize.agent") ?></th>
                        <th scope="col"><?php echo lang("Localize.payment") ?> <?php echo lang("Localize.status") ?></th>
                        <th scope="col">
                            <?php if ($ticketType === 'refund') : ?>
                                <?php echo lang("Localize.refund") ?> <?php echo lang("Localize.detail") ?>
                            <?php elseif ($ticketType === 'cancel') : ?>
                                <?php echo lang("Localize.cancel") ?> <?php echo lang("Localize.detail") ?>
                            <?php else : ?>
                                <?php echo lang("Localize.details") ?>
                            <?php endif; ?>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($ticket as $kye => $ticketvalue) : ?>
                        <tr>
                            <th scope="row"><?php echo $kye + 1; ?></th>
                            <?php if ($role_id == 1) : ?>
                                <td><?php echo esc($ticketvalue->company_name ?? '-'); ?></td>
                            <?php endif; ?>
                            <td><?php echo esc($ticketvalue->booking_id); ?></td>
                            <td><?php echo !empty($ticketvalue->date) ? date('Y-m-d', strtotime($ticketvalue->date)) : '-'; ?></td>
                            <td><?php echo !empty($ticketvalue->journeydata) ? date('Y-m-d', strtotime($ticketvalue->journeydata)) : '-'; ?></td>
                            <td>
                                <?php echo esc($ticketvalue->pickup_location_name ?? ''); ?> - <?php echo esc($ticketvalue->drop_location_name ?? ''); ?>
                                <?php if (!empty($ticketvalue->start_time) || !empty($ticketvalue->end_time)) : ?>
                                    (<?php echo esc($ticketvalue->start_time ?? ''); ?> - <?php echo esc($ticketvalue->end_time ?? ''); ?>)
                                <?php endif; ?>
                            </td>
                            <td><?php echo esc($ticketvalue->sub_pickup_location_name ?? ''); ?> - <?php echo esc($ticketvalue->sub_drop_location_name ?? ''); ?></td>
                            <td class="text-end"><?php echo esc($ticketvalue->totalseat ?? 0); ?></td>
                            <td class="text-end"><?php echo esc($ticketvalue->price ?? 0); ?></td>
                            <td class="text-end"><?php echo esc($ticketvalue->discount ?? 0); ?></td>
                            <td class="text-end"><?php echo esc($ticketvalue->totaltax ?? 0); ?></td>
                            <td class="text-end"><?php echo esc($ticketvalue->paidamount ?? 0); ?></td>
                            <td><?php echo esc(trim(($ticketvalue->booked_by_f_name ?? '') . ' ' . ($ticketvalue->booked_by_l_name ?? ''))); ?></td>
                            <td><?php echo esc(trim(($ticketvalue->agent_fname ?? '') . ' ' . ($ticketvalue->agent_lname ?? ''))); ?></td>
                            <td><?php echo esc($ticketvalue->payment_status ?? '-'); ?></td>
                            <td>
                                <?php if ($ticketType === 'refund') : ?>
                                    <?php echo !empty($ticketvalue->refund_date) ? date('Y-m-d', strtotime($ticketvalue->refund_date)) : '-'; ?>
                                    / <?php echo esc($ticketvalue->refund_cancel_fee ?? 0); ?>
                                    <?php if (!empty($ticketvalue->refund_details)) : ?>
                                        / <?php echo esc($ticketvalue->refund_details); ?>
                                    <?php endif; ?>
                                <?php elseif ($ticketType === 'cancel') : ?>
                                    <?php echo !empty($ticketvalue->cancel_date) ? date('Y-m-d', strtotime($ticketvalue->cancel_date)) : '-'; ?>
                                    / <?php echo esc($ticketvalue->refund_cancel_fee ?? 0); ?>
                                <?php else : ?>
                                    -
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach ?>
                </tbody>
                <?php if (!empty($ticket)) : ?>
                    <tfoot>
                        <tr>
                            <th scope="row" class="text-end" colspan="<?php echo $colspanFooter; ?>"><?php echo lang("Localize.total") ?></th>
                            <th class="text-end"><?php echo array_sum(array_map(static fn($row) => (float) ($row->totalseat ?? 0), $ticket)); ?></th>
                            <th class="text-end"><?php echo $currency_symbol; ?> <?php echo array_sum(array_map(static fn($row) => (float) ($row->price ?? 0), $ticket)); ?></th>
                            <th class="text-end"><?php echo $currency_symbol; ?> <?php echo array_sum(array_map(static fn($row) => (float) ($row->discount ?? 0), $ticket)); ?></th>
                            <th class="text-end"><?php echo $currency_symbol; ?> <?php echo array_sum(array_map(static fn($row) => (float) ($row->totaltax ?? 0), $ticket)); ?></th>
                            <th class="text-end"><?php echo $currency_symbol; ?> <?php echo array_sum(array_map(static fn($row) => (float) ($row->paidamount ?? 0), $ticket)); ?></th>
                            <th colspan="3"></th>
                        </tr>
                    </tfoot>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

<?php echo $this->include('common/datatable_default_lang_change') ?>
<?php echo $this->endSection() ?>
