<!-- To change the layout of the detail view form, we recommend using JS code in hooks/invoice_items-dv.js rather than editing this file -->

<div id="detail-view"></div>
<div class="panel-heading"><h3 class="panel-title"><strong><%%DETAIL_VIEW_TITLE%%></strong></h3></div>

<div id="invoice_items_dv_container-loading" class="text-center text-info text-bold vspacer-lg"><i class="glyphicon glyphicon-refresh loop-rotate"></i> <%%TRANSLATION(Loading ...)%%></div>

<div class="panel-body hidden" id="invoice_items_dv_container">
	<!-- child links -->
	<div class="row vspacer-md">
		<div class="col-xs-12 children-links">
			<div class="clearfix"></div>
		</div>
	</div>
	<hr class="hidden-xs hidden-sm">

	<div class="row">
		<!-- form inputs -->
		<div class="col-md-8 col-lg-10" id="invoice_items_dv_form">
			<fieldset class="form-horizontal">

				<!-- Field: ID -->
				<div class="form-group invoice_items-id">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">ID</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="id"><%%VALUE(id)%%></div>
					</div>
				</div>

				<!-- Field: Invoice -->
				<div class="form-group invoice_items-invoice">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" data-parent_link="view_parent_modal">Invoice</label>
					<div class="col-lg-9">
						<div class="form-control-static">
							<span id="invoice-readonly"><%%COMBOTEXT(invoice)%%></span>
							<%%PLINK(invoice)%%>
						</div>
					</div>
				</div>

				<!-- Field: Item -->
				<div class="form-group invoice_items-item">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="item" data-parent_link="view_parent_modal">Item</label>
					<div class="col-lg-9">
						<div class="lookup-flex"><%%COMBO(item)%%><%%PLINK(item)%%><%%ADDNEW(items)%%></div>
					</div>
				</div>

				<!-- Field: Current price -->
				<div class="form-group invoice_items-current_price">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Current price <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#current_price-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="current_price-description"><div class="alert alert-info bspacer-xs">This is the current catalog price of the selected item.</div></span>
						<div class="form-control-static" id="current_price"></div>
					</div>
				</div>

				<!-- Field: Catalog price at order date -->
				<div class="form-group invoice_items-catalog_price">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Catalog price at order date</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="catalog_price"><%%VALUE(catalog_price)%%></div>
					</div>
				</div>

				<!-- Field: Unit price -->
				<div class="form-group invoice_items-unit_price">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="unit_price">Unit price<span class="text-danger"><%%TRANSLATION(*)%%></span> <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#unit_price-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="unit_price-description"><div class="alert alert-info bspacer-xs">You can either use the catalog price or change to a different price.</div></span>
						<input maxlength="13" type="text" class="form-control" name="unit_price" id="unit_price" value="<%%VALUE(unit_price)%%>" required>
					</div>
				</div>

				<!-- Field: Qty -->
				<div class="form-group invoice_items-qty">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="qty">Qty</label>
					<div class="col-lg-9">
						<input maxlength="13" type="text" class="form-control" name="qty" id="qty" value="<%%VALUE(qty)%%>">
					</div>
				</div>

				<!-- Field: Price -->
				<div class="form-group invoice_items-price">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Price <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#price-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="price-description"><div class="alert alert-info bspacer-xs">Will be automatically calculated from unit price and quantity, excluding taxes.</div></span>
						<div class="form-control-static" id="price"><%%VALUE(price)%%></div>
					</div>
				</div>

			</fieldset>
		</div>

		<!-- DV action buttons -->
		<hr class="hidden-md hidden-lg">
		<div class="col-md-4 col-lg-2" id="invoice_items_dv_action_buttons">
			<div class="btn-toolbar">
				<div class="btn-group-vertical btn-group-lg" style="width: 100%;">
					<%%UPDATE_BUTTON%%>
				</div><p></p>
				<div class="btn-group-vertical btn-group-lg" style="width: 100%;">
					<%%DESELECT_BUTTON%%>
					<%%DVPRINT_BUTTON%%>
					<%%DELETE_BUTTON%%>
				</div><p></p>
				<div class="btn-group-vertical btn-group-lg" style="width: 100%;">
					<%%INSERT_BUTTON%%>
				</div>
			</div>
		</div>
	</div>


</div><!-- /div class="panel-body" -->
	<script>
		$j(function() {
			var add_new_mode = (!$j('input[name=SelectedID]').val());
			var embedded_mode = ($j('input[name=Embedded]').val() == 1);

			/* auto-close embedded page? */
			var autoclose_mode = ($j('input[name=AutoClose]').val() == 1);
			var record_added_ok = (location.search.split('record-added-ok=').length == 2);
			var record_updated_ok = (location.search.split('record-updated-ok=').length == 2);
			if((record_added_ok || record_updated_ok) && embedded_mode && autoclose_mode) {
				setTimeout(AppGini.closeParentModal, 2000);
			}


			/* enable DV action buttons to float on scrolling down the form */
			enable_dvab_floating();

			if(!$j('.table_view').length || $j('[name=SelectedID]').val().length) {
				AppGini.once({
					condition: function() {
						return $j('.form-group.invoice_items-item')
									.find('.nicEdit-main, .select2-container, input[type=radio]')
									.length > 0
								// or read-only DV
								|| (!$j('#update').length && !$j('#insert').length);
					},
					action: function() {
						$j('#invoice_items_dv_container-loading').addClass('hidden');
						$j('#invoice_items_dv_container').removeClass('hidden');
						AppGini.focusFormElement('invoice_items', 'item');
					},
					timeout:  5000,
					doActionOnTimeout:  true
				});
			} else {
				$j('#invoice_items_dv_container-loading').addClass('hidden');
				$j('#invoice_items_dv_container').removeClass('hidden');
			}

			$j('a[href="<%%BASE_UPLOAD_PATH%%>"]').click(function() { return false; });


			if(true /* !embedded_mode */) {
				$j('.add_new_parent').click(function() {
					var pt = $j(this).attr('id').replace(/_add_new$/, '');
					modal_window({
						url: pt + '_view.php?addNew_x=1&Embedded=1',
						close: function() {
							var field_id = $j('#' + pt + '_add_new').prevAll('input:hidden').eq(0).attr('id');
							$j('#' + field_id + '-container').select2('focus').select2('focus');

							/* if a new parent was added, select it */
							var new_id = localStorage.getItem(pt + '_last_added_id');
							if(new_id === null) return;

							localStorage.removeItem(pt + '_last_added_id');
							AppGini.lookupText({
								id: new_id,
								table: 'invoice_items',
								field: field_id,
								callback: function(data) {
									$j('#' + field_id + '-container').select2('data', data, true);
								}
							});
						},
						size: 'full',
						title: $j(this).attr('title')
					});
				});
			}

			AppGini.hideViewParentLinks();

			$j('form').on('click', '.view_parent', function() {
				var pt = $j(this).attr('id').replace(/_view_parent$/, '');
				if($j(this).prevAll('input:hidden').length) {
					var parent_id = $j(this).prevAll('input:hidden').eq(0).val();
				} else {
					var parent_id = $j(this).prevAll().children('input:hidden').eq(0).val();
				}

				/* handle radio lookups */
				if($j(this).prevAll('[type=radio]').length) {
					parent_id = $j(this).prevAll('[type=radio]').eq(0).val();
				}

				if(parent_id != '{empty_value}' && parent_id != undefined && parent_id != '') {
					var parent_link = $j(this).parents('.form-group').children('label').data('parent_link');
					switch(parent_link) {
						case 'view_parent_hidden':
							break;
						case 'view_parent_modal':
							modal_window({
								url: pt + '_view.php?Embedded=1&SelectedID=' + encodeURIComponent(parent_id),
								close: function() {
									var field_id = $j('#' + pt + '_view_parent').prevAll('input:hidden').eq(0).attr('id');
									$j('#' + field_id + '-container').select2('focus').select2('focus');

									// in case the parent record was updated, we should update auto-fills
									var updaterName = field_id + '_update_autofills';
									if(typeof(window[updaterName]) == 'function')
										window[updaterName]();
								},
								size: 'full',
								title: $j(this).attr('title')
							});
							break;
						case 'view_parent_same_window':
							window.location = pt + '_view.php?Embedded=' + (embedded_mode ? '1' : '') + '&SelectedID=' + encodeURIComponent(parent_id);
							break;
						case 'view_parent_new_window':
							window.open(pt + '_view.php?SelectedID=' + encodeURIComponent(parent_id));
							break;
					}

				}
			});


			/* load admin tools for admin user */
			if(!add_new_mode) {
				$j('body').append('<div id="admin-tools-appendix"></div>');
				$j('#admin-tools-appendix').load(
					'ajax_admin_tools.php?table=invoice_items&id=<%%URLVALUE(id)%%>',
					function(resp) {
						if(!resp.length) return;
						$j.ajax('ajax_admin_tools.php?action=get_admin_tools_js&table=invoice_items&id=<%%URLVALUE(id)%%>');
					}
				);
			}
		});
	</script>