<?php
    /**
     * @version    CVS: 1.0.0
     * @package    Com_Dvtr_employees
     * @author     Pawel Potoniec <pawel@devatri.pl>
     * @copyright  2025 Pawel Potoniec
     */
    // No direct access
    defined( '_JEXEC' ) or die;

    use Joomla\CMS\HTML\HTMLHelper;
    use Joomla\CMS\Factory;
    use Joomla\CMS\Uri\Uri;
    use Joomla\CMS\Router\Route;
    use Joomla\CMS\Language\Text;
    use Joomla\CMS\Layout\LayoutHelper;
    use Joomla\CMS\Session\Session;
    use Joomla\CMS\User\UserFactoryInterface;

    HTMLHelper::_( 'bootstrap.tooltip' );
    HTMLHelper::_( 'behavior.multiselect' );
    HTMLHelper::_( 'formbehavior.chosen', 'select' );

    $user       = Factory::getApplication()->getIdentity();
    $userId     = $user->get( 'id' );
    $listOrder  = $this->state->get( 'list.ordering' );
    $listDirn   = $this->state->get( 'list.direction' );
    $canCreate  = $user->authorise( 'core.create', 'com_dvtr_employees' ) && file_exists( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'forms' . DIRECTORY_SEPARATOR . 'employeeform.xml' );
    $canEdit    = $user->authorise( 'core.edit', 'com_dvtr_employees' ) && file_exists( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'forms' . DIRECTORY_SEPARATOR . 'employeeform.xml' );
    $canCheckin = $user->authorise( 'core.manage', 'com_dvtr_employees' );
    $canChange  = $user->authorise( 'core.edit.state', 'com_dvtr_employees' );
    $canDelete  = $user->authorise( 'core.delete', 'com_dvtr_employees' );

    $wa = $this->document->getWebAssetManager();
    $wa->useStyle( 'com_dvtr_employees.list' );

    foreach ( $this->items as $item ) :

        $item->introtext = $item->description ?? '';
        $item->title     = $item->name ?? 'Pracownik';
        $item->created   = $item->created ?? date( 'Y-m-d H:i:s' );

        echo LayoutHelper::render( 'joomla.content.article', [
            'item'   => $item,
            'params' => $this->params,
        ] );
    endforeach;
?>

<?php if ( $this->params->get( 'show_page_heading' ) ) : ?>
    <div class="page-header">
        <h1> <?php echo $this->escape( $this->params->get( 'page_heading' ) ); ?> </h1>
    </div>
<?php endif; ?>
<form action="<?php echo htmlspecialchars( Uri::getInstance()->toString() ); ?>" method="post"
      name="adminForm" id="adminForm">

    <div class="row">

        <h1><?php echo Text::_('COM_DVTR_EMPLOYEES_TITLE_EMPLOYEES');?></h1>
        <?php foreach ( $this->items as $i => $item ) : ?>
            <?php $canEdit = $user->authorise( 'core.edit', 'com_dvtr_employees' ); ?>

            <div class="col-3 index-<?php echo $i % 3; ?>">

                <!--					<td>-->
                <!--						--><?php //echo $item->id; ?>
                <!--					</td>-->
                <div class="employee-avatar">
                    <a href="<?php echo Route::_( 'index.php?option=com_dvtr_employees&view=employee&id=' . (int) $item->id ); ?>">
                        <img src="<?php echo $item->avatar ? $item->avatar :  '/images/O%20instytucie/avatar.png'; ?>" alt="avatar"/>
                    </a>
                </div>

                <div class="employee-name">
                    <?php echo $item->name; ?>
                </div>
                <div class="social-media">
                    <?php if(!empty($item->social_linkedin)):?>
                        <a href="<?php echo $item->social_linkedin ?>">
<!--                            <i class="fa fa-linkedin"></i>-->

                            <img src="/media/com_quantummanager/images/icons/action/linkedin-logo.svg"/>
                        </a>
                    <?php endif;?>

                    <?php if(!empty($item->social_facebook)):?>
                        <a href="<?php echo $item->social_facebook ?>">
<!--                            <i class="fa fa-facebook"></i>-->
                            <img src="/media/com_quantummanager/images/icons/action/facebook-logo.svg"/>
                        </a>
                    <?php endif;?>

                    <?php if(!empty($item->social_instagram)):?>
                        <a href="<?php echo $item->social_instagram ?>">
<!--                            <i class="fa fa-facebook"></i>-->
                            <img src="/media/com_quantummanager/images/icons/action/instagram-logo.svg"/>
                        </a>
                    <?php endif;?>

                    <?php if(!empty($item->social_twitter)):?>
                        <a href="<?php echo $item->social_twitter ?>">
<!--                            <i class="fa fa-facebook"></i>-->
                            <img src="/media/com_quantummanager/images/icons/action/twitter-logo-on-black-background.svg"/>
                        </a>
                    <?php endif;?>
                </div>

            </div>
        <?php endforeach; ?>

    </div>

    <?php if ( $canCreate ) : ?>
        <a href="<?php echo Route::_( 'index.php?option=com_dvtr_employees&task=employeeform.edit&id=0', FALSE, 0 ); ?>"
           class="btn btn-success btn-small"><i
                    class="icon-plus"></i>
            <?php echo Text::_( 'COM_DVTR_EMPLOYEES_ADD_ITEM' ); ?></a>
    <?php endif; ?>

    <input type="hidden" name="task" value=""/>
    <input type="hidden" name="boxchecked" value="0"/>
    <input type="hidden" name="filter_order" value=""/>
    <input type="hidden" name="filter_order_Dir" value=""/>
    <?php echo HTMLHelper::_( 'form.token' ); ?>
</form>

<?php

    if ( $canDelete ) {
        $wa->addInlineScript( "
			jQuery(document).ready(function () {
				jQuery('.delete-button').click(deleteItem);
			});

			function deleteItem() {

				if (!confirm(\"" . Text::_( 'COM_DVTR_EMPLOYEES_DELETE_MESSAGE' ) . "\")) {
					return false;
				}
			}
		", [], [], [ "jquery" ] );
    }
?>
