{extends file=$smarty.const.SMARTY_LAYOUT_MAIN_POPUP_LISTING}

{block name="engine-content"}
    <div class="engine-content">

        <div class="engine-header title popup-header">
            <span class="label">
                {$lang['index']['title']['companiesOnOrder']} {orderLabel ref=$idOrder format={"#"|cat:$convertedId}}
            </span>
        </div>

        <div class="content">

            {if $msgs->getMessages()}
                <div class="messageDivContainer">
                    <ul class="notification" style="display:none;"></ul>
                </div>
            {/if}

            <form name="form_"
                  id="form_"
                  class="form"
                  enctype="multipart/form-data"
                  method="POST"
                  action=""
                  onSubmit="$(this).fire('form:doSubmit'); return false;" >

                <input type="hidden" class="inputElement input-hidden  default-width" id="idOrder" name="idOrder" value="{$idOrder}" />
                <input type="hidden" class="inputElement input-hidden  default-width" id="idCorporateOwner" name="idCorporateOwner" value="" />
                <input type="hidden" class="inputElement input-hidden  default-width" id="idCorporateDelete" name="idCorporateDelete" value="" />
                <input type="hidden" class="inputElement input-hidden  default-width" id="operation" name="operation" value="" />
                <input type="hidden" class="inputElement input-hidden  default-width" id="save" name="save" value="1" />

                <fieldset class="default visibleGroup-0 " id="form_Servicedetails">
                    <legend class="non-collapsible">
                        <div>{$lang['index']['fieldsets']['addNewCorporateOnOrder']}</div>
                    </legend>
                </fieldset>
                <div style="padding:10px;">
                    {$lang['index']['form']['addExistingCompany']}:
                    {$corporateAutocomplete->render()} <a style="margin-left:10px;" href="#;" onclick="corporateInsert();" class="link">{$lang['index']['form']['newCompany']}</a>
                </div>

                {if $companiesList}
                    <fieldset class="default visibleGroup-0 " id="form_Servicedetails">
                        <legend class="non-collapsible">
                            <div>{$lang['index']['fieldsets']['orderCorporatesList']}</div>
                        </legend>
                    </fieldset>

                    {include file="listingContainer.tpl"}
                {/if}

            </form>
        </div>

    </div>
{/block}

{block name="js" append}
    <script type="text/javascript">

        MVC.scheduleOnLoad(function() {

            // display messages
            {if $msgs->getMessages()}
                MVC.notifications = (new _notificationContainer({ animate: true })).attach($('form_').up().down('div.messageDivContainer ul.notification'));
                var messages = [];
                {foreach from=$msgs->getMessages() item=message}
                    messages.push({
                        type: '{$message->getType()}',
                        message: '{$message->getText()}'
                    });
                {/foreach}
                MVC.displayMessages(messages);
            {/if}

            // remove corporate from order
            MVC.listing.prototype.removeCorporateFromOrder = function(element){
                $('idCorporateDelete').value = this.element.up('tr').getAttribute('data-id');
                $('operation').value='removeCorporateFromOrder';
                $('form_').submit();
            }

            // confirm delete
            MVC.listing.prototype.removeCorporateWithConfirmation = function(element){
                this.element = element;
                MVC.callbackWithConfirmation(
                    "",
                    "{$lang['index']['messages']['removeCorporate']}",
                    this.removeCorporateFromOrder.bind(this)
                );
            }

            // insert newly added corporate on order
            MVC.pageRefresh = function (module, action, windowName, execOutput)
            {
               if (module=='Partners/Corporates' && action=='insert' && execOutput.id) {
                   window.location='disp.php?module='+MVC.CURRENT_MODULE+'&action='+MVC.CURRENT_ACTION+'&operation=addCorporateOnOrder&idOrder={$idOrder}&save=1&addCorporate[id]='+execOutput.id;
               }
            }

        });

    </script>
{/block}
