<?php
/*
 #########################################################################
 #                       xt:Commerce Shopsoftware
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #
 # Copyright 2021 xt:Commerce GmbH All Rights Reserved.
 # This file may not be redistributed in whole or significant part.
 # Content of this file is Protected By International Copyright Laws.
 #
 # ~~~~~~ xt:Commerce Shopsoftware IS NOT FREE SOFTWARE ~~~~~~~
 #
 # https://www.xt-commerce.com
 #
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #
 # @copyright xt:Commerce GmbH, www.xt-commerce.com
 #
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #
 # xt:Commerce GmbH, Maximilianstrasse 9, 6020 Innsbruck
 #
 # office@xt-commerce.com
 #
 #########################################################################
 */

defined('_VALID_CALL') or die('Direct Access is not allowed.');

require_once _SRV_WEBROOT . _SRV_WEB_PLUGINS. 'xt_ship_and_track/classes/class.xt_ship_and_track.php';

class xt_ship_and_track_layoutPart extends xt_backend_cls {

    private $_master_key = 'id';

    function setPosition($position)
    {
        $this->position = $position;
    }

    function getdefaultParams(&$header = [], &$params = [])
    {
        $header = array();

        $params['header'] = $header;
        $params['master_key'] = $this->_master_key;
        $params['display_deleteBtn'] = false;
        $params['display_resetBtn'] = true;
        $params['display_editBtn'] = true;
        $params['display_newBtn'] = false;
        $params['display_searchPanel']  = true;

        $params['display_checkCol']  = true;
    }

    function _getParams()
    {
        $header = [];
        $params = [];

        $this->getdefaultParams($header, $params);

        //$params['PageSize'] = 2;

        if (!$this->url_data['edit_id'])
        {
            $rowActionsFunctions = array();
            // open item

            $js = "Ext.MessageBox.show({
                   title:    'Druckposition wählen',
                   msg:      '<input checked=\"1\" value=\"1\" id=\"printPosition1\" name=\"printPosition\" type=\"radio\" /> oben links<br /><input value=\"2\" id=\"printPosition2\" name=\"printPosition\" type=\"radio\" /> oben rechts<br /><input value=\"3\" id=\"printPosition3\" name=\"printPosition\" type=\"radio\" /> unten links<br /><input value=\"4\" id=\"printPosition4\" name=\"printPosition\" type=\"radio\" /> unten rechts<br />',
                   buttons:  Ext.MessageBox.OKCANCEL,
                   fn: function(btn) {
                      if( btn == 'ok') {
                          var pos = $(\"input:radio[name ='printPosition']:checked\").val();
                          //alert(pos);
                          window.open('adminHandler.php?plugin=xt_ship_and_track&load_section=xt_ship_and_track&pg=printLabel&type=pdf&orders_id='+record.data.xt_orders_id+'&tracking_code='+record.data.hermes_order_no+'&pos='+pos+'&sec='+csrf_key,'_blank');
                      }
                   }
                });";
            $rowActionsFunctions['PRINT_LABEL'] = $js;
            $rowActions[] = array('iconCls' => 'PRINT_LABEL', 'qtipIndex' => 'qtip1', 'tooltip' => TEXT_PRINT_LABEL);


            if (count($rowActionsFunctions) > 0) {
                $params['rowActions'] = $rowActions;
                $params['rowActionsFunctions'] = $rowActionsFunctions;
            }

            if (count($rowActions) > 0) {
                $params['rowActions'] = $rowActions;
                $params['rowActionsFunctions'] = $rowActionsFunctions;
            }
        }

        $ext = new ExtFunctions();
        $ext->setMasterKey($this->_master_key);

        $params['UserButtons']      = $UserButtons;

        return $params;
    }

    function _get($ID = 0, $format = true)
    {
        if ($this->position != 'admin') return false;


    $data = [];
        $obj = new stdClass;

        $count_data = count($data);
        $obj->totalCount = $count_data;
        $obj->data = $data;
        return $obj;
    }

    function _set($data, $set_type = 'edit')
    {
        global $db;

        $r = new stdClass();
        $r->success = false;
        $r->msg = false;
        $r->errorMsg = false;

        return json_decode($r);
    }

    function _unset($id = 0)
    {
        return false;
    }

    public static function orderEdit_userButtonsWindows($orderId)
    {
        global $db;

        $sandbox = XT_SHIPCLOUD_SANDBOX == 1 ? '   <span style="color:#c6080b">SANDBOX</span>' : '';
        $infoText = ' ('.TEXT_SHIPCLOUD_ORDER.' '.$orderId. ')'.$sandbox;
        define("TEXT_SHIPCLOUD_CREATE_LABEL".$orderId , "shipcloud".$infoText);

        $extF_editAddress = new ExtFunctions();
        $code = 'shipcloud_add_parcel';
        $extF_editAddress->setCode($code);
        $panel = self::getAddParcelWindowPanel_shipcloud($orderId);
        $remoteWindow = ExtFunctions::_RemoteWindow2(TEXT_SHIPCLOUD .$infoText, $code, $panel, 700, 725, 'window');
        $remoteWindow->setModal(true)
            ->attachListener('render',
                new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                this.setPosition(this.x, 20);
            "))
            );

        $js_add_parcel = "var orders_id = ".$orderId.";";
        $js_add_parcel.= $remoteWindow->getJavascript(false, "new_window").' new_window.show();';
        $UserButtons[$code] = array('text'=>TEXT_SHIPCLOUD, 'style'=>$code, 'icon'=>'../../../plugins/xt_ship_and_track/images/icons/shipcloud16.png', 'acl'=>'edit_address', 'stm' => $js_add_parcel);
        $params['display_'.$code.'Btn'] = true;


        // panel and buttons
        $pnl = new PhpExt_Panel();

        $pnl->getTopToolbar()->addTextItem('Shipcloud', '<span style="font-weight: bold">Shipcloud</span>');
        $pnl->getTopToolbar()->addSeparator('|');

        $pnl->getTopToolbar()->addButton(1, TEXT_SHIPCLOUD_CREATE_LABEL, null/*'/../plugins/xt_ship_and_track/images/icons/shipcloud16.png'*/, new PhpExt_Handler(PhpExt_Javascript::stm($js_add_parcel)));
        $pnl->setRenderTo(PhpExt_Javascript::variable("Ext.get('shipCloudMenubar'+".$orderId.")"));
/*
        $js = PhpExt_Ext::onReady(
            '$($("#memoContainer"+'.$orderId.').parent().find(\'div.text\')[0]).css(\'margin-top\', \'30px\');',
            '$("#memoContainer"+'.$orderId.').parent().find(\'#order-menubar'.$orderId.'\').append("<div id=\'shipCloudMenubar'.$orderId.'\'></div>");',
            $pnl->getJavascript(false, "shipCloudMenubar")
        );
*/
        return $pnl;
    }



    /**
     *     tabs and panels
     */

    static function getAddParcelWindowPanel_shipcloud($orders_id)
    {
        global $admin_order_edit;
        if($orders_id)
        {
            $order_data = $admin_order_edit->getOrderData();
            $delivery_addr = $order_data['order_data']['delivery'];
        }
        else {
            $delivery_addr = array();
        }

        xt_shipcloud_settings::readSettings();

        $Panel = new PhpExt_Form_FormPanel('addParcelForm_shipcloud');
        $Panel->setId('addParcelForm_shipcloud'.$orders_id)
            ->setTitle(__define('TEXT_SHIPCLOUD_CREATE_LABEL'))
            ->setAutoWidth(true)
            ->setAutoHeight(true)
            ->setBodyStyle('padding: 10px;')
            ->setUrl("adminHandler.php?plugin=xt_ship_and_track&load_section=xt_ship_and_track&pg=saveOrder_shipcloud&orders_id=".$orders_id.'&sec='.$_SESSION['admin_user']['admin_key']);

        /**
         *   combo paket vorlagen
         */
        $eF = new ExtFunctions();
        $gridtype = $eF->getSetting('gridType');
        $eF->setSetting('gridType', 'EditGrid');
        $combo = $eF->_comboBox('shipcloud_package', __define('TEXT_XT_SHIPCLOUD_PACKAGE_TEMPLATE'), 'DropdownData.php?get=shipcloud_packages&plugin_code=xt_ship_and_track')
            ->setId('shipcloud_package'.$orders_id)
            ->setWidth(200)
            ->setLazyInit(false)
            ->attachListener('select',
                new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                 setShipcloudPackageTemplate(this.getValue(), $orders_id, shipcloud_packages);
            ")))
            ->attachListener('change',
                new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                //
            ")));
        $combo->getStore()->attachListener('load',
            new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                setTimeout(function(){ initShipcloudCombo('shipcloud_package', $orders_id ) }, 500);
            ")));
        $Panel->addItem($combo);


        /**
         *   länge breite ...
         */
        $Panel->addItem(PhpExt_Form_TextField::createTextField('package_length', __define('TEXT_SHIPCLOUD_PACKAGE_LENGTH'))->setAllowBlank(false));
        $Panel->addItem(PhpExt_Form_TextField::createTextField('package_width', __define('TEXT_SHIPCLOUD_PACKAGE_WIDTH'))->setAllowBlank(false));
        $Panel->addItem(PhpExt_Form_TextField::createTextField('package_height', __define('TEXT_SHIPCLOUD_PACKAGE_HEIGHT'))->setAllowBlank(false));
        $Panel->addItem(PhpExt_Form_TextField::createTextField('package_weight', __define('TEXT_SHIPCLOUD_PACKAGE_WEIGHT'))->setAllowBlank(false));


        /**
         *   resize listener
         */
        $resize_listener = new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                 syncRemoteWindowSize();
            "));


        /**
         *   combo versender
         */
        $eF = new ExtFunctions();
        $eF->setSetting('gridType', 'EditGrid');
        $combo = $eF->_comboBox('shipcloud_carrier', __define('TEXT_XT_SHIPPER'), 'DropdownData.php?get=shipcloud_carriers&plugin_code=xt_ship_and_track')
            ->setId('shipcloud_carrier'.$orders_id)
            ->setWidth(200)->setAllowBlank(false)
            ->setLazyInit(false)
            ->attachListener('select',
                new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                 setShipcloudCarrierSelection(this.getValue(), $orders_id, shipcloud_carriers);
                 syncRemoteWindowSize();
            ")));
        $combo->getStore()->attachListener('load',
            new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                setTimeout(function(){ initShipcloudCombo('shipcloud_carrier', $orders_id ); }, 500);
            ")));
        $Panel->addItem($combo);

        /**
         *   combo versender service
         */
        $combo = $eF->_comboBox('shipcloud_service', __define('TEXT_SHIPCLOUD_CARRIER_SERVICE'));
        $combo->setId('shipcloud_service'.$orders_id)
            ->setDisabled(true)->setAllowBlank(false)
            ->setValue( __define('TEXT_SHIPCLOUD_CHOOSE_CARRIER_FIRST'))
            ->setWidth(200)
            ->attachListener('select',
                new PhpExt_Listener(PhpExt_Javascript::functionDef(null, "
                 applyShipcloudServiceSelection(this.getValue(), $orders_id);
            ")));

        $Panel->addItem($combo);

        /**
         *   combo versender paket typ
         */
        $combo = $eF->_comboBox('shipcloud_package_type', __define('TEXT_SHIPCLOUD_CARRIER_PACKAGE_TYPE'))
            ->setId('shipcloud_package_type'.$orders_id)
            ->setDisabled(true)->setAllowBlank(false)
            ->setValue( __define('TEXT_SHIPCLOUD_CHOOSE_CARRIER_FIRST'))
            ->setWidth(200);
        $Panel->addItem($combo);


        /**
         *   sendungsbeschreibung
         */
        $ta = PhpExt_Form_TextArea::createTextArea('description',  __define('TEXT_SHIPCLOUD_DESCRIPTION'))
            ->setCssStyle('width:90%')->setHeight(50);

        if($orders_id && $order_data && is_array($order_data['order_products']))
        {
            $p_names = array();
            foreach($order_data['order_products'] as $k => $v)
            {
                $p_names[] = $v['products_name'];
            }
            $desc = implode(' | ', $p_names);
            $desc = str_replace("'",'', $desc);
            $ta->setValue($desc);
        }
        $Panel->addItem($ta);



        $extFnc = new ExtFunctions();
        $comboCountry_to = $extFnc->_comboBox( 'to_country', __define('TEXT_SHIPCLOUD_COUNTRY'),'DropdownData.php?get=countries')->setWidth(120);
        $comboCountry_to->setValue($delivery_addr['country_code']);
        $comboCountry_from = $extFnc->_comboBox('from_country', __define('TEXT_SHIPCLOUD_COUNTRY'),'DropdownData.php?get=countries')->setWidth(120);
        $comboCountry_from->setValue(XT_SHIPCLOUD_FROM_COUNTRY);
        $comboCountry_retoure = $extFnc->_comboBox('retoure_country', __define('TEXT_SHIPCLOUD_COUNTRY'),'DropdownData.php?get=countries')->setWidth(120);

        if($orders_id)
        {
            /**
             *    additional services DHL
             */
            $field_set_add_service_dhl = new PhpExt_Form_FieldSet();
            $field_set_add_service_dhl->setBorder(true)
                ->setId('group_add_service_dhl' . $orders_id)
                ->setBodyBorder(true)
                ->setCheckboxToggle(true)
                ->setCheckboxName('add_service_dhl')
                ->setTitle(TEXT_SHIPCLOUD_ADDITIONAL_SERVICES . ' DHL')
                ->setAutoHeight(true)
                ->setDefaults(new PhpExt_Config_ConfigObject(array("margin-top" => "100px")))
                ->setCollapsed(true)
                ->attachListener('expand', $resize_listener)
                ->attachListener('collapse', $resize_listener);

            $tf = PhpExt_Form_TextField::createTextField('add_service_dhl_advance_notice_email', __define('TEXT_SHIPCLOUD_ADVANCE_NOTICE_EMAIL'));
            $tf->setValue("");
            $field_set_add_service_dhl->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('add_service_dhl_cash_on_delivery', __define('TEXT_SHIPCLOUD_CASH_ON_DELIVERY'));
            $tf->setValue("")->setId('add_service_dhl_cash_on_delivery' . $orders_id);
            $field_set_add_service_dhl->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('add_service_dhl_declared_value', __define('TEXT_SHIPCLOUD_DECLARED_VALUE_DHL'));
            $tf->setValue("")->setId('add_service_dhl_declared_value' . $orders_id);
            $field_set_add_service_dhl->addItem($tf);
            $Panel->addItem($field_set_add_service_dhl);

            /**
             *    additional services DPD
             */
            $field_set_add_service_dpd = new PhpExt_Form_FieldSet();
            $field_set_add_service_dpd->setBorder(true)
                ->setId('group_add_service_dpd' . $orders_id)
                ->setBodyBorder(true)
                ->setCheckboxToggle(true)
                ->setCheckboxName('add_service_dpd')
                ->setTitle(TEXT_SHIPCLOUD_ADDITIONAL_SERVICES . ' DPD')
                ->setAutoHeight(true)
                ->setDefaults(new PhpExt_Config_ConfigObject(array("margin-top" => "100px")))
                ->setCollapsed(true)
                ->attachListener('expand', $resize_listener)
                ->attachListener('collapse', $resize_listener);

            $tf = PhpExt_Form_TextField::createTextField('add_service_dpd_advance_notice_email', __define('TEXT_SHIPCLOUD_ADVANCE_NOTICE_EMAIL'));
            $tf->setValue("");
            $field_set_add_service_dpd->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('add_service_dpd_advance_notice_sms', __define('TEXT_SHIPCLOUD_ADVANCE_NOTICE_SMS'));
            $tf->setValue("+");
            $field_set_add_service_dpd->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('add_service_dpd_drop_authorization_msg', __define('TEXT_SHIPCLOUD_DROP_AUTHORIZATION_MSG'));
            $tf->setValue("");
            $field_set_add_service_dpd->addItem($tf);
            $tf = PhpExt_Form_Checkbox::createCheckbox('add_service_dpd_saturday_delivery', __define('TEXT_SHIPCLOUD_SATURDAY_DELIVERY'));
            $tf->setValue("");
            $field_set_add_service_dpd->addItem($tf);
            $Panel->addItem($field_set_add_service_dpd);
        }


        /**
         *    pickup
         */
        $field_set_pickup = new PhpExt_Form_FieldSet();
        $field_set_pickup ->setBorder(true)
            ->setId('shipcloud_fieldset_pickup_'.$orders_id)
            ->setBodyBorder(true)
            ->setCheckboxToggle(true)
            ->setCheckboxName('pickup_fieldset')
            ->setTitle(TEXT_SHIPCLOUD_PICKUP)
            ->setAutoHeight(true)
            ->setDefaults(new PhpExt_Config_ConfigObject(array("margin-top"=>"80px")))
            ->setCollapsed(true);

        $columnLayout = new PhpExt_Layout_ColumnLayout();
        $columnPanel = new PhpExt_Panel();
        $columnPanel->setBorder(false)->setLayout($columnLayout);

        // left column
        $formLayout = new PhpExt_Layout_FormLayout();
        $firstColumn = new PhpExt_Panel();
        $firstColumn->setId('panel_pickup_earliest_'.$orders_id)
            ->setCssStyle('padding:5px;')->setBorder(false)->setLayout($formLayout);

        $d = new DateTime();
        $d->add(new DateInterval('P1D'));
        $tf = PhpExt_Form_DateField::createDateField('pickup_earliest_date',__define('TEXT_SHIPCLOUD_PICKUP_EARLIEST_DATE'));
        $tf->setFormat('d.m.Y')->setValue($d->format('d.m.Y'))->setWidth(85);
        $firstColumn->addItem($tf);
        $tf = PhpExt_Form_TimeField::createTimeField('pickup_earliest_time',__define('TEXT_SHIPCLOUD_PICKUP_EARLIEST_TIME'));
        $tf->setFormat('H:i')->setValue('15:00')->setWidth(85);
        $firstColumn->addItem($tf);
        $tf = PhpExt_Form_Checkbox::createCheckbox('pickup', __define('TEXT_PICKUP_ASK'));
        $tf->setValue("");
        $firstColumn->addItem($tf);
        $columnPanel->addItem($firstColumn, new PhpExt_Layout_ColumnLayoutData(0.5));

        // right column
        $secondColumn = new PhpExt_Panel();
        $secondColumn->setId('panel_pickup_latest_'.$orders_id)
            ->setCssStyle('padding:5px;')->setBorder(false)->setLayout($formLayout);

        $d = new DateTime();
        $d->add(new DateInterval('P1D'));
        $tf = PhpExt_Form_DateField::createDateField('pickup_latest_date',__define('TEXT_SHIPCLOUD_PICKUP_LATEST_DATE'));
        $tf->setFormat('d.m.Y')->setValue($d->format('d.m.Y'))->setWidth(85);
        $secondColumn->addItem($tf);
        $tf = PhpExt_Form_TimeField::createTimeField('pickup_latest_time',__define('TEXT_SHIPCLOUD_PICKUP_LATEST_TIME'));
        $tf->setFormat('H:i')->setValue('16:00')->setWidth(85);
        $secondColumn->addItem($tf);

        $columnPanel->addItem($secondColumn, new PhpExt_Layout_ColumnLayoutData(0.5));

        $field_set_pickup
            ->setCollapsed(true)->attachListener('expand', $resize_listener)
            ->setCollapsed(true)->attachListener('collapse',$resize_listener);

        $field_set_pickup->addItem($columnPanel);

        $Panel->addItem($field_set_pickup);



        $columnLayout = new PhpExt_Layout_ColumnLayout();
        $columnPanel = new PhpExt_Panel();
        $columnPanel->setBorder(false)->setLayout($columnLayout);

        $firstColumn = false;
        /**
         *   Empfänger
         */
        if($orders_id)
        {
            $firstColumn = new PhpExt_Panel();
            $firstColumn->setCssStyle('padding:5px;')->setBorder(false);

            $street_from = xt_ship_and_track::splitAddress($delivery_addr['street_address'], xt_ship_and_track::getSplit());

            $field_set_to = new PhpExt_Form_FieldSet();
            $field_set_to ->setBorder(true)
                ->setId('shipcloud_fieldset_to_'.$orders_id)
                ->setBodyBorder(true)
                ->setCheckboxToggle(true)
                ->setCheckboxName('override_default_to')
                ->setTitle(TEXT_SHIPCLOUD_EDIT_TO)
                ->setAutoHeight(true)
                ->setDefaults(new PhpExt_Config_ConfigObject(array("margin-top"=>"800px")))
                ->setCollapsed(true);

            $Panel->addItem(PhpExt_Form_Hidden::createHidden('to_email', $order_data['order_customer']['customers_email_address']));
            $tf = PhpExt_Form_TextField::createTextField('to_first_name', __define('TEXT_SHIPCLOUD_FIRST_NAME'));
            $tf->setValue($delivery_addr['firstname']);
            $field_set_to->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('to_last_name', __define('TEXT_SHIPCLOUD_LAST_NAME'));
            $tf->setValue($delivery_addr['lastname']);
            $field_set_to->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('to_company', __define('TEXT_SHIPCLOUD_COMPANY'));
            $tf->setValue($delivery_addr['company']);
            $field_set_to->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('to_care_of', __define('TEXT_SHIPCLOUD_CARE_OF'));
            $tf->setValue($delivery_addr['address_addition']);
            $field_set_to->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('to_street', __define('TEXT_SHIPCLOUD_STREET'));
            $tf->setValue($street_from[0]);
            $field_set_to->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('to_street_no', __define('TEXT_SHIPCLOUD_STREET_NO'));
            $tf->setValue($street_from[1]);
            $field_set_to->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('to_city', __define('TEXT_SHIPCLOUD_CITY'));
            $tf->setValue($delivery_addr['city']);
            $field_set_to->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('to_zip_code', __define('TEXT_SHIPCLOUD_ZIP_CODE'));
            $tf->setValue($delivery_addr['postcode']);
            $field_set_to->addItem($tf);
            $tf =PhpExt_Form_TextField::createTextField('to_state', __define('TEXT_SHIPCLOUD_STATE'));
            $tf->setValue($delivery_addr['federal_state_code'] ? $delivery_addr['federal_state_code'] : '');
            $field_set_to->addItem($tf);
            /*
            $tf = PhpExt_Form_TextField::createTextField('to_country', __define('TEXT_SHIPCLOUD_COUNTRY'));
            $tf->setValue($delivery_addr['country_code']);
            */
            $field_set_to->addItem($comboCountry_to);
            $tf = PhpExt_Form_TextField::createTextField('to_phone', __define('TEXT_SHIPCLOUD_PHONE'));
            $tf->setValue($delivery_addr['mobile_phone'] ? $delivery_addr['mobile_phone'] : $delivery_addr['phone']);
            $field_set_to->addItem($tf)
                ->setCollapsed(true)->attachListener('expand', $resize_listener)
                ->setCollapsed(true)->attachListener('collapse',$resize_listener);
            $firstColumn->addItem($field_set_to);
        }

        /**
         *    Absender
         */

        $field_set_from = new PhpExt_Form_FieldSet();
        $field_set_from ->setBorder(true)
            ->setId('shipcloud_fieldset_from_'.$orders_id)
            ->setBodyBorder(true)
            ->setCheckboxToggle(true)
            ->setCheckboxName('override_default_from')
            ->setTitle(TEXT_SHIPCLOUD_OVERRIDE_FROM)
            ->setAutoHeight(true)
            ->setDefaults(new PhpExt_Config_ConfigObject(array("margin-top"=>"80px")))
            ->setCollapsed(true)->setHideMode(PhpExt_Component::HIDE_MODE_DISPLAY)->setHideParent(true);

        $tf = PhpExt_Form_TextField::createTextField('from_first_name', __define('TEXT_SHIPCLOUD_FIRST_NAME'));
        $tf->setValue(XT_SHIPCLOUD_FROM_FIRSTNAME);
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_last_name', __define('TEXT_SHIPCLOUD_LAST_NAME'));
        $tf->setValue(XT_SHIPCLOUD_FROM_LASTNAME);
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_company', __define('TEXT_SHIPCLOUD_COMPANY'));
        $tf->setValue(XT_SHIPCLOUD_FROM_COMPANY);
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_care_of', __define('TEXT_SHIPCLOUD_CARE_OF'));
        $tf->setValue(XT_SHIPCLOUD_FROM_CAREOF);
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_street', __define('TEXT_SHIPCLOUD_STREET'));
        $tf->setValue(XT_SHIPCLOUD_FROM_STREET);
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_street_no', __define('TEXT_SHIPCLOUD_STREET_NO'));
        $tf->setValue(XT_SHIPCLOUD_FROM_HOUSENO);
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_city', __define('TEXT_SHIPCLOUD_CITY'));
        $tf->setValue(XT_SHIPCLOUD_FROM_CITY);
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_zip_code', __define('TEXT_SHIPCLOUD_ZIP_CODE'));
        $tf->setValue(XT_SHIPCLOUD_FROM_ZIP);
        $field_set_from->addItem($tf);
        $tf =PhpExt_Form_TextField::createTextField('from_state', __define('TEXT_SHIPCLOUD_STATE'));
        $tf->setValue(XT_SHIPCLOUD_FROM_STATE);
        $field_set_from->addItem($tf);
        /*
        $field_set_from->addItem($tf);
        $tf = PhpExt_Form_TextField::createTextField('from_country', __define('TEXT_SHIPCLOUD_COUNTRY'));
        $tf->setValue("");
        */
        $comboCountry_from->setValue(XT_SHIPCLOUD_FROM_COUNTRY);
        $field_set_from->addItem($comboCountry_from);
        $tf = PhpExt_Form_TextField::createTextField('from_phone', __define('TEXT_SHIPCLOUD_PHONE'));
        $tf->setValue(XT_SHIPCLOUD_FROM_PHONE);
        $field_set_from->addItem($tf);

        $field_set_from->setCollapsed(true)->attachListener('expand', $resize_listener)
            ->setCollapsed(true)->attachListener('collapse',$resize_listener);

        $secondColumn = new PhpExt_Panel();
        $secondColumn->setCssStyle('padding:5px;')->setBorder(false)->setId('shipcloud_panel_from_'.$orders_id)->setHideMode(PhpExt_Component::HIDE_MODE_DISPLAY);
        $secondColumn->addItem($field_set_from);


        /*
         *  retore addresse
         *
         */
        $thirdColumn = false;
        if(defined('XT_SHIPCLOUD_DIFFERENT_RETOURE') && XT_SHIPCLOUD_DIFFERENT_RETOURE==="1")
        {
            $field_set_retoure = new PhpExt_Form_FieldSet();
            $field_set_retoure->setBorder(true)
                ->setId('shipcloud_fieldset_retoure_' . $orders_id)
                ->setBodyBorder(true)
                ->setCheckboxToggle(true)
                ->setCheckboxName('override_default_from')
                ->setTitle(TEXT_SHIPCLOUD_RETOURE_TO)
                ->setAutoHeight(true)
                ->setDefaults(new PhpExt_Config_ConfigObject(array("margin-top" => "100px")))
                ->setCollapsed(true);

            $tf = PhpExt_Form_TextField::createTextField('retoure_first_name', __define('TEXT_SHIPCLOUD_FIRST_NAME'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_FIRSTNAME);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_last_name', __define('TEXT_SHIPCLOUD_LAST_NAME'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_LASTNAME);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_company', __define('TEXT_SHIPCLOUD_COMPANY'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_COMPANY);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_care_of', __define('TEXT_SHIPCLOUD_CARE_OF'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_CAREOF);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_street', __define('TEXT_SHIPCLOUD_STREET'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_STREET);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_street_no', __define('TEXT_SHIPCLOUD_STREET_NO'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_HOUSENO);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_city', __define('TEXT_SHIPCLOUD_CITY'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_CITY);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_zip_code', __define('TEXT_SHIPCLOUD_ZIP_CODE'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_ZIP);
            $field_set_retoure->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('retoure_state', __define('TEXT_SHIPCLOUD_STATE'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_STATE);
            $field_set_retoure->addItem($tf);
            /*
            $field_set_from->addItem($tf);
            $tf = PhpExt_Form_TextField::createTextField('from_country', __define('TEXT_SHIPCLOUD_COUNTRY'));
            $tf->setValue("");
            */
            $comboCountry_retoure->setValue(XT_SHIPCLOUD_RETOURE_COUNTRY);
            $field_set_retoure->addItem($comboCountry_retoure);
            $tf = PhpExt_Form_TextField::createTextField('retoure_phone', __define('TEXT_SHIPCLOUD_PHONE'));
            $tf->setValue(XT_SHIPCLOUD_RETOURE_PHONE);
            $field_set_retoure->addItem($tf);

            $field_set_retoure->setCollapsed(true)->attachListener('expand', $resize_listener)
                ->setCollapsed(true)->attachListener('collapse', $resize_listener);

            $thirdColumn = new PhpExt_Panel();
            $thirdColumn->setCssStyle('padding:5px;')->setBorder(false);
            $thirdColumn->addItem($field_set_retoure);
        }

        if(is_object($firstColumn))
        {
            $columnPanel->addItem($firstColumn, new PhpExt_Layout_ColumnLayoutData(0.5));
            $columnPanel->addItem($secondColumn, new PhpExt_Layout_ColumnLayoutData(0.5));
            if(is_object($thirdColumn))
            {
                $columnPanel->addItem($thirdColumn, new PhpExt_Layout_ColumnLayoutData(0.5));
            }
        }
        else {
            $columnPanel->addItem($secondColumn, new PhpExt_Layout_ColumnLayoutData(1));
            if(is_object($thirdColumn))
            {
                $columnPanel->addItem($thirdColumn, new PhpExt_Layout_ColumnLayoutData(1));
            }
        }




        $Panel->addItem($columnPanel);

        /**
         *    hidden fields
         */
        $Panel->addItem(PhpExt_Form_Hidden::createHidden('quote', 0));
        $Panel->addItem(PhpExt_Form_Hidden::createHidden('prepare_label', 0));
        $Panel->addItem(PhpExt_Form_Hidden::createHidden('shop_id', $admin_order_edit->order_data['order_data']['shop_id']));
        if($orders_id)
        {
            $hidden = PhpExt_Form_Hidden::createHidden('order_total_price', number_Format($admin_order_edit->order_data['order_total']['total']['plain'], 2, ',', ''));
            $hidden->setId('order_total_price'.$orders_id);
            $Panel->addItem($hidden);
        }
        else {
            $Panel->addItem(PhpExt_Form_Hidden::createHidden('order_ids', 0));
        }

        /**
         *    Buttons
         */
        if($orders_id || true)
        {
            $quoteBtn = PhpExt_Button::createTextButton(__define("BUTTON_SHIPCLOUD_QUOTE"),
                new PhpExt_Handler(PhpExt_Javascript::stm("
            shipcloud_sendForm(" . $orders_id . ", 'quote');
            "))
            );
            $quoteBtn->setType(PhpExt_Button::BUTTON_TYPE_BUTTON);
            $quoteBtn->setId("BUTTON_SHIPCLOUD_QUOTE" . $orders_id);
            $Panel->addButton($quoteBtn);
        }

        /*
        if($orders_id)
        {
            $aveBtn = PhpExt_Button::createTextButton(__define("BUTTON_SHIPCLOUD_SAVE"),
                new PhpExt_Handler(PhpExt_Javascript::stm("
            shipcloud_sendForm(" . $orders_id . ", 'prepare_label');
            "))
            );
            $aveBtn->setType(PhpExt_Button::BUTTON_TYPE_SUBMIT);
            $aveBtn->setId("BUTTON_SHIPCLOUD_SAVE" . $orders_id);
            if ($orders_id)
            {
                $Panel->addButton($aveBtn);
            }
        }
        */

        $submitBtn = PhpExt_Button::createTextButton(__define("TEXT_SHIPCLOUD_CREATE_LABEL"),
            new PhpExt_Handler(PhpExt_Javascript::stm("
            shipcloud_sendForm(".$orders_id.");
            "))
        );

        $submitBtn->setType(PhpExt_Button::BUTTON_TYPE_SUBMIT);
        $submitBtn->setId("TEXT_SHIPCLOUD_CREATE_LABEL".$orders_id);
        $Panel->addButton($submitBtn);

        return $Panel;
    }


    /**
     *    ExtJs adds
     */



}