<?php

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

$aColumns = [
    'p.name',
    'p.price',
    'prof.name as professional_name',
];
$sIndexColumn = 'p.id';
$sTable       = db_prefix() . 'products p';

$join = [
    'LEFT JOIN ' . db_prefix() . 'professionals prof ON prof.id = p.profissional_id',
];

$result  = data_tables_init($aColumns, $sIndexColumn, $sTable, $join, [], ['p.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 profissional
    $row[] = $aRow['professional_name'];

    // Ações
    $options  = '<div class="tw-flex tw-items-center tw-space-x-2">';
    $options .= '<a href="' . admin_url('products/product/' . $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('products/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;
}