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

<style>

	[name="division[account]"] {
		margin-top: 0.2em;
	}

</style>

<H1>{$layout.pagetitle|escape}</H1>
<script src="js/location_box.js"></script>
<script src="js/location_box_expandable.js"></script>

{include file="customer/customercompanyselectiondialog.html"}

<form method="POST" action="?m=divisionedit&id={$division.id}" name="division">
<input type="submit" class="hiddenbtn">
<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 98%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				{icon name="division"}
			</td>
			<td>
				<strong>{trans("Name:")}</strong>
			</td>
			<td>
				<input type="text" name="division[shortname]" value="{$division.shortname|escape}" size="40"
					{tip text="Enter division short name" trigger="shortname" bold=1} required>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				{icon name="division"}
			</td>
			<td class="nobr">
				<strong>{trans("Long name:")}</strong>
			</td>
			<td>
				<textarea NAME="division[name]" COLS="45" ROWS="4" {tip text="Enter division long name" trigger="name"}
					>{$division.name|escape}</textarea>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="label"}
			</td>
			<td class="nobr">
				<strong>{trans("Text label:")}</strong>
			</td>
			<td>
				<input type="text" name="division[label]" value="{$division.label|escape}" size="40"
					   {tip text="Enter text label under which division is visible in user interface" trigger="label"}>
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="user"}
			</td>
			<td class="valign-top nobr">
				<label for="natural-person">
					<strong>{trans("Natural person")}</strong>
				</label>
			</td>
			<td>
				<input type="checkbox" id="natural-person" name="division[naturalperson]" value="1"
					{if !empty($division['naturalperson'])} checked{/if}>
				<input type="text" name="division[firstname]"
					value="{$division.firstname|escape}"
					maxlength="100"
					placeholder="{trans("first name")}"
					{tip text="first name" trigger="firstname" class="natural-person-property"}
					{if !$division.firstname || !$division.lastname || !$division.birthdate} disabled{/if}>
				<input type="text" name="division[lastname]"
					value="{$division.lastname|escape}"
					maxlength="100"
					placeholder="{trans("last name")}"
					{tip text="last name" trigger="lastname" class="natural-person-property"}
					{if !$division.firstname || !$division.lastname || !$division.birthdate} disabled{/if}>
				<input type="text" name="division[birthdate]" id="birthdate"
					value="{$division.birthdate}"
					size="15"
					placeholder="{trans("birth date")}"
					{tip text="birth date" trigger="birthdate" class="natural-person-property lms-ui-date lms-ui-date-unix"}
					data-year-range="c-100:c"
					{if !$division.firstname || !$division.lastname || !$division.birthdate} disabled{/if}>
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="home"}
			</td>
			<td class="nobr valign-top">
				<strong>{trans("Address")}</strong>
			</td>
			<td>
				{location_box
					prefix               = "division"
					address_id           = "{$division.address_id}"
					location_name        = "{$division.location_name}"
					location_state_name  = "{$division.location_state_name}"
					location_state       = "{$division.location_state}"
					location_city_name   = "{$division.location_city_name}"
					location_city        = "{$division.location_city}"
					location_street_name = "{$division.location_street_name}"
					location_street      = "{$division.location_street}"
					location_house       = "{$division.location_house}"
					location_flat        = "{$division.location_flat}"
					location_zip         = "{$division.location_zip}"
					location_country_id  = "{$division.location_country_id}"
					location_postoffice  = "{$division.location_postoffice}"
					teryt                = "{$division.teryt}"
				}
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="customer-location"}
			</td>
			<td class="nobr valign-top">
				<strong>{trans("Office address")}</strong>
			</td>
			<td>
				{location_box_expandable data=$division.office_address}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="id"}
			</td>
			<td class="nobr">
				<strong>{trans("TEN")}</strong>
			</td>
			<td>
				<input type="text" size="40" value="{$division.ten}" name="division[ten]" {tip text="Enter Tax Exempt Number (optional)" trigger="ten"}>
				{if $division.tenwarning}
					<input type="hidden" name="division[tenwarning]" value="1">
				{/if}
				{button type="link" icon="division" class="gusapi-button"
					data_type=Utils::GUS_REGON_API_SEARCH_TYPE_TEN
					tip="Click here to autocomplete company data from GUS REGON database"}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="tax"}
			</td>
			<td class="nobr">
				<strong>{trans("Tax Office Code:")}</strong>
			</td>
			<td>
				{if isset($_TAX_OFFICE_CODES)}
					<select name="division[tax_office_code]" {tip text="Enter Tax Office Code (optional)" trigger="tax_office_code"}>
						<option value="0">{trans("— none —")}</option>
						{foreach $_TAX_OFFICE_CODES as $code => $tax_office}
							<option value="{$code}"{if $division.tax_office_code == $code} selected{/if}>{$tax_office}</option>
						{/foreach}
					</select>
				{else}
					<input type="text" size="10" value="{if $division.tax_office_code > 0}{$division.tax_office_code}{/if}"
						name="division[tax_office_code]" {tip text="Enter Tax Office Code (optional)" trigger="tax_office_code"}>
				{/if}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="id"}
			</td>
			<td class="nobr">
				<strong>{trans("REGON")}</strong>
			</td>
			<td>
				<input type="text" size="40" value="{$division.regon}" name="division[regon]"
					{tip text="Enter Business Registration Number (optional)" trigger="regon"}>
				{button type="link" icon="division" class="gusapi-button"
					data_type=Utils::GUS_REGON_API_SEARCH_TYPE_REGON
					tip="Click here to autocomplete company data from GUS REGON database"}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="id"}
			</td>
			<td class="nobr">
				<strong>{trans("RBE Name:")}</strong>
			</td>
			<td>
				<textarea name="division[rbename]" cols="45" rows="4"
					{tip text="Enter National Court Register / Register of Business Entities Name (optional)" trigger="rbename"}
					>{$division.rbename|escape}</textarea>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="id"}
			</td>
			<td class="nobr">
				<strong>{trans("RBE:")}</strong>
			</td>
			<td>
				<input type="text" value="{$division.rbe}" name="division[rbe]" size="30"
					{tip text="Enter National Court Register / Register of Business Entities Number (optional)" trigger="rbe"}>
				{button type="link" icon="division" class="gusapi-button"
					data_type=Utils::GUS_REGON_API_SEARCH_TYPE_RBE
					tip="Click here to autocomplete company data from GUS REGON database"}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="phone"}
			</td>
			<td class="nobr">
				<strong>{trans("Telecom number:")}</strong>
			</td>
			<td>
				<input type="text" value="{$division.telecomnumber}" name="division[telecomnumber]" size="30"
					{tip text="Enter Telecom Registry Number (optional)" trigger="telecomnumber"}>
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="account-number"}
			</td>
			<td class="nobr valign-top">
				<strong>{trans("Bank account:")}</strong>
			</td>
			<td>
				{trans("Bank:")}
				<input type="text" name="division[bank]" value="{$division.bank}" size="45"
					{tip text="Enter bank name (optional)" trigger="bank"}>
				<br>
				{trans("Number:")}
				<input type="text" name="division[account]" VALUE="{$division.account}" SIZE="45"
					{tip text="Enter bank account or mass payments account prefix (optional)" trigger="account"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="mail"}
			</td>
			<td class="nobr">
				<strong>{trans("Email:")}</strong>
			</td>
			<td>
				<input type="text" value="{$division.email}" name="division[email]" size="40"
					{tip text="Enter e-mail address (optional)" trigger="email"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="phone"}
			</td>
			<td class="nobr">
				<strong>{trans("Phone")}</strong>
			</td>
			<td>
				<input type="text" value="{$division.phone}" name="division[phone]" size="40"
					{tip text="Enter phone number (optional)" trigger="phone"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="phone"}
			</td>
			<td class="nobr">
				<strong>{trans("Service Phone")}</strong>
			</td>
			<td>
				<input type="text" value="{$division.servicephone}" name="division[servicephone]" size="40"
						{tip text="Enter phone number (optional)" trigger="servicephone"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="url"}
			</td>
			<td class="nobr">
				<strong>{trans("URL")}</strong>
			</td>
			<td>
				<input type="text" size="80" name="division[url]"
					value="{$division.url}"
					placeholder="{trans("URL address")}"
					{tip trigger="url"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="url"}
			</td>
			<td class="nobr">
				<strong>{trans("Userpanel URL")}</strong>
			</td>
			<td>
				<input type="text" size="80" name="division[userpanel_url]"
					value="{$division.userpanel_url}"
					placeholder="{trans("URL address")}"
					{tip trigger="userpanel_url"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="info"}
			</td>
			<td class="nobr">
				<strong>{trans("Description:")}</strong>
			</td>
			<td>
				<textarea name="division[description]" cols="45" rows="4"
					{tip text="Enter additional information (optional)"}>{$division.description}</textarea>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="wait"}
			</td>
			<td class="nobr">
				<strong>{trans("Locked:")}</strong>
			</td>
			<td>
				<input type="checkbox" name="division[status]" value="1"{if $division.status} checked{/if}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="print"}
			</td>
			<td class="nobr">
				<strong>{trans("Invoice header:")}</strong>
			</td>
			<td>
				<textarea name="division[inv_header]" cols="45" rows="4"
					{tip text="Enter invoice header (optional)" trigger="inv_header"}>{$division.inv_header}</textarea>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="print"}
			</td>
			<td class="nobr">
				<strong>{trans("Invoice footer:")}</strong>
			</td>
			<td>
				<textarea name="division[inv_footer]" cols="45" rows="4"
					{tip text="Enter invoice footer (optional)" trigger="inv_footer"}>{$division.inv_footer}</textarea>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="signature"}
			</td>
			<td class="nobr">
				<strong>{trans("Invoice author:")}</strong>
			</td>
			<td>
				<input type="text" name="division[inv_author]" value="{$division.inv_author}" size="45"
					{tip text="Enter invoices author (optional)" trigger="inv_author"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="map-pin"}
			</td>
			<td class="nobr">
				<strong>{trans("Invoice creation place:")}</strong>
			</td>
			<td>
				<input type="text" name="division[inv_cplace]" value="{$division.inv_cplace}" size="45"
					{tip text="Enter invoices creation place (optional)" trigger="inv_cplace"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="paytype"}
			</td>
			<td>
				<strong>{trans("Payment type:")}</strong>
			</td>
			<td>
				{paytypes elemname="division[inv_paytype]" selected=$division.inv_paytype
					tip="Select default invoices payment type"}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="deadline"}
			</td>
			<td>
				<strong>{trans("Deadline:")}</strong>
			</td>
			<td>
				<select name="division[inv_paytime]" {tip text="Select default deadline in days" trigger="inv_paytime"}>
					<option value=""{if !isset($division.inv_paytime) || !strlen($division.inv_paytime)} selected{/if}>— {trans("default<!paytime>")} —</option>
					<option value="0"{if $division.inv_paytime == "0"} selected{/if}>0</option>
					<option value="7"{if $division.inv_paytime == 7} selected{/if}>7</option>
					<option value="10"{if $division.inv_paytime == 10} selected{/if}>10</option>
					<option value="14"{if $division.inv_paytime == 14} selected{/if}>14</option>
					<option value="21"{if $division.inv_paytime == 21} selected{/if}>21</option>
					<option value="30"{if $division.inv_paytime == 30} selected{/if}>30</option>
				</select>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="user"}
			</td>
			<td class="nobr">
				<strong>{trans("Users:")}</strong>
			</td>
			<td>
				<label for="division[users][]">
					<select size="4" name="division[users][]" multiple {tip text="Select users" class="lms-ui-multiselect"}
						data-default-value="{trans("— none —")}" data-shorten-to-default-value="false">
						{foreach $userslist as $user}
							<option value="{$user.id}" {if !empty($division_users) && in_array($user.id, $division_users)} selected{/if}>
								{$user.login|trunescape:30}
							</option>
						{/foreach}
					</select>
				</label>
			</td>
		</tr>
		<tr>
			<td colspan="3" class="lms-ui-box-buttons">
				{button type="submit" icon="save" label="Submit"}
				{button type="link-button" icon="cancel" label="Cancel" href="?m=divisionlist"}
			</td>
		</tr>
    </tbody>
</table>
<script>

	$(function() {
		$('form[name="division"] [name="division[shortname]"]').focus();

		$('#natural-person').change(function() {
			$('.natural-person-property').prop('disabled', !$(this).prop('checked'));
			$('#birthdate').datepicker('option', 'disabled', !$(this).prop('checked'))
				.next().prop('disabled', !$(this).prop('checked'));
		}).change();

		//check_teryt(['location', 'location_city'], true);

		$('.gusapi-button').click(function() {
			var identifier = $(this).siblings('input[type="text"]').val().replace(/-/g, '');
			if (!identifier.length) {
				return;
			}
			var that = this;
			GusApiGetCompanyDetails(
				$(this).attr('data-type'),
				identifier,
				function(data) {
					var propertyToSelectors = {
						lastname: [ '[name="division[shortname]"]', '[name="division[name]"]' ],
						name: [ '[name="division[shortname]"]', '[name="division[name]"]' ],
						rbe: '[name="division[rbe]"]',
						rbename: '[name="division[rbename]"]',
						regon: '[name="division[regon]"]',
						ten: '[name="division[ten]"]',
						addresses: [
							{
								location_state: '[name="division[location_state]"]',
								location_state_name: '[name="division[location_state_name]"]',
								location_city: '[name="division[location_city]"]',
								location_city_name: '[name="division[location_city_name]"]',
								location_street: '[name="division[location_street]"]',
								location_street_name: '[name="division[location_street_name]"]',
								location_house: '[name="division[location_house]"]',
								location_flat: '[name="division[location_flat]"]',
								location_zip: '[name="division[location_zip]"]',
								location_postoffice: '[name="division[location_postoffice]"]',
								teryt: '[name="division[teryt]"]'
							},
							{
								location_state: '[name="division[office_address][location_state]"]',
								location_state_name: '[name="division[office_address][location_state_name]"]',
								location_city: '[name="division[office_address][location_city]"]',
								location_city_name: '[name="division[office_address][location_city_name]"]',
								location_street: '[name="division[office_address][location_street]"]',
								location_street_name: '[name="division[office_address][location_street_name]"]',
								location_house: '[name="division[office_address][location_house]"]',
								location_flat: '[name="division[office_address][location_flat]"]',
								location_zip: '[name="division[office_address][location_zip]"]',
								location_postoffice: '[name="division[office_address][location_postoffice]"]',
								teryt: '[name="division[office_address][teryt]"]'
							}
						]
					}

					if (data.length > 1) {
						customerCompanySelectionDialog(data, that).done(function(company) {
							GusApiFinished(
								propertyToSelectors,
								company
							);
						});
					} else {
						GusApiFinished(
							propertyToSelectors,
							data[0]
						);
					}
				}
			);
			return false;
		});
	});

</script>
{/block}
