<script src="js/location_box.js"></script>
{include file="google-maps.html"}

<style>

	#netdev.lms-ui-box-container .lms-ui-box-row-label {
		width: 12em;
	}

</style>

<script type="text/javascript" src="js/location_box.js"></script>

<form name="netdev" id="netdev" method="post" action="?m={$layout.module}{if $layout.module == 'netdevedit'}&id={$netdev.id}{/if}">
	<input type="submit" class="hiddenbtn">
	<input type="hidden" name="netdev[pagetitle]" value="{$netdev.pagetitle|escape}">

	{box_container id="netdev"}
		{box_header icon="netdev" icon_class="fa-fw" label="Name"}
			<input type="text" name="netdev[name]" value="{$netdev.name}" maxlength="100" required
				{tip class="lms-ui-autogrow" trigger="name" bold=1}>
		{/box_header}

		{box_contents}
			{box_panel}

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

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

				{box_row icon="hardware" icon_class="fa-fw" label="Manufacturer"}
					<input type="hidden" id="producer-value" name="netdev[producer]">
					<input type="hidden" id="producerid-value" name="netdev[producerid]">
					<select id="producer" maxlength="100"
							data-value="{$netdev.producer}"
							data-id-value="{$netdev.producerid}"
							data-alt-field="#producerid-value"
							data-alt-invalid-field="#producer-value"
							{tip class="lms-ui-combobox" trigger="producer"}>
						{foreach $producers as $producer}
							<option value="{$producer.id}" {if $producer.id == $netdev.producerid} selected{/if}>{$producer.name}</option>
						{/foreach}
					</select>
				{/box_row}

				{box_row icon="hardware" icon_class="fa-fw"  label="Model"}
					<input type="hidden" id="model-value" name="netdev[model]">
					<input type="hidden" id="modelid-value" name="netdev[modelid]">
					<select id="model" maxlength="100"
							data-value="{$netdev.model}"
							data-id-value="{$netdev.modelid}"
							data-alt-field="#modelid-value"
							data-alt-invalid-field="#model-value"
							{tip class="lms-ui-combobox" trigger="model"}></select>
				{/box_row}

				{box_row icon="sn" icon_class="fa-fw" label="Serial number"}
					<input type="text" name="netdev[serialnumber]" value="{$netdev.serialnumber}"
						{tip}>
				{/box_row}

				{box_row icon="port" icon_class="fa-fw" label="Number of ports"}
					<input type="text" name="netdev[ports]" value="{if $netdev.ports}{$netdev.ports}{/if}"
							{tip trigger="ports"}>
				{/box_row}

				{box_row icon="customer" icon_class="fa-fw" label="Owner"}
					{if isset($customer.id)}
						{customerlist form="netdev" customers=$customers selected=$customer.id version=2
							selectname="customer" inputname="netdev[ownerid]" select_id="ownerid_select"
							input_id="ownerid_input" customOnChange="customerChanged()"}
					{else}
						{customerlist form="netdev" customers=$customers selected=$netdev.ownerid version=2
							selectname="customer" inputname="netdev[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 $netdev.address_id > 0 && $netdev.ownerid}
						{$selected_address_id = $netdev.address_id}
					{else}
						{$selected_address_id = null}
					{/if}
					{include file="customer/customeraddresses.html" id="customer_addresses" name="netdev[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="netdev" address_id="{$netdev.address_id}"
						location_name="{$netdev.location_name}"
						location_state_name="{$netdev.location_state_name}"
						location_state="{$netdev.location_state}"
						location_city_name="{$netdev.location_city_name}"
						location_city="{$netdev.location_city}"
						location_postoffice="{$netdev.location_postoffice}"
						location_street_name="{$netdev.location_street_name}"
						location_street="{$netdev.location_street}"
						location_house="{$netdev.location_house}"
						location_flat="{$netdev.location_flat}"
						location_zip="{$netdev.location_zip}"
						location_country_id="{$netdev.location_country_id}"
						teryt="{$netdev.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_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="netdev[longitude]" value="{$netdev.longitude}"
							{tip trigger="longitude"}>
					{button type="link" icon="map" tip="Click to select GPS coordinates from map"
						onClick="return gpscoordschoosewin(document.netdev.elements['netdev[longitude]'], document.netdev.elements['netdev[latitude]']);"}
				{/box_row}

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

{if ConfigHelper::checkConfig('phpui.radius')}
				{box_row icon="radius" icon_class="fa-fw" label="Shortname"}
					<input type="text" name="netdev[shortname]" value="{$netdev.shortname}" maxlength="32"
							{tip trigger="shortname"}>
				{/box_row}

				{box_row icon="radius" icon_class="fa-fw" label="Type"}
					<select name="netdev[nastype]" {tip trigger="nastype"}>
						<option value="0"></option>
						{foreach $nastypes as $key => $item}
							<option value="{$key}"{if $key == $netdev.nastype} selected{/if}>{$item.name}</option>
						{/foreach}
					</select>
				{/box_row}

				{box_row icon="radius" icon_class="fa-fw" label="Max clients"}
					<input type="text" name="netdev[clients]" value="{if $netdev.clients}{$netdev.clients}{/if}"
							{tip trigger="clients"}>
				{/box_row}

				{box_row icon="radius" icon_class="fa-fw" label="<!radius>Login"}
					<input type="text" name="netdev[login]" value="{$netdev.login}" maxlength="60">
				{/box_row}

				{box_row icon="radius" icon_class="fa-fw" label="Secret"}
					<input type="text" name="netdev[secret]" value="{$netdev.secret}" maxlength="60">
				{/box_row}

				{box_row icon="radius" icon_class="fa-fw" label="Community"}
					<input type="text" name="netdev[community]" value="{$netdev.community}" maxlength="50">
				{/box_row}
{/if}

{if isset($channels)}
				{box_row icon="img/channel.gif" label="Channel:"}
					<select name="netdev[channelid]" {tip text="Select channel (optional)" trigger="channelid"}>
						<option value="">{trans("— none —")}</option>
						{foreach $channels as $item}
							<option value="{$item.id}"
								{if $item.id == $netdev.channelid} selected{/if}>{$item.name}</option>
						{/foreach}
					</select>
				{/box_row}
{/if}

			{/box_panel}
			{box_panel}

				{box_row icon="calendar" icon_class="fa-fw" label="Purchase date"}
					<input type="text" name="netdev[purchasetime]" value="{if $netdev.purchasetime}{$netdev.purchasetime}{/if}"
						size="10" maxlength="10"
						placeholder="{trans(" yyyy/mm/dd")}"
						{tip class="lms-ui-date lms-ui-date-unix" trigger="purchasetime" text="Enter device purchase date in 'yyyy/mm/dd' format or click to choose date from calendar (optional)"}>
				{/box_row}

				{box_row icon="warranty" icon_class="fa-fw" label="Warranty"}
					<select size="1" name="netdev[guaranteeperiod]" {tip}>
						{foreach $_GUARANTEEPERIODS as $key => $period}
							<option value="{$key}" {if $netdev.guaranteeperiod == $key} selected{/if}>{$period}</option>
						{/foreach}
					</select>
				{/box_row}

				{box_row icon="description" icon_class="fa-fw" label="Description"}
					<textarea name="netdev[description]" cols="40" rows="5"
						>{$netdev.description|escape}</textarea>
				{/box_row}

				{box_row icon="invproject" icon_class="fa-fw" label="Investment project"}
					<input type="hidden" name="netdev[project]" id="project-value">
					<input type="hidden" name="netdev[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" trigger="project"}>
						<option value="0" {if !$netdev.invprojectid} selected{/if}>{trans("— none —")}</option>
						<option value="1" {if $netdev.invprojectid == 1} selected{/if}>{trans("From root device")}</option>
						{foreach $NNprojects as $project}
							<option value="{$project.id}"
								{if ($netdev.invprojectid == $project.id)} selected{/if}>{$project.name}</option>
						{/foreach}
					</select>
				{/box_row}

				{box_row icon="netnode" icon_class="fa-fw" label="Network Node"}
					<select name="netdev[netnodeid]" {tip class="lms-ui-advanced-select-test"}>
						<option value="-1">{trans("None")}</option>
						{foreach $NNnodes as $node}
							<option value="{$node.id}"
								{if ($netdev.netnodeid == $node.id)} selected{/if}>{$node.name}</option>
						{/foreach}
					</select>
				{/box_row}

			{/box_panel}
		{/box_contents}

		{box_buttons}
			{block name="netdevmodify-buttons"}
				{button type="submit" id="submit_button" icon="save" accesskey="s" label="Submit"}
				{button icon="cancel" onclick="location.href = '?m=netdevlist';" label="Cancel"}
			{/block}
		{/box_buttons}
	{/box_container}
</form>

<script>

	var models = JSON.parse('{json_encode($models)}');

	function initModels(producerid) {
		var values = [];
		$.each(models[producerid], function (key, item) {
			values.push({
				value: item.id,
				text: item.name
			});
		});
		$('#model').scombobox('fill', values);
	}

	function updateModels() {
		var producerid = $('#producerid-value').val();
		if (producerid.length) {
			initModels(producerid);
			$('#model').scombobox('val', '');
		} else {
			$('#model').scombobox('fill', []);
		}
	}

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

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

			if (owner_id != 'undefined' && owner_id > 0 && $('#customer_addresses').val() > 0) {
				location = {
					address_id: $('#customer_addresses').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');
			}
		});

		$('#producer').scombobox('change', function () {
			updateModels();
		});
/*
		$("#modelselect").click(function () {
			netdevmodelchoosewin('netdev', 'netdev', $('#netdevmodelid'), $('#producer').val(), $('#model').val());
			event.preventDefault();
		});
*/

		{if $netdev.producerid}
			initModels({$netdev.producerid});
			$('#model').scombobox('val', '{if $netdev.modelid}{$netdev.modelid}{else}{$netdev.model}{/if}');
		{else}
			$('#producer').scombobox('val', '{$netdev.producer}');
		{/if}
	});

	/*!
     * \brief Show / hide location choose box.
     * If owner id is set then show list with customer addresses,
     * else show location box to manual set location for network device.
     *
     * \param customer_id
     */
	function updateLocationBoxesDisplay(customer_id) {
		if (customer_id == 'undefined' || customer_id == 0) {
			$('#location_select').hide();
			$('#location_box').show();
		} else {
			$('#location_select').show();
			$('#location_box').hide();
		}
	}

	initAdvancedSelectsTest('#customer_addresses');

	/*!
     * \brief Update customer addresses list on input change.
     */
	function customerChanged() {
		var id = $('#ownerid_input').val();

		updateLocationBoxesDisplay(id);

		getCustomerAddresses(id, function (addresses) {
			setAddressList('#customer_addresses', addresses);
		});
	}

	updateLocationBoxesDisplay({intval($netdev.ownerid)});

</script>
