// TODO
        unset($propertySettings['INPUT_FIELD_LOCATION']);
    }
}
// RELATION SETTINGS ///////////////////////////////////////////////////////////////////////////////////////////////////
// payment system options
$paymentOptions = array();
$result = CSalePaySystem::GetList(array("SORT" => "ASC", "NAME" => "ASC"), array("ACTIVE" => "Y"), false, false, array("ID", "NAME", "ACTIVE", "SORT", "LID"));
while ($row = $result->Fetch()) {
    $paymentOptions[$row['ID']] = $row['NAME'] . ($row['LID'] ? " ({$row['LID']}) " : ' ') . "[{$row['ID']}]";
}
// delivery system options
$deliveryOptions = array();
foreach (\Bitrix\Sale\Delivery\Services\Manager::getActiveList(true) as $deliveryId => $deliveryFields) {
    $name = $deliveryFields["NAME"] . " [" . $deliveryId . "]";
    $sites = \Bitrix\Sale\Delivery\Restrictions\Manager::getSitesByServiceId($deliveryId);
    if (!empty($sites)) {
        $name .= " (" . implode(", ", $sites) . ")";
    }
    $deliveryOptions[$deliveryId] = $name;
}
$relationsSettings = array('P' => array('TYPE' => 'ENUM', 'LABEL' => Loc::getMessage('SALE_PROPERTY_PAYSYSTEM'), 'OPTIONS' => $paymentOptions, 'MULTIPLE' => 'Y', 'SIZE' => '5'), 'D' => array('TYPE' => 'ENUM', 'LABEL' => Loc::getMessage('SALE_PROPERTY_DELIVERY'), 'OPTIONS' => $deliveryOptions, 'MULTIPLE' => 'Y', 'SIZE' => '5'));
// VALIDATE AND SAVE POST //////////////////////////////////////////////////////////////////////////////////////////////
if ($_SERVER['REQUEST_METHOD'] == 'POST' && (isset($_POST["apply"]) || isset($_POST["save"])) && bitrix_sessid()) {
    // validate property
    foreach ($propertySettings as $name => $input) {
        if ($error = Input\Manager::getError($input, $property[$name])) {
            if ($input['MULTIPLE'] && $input['MULTIPLE'] == 'Y') {
                $errorString = '';
                foreach ($error as $k => $v) {
                    $errorString .= ' ' . ++$k . ': ' . implode(', ', $v) . ';';
		border-color: #87919C #959EA9 #9EA7B1;
		border-image: none;
		border-radius: 4px 4px 4px 4px;
		border-style: solid;
		border-width: 1px;
		box-shadow: 0 1px 0 0 rgba(255, 255, 255, 0.3), 0 2px 2px -1px rgba(180, 188, 191, 0.7) inset;
		color: #000000;
		display: inline-block;
		outline: medium none;
		vertical-align: middle;
		!important;
	}
</style>';
Asset::getInstance()->addString($lheStyle);
Asset::getInstance()->addJs("/bitrix/js/sale/pay_system.js");
\Bitrix\Sale\Delivery\Restrictions\Manager::getClassesList();
$instance = Application::getInstance();
$context = $instance->getContext();
$request = $context->getRequest();
$server = $context->getServer();
$documentRoot = Application::getDocumentRoot();
$id = (int) $request->get('ID');
if (CModule::IncludeModule("fileman")) {
    $bFilemanModuleInst = true;
}
$aTabs = array(array("DIV" => "edit1", "TAB" => GetMessage("SPSN_TAB_PAYSYS"), "ICON" => "sale", "TITLE" => GetMessage("SPSN_TAB_PAYSYS_DESCR")));
if ($id > 0 && $request->getRequestMethod() !== 'POST') {
    $aTabs[] = array("DIV" => "edit3", "TAB" => GetMessage("SPS_PAY_SYSTEM_RESTRICTION"), "ICON" => "sale", "TITLE" => GetMessage("SPS_PAY_SYSTEM_RESTRICTION_DESC"));
}
$tabControl = new CAdminTabControl("tabControl", $aTabs);
$errorMessage = '';
 /** @deprecated */
 public static function Set($code, $arData, $siteId = false)
 {
     global $APPLICATION;
     $serviceParams = self::getServiceParams($code, $siteId);
     $id = isset($serviceParams["ID"]) ? $serviceParams["ID"] : false;
     $update = intval($id) > 0;
     $fields = array_intersect_key($arData, Bitrix\Sale\Delivery\Services\Table::getMap());
     if (!$update) {
         $fields["CODE"] = $code;
         if (!isset($arData["CLASS_NAME"])) {
             $fields["CLASS_NAME"] = '\\Bitrix\\Sale\\Delivery\\Services\\Automatic';
         } else {
             $fields["CLASS_NAME"] = $arData["CLASS_NAME"];
         }
     }
     if (isset($arData["PARENT_ID"])) {
         $fields["PARENT_ID"] = $arData["PARENT_ID"];
     } elseif (!$update) {
         $fields["PARENT_ID"] = 0;
     }
     if (!defined('SALE_DH_INITIALIZED')) {
         CSaleDeliveryHandler::Initialize();
     }
     $handlers = self::__getRegisteredHandlers();
     if (isset($serviceParams["CONFIG"])) {
         $fields["CONFIG"] = $serviceParams["CONFIG"];
     } else {
         $fields["CONFIG"] = array();
     }
     if (isset($arData["CONFIG"])) {
         if (isset($handlers[$code]["DBSETSETTINGS"]) && is_callable($handlers[$code]["DBSETSETTINGS"])) {
             if (!($strOldSettings = call_user_func($handlers[$code]["DBSETSETTINGS"], $arData["CONFIG"]))) {
                 $APPLICATION->ThrowException("Can't save delivery services's old settings");
                 return false;
             }
         } else {
             $strOldSettings = $arData["CONFIG"];
         }
         $strOldSettings = serialize($strOldSettings);
         $fields["CONFIG"]["MAIN"]["OLD_SETTINGS"] = $strOldSettings;
     }
     if (!empty($arData["BASE_CURRENCY"])) {
         $fields["CURRENCY"] = $arData["BASE_CURRENCY"];
     } elseif (!empty($serviceParams["CURRENCY"])) {
         $fields["CURRENCY"] = $serviceParams["CURRENCY"];
     } elseif (!empty($handlers[$code]["BASE_CURRENCY"])) {
         $fields["CURRENCY"] = $handlers[$code]["BASE_CURRENCY"];
     } else {
         $fields["CURRENCY"] = COption::GetOptionString('sale', 'default_currency', 'RUB');
     }
     if (!empty($arData["SID"])) {
         $fields["CONFIG"]["MAIN"]["SID"] = $arData["SID"];
     }
     if (isset($arData["TAX_RATE"]) && floatval($arData["TAX_RATE"]) > 0) {
         $fields["CONFIG"]["MAIN"]["MARGIN_VALUE"] = $arData["TAX_RATE"];
         $fields["CONFIG"]["MAIN"]["MARGIN_TYPE"] = "%";
     } elseif (!$update) {
         $fields["CONFIG"]["MAIN"]["MARGIN"] = 0;
     }
     if (!empty($arData["PROFILE_ID"])) {
         $fields["CONFIG"]["MAIN"]["PROFILE_ID"] = $arData["PROFILE_ID"];
     }
     if (isset($arData["LOGOTIP"]) && is_array($arData["LOGOTIP"])) {
         $fields["LOGOTIP"] = $arData["LOGOTIP"];
         $fields["LOGOTIP"]["MODULE_ID"] = "sale";
         CFile::SaveForDB($fields, "LOGOTIP", "sale/delivery/logotip");
     }
     if ($update) {
         $res = \Bitrix\Sale\Delivery\Services\Manager::update($id, $fields);
     } else {
         $res = \Bitrix\Sale\Delivery\Services\Manager::add($fields);
     }
     if (!$res->isSuccess()) {
         throw new \Bitrix\Main\SystemException(implode("\n", $res->getErrorMessages()));
     }
     if (!$update) {
         $id = $res->getId();
     }
     if (is_array($arData["PROFILES"])) {
         foreach ($arData["PROFILES"] as $profileCode => $profileData) {
             if (strlen($profileData["TITLE"]) > 0) {
                 $name = $profileData["TITLE"];
             } elseif (strlen($handlers[$code]['PROFILES'][$profileCode]['TITLE']) > 0) {
                 $name = $handlers[$code]['PROFILES'][$profileCode]['TITLE'];
             } else {
                 $name = "-";
             }
             self::Set($code . ":" . $profileCode, array("NAME" => $name, "DESCRIPTION" => isset($profileData["DESCRIPTION"]) ? $profileData["DESCRIPTION"] : '', "ACTIVE" => isset($profileData["ACTIVE"]) ? $profileData["ACTIVE"] : "N", "TAX_RATE" => isset($profileData["TAX_RATE"]) ? $profileData["TAX_RATE"] : 0, "PARENT_ID" => isset($profileData["PARENT_ID"]) ? $profileData["PARENT_ID"] : $id, "SORT" => isset($arData["SORT"]) ? $arData["SORT"] : 100, "RESTRICTIONS_WEIGHT" => isset($profileData["RESTRICTIONS_WEIGHT"]) ? $profileData["RESTRICTIONS_WEIGHT"] : false, "RESTRICTIONS_SUM" => isset($profileData["RESTRICTIONS_SUM"]) ? $profileData["RESTRICTIONS_SUM"] : false, "RESTRICTIONS_DIMENSIONS" => isset($profileData["RESTRICTIONS_DIMENSIONS"]) ? $profileData["RESTRICTIONS_DIMENSIONS"] : false, "RESTRICTIONS_MAX_SIZE" => isset($profileData["RESTRICTIONS_MAX_SIZE"]) ? $profileData["RESTRICTIONS_MAX_SIZE"] : 0, "RESTRICTIONS_DIMENSIONS_SUM" => isset($profileData["RESTRICTIONS_DIMENSIONS_SUM"]) ? $profileData["RESTRICTIONS_DIMENSIONS_SUM"] : 0, "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Services\\AutomaticProfile', "PROFILE_ID" => $profileCode), strlen($siteId) > 0 ? $siteId : "");
         }
     }
     if (strlen($siteId) > 0) {
         if (!self::saveRestrictionBySiteId($id, $siteId, $update)) {
             $APPLICATION->ThrowException("Can't save delivery restriction by site", "SITE_ID");
             return false;
         }
     } elseif ($update) {
         \Bitrix\Sale\Delivery\Restrictions\Manager::deleteByDeliveryIdClassName($id, '\\Bitrix\\Sale\\Delivery\\Restrictions\\BySite');
     }
     if (is_array($arData["RESTRICTIONS_WEIGHT"]) && (floatval($arData["RESTRICTIONS_WEIGHT"][0]) > 0 || floatval($arData["RESTRICTIONS_WEIGHT"][1]) > 0)) {
         if (!self::saveRestrictionByWeight($id, $arData["RESTRICTIONS_WEIGHT"], $update)) {
             $APPLICATION->ThrowException("Can't save delivery restriction by weight", "RESTRICTIONS_WEIGHT");
             return false;
         }
     } elseif ($update) {
         \Bitrix\Sale\Delivery\Restrictions\Manager::deleteByDeliveryIdClassName($id, '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByWeight');
     }
     if (is_array($arData["RESTRICTIONS_SUM"]) && (floatval($arData["RESTRICTIONS_SUM"][0]) > 0 || floatval($arData["RESTRICTIONS_SUM"][1]) > 0)) {
         if (!self::saveRestrictionByPrice($id, $arData["RESTRICTIONS_SUM"], $fields["CURRENCY"], $update)) {
             $APPLICATION->ThrowException("Can't save delivery restriction by sum", "RESTRICTIONS_SUM");
             return false;
         }
     } elseif ($update) {
         \Bitrix\Sale\Delivery\Restrictions\Manager::deleteByDeliveryIdClassName($id, '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPrice');
     }
     if (is_array($arData["RESTRICTIONS_DIMENSIONS"]) && (floatval($arData["RESTRICTIONS_DIMENSIONS"][0]) > 0 || floatval($arData["RESTRICTIONS_DIMENSIONS"][1]) > 0 || floatval($arData["RESTRICTIONS_DIMENSIONS"][3]) > 0)) {
         if (!self::saveRestrictionByDimensions($id, array("LENGTH" => count($arData["RESTRICTIONS_DIMENSIONS"][0]) > 0 ? $arData["RESTRICTIONS_DIMENSIONS"][0] : 0, "WIDTH" => isset($arData["RESTRICTIONS_DIMENSIONS"][1]) ? $arData["RESTRICTIONS_DIMENSIONS"][1] : 0, "HEIGHT" => isset($arData["RESTRICTIONS_DIMENSIONS"][2]) ? $arData["RESTRICTIONS_DIMENSIONS"][2] : 0), $update)) {
             $APPLICATION->ThrowException("Can't save delivery restriction by dimensions");
             return false;
         }
     } elseif ($update) {
         \Bitrix\Sale\Delivery\Restrictions\Manager::deleteByDeliveryIdClassName($id, '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByDimensions');
     }
     if (floatval($arData["RESTRICTIONS_MAX_SIZE"]) > 0) {
         if (!self::saveRestrictionByMaxSize($id, $arData["RESTRICTIONS_MAX_SIZE"], $update)) {
             $APPLICATION->ThrowException("Can't save delivery restriction by maxx size", "RESTRICTIONS_MAX_SIZE");
             return false;
         }
     } elseif ($update) {
         \Bitrix\Sale\Delivery\Restrictions\Manager::deleteByDeliveryIdClassName($id, '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByMaxSize');
     }
     return $id;
 }