<?php

defined('BASEPATH') or exit('No direct script access allowed');

$aColumns = [
    'gi.name',
    'gi.price',
    'ig.name as item_group_name',
    'p.name as professional_name',
    'gi.enable_builder',
];
$sIndexColumn = 'gi.id';
$sTable       = db_prefix() . 'group_items gi';

$join = [
    'LEFT JOIN ' . db_prefix() . 'items_groups ig ON ig.id = gi.item_group_id',
    'LEFT JOIN ' . db_prefix() . 'professionals p ON p.id = gi.professional_id',
];

$result  = data_tables_init($aColumns, $sIndexColumn, $sTable, $join, [], ['gi.id']);
$output  = $result['output'];
$rResult = $result['rResult'];

foreach ($rResult as $aRow) {
    $row = [];

    // Nome
    $row[] = $aRow['name'];

    // Preço (formate como precisar)
    $row[] = app_format_money($aRow['price'], get_base_currency()->symbol);

    // Nome do grupo
    $row[] = $aRow['item_group_name'];

    // Nome do profissional
    $row[] = $aRow['professional_name'];

    // Enable Builder
    $row[] = $aRow['enable_builder']
        ? '<span class="label label-success">' . _l('Sim') . '</span>'
        : '<span class="label label-default">' . _l('Não') . '</span>';

    // Ações
    $options  = '<div class="tw-flex tw-items-center tw-space-x-2">';
    $options .= '<a href="' . admin_url('group_items/group_item/' . $aRow['id']) . '" class="tw-text-neutral-500 hover:tw-text-neutral-700">
                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                 </a>';
    $options .= '<a href="' . admin_url('group_items/delete/' . $aRow['id']) . '" class="tw-text-neutral-500 hover:tw-text-neutral-700 _delete">
                    <i class="fa-regular fa-trash-can fa-lg"></i>
                 </a>';
    $options .= '</div>';

    $row[] = $options;

    $output['aaData'][] = $row;
}