<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/jspf/taglibs.jsp"%>

<c:set var="uiid" value="${u:uiid()}"/>

<html:form action="${form.requestURI}">
	<html:hidden property="method"/>
	<p:check action="/user/plugin/inventory/device:create">
		<c:url var="url" value="${form.requestURI}">
			<c:param name="method" value="create"/>
			<c:param name="returnUrl" value="${form.requestUrl}"/>
		</c:url>
		<ui:button type="add" styleClass="mr1" onclick="$$.ajax.load('${url}', $('#${uiid}').parent(), {control: this})"/>
	</p:check>

	<p:check action="/user/plugin/inventory/device:manufacturerList">
		<c:url var="mUrl" value="${form.requestURI}">
			<c:param name="method" value="manufacturerList"/>
		</c:url>
		<button type="button" class="btn-white mr1"
			onclick="$$.ajax.load('${mUrl}', $('#${uiid}').parent(), {control: this})">
			${l.l('Производители')}
		</button>
	</p:check>

	<p:check action="/user/plugin/inventory/device:deviceTypeList">
		<c:url var="dtUrl" value="${form.requestURI}">
			<c:param name="method" value="deviceTypeList"/>
		</c:url>
		<button type="button" class="btn-white mr1"
			onclick="$$.ajax.load('${dtUrl}', $('#${uiid}').parent(), {control: this})">
			${l.l('Типы устройств')}
		</button>
	</p:check>

	<ui:toggle name="showDeleted" value="${frd.showDeleted}" prefixText="${l.l('Показать удалённые')}"
		styleClass="ml1" onChange="$$.ajax.load(this.form, $('#${uiid}').parent())"/>
</html:form>

<table id="${uiid}" class="data mt1 hl">
	<tr>
		<td width="1em">&nbsp;</td>
		<td>ID</td>
		<td>${l.l('Наименование')}</td>
		<td>${l.l('Тип устройства')}</td>
		<td>${l.l('Склад')}</td>
		<td>${l.l('IP-адрес')}</td>
		<td>${l.l('Серийный номер')}</td>
		<td>${l.l('Статус')}</td>
	</tr>
	<c:forEach var="item" items="${frd.deviceList}">
		<c:set var="menuUiid" value="${u:uiid()}"/>
		<tr class="${item.deleted ? 'inv-deleted' : ''}">
			<td>
				<ui:popup-menu id="${menuUiid}">
					<p:check action="/user/plugin/inventory/device:show">
						<c:url var="showUrl" value="${form.requestURI}">
							<c:param name="method" value="show"/>
							<c:param name="id" value="${item.id}"/>
						</c:url>
						<li><a href="#"
							onclick="$$.ajax.load('${showUrl}', $('#${uiid}').parent(), {control: this}); return false;">
							<i class="ti-eye"></i> ${l.l('Просмотр')}</a></li>
					</p:check>

					<c:if test="${item.deleted}">
						<p:check action="/user/plugin/inventory/device:restore">
							<c:url var="restoreUrl" value="${form.requestURI}">
								<c:param name="method" value="restore"/>
								<c:param name="id" value="${item.id}"/>
							</c:url>
							<li><a href="#"
								onclick="$$.ajax.post('${restoreUrl}', {control: this}).done(() => $$.ajax.load('${form.requestUrl}', $('#${uiid}').parent())); return false;">
								<i class="ti-reload"></i> ${l.l('Восстановить')}</a></li>
						</p:check>
					</c:if>

					<c:if test="${not item.deleted}">
						<p:check action="/user/plugin/inventory/device:delete">
							<c:url var="delUrl" value="${form.requestURI}">
								<c:param name="method" value="delete"/>
								<c:param name="id" value="${item.id}"/>
							</c:url>
							<li><a href="#"
								onclick="if ($$.confirm.del()) $$.ajax.post('${delUrl}', {control: this}).done(() => $$.ajax.load('${form.requestUrl}', $('#${uiid}').parent())); return false;">
								<i class="ti-trash"></i> ${l.l('Удалить')}</a></li>
						</p:check>
					</c:if>
				</ui:popup-menu>
				<ui:button type="more" styleClass="btn-small" onclick="$$.ui.menuInit($(this), $('#${menuUiid}'), 'left', true);"/>
			</td>
			<td>${item.id}</td>
			<td>${item.name}</td>
			<td>${item.deviceTypeTitle}</td>
			<td>${item.storeTitle}</td>
			<td>${item.ip}</td>
			<td>${item.serialNumber}</td>
			<td>
				<span class="inv-status-${item.status}">
					<c:choose>
						<c:when test="${item.status eq 'planned'}">${l.l('Планируется')}</c:when>
						<c:when test="${item.status eq 'active'}">${l.l('Активен')}</c:when>
						<c:when test="${item.status eq 'maintenance'}">${l.l('На обслуживании')}</c:when>
						<c:when test="${item.status eq 'decommissioned'}">${l.l('Выведен из эксплуатации')}</c:when>
						<c:otherwise>${item.status}</c:otherwise>
					</c:choose>
				</span>
			</td>
		</tr>
	</c:forEach>
</table>
