<style type="text/css">
    .uploader {
        cursor: pointer;
    }

    .uploader img {
        max-height: 52px !important;
        max-width: 143px !important;
    }

    .uploaders {
        display: none;
    }
</style>

<?php

if ($form->isNew()) {
    $obj = $form->getObject();

    $value['direct_price'] = 0;

} else {
    $obj = $form->getObject();
}

$tags = Doctrine::getTable('Tag')->findAll();

?>

<div class="sf_admin_form_row sf_admin_text tabbed_div">
    <div>
        <span class="inline-label">Цены</span>
        <div class="content">
            <div style="display: inline-block;padding-left: 5px;min-width: 720px;">
                <label>
                    <i class="br5"></i>
                    <i class="br5"></i>
                    <table cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="55%">
                                Цена за одну единицу
                            </td>
                            <td width="20%">

                            </td width="25%">
                            <td colspan="2" style="width:1%; padding-left: 5px">
                                руб
                            </td>
                        </tr>
                    </table>
                </label>
                <i class="br15"></i>
                <div style="width: 100%; text-align: center"><i>или</i></div>
                <i class="br15"></i>
                Торговые предложения
                <i class="br5"></i>
                <div class="offer_group">
                    <?php

                    $offer_counter = 0;

                    $offers = $obj->getOffer();

                    if (count($offers) > 0) {
                        foreach ($offers as $offer) {
                            ?>

                            <div class="offer custom_offer">
                                <div class="custom_offer_closer" onclick="$(this).parent().remove()">×</div>
                                <table cellspacing="0" cellpadding="0" style="width: 100%">
                                    <tr>
                                        <td width="55%">
                                            <input style="width: 100%" maxlength="255"
                                                   name="product_custom[custom_offer][<?php echo $offer->getId() ?>][title_ru]"
                                                   type="text"
                                                   value="<?php echo $offer->getTranslation()->ru->title ?>">
                                            <div class="input_label_overlay">RU</div>
                                            <i class="br5"></i>
                                            <input style="width: 100%" maxlength="255"
                                                   name="product_custom[custom_offer][<?php echo $offer->getId() ?>][title_en]"
                                                   type="text"
                                                   value="<?php echo $offer->getTranslation()->en->title ?>">
                                            <div class="input_label_overlay">EN</div>
                                        </td>
                                        <td width="20%">
                                            <input style="width: 100%; height: 53px" maxlength="255"
                                                   name="product_custom[custom_offer][<?php echo $offer->getId() ?>][price]"
                                                   type="number" value="<?php echo $offer->getPrice() ?>">
                                        </td>
                                        <td width="5%" style="padding-left: 5px">руб</td>
                                        <td width="20%">
                                            <input type="file" class="uploaders pseudo_button_file" name="file"
                                                   id="<?php echo $offer->getId() ?>__uploader"/>
                                            <div class="uploader upload_img_btn">
                                                <?php

                                                if ($offer['img']) {
                                                    echo '<img src="/u/i/' . $offer["img"] . '">';
                                                } else {
                                                    echo 'Загрузить изображение';
                                                }

                                                ?>
                                            </div>
                                            <input type="text" class="offerImg offerImg<?php echo $offer->getId() ?>"
                                                   name="product_custom[custom_offer][<?php echo $offer->getId() ?>][img]"
                                                   value="<?php echo $offer['img'] ?>">
                                        </td>
                                    </tr>
                                </table>
                            </div>

                            <?php

                            $offer_counter++;

                        }


                    }


                    ?>

                </div>
                <i class="br10"></i>
                <div class="green_btn" style="cursor: pointer" onclick="add_offer()">Добавить предложение</div>
            </div>
        </div>
    </div>
</div>

<i class="br10"></i>

<?php
include_partial('product/props', array('form' => $form))
?>

<script type="text/javascript">
    function add_offer() {
        var newId = 'new_offer_' + ($('.js_new_offer').length + 1);
        var html = '<div class="offer custom_offer js_new_offer">' +
            '<div class="custom_offer_closer" onclick="$(this).parent().remove()">×</div>' +
            '<table cellspacing="0" cellpadding="0" style="width: 100%">' +
            '<tr>' +
            '<td width="55%">' +
            '<input style="width: 100%" maxlength="255" name="product_custom[custom_offer][' + newId + '][title_ru]" type="text">' +
            '<div class="input_label_overlay">RU</div>' +
            '<i class="br5"></i>' +
            '<input style="width: 100%" maxlength="255" name="product_custom[custom_offer][' + newId + '][title_en]" type="text">' +
            '<div class="input_label_overlay">EN</div>' +
            '</td>' +
            '<td width="20%">' +
            '<input style="width: 100%; height: 53px" maxlength="255" name="product_custom[custom_offer][' + newId + '][price]" type="number">' +
            '</td>' +
            '<td width="5%" style="padding-left: 5px">руб</td>' +
            '<td width="20%">' +
            '<input type="file" class="uploaders pseudo_button_file" name="file" id="' + newId + '__uploader" />' +
            '<div class="uploader upload_img_btn">Загрузить изображение</div>' +
            '<input type="text" class="offerImg offerImg' + newId + '" name="product_custom[custom_offer][' + newId + '][img]">' +

            '</td>' +
            '</tr>' +
            '</table>' +
            '</div>';

        $('.offer_group').append(html);

        $('#' + newId + '__uploader').liteUploader({
            singleFileUploads: true,
            script: sf_prefix + '/uploader?key=catalog'
        }).on('lu:success', function (e, response) {
            var obj = JSON.parse(response);
            var filename = obj.filename;
            //alert(filename);
            $(this).next().html('<img src="/u/i/' + filename + '">').next().val(filename);
        });
    }

    $('.uploaders').liteUploader({
        singleFileUploads: true,
        script: sf_prefix + '/uploader?key=catalog'
    }).on('lu:success', function (e, response) {
        var obj = JSON.parse(response);
        var filename = obj.filename;
        $(this).next().html('<img src="/u/i/' + filename + '">').next().val(filename);
    });

    $('body').on('change', '.uploaders', function () {
        startUploader($(this));
    });

    $('body').on('click', '.uploader', function (e) {
        $(this).prev().trigger('click');
    });

    function startUploader(_this) {
        $(_this).data('liteUploader').startUpload();
    }

    $('#product_supplier_id').change(function () {
        if ($('#product_supplier_id').val() == '') {
            $('.sf_admin_form_field_supplier_price').css('display', 'none');
        }
        else {
            $('.sf_admin_form_field_supplier_price').css('display', 'block');
        }
    }).change();

</script>



