<?php
$toolbarFilterSchema = is_array($toolbarFilterSchema ?? null) ? $toolbarFilterSchema : [];
$searchToolbarFilterSchema = is_array($searchToolbarFilterSchema ?? null) ? $searchToolbarFilterSchema : [];
$drawerToolbarFilterSchema = is_array($drawerToolbarFilterSchema ?? null) ? $drawerToolbarFilterSchema : [];
$toolbarFilterState = is_array($toolbarFilterState ?? null) ? $toolbarFilterState : [];
$toolbarOptionSets = is_array($toolbarOptionSets ?? null) ? $toolbarOptionSets : [];
$clientFilterSchema = is_array($clientFilterSchema ?? null) ? $clientFilterSchema : [];
$searchConfig = is_array($searchConfig ?? null) ? $searchConfig : null;
$filterChipMeta = is_array($filterChipMeta ?? null) ? $filterChipMeta : [];
$canManage = $canManage ?? false;
?>
<?php
$listTitle = t('Updates');
ob_start();
?>
<button type="button" class="secondary outline" id="updates-refresh-button" data-tooltip="<?php e(t('Refresh data')); ?>" data-tooltip-pos="left">
  <i class="bi bi-arrow-clockwise"></i> <?php e(t('Refresh data')); ?>
</button>
<?php
$listTitleActionsHtml = ob_get_clean();
require templatePath('partials/app-list-titlebar.phtml');
?>
<?php
$filterUiNamespace = 'helpdesk-updates';
require templatePath('partials/app-list-filters.phtml');
?>
<div class="app-list-table">
  <div id="helpdesk-updates-grid"></div>
</div>

<?php if ($canManage): ?>
<dialog
  id="update-assign-dialog"
  aria-labelledby="update-assign-dialog-title"
>
  <article class="app-dialog-md">
    <header>
      <h2 id="update-assign-dialog-title"><?php e(t('Assign update')); ?></h2>
      <button type="button" class="close" data-update-assign-close aria-label="<?php e(t('Close')); ?>" title="<?php e(t('Close')); ?>"></button>
    </header>
    <form id="update-assign-form" method="post">
      <p class="app-muted" id="update-assign-context"></p>

      <label for="update-assign-domain"><?php e(t('Domain')); ?></label>
      <select id="update-assign-domain" name="domain_no" required>
        <option value=""><?php e(t('Loading domains...')); ?></option>
      </select>

      <label for="update-assign-gitea"><?php e(t('Gitea link')); ?></label>
      <input type="url" id="update-assign-gitea" name="gitea_path" placeholder="<?php e(t('e.g. https://gitea.example.com/org/repo/src/branch/main/updates/2026-02')); ?>">

      <label for="update-assign-notes"><?php e(t('Notes')); ?></label>
      <textarea id="update-assign-notes" name="notes" rows="3"></textarea>

      <input type="hidden" name="ticket_no" id="update-assign-ticket-no">
      <input type="hidden" name="debitor_no" id="update-assign-debitor-no">
      <input type="hidden" name="debitor_name" id="update-assign-debitor-name">
      <input type="hidden" name="category_code" id="update-assign-category-code">
      <input type="hidden" name="domain_url" id="update-assign-domain-url">

      <footer>
        <button type="button" class="secondary outline" data-update-assign-close><?php e(t('Cancel')); ?></button>
        <button type="submit" class="primary" id="update-assign-submit"><?php e(t('Save')); ?></button>
      </footer>
    </form>
  </article>
</dialog>
<?php endif; ?>

<script src="<?php e(assetVersion('vendor/gridjs/gridjs.umd.js')); ?>"></script>
<script type="application/json" id="page-config-helpdesk-updates"><?php gridJsonForJs([
    'dataUrl' => lurl('helpdesk/updates-data'),
    'assignUrl' => $canManage ? lurl('helpdesk/updates/assign') : '',
    'domainsDataUrl' => $canManage ? lurl('helpdesk/update-domains-data') : '',
    'gridLang' => gridLang(),
    'gridSearch' => $searchConfig,
    'filterSchema' => $clientFilterSchema,
    'filterChipMeta' => $filterChipMeta,
    'canManage' => $canManage,
    'csrfKey' => $csrfKey ?? '',
    'csrfToken' => $csrfToken ?? '',
    'labels' => [
        'ticketNo' => t('Ticket'),
        'ticketDescription' => t('Description'),
        'category' => t('Type'),
        'debitor' => t('Customer'),
        'domain' => t('Domain'),
        'giteaPath' => t('Gitea link'),
        'createdOn' => t('Created'),
        'ticketState' => t('Ticket status'),
        'assignment' => t('Assignment'),
        'actions' => t('Actions'),
        'assign' => t('Assign'),
        'edit' => t('Edit'),
        'noUpdates' => t('No update tickets found'),
        'loadError' => t('Failed to load update tickets'),
        'assignSuccess' => t('Update assigned successfully'),
        'assignError' => t('Failed to assign update'),
        'giteaLink' => t('Open in Gitea'),
        'selectDomain' => t('Select domain...'),
        'loadingDomains' => t('Loading domains...'),
        'noDomains' => t('No domains found for this customer'),
        'update' => t('Update'),
        'hotfix' => t('Hotfix'),
        'unassigned' => t('Unassigned'),
        'assigned' => t('Assigned'),
        'completed' => t('Completed'),
        'all' => t('All'),
        'filterStatus' => t('Status'),
        'filterType' => t('Type'),
    ],
]); ?></script>
<script type="module" src="<?php e(assetVersion('modules/helpdesk/js/pages/helpdesk-updates-index.js')); ?>"></script>
