<?php
/* @package Joomla
 * @copyright Copyright (C) Open Source Matters. All rights reserved.
 * @license   http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * @extension Phoca Extension
 * @copyright Copyright (C) Jan Pavelka www.phoca.cz
 * @license   http://www.gnu.org/copyleft/gpl.html GNU/GPL
 */
defined('_JEXEC') or die();

// ASSOCIATION
JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');


$app   = JFactory::getApplication();
$input = $app->input;
$r     = $this->r;


// phocacartitem-form ==> adminForm
$js = '
var phRequestActive = null;

function phCheckRequestStatus(i, task) {
    i++;
    if (i > 30) {
        /* Stop Loop */
        phRequestActive = null;
    }

    if (phRequestActive) {
        setTimeout(function(){
            phCheckRequestStatus(i, task);
        }, 1000);
    } else {
        if (task == "' . $this->t['task'] . '.cancel" || task == "phocacartwizard.backtowizard" || document.formvalidator.isValid(document.getElementById("adminForm"))) {
            Joomla.submitform(task, document.getElementById("adminForm"));

            /* Close Modal */
            if (task !== "phocacartitem.apply") {
                window.parent.jQuery("#phocacartitemEdit' . $this->item->id . 'Modal").modal("hide");
            }
        }
        else {
            Joomla.renderMessages({error: ["' . JText::_('JGLOBAL_VALIDATION_FORM_FAILED', true) . '"]});
        }
    }
}

Joomla.submitbutton = function(task) {
    phCheckRequestStatus(0, task);
}
';
JFactory::getDocument()->addScriptDeclaration($js);

// ASSOCIATION
$assoc = JLanguageAssociations::isEnabled();
// In case of modal
$isModal = $input->get('layout') == 'modal' ? true : false;
$layout  = $isModal ? 'modal' : 'edit';
$tmpl    = $isModal || $input->get('tmpl', '', 'cmd') === 'component' ? 'component' : '';

// Fieldsets to not automatically render by /layouts/joomla/edit/params.php
$this->ignore_fieldsets = array('details', 'item_associations', 'jmetadata');

echo $r->startForm($this->t['o'], $this->t['task'], (int)$this->item->id, 'adminForm', 'adminForm', '', $layout, $tmpl);
// First Column
echo '<div class="span12 form-horizontal">';
$tabs                   = array();
$tabs['general']        = JText::_($this->t['l'] . '_GENERAL_OPTIONS');
$tabs['image']          = JText::_($this->t['l'] . '_IMAGE_OPTIONS');
$tabs['attributes']     = JText::_($this->t['l'] . '_ATTRIBUTES');
$tabs['specifications'] = JText::_($this->t['l'] . '_SPECIFICATIONS');
$tabs['related']        = JText::_($this->t['l'] . '_RELATED_PRODUCTS');
$tabs['stock']          = JText::_($this->t['l'] . '_STOCK_OPTIONS');
$tabs['discount']       = JText::_($this->t['l'] . '_DISCOUNT_OPTIONS');
$tabs['download']       = JText::_($this->t['l'] . '_DOWNLOAD_OPTIONS');
$tabs['size']           = JText::_($this->t['l'] . '_SIZE_OPTIONS');
$tabs['reward']         = JText::_($this->t['l'] . '_REWARD_POINTS');
$tabs['publishing']     = JText::_($this->t['l'] . '_PUBLISHING_OPTIONS');
$tabs['feed']           = JText::_($this->t['l'] . '_FEED_OPTIONS');
$tabs['metadata']       = JText::_($this->t['l'] . '_METADATA_OPTIONS');
if (!$isModal && $assoc) {
    $tabs['associations'] = JText::_($this->t['l'] . '_ASSOCIATIONS');
}

echo $r->navigation($tabs);

echo $r->startTabs();

echo $r->startTab('general', $tabs['general'], 'active');


// Customer Group Price
$idMd       = 'phEditProductPriceGroupModal';
$textButton = 'COM_PHOCACART_CUSTOMER_GROUP_PRICES';
$w          = 500;
$h          = 400;

echo '<div class="ph-float-right ph-admin-additional-box">';


if ($this->item->image != '') {
    $pathImage = PhocacartPath::getPath('productimage');
    $image     = PhocacartImage::getThumbnailName($pathImage, $this->item->image, 'small');
    echo '<div class="ph-admin-additional-box-img-box"><img src="' . Juri::root() . $image->rel . '" alt="" /></div><hr />';
}

$linkStatus = JRoute::_('index.php?option=' . $this->t['o'] . '&view=phocacarteditproductpricegroup&tmpl=component&id=' . (int)$this->item->id);
echo '<a href="#' . $idMd . '" role="button" class="ph-u ' . $idMd . 'ModalButton" data-toggle="modal" title="' . JText::_($textButton) . '" data-src="' . $linkStatus . '" data-height="' . $h . '" data-width="' . $w . '">' . JText::_($textButton) . '</a>';
echo $r->modalWindowDynamic($idMd, $textButton, $w, $h, false);


// Product Price History

$idMd       = 'phEditProductPriceHistoryModal';
$textButton = 'COM_PHOCACART_PRODUCT_PRICE_HISTORY';
$w          = 500;
$h          = 400;

$linkStatus = JRoute::_('index.php?option=' . $this->t['o'] . '&view=phocacarteditproductpricehistory&tmpl=component&id=' . (int)$this->item->id);
echo '<br /><a href="#' . $idMd . '" role="button" class="ph-u ' . $idMd . 'ModalButton" data-toggle="modal" title="' . JText::_($textButton) . '" data-src="' . $linkStatus . '" data-height="' . $h . '" data-width="' . $w . '">' . JText::_($textButton) . '</a>';
echo $r->modalWindowDynamic($idMd, $textButton, $w, $h, false);


// Preview
if ((int)$this->item->id > 0) {

    $catidA = PhocacartCategoryMultiple::getCategories((int)$this->item->id, 1);
    if (isset($catidA[0]) && $catidA[0] > 0) {
        $idPr       = 'phEditProductPreview';
        $textButton = 'COM_PHOCACART_PRODUCT_PREVIEW';
        $w          = 500;
        $h          = 400;

        $linkPreview = PhocacartRoute::getItemRoute((int)$this->item->id, (int)$catidA[0], '', '', array('pl-PL'), 1) /* . '&tmpl=component'*/
        ;

        $linkPreview = PhocacartPath::getRightPathLink($linkPreview);


        echo '<br /><a href="#' . $idPr . '" role="button" class="ph-u ' . $idPr . 'ModalButton" data-toggle="modal" title="' . JText::_($textButton) . '" data-src="' . $linkPreview . '" data-height="' . $h . '" data-width="' . $w . '">' . JText::_($textButton) . '</a>';

        $footer = '<span class="ph-warning-modal-window">' . JText::_('COM_PHOCACART_YOU_ARE_PREVIEWING_LIVE_PAGE') . '</span><button type="button" class="btn" data-dismiss="modal" aria-hidden="true">' . JText::_('COM_PHOCACART_CLOSE') . '</button>';
        echo $r->modalWindowDynamic($idPr, $textButton, $w, $h, false, 0, '', '', $footer);

    }
}

echo '</div>';

// ORDERING cannot be used
$formArray = array('title', 'alias', 'price', 'price_original', 'tax_id', 'catid_multiple', 'manufacturer_id', 'sku', 'upc', 'ean', 'jan', 'mpn', 'isbn', 'serial_number', 'registration_key', 'external_id', 'external_key', 'external_link', 'external_text', 'external_link2', 'external_text2', 'access', 'group', 'featured', 'featured_background_image', 'video', 'public_download_file', 'public_download_text', 'public_play_file', 'public_play_text', 'condition', 'type_feed', 'type_category_feed');
echo $r->group($this->form, $formArray);
$formArray = array('description');
echo $r->group($this->form, $formArray, 1);
$formArray = array('description_long');
echo $r->group($this->form, $formArray, 1);
$formArray = array('features');
echo $r->group($this->form, $formArray, 1);
//$formArray = array ('upc', 'ean', 'jan', 'mpn', 'isbn');
//echo $r->group($this->form, $formArray);


// ASSOCIATION
$this->form->setFieldAttribute('id', 'type', 'hidden');
$formArray = array('id');
echo $r->group($this->form, $formArray);

echo $r->endTab();



// IMAGES
echo $r->startTab('image', $tabs['image']);


$formArray = array('image');
echo $r->group($this->form, $formArray);
echo '<h3>' . JText::_($this->t['l'] . '_ADDITIONAL_IMAGES') . '</h3>';

$formArray = array('additional_images');// , 'download_hits' - it is counted in orders
echo $r->group($this->form, $formArray);

echo $r->endTab();


// ATTRIBUTES, OPTIONS
/*
$pathAttributes = PhocacartPath::getPath('attributefile');
$w = 700;
$h = 400;
$urlO 	= 'index.php?option=com_phocacart&amp;view=phocacartmanager&amp;tmpl=component&amp;manager=productimage&amp;field=jform_optionimage';
$urlO2 	= 'index.php?option=com_phocacart&amp;view=phocacartmanager&amp;tmpl=component&amp;manager=productimage&amp;field=jform_optionimage_medium';
$urlO3 	= 'index.php?option=com_phocacart&amp;view=phocacartmanager&amp;tmpl=component&amp;manager=productimage&amp;field=jform_optionimage_small';
$urlO4 	= 'index.php?option=com_phocacart&amp;view=phocacartmanager&amp;tmpl=component&amp;manager=attributefile&amp;field=jform_optiondownload_file';


*/
echo $r->startTab('attributes', $tabs['attributes']);
echo '<h3>' . JText::_($this->t['l'] . '_ATTRIBUTES') . '</h3>';
$formArray = array('attributes');
echo $r->group($this->form, $formArray);
echo $r->endTab();


// SPECIFICATIONS
/*$w = 700;
$h = 400;
$urlO 	= 'index.php?option=com_phocacart&amp;view=phocacartmanager&amp;tmpl=component&amp;manager=productimage&amp;field=jform_specimage';
$urlO2 	= 'index.php?option=com_phocacart&amp;view=phocacartmanager&amp;tmpl=component&amp;manager=productimage&amp;field=jform_specimage_medium';
$urlO3 	= 'index.php?option=com_phocacart&amp;view=phocacartmanager&amp;tmpl=component&amp;manager=productimage&amp;field=jform_specimage_small';
*/
echo $r->startTab('specifications', $tabs['specifications']);
echo '<h3>' . JText::_($this->t['l'] . '_SPECIFICATIONS') . '</h3>';
$formArray = array('specifications');
echo $r->group($this->form, $formArray);
echo $r->endTab();


// RELATED
echo $r->startTab('related', $tabs['related']);
$formArray = array('related');
echo $r->group($this->form, $formArray);
echo $r->endTab();


// STOCK
echo $r->startTab('stock', $tabs['stock']);

$idMd       = 'phEditStockAdvancedModal';
$textButton = 'COM_PHOCACART_ADVANCED_STOCK_OPTIONS';
$w          = 500;
$h          = 400;

$linkStatus = JRoute::_('index.php?option=' . $this->t['o'] . '&view=phocacarteditstockadvanced&tmpl=component&id=' . (int)$this->item->id);
echo '<div class="ph-float-right ph-admin-additional-box"><a href="#' . $idMd . '" role="button" class="ph-u ' . $idMd . 'ModalButton" data-toggle="modal" title="' . JText::_($textButton) . '" data-src="' . $linkStatus . '" data-height="' . $h . '" data-width="' . $w . '">' . JText::_($textButton) . '</a>';
echo $r->modalWindowDynamic($idMd, $textButton, $w, $h, false);

echo '</div>';

$formArray = array('stock', 'stock_calculation', 'min_quantity', 'min_multiple_quantity', 'min_quantity_calculation', 'stockstatus_a_id', 'stockstatus_n_id', 'delivery_date');
echo $r->group($this->form, $formArray);
echo $r->endTab();


// PRODUCT DISCOUNTS
echo $r->startTab('discount', $tabs['discount']);
echo '<h3>' . JText::_($this->t['l'] . '_PRODUCT_DISCOUNT') . '</h3>';
$formArray = array('discounts');
echo $r->group($this->form, $formArray);
echo $r->endTab();


// DOWNLOAD
echo $r->startTab('download', $tabs['download']);
$formArray = array('download_folder', 'download_token', 'download_file', 'download_days');// , 'download_hits' - it is counted in orders
echo $r->group($this->form, $formArray);

echo '<h3>' . JText::_($this->t['l'] . '_ADDITIONAL_DOWNLOAD_FILES') . '</h3>';
$formArray = array('additional_download_files');// , 'download_hits' - it is counted in orders
echo $r->group($this->form, $formArray);
echo $r->endTab();


// SIZE
echo $r->startTab('size', $tabs['size']);
$formArray = array('length', 'width', 'height', 'weight', 'volume', 'unit_amount', 'unit_unit',);
echo $r->group($this->form, $formArray);
echo $r->endTab();

// REWARD POINTS
echo $r->startTab('reward', $tabs['reward']);

$idMd       = 'phEditProductPointGroupModal';
$textButton = 'COM_PHOCACART_CUSTOMER_GROUP_RECEIVED_POINTS';
$w          = 500;
$h          = 400;

$linkStatus = JRoute::_('index.php?option=' . $this->t['o'] . '&view=phocacarteditproductpointgroup&tmpl=component&id=' . (int)$this->item->id);
echo '<div class="ph-float-right ph-admin-additional-box"><a href="#' . $idMd . '" role="button" class="ph-u ' . $idMd . 'ModalButton" data-toggle="modal" title="' . JText::_($textButton) . '" data-src="' . $linkStatus . '" data-height="' . $h . '" data-width="' . $w . '">' . JText::_($textButton) . '</a>';

echo $r->modalWindowDynamic($idMd, $textButton, $w, $h, false);
echo '</div>';


$formArray = array('points_needed', 'points_received');
echo $r->group($this->form, $formArray);
echo $r->endTab();



// PUBLISHING
echo $r->startTab('publishing', $tabs['publishing']);
foreach ($this->form->getFieldset('publish') as $field) {

    echo '<div class="control-group">';
    if (!$field->hidden) {
        echo '<div class="control-label">' . $field->label . '</div>';
    }
    echo '<div class="controls">';
    echo $field->input;
    echo '</div></div>';
}

echo $this->loadTemplate('parameter');
echo $r->endTab();


// FEED
echo $r->startTab('feed', $tabs['feed']);
echo $this->loadTemplate('feed');
echo $r->endTab();


// METADATA
echo $r->startTab('metadata', $tabs['metadata']);
echo $this->loadTemplate('metadata');
echo $r->endTab();


// ASSOCIATION
$assoc = JLanguageAssociations::isEnabled();

if (!$isModal && $assoc) {
    echo $r->startTab('associations', $tabs['associations']);
    echo $this->loadTemplate('associations');
    echo $r->endTab();
} else if ($isModal && $assoc) {
    echo '<div class="hidden">' . $this->loadTemplate('associations') . '</div>';
}


echo $r->endTabs();
echo '</div>';//end span10
// Second Column
//echo '<div class="col-xs-12 col-sm-2 col-md-2">';


//echo '</div>';//end span2
echo $r->formInputs($this->t['task']);

if ($forcedLanguage = JFactory::getApplication()->input->get('forcedLanguage', '', 'CMD')) {
    echo '<input type="hidden" name="forcedLanguage" value="' . $forcedLanguage . '" />';
}
echo $r->endForm();


?>

