{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!--// $Id$ //-->

{$gps_coordinate_url = ConfigHelper::getConfig('phpui.gps_coordinate_url', 'https://www.google.com/maps/search/?api=1&query=%latitude%2C%longitude')}

<h1>{$layout.pagetitle}</h1>

<style>

    div.lms-ui-filter-container {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-wrap: wrap;
    }

    div.lms-ui-filter-container div.lms-ui-filter-definitions {
        display: flex;
        flex-wrap: nowrap;
        flex-direction: column;
        align-items: flex-start;
        max-width: 80%;
    }

    div.lms-ui-filter-container div.lms-ui-filter-buttons {
        flex-grow: 1;
    }

    div.lms-ui-filter-container div.lms-ui-filter-definition {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        white-space: normal;
    }

    div.lms-ui-filter-container div.lms-ui-filter-definition > .filter-item {
        margin: 0.2em;
        display: inline-flex;
        flex-wrap: nowrap;
        align-items: center;
    }

    div.lms-ui-filter-container div.lms-ui-filter-definition > .filter-item > :not(:last-child) {
        margin-right: 0.5em;
    }

    div.lms-ui-filter-container div.lms-ui-filter-definition > .filter-item > * > :not(:first-child) {
        margin-left: 0.5em;
    }

    div.lms-ui-filter-container div.lms-ui-filter-buttons .filter-button {
        cursor: pointer;
    }

    div.lms-ui-filter-container div.lms-ui-filter-buttons..filter-button:not(:last-child) {
        margin-left: 0.5em;
    }

    .operation-panels {
        display: flex;
        flex-direction: column;
        width: fit-content;
    }

    .operation-panels.delete-check-all-panel {
        width: auto;
    }

    .operation-panel {
        display: flex;
        flex-wrap: wrap;
        flex-grow: 1;
        align-items: center;
    }

    .operation-panel > * {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        margin: 0.4em;
    }

    .operation-panel > * > :not(:first-child) {
        margin-left: 0.5em;
    }

    .operation-panel:nth-child(3) {
        justify-content: center;
    }

    #delete-check-all-panel {
        justify-content: flex-end;
    }

    .summary {
        border-top: none;
    }

    .lms-ui-hint-content > table {
        margin-top: 0.5em;
        margin-bottom: 0.5em;
    }

</style>

<form name="netranges" id="netranges" method="POST">
    <input type="submit" class="hiddenbtn">
</form>

<form name="update-range" id="update-range" method="POST" action="?m={$layout.module}">
    <input type="submit" class="hiddenbtn">
</form>

<table class="lmsbox lms-ui-background-cycle">
    <colgroup>
        <col style="width: 1%;">
        <col style="width: 1%;">
        <col style="width: 1%;">
        <col style="width: 1%;">
        <col style="width: 1%;">
        <col style="width: 1%;">
        <col style="width: 94%;">
    </colgroup>
    <thead>
        <tr>
            <td colspan="7">
                <div class="lms-ui-filter-container">
                    <div class="lms-ui-filter-definitions">
                        <div class="lms-ui-filter-definition">
                            {icon name="filter" class="fa-fw"}
                            <label class="filter-item">
                                <span>{trans("State")}</span>
                                <select name="filter[stateid]" form="netranges" class="lms-ui-advanced-select-test filter-element"
                                    data-placeholder="{trans("— none —")}"
                                    data-options='{ "allow_single_deselect": true }'>
                                    <option value=""></option>
                                    {foreach $boroughs as $state}
                                        <option value="{$state.id}"{if $state.id == $filter.stateid} selected{/if}
                                            >{$state.label|escape}</option>
                                    {/foreach}
                                </select>
                            </label>
                            <label class="filter-item"{if empty($filter.stateid)} style="display: none;"{/if}>
                                <span>{trans("District")}</span>
                                <select name="filter[districtid]" form="netranges" class="lms-ui-advanced-select-test filter-element"
                                    data-placeholder="{trans("— none —")}"
                                    data-options='{ "allow_single_deselect": true }'>
                                    <option value=""></option>
                                    {if !empty($filter.stateid)}
                                        {foreach $boroughs["id-"|cat:$filter.stateid].districts as $district}
                                            <option value="{$district.id}"{if $district.id == $filter.districtid} selected{/if}
                                                >{$district.label|escape}</option>
                                        {/foreach}
                                    {/if}
                                </select>
                            </label>
                            <label class="filter-item"{if empty($filter.districtid)} style="display: none;"{/if}>
                                <span>{trans("Commune")}</span>
                                <select name="filter[boroughid]" form="netranges" class="lms-ui-advanced-select-test filter-element"
                                    data-placeholder="{trans("— none —")}"
                                    data-options='{ "allow_single_deselect": true }'>
                                    <option value=""></option>
                                    {if !empty($filter.districtid)}
                                        {foreach $boroughs["id-"|cat:$filter.stateid].districts["id-"|cat:$filter.districtid].boroughs as $borough}
                                            <option value="{$borough.id}"{if $borough.id == $filter.boroughid} selected{/if}
                                                >{$borough.label|escape}</option>
                                        {/foreach}
                                    {/if}
                                </select>
                            </label>
                            <label class="filter-item"{if empty($filter.boroughid)} style="display: none;"{/if}>
                                <span>{trans("City")}</span>
                                <select name="filter[cityid]" form="netranges" class="lms-ui-advanced-select-test filter-element"
                                    data-placeholder="{trans("— none —")}"
                                    data-options='{ "allow_single_deselect": true }'>
                                    <option value=""></option>
                                    {if !empty($filter.boroughid)}
                                        {foreach $cities as $city}
                                            <option value="{$city.id}"{if $city.id == $filter.cityid} selected{/if}
                                                >{$city.label|escape}</option>
                                        {/foreach}
                                    {/if}
                                </select>
                            </label>
                            <label class="filter-item"{if empty($filter.cityid)} style="display: none;"{/if}>
                                <span>{trans("Street")}</span>
                                <select name="filter[streetid]" form="netranges" class="lms-ui-advanced-select-test filter-element"
                                    data-placeholder="{trans("— none —")}"
                                    data-options='{ "allow_single_deselect": true }'>
                                    <option value=""></option>
                                    {if !empty($filter.cityid)}
                                        {foreach $streets as $street}
                                            <option value="{$street.id}"{if $street.id == $filter.streetid} selected{/if}
                                                >{$street.label|escape}</option>
                                        {/foreach}
                                    {/if}
                                </select>
                            </label>
                            <label class="filter-item"{if empty($filter.cityid) || !empty($streets) && empty($filter.streetid)} style="display: none;"{/if}>
                                <span>{trans("Numbers")}</span>
                                <select name="filter[numberparity]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    <option value="odd"{if $filter.numberparity == 'odd'} selected{/if}>{trans("odd")}</option>
                                    <option value="even"{if $filter.numberparity == 'even'} selected{/if}>{trans("even")}</option>
                                </select>
                            </label>
                        </div>
                        <div class="lms-ui-filter-definition">
                            {icon name="filter" class="fa-fw"}
                            <label class="filter-item">
                                {trans("<!netrange>Project")}
                                <select name="filter[project]" form="netranges" class="filter-element">
                                    <option value="">{trans("<!invproject>— ignore —")}</option>
                                    <option value="-1"{if $filter['project'] == -1} selected{/if}>{trans("<!invproject>— any —")}</option>
                                    <option value="-2"{if $filter['project'] == -2} selected{/if}>{trans("<!invproject>— without project —")}</option>
                                    {foreach $invprojects as $invproject}
                                        <option value="{$invproject.id}"{if $invproject.id == $filter['project']} selected{/if}>{$invproject.name|escape}</option>
                                    {/foreach}
                                </select>
                            </label>
                            <label class="filter-item">
                                {trans("<!netrange>Without ranges")}
                                <select name="filter[without-ranges]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    <option value="1"{if $filter['without-ranges'] == 1} selected{/if}>{trans("yes")}</option>
                                    <option value="2"{if $filter['without-ranges'] == 2} selected{/if}>{trans("no")}</option>
                                </select>
                            </label>
                            <label class="filter-item">
                                {trans("<!netrange>Existing")}
                                <select name="filter[existing]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    <option value="1"{if $filter['existing'] == 1} selected{/if}>{trans("yes")}</option>
                                    <option value="2"{if $filter['existing'] == 2} selected{/if}>{trans("no")}</option>
                                </select>
                            </label>
                            <label class="filter-item">
                                {trans("<!netrange>Link")}
                                <select name="filter[linktype]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    {foreach $linktechnologies as $linktype => $linktech}
                                        <option value="{$linktype}"{if $linktype === $filter.linktype} selected{/if}>{$_LINKTYPES[$linktype]}</option>
                                    {/foreach}
                                </select>
                            </label>
                            <label class="filter-item" {if $filter.linktype == ''} style="display: none;"{/if}>
                                {trans("<!netrange>Technology")}
                                <select name="filter[linktechnology]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    {if $filter.linktype != ''}
                                        {foreach $linktechnologies[$filter.linktype] as $technology => $label}
                                            <option value="{$technology}"{if $technology == $filter.linktechnology} selected{/if}>{$label}</option>
                                        {/foreach}
                                    {/if}
                                </select>
                            </label>
                            <label class="filter-item" {if $filter.linktype == '' || $filter.linktechnology == ''} style="display: none;"{/if}>
                                {trans("Download")}
                                <select name="filter[downlink]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    {foreach $linkspeeds as $speed => $label}
                                        <option value="{$speed}"{if $speed == $filter.downlink} selected{/if}>{$label}</option>
                                    {/foreach}
                                </select>
                            </label>
                            <label class="filter-item" {if $filter.linktype == '' || $filter.linktechnology == ''} style="display: none;"{/if}>
                                {trans("Upload")}
                                <select name="filter[uplink]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    {foreach $linkspeeds as $speed => $label}
                                        <option value="{$speed}"{if $speed == $filter.uplink} selected{/if}>{$label}</option>
                                    {/foreach}
                                </select>
                            </label>
                            <label class="filter-item">
                                {trans("<!netrange>Type")}
                                <select name="filter[type]" form="netranges" class="filter-element">
                                    <option value="">{trans("— any —")}</option>
                                    <option value="1"{if $filter.type == 1} selected{/if}>{trans("<!netrange>real")}</option>
                                    <option value="2"{if $filter.type == 2} selected{/if}>{trans("<!netrange>theoretical")}</option>
                                </select>
                            </label>
                            <span class="filter-item">
                                <span>
                                    {trans("<!netrange>Services")}
                                </span>
                                <label>
                                    <input type="hidden" name="filter[services][1]" value="0" form="netranges">
                                    <input type="checkbox" name="filter[services][1]" value="1" form="netranges"
                                        class="filter-element"
                                        {if ($filter.services & 1) > 0} checked{/if}>
                                    {trans("<!netrange>wholesale")}
                                </label>
                                <label>
                                    <input type="hidden" name="filter[services][2]" value="0" form="netranges">
                                    <input type="checkbox" name="filter[services][2]" value="1" form="netranges"
                                        class="filter-element"
                                        {if ($filter.services & 2) > 0} checked{/if}>
                                    {trans("<!netrange>retail")}
                                </label>
                            </span>
                            <label class="filter-item">
                                <span{if empty($foreign_entities)} class="lms-ui-disabled"{/if}>
                                    {trans("<!netrange>Foreign entity")}
                                </span>
                                <select name="filter[foreign-entity]" form="netranges" class="filter-element"
                                    {if empty($foreign_entities)} disabled{/if}>
                                    <option value="">{trans("<!netrange-foreign-entity>— any —")}</option>
                                    <option value="-1"{if $filter['foreign-entity'] == '-1'} selected{/if}>{trans("<!netrange-foreign-entity>— none —")}</option>
                                    {foreach $foreign_entities as $foreign_entity_id => $foreign_entity_name}
                                        <option value="{$foreign_entity_id|escape}"
                                            {if $filter['foreign-entity'] == $foreign_entity_id} selected{/if}>{if isset($foreign_entity_name)}{$foreign_entity_name|escape}{else}{$foreign_entity_id|escape}{/if}</option>
                                    {/foreach}
                                </select>
                            </label>
                        </div>
                    </div>
                    <div class="lms-ui-filter-buttons">
                        {icon name="clear" class="filter-button" id="clear-filter"}
                        {icon name="next" class="filter-button" id="apply-filter"}
                    </div>
                </div>
            </td>
        </tr>
        {if $pagination->getTotal() != 0}
            <tr>
                <td class="lms-ui-pagination" colspan="7">
                    {include file="pagination.html" form="netranges"}
                </td>
            </tr>
        {/if}
        <tr>
            <td>
                <strong>{trans("Location")}</strong>
                <br>
                <strong>{trans("Territory unit")}</strong>
            </td>
            <td>
                <span class="nobr">
                    <strong>{trans("GPS coordinates")}</strong>
                </span>
                <br>
                <strong>{trans("<!netrange>Project")}</strong>
            </td>
            <td class="nobr">
                <strong>{trans("Link type")}</strong>
                <br>
                <strong>{trans("<!netrange>Link technology")}</strong>
            </td>
            <td class="nobr">
                <strong>{trans("Download")}</strong>
                <br>
                <strong>{trans("Upload")}</strong>
            </td>
            <td class="nobr">
                <strong>{trans("Type")}</strong>
                <br>
                <strong>{trans("<!netrange>Services")}</strong>
                <br>
                <strong>{trans("<!netrange>Foreign entity")}</strong>
            </td>
            <td class="nobr">
                <strong>{trans("<!netrange>Existing")}</strong>
                <br>
                <strong>{trans("<!netrange>Major technology")}</strong>
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
    </thead>
    <tbody class="lms-ui-multi-check">
        {foreach $buildings as $building}
            <tr class="highlight"{if !empty($building.netrangeid)} data-netrangeid="{$building.netrangeid}"{/if}>
                <td class="nobr">
                    {$building.zip} {$building.city|escape},
                    {if strlen($building.rstreet)}
                        {$building.rstreet|escape}
                    {/if}
                    {$building.building_num|escape}
                    <br>
                    <span class="blend">
                        ({$building.state|escape} / {$building.district|escape} / {$_BOROUGHTYPES[$building.boroughtype]} {$building.borough|escape})
                    </span>
                </td>
                <td class="nobr">
                    <a href="{$gps_coordinate_url|replace:"%longitude":$building.longitude|replace:"%latitude":$building.latitude}">
                        {icon name="location" class="fa-fw"}
                    </a>
                    {$building.longitude}, {$building.latitude}
                    <br>
                    {if !empty($building.netrangeid)}
                        {if empty($building.invprojectid)}
                            —
                        {else}
                            <span class="nobr">
                                <a href="?m=invprojectedit&id={$building.invprojectid}">
                                    {icon name="money" class="fa-fw"}
                                    (#{$building.invprojectid})
                                    {$invprojects[$building.invprojectid].name|escape}
                                </a>
                            </span>
                        {/if}
                    {/if}
                </td>
                <td>
                    {if !empty($building.netrangeid)}
                        <span class="nobr">{$_LINKTYPES[$building.linktype]}</span>
                    {/if}
                    {if !empty($building.netrangeid)}
                        <br>
                        <span class="nobr">{$linktechnologies[$building.linktype][$building.linktechnology]}</span>
                    {/if}
                </td>
                <td>
                    {if !empty($building.netrangeid)}
                        <span class="nobr">{$linkspeeds[$building.downlink]}</span>
                    {/if}
                    {if !empty($building.netrangeid)}
                        <br>
                        <span class="nobr">{$linkspeeds[$building.uplink]}</span>
                    {/if}
                </td>
                <td>
                    {if !empty($building.netrangeid)}
                        <span class="nobr">
                            {if $building.type == 1}
                                {trans("<!netrange>real")}
                            {elseif $building.type == 2}
                                {trans("<!netrange>theoretical")}
                            {/if}
                        </span>
                    {/if}
                    {if !empty($building.netrangeid)}
                        <br>
                        <span class="nobr">{strip}
                            {if ($building.services & 1) > 0}
                                {trans("<!netrange>wholesale")}
                            {/if}
                            {if ($building.services & 2) > 0}
                                {if ($building.services & 1) > 0}
                                    ,&nbsp;
                                {/if}
                                {trans("<!netrange>retail")}
                            {/if}
                        {/strip}</span>
                    {/if}
                    {if !empty($building.netrangeid)}
                        <br>
                        <span class="nobr">
                            {if strlen($building.foreignentity)}
                                {if isset($foreign_entities[$building.foreignentity])}
                                    {$foreign_entities[$building.foreignentity]|escape}
                                {else}
                                    {$building.foreignentity|escape}
                                {/if}
                            {else}
                                —
                            {/if}
                        </span>
                    {/if}
                </td>
                <td class="text-center">
                    {if !empty($building.existing)}
                        {capture assign="hint"}
                            {if !empty($building.linktechnologies)}
                                <table class="lmsbox-inner lms-ui-background-cycle">
                                    <caption><strong>{trans("<!netrange>Technologies")}</strong></caption>
                                    <thead>
                                        <tr>
                                            <td>
                                                <strong>{trans("<!netrange>Technology")}</strong>
                                            </td>
                                            <td class="text-center">
                                                <strong>{trans("<!netrange>Node count")}</strong>
                                            </td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {foreach $building.linktechnologies as $linktechnology => $count}
                                            <tr>
                                                <td>
                                                    {if $linktechnology < 100}
                                                        {$_LINKTECHNOLOGIES[$smarty.const.LINKTYPE_WIRE][$linktechnology]} ({$_LINKTYPES[$smarty.const.LINKTYPE_WIRE]})
                                                    {elseif $linktechnology < 200}
                                                        {$_LINKTECHNOLOGIES[$smarty.const.LINKTYPE_WIRELESS][$linktechnology]} ({$_LINKTYPES[$smarty.const.LINKTYPE_WIRELESS]})
                                                    {else}
                                                        {$_LINKTECHNOLOGIES[$smarty.const.LINKTYPE_FIBER][$linktechnology]} ({$_LINKTYPES[$smarty.const.LINKTYPE_FIBER]})
                                                    {/if}
                                                </td>
                                                <td class="text-center">
                                                    {$count}
                                                </td>
                                            </tr>
                                        {/foreach}
                                    </tbody>
                                </table>
                            {/if}
                            {if !empty($building.customers)}
                                <table class="lmsbox-inner lms-ui-background-cycle">
                                    <caption><strong>{trans("<!netrange>Customers")}</strong></caption>
                                    <thead>
                                        <tr>
                                            <td>
                                                <strong>{trans("<!netrange>Customer name")}</strong>
                                            </td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {foreach $building.customers as $customer}
                                            <tr>
                                                <td>
                                                    <a href="?m=customerinfo&id={$customer.id}">{$customer.name|escape} (#{$customer.id})</a>
                                                </td>
                                            </tr>
                                        {/foreach}
                                    </tbody>
                                </table>
                            {/if}
                        {/capture}
                        {hint icon="checked" text=$hint}
                    {/if}
                    <br>
                    {if !empty($building.linktechnologies)}
                        {$linktechnology = key($building.linktechnologies)}
                        {if $linktechnology < 100}
                            {$_LINKTECHNOLOGIES[$smarty.const.LINKTYPE_WIRE][$linktechnology]} ({$_LINKTYPES[$smarty.const.LINKTYPE_WIRE]})
                        {elseif $linktechnology < 200}
                            {$_LINKTECHNOLOGIES[$smarty.const.LINKTYPE_WIRELESS][$linktechnology]} ({$_LINKTYPES[$smarty.const.LINKTYPE_WIRELESS]})
                        {else}
                            {$_LINKTECHNOLOGIES[$smarty.const.LINKTYPE_FIBER][$linktechnology]} ({$_LINKTYPES[$smarty.const.LINKTYPE_FIBER]})
                        {/if}
                    {/if}
                </td>
                <td class="text-right">
                    {if !empty($building.netrangeid)}
                        <input type="checkbox" name="range[ranges][{$building.netrangeid}]" value="{$building.id}"
                            form="update-range" class="lms-ui-multi-check">
                    {else}
                        <input type="checkbox" name="range[buildings][{$building.buildingid}]" value="{$building.id}"
                               form="update-range" class="lms-ui-multi-check">
                    {/if}
                </td>
            </tr>
        {foreachelse}
            <tr>
                <td colspan="7" class="empty-table">
                    {if empty($filter.stateid) || empty($filter.districtid)}
                        {trans("State and district should be selected at least.")}
                    {else}
                        {trans("No such buildings matching search criteria or list is empty.")}
                    {/if}
                </td>
            </tr>
        {/foreach}
    </tbody>
    <tfoot>
        <tr>
            <td colspan="7" class="text-center summary">
                <strong>
                    {t a=$unique_total}<!netrange>Buildings: $a{/t},
                    {t a=$ranges}<!netrange>Ranges: $a{/t},
                    {t a=$existing}<!netrange>Existing: $a{/t}
                </strong>
            </td>
        </tr>
        {if $pagination->getTotal() != 0}
            <tr>
                <td class="lms-ui-pagination" colspan="7">
                    {include file="pagination.html" form="netranges"}
                </td>
            </tr>
        {/if}
        <tr>
            <td colspan="7">
                <table style="width: 100%;">
                    <tr>
                        <td class="text-left">
                            <div class="operation-panels">
                                <div class="operation-panel">
                                    <label>
                                        {trans("<!netrange>Project")}
                                        <select name="range[project]" form="update-range">
                                            <option value="">{trans("<!invproject>— none —")}</option>
                                            {foreach $invprojects as $invproject}
                                                <option value="{$invproject.id}"{if $invproject.id == $range.project} selected{/if}>{$invproject.name|escape}</option>
                                            {/foreach}
                                        </select>
                                    </label>
                                    <label>
                                        {trans("<!netrange>Link")}
                                        <select name="range[linktype]" form="update-range" required>
                                            <option value="">{trans("— none —")}</option>
                                            {foreach $_LINKTYPES as $type => $label}
                                                <option value="{$type}"{if $type === $range.linktype} selected{/if}>{$label}</option>
                                            {/foreach}
                                        </select>
                                    </label>
                                    <label{if $range.linktype == ''} style="display: none;"{/if}>
                                        {trans("<!netrange>Technology")}
                                        <select name="range[linktechnology]" form="update-range" required>
                                            <option value="">{trans("— none —")}</option>
                                            {if $range.linktype != ''}
                                                {foreach $linktechnologies[$range.linktype] as $technology => $label}
                                                    <option value="{$technology}"{if $technology == $range.linktechnology} selected{/if}>{$label}</option>
                                                {/foreach}
                                            {/if}
                                        </select>
                                    </label>
                                    <label {if $range.linktype == '' || $range.linktechnology == ''} style="display: none;"{/if}>
                                        {trans("Download")}
                                        <select name="range[downlink]" form="update-range" required>
                                            <option value="">{trans("— none —")}</option>
                                            {foreach $linkspeeds as $speed => $label}
                                                <option value="{$speed}"{if $speed == $range.downlink} selected{/if}
                                                    {if $range.linktype == $smarty.const.WIRELESS
                                                        && !empty($range.linktechnology)
                                                        && $range.linktechnology == 150
                                                        && $speed < 30} style="display: none;"{/if}>{$label}</option>
                                            {/foreach}
                                        </select>
                                    </label>
                                    <label {if $range.linktype == '' || $range.linktechnology == ''} style="display: none;"{/if}>
                                        {trans("Upload")}
                                        <select name="range[uplink]" form="update-range" required>
                                            <option value="">{trans("— none —")}</option>
                                            {foreach $linkspeeds as $speed => $label}
                                                <option value="{$speed}"{if $speed == $range.uplink} selected{/if}
                                                    >{$label}</option>
                                            {/foreach}
                                        </select>
                                    </label>
                                </div>
                                <div class="operation-panel">
                                    <label>
                                        {trans("<!netrange>Type")}
                                        <select name="range[type]" form="update-range" required>
                                            <option value="">{trans("— none —")}</option>
                                            <option value="1"{if $range.type == 1} selected{/if}>{trans("<!netrange>real")}</option>
                                            <option value="2"{if $range.type == 2} selected{/if}>{trans("<!netrange>theoretical")}</option>
                                        </select>
                                    </label>
                                    <span>
                                        <span>
                                            {trans("<!netrange>Services")}
                                        </span>
                                        <label>
                                            <input type="hidden" name="range[services][1]" value="0" form="update-range">
                                            <input type="checkbox" name="range[services][1]" value="1" class="range-services"
                                                form="update-range" required
                                                {if ($range.services & 1) > 0} checked{/if}>
                                            {trans("<!netrange>wholesale")}
                                        </label>
                                        <label>
                                            <input type="hidden" name="range[services][2]" value="0" form="update-range">
                                            <input type="checkbox" name="range[services][2]" value="2" class="range-services"
                                                form="update-range" required
                                                {if ($range.services & 2) > 0} checked{/if}>
                                            {trans("<!netrange>retail")}
                                        </label>
                                    </span>
                                    <label>
                                        <span{if empty($foreign_entities)} class="lms-ui-disabled"{/if}>
                                            {trans("<!netrange>Foreign entity")}
                                        </span>
                                        <select name="range[foreign-entity]" form="update-range"
                                            {if empty($foreign_entities)} disabled{/if}>
                                            <option value="">{trans("<!netrange-foreign-entity>— none —")}</option>
                                            {foreach $foreign_entities as $foreign_entity_id => $foreign_entity_name}
                                                <option value="{$foreign_entity_id|escape}"
                                                    {if $range['foreign-entity'] == $foreign_entity_id} selected{/if}>{if isset($foreign_entity_name)}{$foreign_entity_name|escape}{else}{$foreign_entity_id|escape}{/if}</option>
                                            {/foreach}
                                        </select>
                                    </label>
                                </div>
                                <div class="operation-panel">
                                    {button id="update-button" icon="apply" label="Update" form="update-range" type="submit"}
                                    {button id="add-button" icon="add" label="Add" form="update-range" type="submit"}
                                </div>
                            </div>
                        </td>
                        <td class="text-right valign-top">
                            <div class="operation-panels delete-check-all-panel">
                                <div class="operation-panel" id="delete-check-all-panel">
                                    {button icon="delete" label="Delete" id="delete-button"}
                                    <label>
                                        {trans("Check All")}
                                        <input type="checkbox" name="allbox" class="lms-ui-multi-check-all" value="1">
                                    </label>
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </tfoot>
</table>

<script>

    $(function() {
        var boroughs = JSON.parse('{json_encode($boroughs, JSON_FORCE_OBJECT)}');

        $('.filter-element').change(function() {
            var filterElements = $('.lms-ui-filter-definition:first-child .filter-element').filter('select');
            var elem;
            if ($(this).val() == '') {
                for (var i = 0; i < filterElements.length; i++) {
                    if ($(filterElements.get(i)).is($(this))) {
                        break;
                    }
                }
                for (var j = i + 1; j < filterElements.length; j++) {
                    elem = $(filterElements.get(j));
                    elem.html('<option value="">{trans("— none —")}</option>');
                    elem.val('');
                    elem.closest('.filter-item').hide();
                    if (elem.is('.lms-ui-advanced-select-test')) {
                        updateAdvancedSelectsTest(elem);
                    }
                }
            } else {
                for (var i = 0; i < filterElements.length; i++) {
                    if ($(filterElements.get(i)).is($(this))) {
                        break;
                    }
                }
                if (i < filterElements.length - 1) {
                    elem = $(filterElements.get(i + 1));
                    var elemName = elem.attr('name');
                    var html ='<option value="">{trans("— none —")}</option>';
                    var options = [];
                    switch ($(this).attr('name')) {
                        case 'filter[stateid]':
                            options = boroughs["id-" + $(this).val()].districts;
                            break;
                        case 'filter[districtid]':
                            options = boroughs["id-" + $('[name="filter[stateid]"]').val()].districts["id-" + $(this).val()].boroughs;
                            break;
                        case 'filter[boroughid]':
                            $.ajax({
                                method: "GET",
                                dataType: "json",
                                data: {
                                    api: 1,
                                    boroughid: $(this).val()
                                },
                                success: function(options) {
                                    $.each(options, function(id, option) {
                                        html += '<option value="' + option.id + '">' + escapeHtml(option.label) + '</option>';
                                    });
                                    elem.html(html);
                                    updateAdvancedSelectsTest(elem);
                                    elem.closest('.filter-item').show();
                                }
                            });
                            break;
                        case 'filter[cityid]':
                            $.ajax({
                                method: "GET",
                                dataType: "json",
                                data: {
                                    api: 1,
                                    cityid: $(this).val()
                                },
                                success: function(options) {
                                    $.each(options, function(id, option) {
                                        html += '<option value="' + option.id + '">' + escapeHtml(option.label) + '</option>';
                                    });
                                    elem.html(html);
                                    updateAdvancedSelectsTest(elem);
                                    elem.closest('.filter-item').show();
                                    elem.closest('.filter-item').next().toggle(!options.length || elem.val() != '').find('.filter-element').val('');
                                }
                            });
                            break;
                        case 'filter[streetid]':
                            elem.closest('.filter-item').toggle($(this).find('option').length <= 1 || $(this).val() != '');
                            break;
                    }
                    if (["filter[stateid]", "filter[districtid]", "filter[boroughid]"].indexOf(elemName) != -1) {
                        $.each(options, function (id, option) {
                            html += '<option value="' + option.id + '">' + escapeHtml(option.label) + '</option>';
                        });
                        elem.html(html);
                        updateAdvancedSelectsTest(elem);
                        elem.closest('.filter-item').show();
                    }
                    for (var j = i + 2; j < filterElements.length; j++) {
                        var elem2 = $(filterElements.get(j));
                        if (elem2.attr('name') != 'filter[numberparity]') {
                            elem2.html('<option value=""></option>');
                            elem2.val('');
                            elem2.closest('.filter-item').hide();
                            if (elem2.is('.lms-ui-advanced-select-test')) {
                                updateAdvancedSelectsTest(elem);
                            }
                        }
                    }
                }
            }
        });

        $('#clear-filter').click(function() {
            $('.filter-element').each(function() {
                if ($(this).is(':checkbox')) {
                    $(this).prop('checked', false);
                } else {
                    $(this).val('');
                }
            });
            $('#netranges').submit();
        });

        $('#apply-filter').click(function() {
            $('#netranges').submit();
        });

        var linkTechnologies = {};
        {foreach $linktechnologies as $type => $technologies}
            {foreach $technologies as $technology => $label}
                if (!linkTechnologies.hasOwnProperty("{$type}")) {
                    linkTechnologies["{$type}"] = {};
                }
                linkTechnologies["{$type}"]["{$technology}"] = "{$label}";
            {/foreach}
        {/foreach}

        $('[name$="[linktype]"]').change(function() {
            var that = this;
            var linktype = $(this).val();
            var prefix = $(this).attr('name').match(/^range/) ? 'range' : 'filter';
            var select = $('[name="' + prefix + '[linktechnology]"]');
            var linktechnology = select.val();
            var html = '<option value="">' + (prefix == 'range' ? '{trans("— none —")}' : '{trans("— any —")}') +'</option>';
            if ($(this).val() !== '') {
                $.each(linkTechnologies[linktype], function (index, label) {
                    html += '<option value="' + index + '"' + (index == linktechnology ? ' selected' : '') + '>' + label + '</option>';
                });
            }
            select.html(html);
            select.prop('required', function() {
                if (prefix == 'range') {
                    return linktype !== '';
                } else {
                    return $(this).prop('required');
                }
            }).closest('label').toggle(linktype !== '');
            $('[name="' + prefix + '[downlink]"],[name="' + prefix + '[uplink]"]').prop('required', false).closest('label').hide();
            if (prefix == 'range') {
                $('[name="range[downlink]"]').each(function() {
                    $(this).find('option').each(function() {
                        var val = $(this).val();
                        $(this).toggle(linktype !== '{$smarty.const.LINKTYPE_WIRELESS}' || val === '' || parseInt(linktechnology) != 150 || parseInt(val) >= 30);
                    });
                });
            }
        }).change();

        $('[name$="[linktechnology]"]').change(function() {
            var prefix = $(this).attr('name').match(/^range/) ? 'range' : 'filter';
            var that = this;
            var linktechnology = $(that).val();
            $('[name="' + prefix + '[downlink]"],[name="' + prefix + '[uplink]"]')
                .prop('required', function() {
                    if (prefix == 'range') {
                        return linktechnology !== '';
                    } else {
                        return $(this).prop('required');
                    }
                }).closest('label').toggle(linktechnology !== '');
            if (prefix == 'range') {
                var linktype = $('[name="range[linktype]"]').val();
                $('[name="range[downlink]"]').each(function() {
                    $(this).find('option').each(function() {
                        var val = $(this).val();
                        $(this).toggle(linktype !== '{$smarty.const.LINKTYPE_WIRELESS}' || val === '' || parseInt(linktechnology) != 150 || parseInt(val) >= 30);
                    });
                });
            }
        }).change();

        var rangeServices = $('.range-services');
        rangeServices.change(function() {
            $.each(rangeServices, function () {
                $(this).prop('required', !rangeServices.filter(':checked').length);
            });
            var foreignEntityDisabled = $(this).val() == '2' && !$(this).prop('checked');
            $('[name="range[foreign-entity]"]').prop('disabled', foreignEntityDisabled).prev().toggleClass('lms-ui-disabled', foreignEntityDisabled);
        }).change();

        $('[name="filter[services][2]"]').change(function() {
            var checked = $(this).prop('checked');
            $('[name="filter[foreign-entity]"]').prop('disabled', !checked).prev().toggleClass('lms-ui-disabled', !checked);
        }).change();

        $('#update-range').submit(function(e) {
            if (!$('.lms-ui-multi-check').find(':checked').length) {
                var button = $('#update-button');

                alertDialog("{trans("No building selected!")}", button);
                button.prop('disabled', false);
                return false;
            }
            if (e.originalEvent) {
                $(this).attr('action', function() {
                    return $(this).attr('action') + '&' +
                        ($(e.originalEvent.submitter).is('#add-button') ? 'add' : 'update') + '=1';
                });
            }
        });

        $('#delete-button').click(function() {
            if ($('.lms-ui-multi-check').find('[data-netrangeid] :checked').length) {
                $(this).prop('disabled', true);
                $('#update-range').attr('novalidate', '').attr('action', function () {
                    return $(this).attr('action') + '&delete=1';
                }).submit();
            } else {
                alertDialog("{trans("No network range selected!")}", this);
                return false;
            }
        });
    });

</script>
{/block}
