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

<style>

	#netnode.lms-ui-box-container .lms-ui-box-row-label {
		width: 12em;
	}
	#netnode.lms-ui-box-container .lms-ui-box-row-field.ownership {
		flex-direction: column !important;
	}

	#netnode-services {
		align-items: flex-start;
	}

	#netnode-services .lms-ui-box-row-field {
		flex-direction: column;
		align-items: flex-start;
	}

	#coowner {
		width: 25em;
	}

</style>

<script src="js/location_box.js"></script>

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

{box_container id="netnode"}
	{box_header icon="netnode" label="Name" content_id="netnode-name"}
		<input type="text" name="netnode[name]" value="{$netnode.name}" maxlength="32" required
			id="netnode-name"
			{tip trigger="name" text="<!netnode>Enter node name" bold=1}>
	{/box_header}

	{box_contents}
		{box_panel}

{if $divisions}
			{box_row icon="division" icon_class="fa-fw" label="Division"}
				<select name="netnode[divisionid]" {tip text="Select division"}>
					{foreach $divisions as $division}
						<option value="{$division.id}"
							{if $netnode.divisionid == $division.id} selected{/if}>{$division.label|escape}</option>
					{/foreach}
				</select>
			{/box_row}
{/if}

			{box_row icon="type" icon_class="fa-fw" label="Type"}
				{$netelementtypes = Localisation::arraySort($_NETELEMENTTYPES)}
				<select name="netnode[type]" {tip text="Enter node type" class="lms-ui-advanced-select-test show-group-labels"}>
					<option value="0">{trans("<!netelemtype>— other —")}</option>
					{foreach $_NETELEMENTTYPEGROUPS as $group => $types}
						<optgroup label="{$group}">
							{foreach $netelementtypes as $idx => $type}
								{if $types[$idx]}
									<option value="{$idx}"
										{if $netnode.type == $idx} selected{/if}>{$type}</option>
								{/if}
							{/foreach}
						</optgroup>
					{/foreach}
				</select>
			{/box_row}

			{box_row icon="status" icon_class="fa-fw" label="Status"}
				<select name="netnode[status]" {tip text="Enter node status"}>
					{foreach $_NETELEMENTSTATUSES as $idx => $status}
						<option value="{$idx}"
							{if $netnode.status == $idx} selected{/if}>{$status}</option>
					{/foreach}
				</select>
			{/box_row}

			{box_row icon="invproject" icon_class="fa-fw" label="Project"}
				<input type="hidden" name="netnode[project]" id="project-value">
				<input type="hidden" name="netnode[projectid]" id="projectid-value">
				<select id="project" data-value="{$netdev.project}"
					data-alt-field="#projectid-value"
					data-alt-invalid-field="#project-value"
					{tip class="lms-ui-combobox" text="Select project for this node" trigger="project"}>
					<option value="0"{if !$netdev.invprojectid} selected{/if}>{trans("— none —")}</option>
					{foreach $NNprojects as $project}
						<option value="{$project.id}"
							{if ($netnode.invprojectid == $project.id)} selected{/if}>{$project.name|escape}</option>
					{/foreach}
				</select>
			{/box_row}

			{box_row icon="user" icon_class="fa-fw" label="Ownership"}
				<select name="netnode[ownership]" id="ownership" value="{$netnode.ownership}"
					{tip text="Select ownership for this node"}>
					{foreach $_NETELEMENTOWNERSHIPS as $idx => $ownership}
						<option value="{$idx}"
							{if $netnode.ownership == $idx} selected{/if}>{$ownership}</option>
					{/foreach}
				</select>
			{/box_row}

			{box_row id="ownername" icon="division" icon_class="fa-fw" label="Foreign entity"}
				<select name="netnode[coowner]" class="lms-ui-combobox" id="coowner"
					data-value="{$netnode.coowner}">
					{foreach $foreign_entities as $foreign_entity}
						<option value="{$foreign_entity.id|escape}"
							{if $foreign_entity.name == $netnode.coowner} selected{/if}
							>{$foreign_entity.name}{if !empty($foreign_entity.type)} ({trans("TEN")} {$foreign_entity.id}){/if}</option>
					{/foreach}
				</select>
			{/box_row}

			{box_row id="customer" icon="customer" icon_class="fa-fw" label="Owner"}
				{if isset($nodenode.id)}
					{customerlist form="netnode-form" customers=$customers selected=$customer.id version=2
						selectname="customer" inputname="netnode[ownerid]" select_id="ownerid_select"
						input_id="ownerid_input" customOnChange="customerChanged()"}
				{else}
					{customerlist form="netnode-form" customers=$customers selected=$netnode.ownerid version=2
						selectname="customer" inputname="netnode[ownerid]" select_id="ownerid_select"
						input_id="ownerid_input" customOnChange="customerChanged()"}
				{/if}
			{/box_row}

			{box_row id="location_select" icon="home" icon_class="fa-fw" label="Location"}
				{if $netnode.ownerid && $netnode.address_id > 0}
					{$selected_address_id = $netnode.address_id}
				{else}
					{$selected_address_id = null}
				{/if}
				{include file="customer/customeraddresses.html" id="customer_addresses" name="netnode[customer_address_id]"
					trigger="address_id" selected_address_id=$selected_address_id}
			{/box_row}

			{box_row id="location_box" icon="home" icon_class="fa-fw" label="Location"}
				{location_box
					prefix               = "netnode"
					address_id           = "{$netnode.address_id}"
					location_name        = "{$netnode.location_name}"
					location_state_name  = "{$netnode.location_state_name}"
					location_state       = "{$netnode.location_state}"
					location_city_name   = "{$netnode.location_city_name}"
					location_city        = "{$netnode.location_city}"
					location_postoffice  = "{$netnode.location_postoffice}"
					location_street_name = "{$netnode.location_street_name}"
					location_street      = "{$netnode.location_street}"
					location_house       = "{$netnode.location_house}"
					location_flat        = "{$netnode.location_flat}"
					location_zip         = "{$netnode.location_zip}"
					location_country_id  = "{$netnode.location_country}"
					teryt                = "{$netnode.teryt}"
					allow_empty_streets = ConfigHelper::checkConfig('teryt.allow_empty_streets', true)
					allow_empty_building_numbers = ConfigHelper::checkConfig('teryt.allow_empty_building_numbers', true)
				}
			{/box_row}

		{/box_panel}
		{box_panel}

			{box_row}
				{icon name="network"}
				<span class="lms-ui-button" id="set_gps">
					{trans("Determine GPS coordinates automatically")}
				</span>
				&nbsp;
				{reset_to_defaults target="#longitude,#latitude"}
			{/box_row}

			{box_row icon="location" icon_class="fa-fw" label="GPS longitude"}
				<input id="longitude" type="text" name="netnode[longitude]" value="{$netnode.longitude}"
					{tip text="Enter node longitude (optional)" trigger="longitude"}>
				{button type="link" icon="map" tip="Click to select GPS coordinates from map"
					onclick="return gpscoordschoosewin(document.netnode.elements['netnode[longitude]'], document.netnode.elements['netnode[latitude]']);"}
			{/box_row}

			{box_row icon="location" icon_class="fa-fw" label="GPS latitude"}
				<input id="latitude" type="text" name="netnode[latitude]" value="{$netnode.latitude}"
					{tip text="Enter node latitude (optional)" trigger="latitude"}>
				{button type="link" icon="map" tip="Click to select GPS coordinates from map"
					onclick="return gpscoordschoosewin(document.netnode.elements['netnode[longitude]'], document.netnode.elements['netnode[latitude]']);"}
			{/box_row}

			{box_row icon="share" icon_class="fa-fw" label="Sharing surface"}
				<label>
					<input type="radio" name="netnode[uip]" value="1"
						{tip text="Możliwość udostępniania innym podmiotom powierzchni w obiekcie, w którym znajduje się węzeł"}
						{if !empty($netnode['uip'])} checked{/if}>
					{trans("yes")}
				</label>
				<label>
					<input type="radio" name="netnode[uip]" value="0"{if empty($netnode['uip'])} checked{/if}>
					{trans("no")}
				</label>
			{/box_row}

			{box_row icon="antenna" icon_class="fa-fw" label="Antennas installation"}
				<label>
					<input type="radio" name="netnode[miar]" value="1"
						{tip text="Możliwość instalacji anten dla radiowej sieci dostępowej lub anten dla radiolinii w obiekcie, w którym znajduje się węzeł"}
						{if !empty($netnode['miar'])} checked{/if}>
					{trans("yes")}
				</label>
				<label>
					<input type="radio" name="netnode[miar]" value="0"
						{if empty($netnode['miar'])} checked{/if}>
					{trans("no")}
				</label>
			{/box_row}

			{box_row icon="options" icon_class="fa-fw" label="Flags"}
				{foreach $_NETWORK_NODE_FLAGS as $flag => $label}
					<label>
						<input type="checkbox" name="netnode[flags][{$flag}]" value="{$flag}"
							{if !empty($netnode.flags[$flag])} checked{/if}>
						{$label}
					</label>
				{/foreach}
			{/box_row}

			{box_row icon="money" icon_class="fa-fw" label="Services" id="netnode-services"}
				{foreach $_NETWORK_NODE_SERVICES as $service => $label}
					<label>
						<input type="checkbox" name="netnode[services][{$service}]" value="{$service}"
							{if !empty($netnode.services[$service])} checked{/if}>
						{$label}
					</label>
				{/foreach}
			{/box_row}

			{box_row icon="note" icon_class="fa-fw" label="Note"}
				<textarea id="info" name="netnode[info]"
					{tip text="Enter notes (optional)"} cols="50" rows="5">{$netnode.info|default:""|escape}</textarea>
			{/box_row}

			{box_row icon="calendar" icon_class="fa-fw" label="Last inspection"}
				<input type="text" name="netnode[lastinspectiontime]" maxlength="10" value="{$netnode.lastinspectiontime}"
					placeholder="{trans("yyyy/mm/dd")}"
					{tip class="lms-ui-date lms-ui-date-unix" text="Enter date in 'yyyy/mm/dd' format or click to choose it from calendar" trigger="lastinspectiontime"}>
			{/box_row}

			{box_row icon="phone" icon_class="fa-fw" label="Administrative contact"}
				<input type="text" name="netnode[admcontact]" maxlength="10" value="{$netnode.admcontact}">
			{/box_row}

		{/box_panel}
	{/box_contents}

	{box_buttons}
		{button type="submit" icon="save" accesskey="s" label="Submit"}
		{button icon="cancel" onclick="location.href='?m=netnodeinfo&id={$netnode.id}';" label="Cancel"}
	{/box_buttons}
{/box_container}

<script>

	initAdvancedSelectsTest('#customer_addresses');

	$(function() {
		$('[name="netnode[name]"]').focus();

		$('#ownership').change(function() {
			var show_selectors = [];
			var hide_selectors = [];
			var customerid = $('#ownerid_input').val();
			if ([ '1', '2' ].indexOf($(this).val()) != -1) {
				show_selectors = [ '#ownername', '#customer' ];
				if (customerid == 'undefined' || customerid == 0) {
					show_selectors.push('#location_box');
					hide_selectors.push('#location_select');
				} else {
					hide_selectors.push('#location_box');
					show_selectors.push('#location_select');
				}
			} else {
				hide_selectors = [ '#ownername', '#customer', '#location_select' ];
				show_selectors = [ '#location_box' ];
			}
			$(show_selectors.join(',')).show();
			$(hide_selectors.join(',')).hide();
		}).change();

		/*
         * \brief Update address location update. Require google API key.
         */
		$( '#set_gps' ).click( function() {
			var location = null;

			if ($('[name="netnode[address_id]"]').val() > 0) {
				location = {
					address_id: $('[name="netnode[address_id]"]').val()
				}
			} else if ($('[data-address="city"]').val().length) {
				location = {
					street: $('[data-address="street"]').val(),
					house: $('[data-address="house"]').val(),
					flat: $('[data-address="flat"]').val()
				}
				if ($('[data-address="teryt-checkbox"]').prop('checked') && $('[data-address="city-hidden"]').val() > 0) {
					location.city_id = $('[data-address="city-hidden"]').val();
					location.street_id = $('[data-address="street-hidden"]').val();
				} else {
					location.state = $('[data-address="state"]').val();
					location.zip = $('[data-address="zip"]').val();
					location.city = $('[data-address="city"]').val();
				}
			}

			if (location) {
				xajax_get_gps_coordinates(location, '#latitude', '#longitude');
			}
		});
	});

	function customerChanged() {
		$('#ownership').change();

		getCustomerAddresses($('#ownerid_input').val(), function(addresses) {
			setAddressList('#customer_addresses', addresses);
		});
	};

</script>
{/block}
