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

<c:set var="inst" value="${frd.instance}"/>

<h3 class="mb1">
	${l.l('Очередь экспорта')}
	<c:if test="${not empty inst}"> — ${inst.title}</c:if>
</h3>

<c:url var="backUrl" value="/admin/plugin/sync1c/instance.do"/>
<button type="button" class="btn-white mb1"
	onclick="$$.ajax.loadContent('${backUrl}')">&larr; ${l.l('Назад')}</button>

<c:if test="${not empty inst and inst.protocol eq 'file'}">
	<c:url var="downloadUrl" value="/admin/plugin/sync1c/instance.do">
		<c:param name="method" value="exportQueueDownload"/>
		<c:param name="instanceId" value="${inst.id}"/>
	</c:url>
	<a href="${downloadUrl}" class="btn-white mb1 ml1">${l.l('Скачать CSV')}</a>

	<c:url var="confirmAllUrl" value="/admin/plugin/sync1c/instance.do">
		<c:param name="method" value="exportQueueConfirmAll"/>
		<c:param name="instanceId" value="${inst.id}"/>
	</c:url>
	<button type="button" class="btn-green mb1 ml1" onclick="
		$$.ajax.post('${confirmAllUrl}', {control: this}).done((r) => {
			alert('OK: ' + r.data.count + ' ${l.l('подтверждено')}');
			$$.ajax.loadContent('${form.requestUrl}');
		}); return false;">${l.l('Подтвердить всё')}</button>
</c:if>

<table class="data hl">
	<tr>
		<td width="1em">&nbsp;</td>
		<td>ID</td>
		<td>${l.l('Процесс')}</td>
		<td>${l.l('Склад 1С')}</td>
		<td>${l.l('Статус')}</td>
		<td>${l.l('Попыток')}</td>
		<td>${l.l('Ошибка отправки')}</td>
		<td>${l.l('Дата создания')}</td>
	</tr>
	<c:forEach var="q" items="${frd.queue}">
		<c:set var="menuUiid" value="${u:uiid()}"/>
		<tr>
			<td>
				<ui:popup-menu id="${menuUiid}">
					<c:choose>
						<c:when test="${not empty inst and inst.protocol eq 'file'}">
							<c:if test="${q.status == 'pending'}">
								<p:check action="/admin/plugin/sync1c/instance:exportQueueConfirm">
									<c:url var="confirmUrl" value="/admin/plugin/sync1c/instance.do">
										<c:param name="method" value="exportQueueConfirm"/>
										<c:param name="id" value="${q.id}"/>
									</c:url>
									<li><a href="#" onclick="$$.ajax.post('${confirmUrl}', {control: this}).done(() =>
										$$.ajax.loadContent('${form.requestUrl}')); return false;">
										<i class="ti-check"></i> ${l.l('Подтвердить')}</a></li>
								</p:check>
							</c:if>
						</c:when>
						<c:otherwise>
							<p:check action="/admin/plugin/sync1c/instance:exportQueueRetry">
								<c:url var="retryUrl" value="/admin/plugin/sync1c/instance.do">
									<c:param name="method" value="exportQueueRetry"/>
									<c:param name="id" value="${q.id}"/>
								</c:url>
								<li><a href="#" onclick="$$.ajax.post('${retryUrl}', {control: this}).done(() =>
									$$.ajax.loadContent('${form.requestUrl}')); return false;">
									<i class="ti-reload"></i> ${l.l('Повторить')}</a></li>
							</p:check>
						</c:otherwise>
					</c:choose>

					<p:check action="/admin/plugin/sync1c/instance:exportQueueDelete">
						<c:url var="delUrl" value="/admin/plugin/sync1c/instance.do">
							<c:param name="method" value="exportQueueDelete"/>
							<c:param name="id" value="${q.id}"/>
						</c:url>
						<li><a href="#" onclick="$$.ajax.post('${delUrl}', {control: this}).done(() =>
							$$.ajax.loadContent('${form.requestUrl}')); return false;">
							<i class="ti-trash"></i> ${l.l('Удалить')}</a></li>
					</p:check>
				</ui:popup-menu>
				<ui:button type="more" styleClass="btn-small" onclick="$$.ui.menuInit($(this), $('#${menuUiid}'), 'left', true);"/>
			</td>
			<td>${q.id}</td>
			<td>${q.processId}</td>
			<td><small>${q.warehouseId}</small></td>
			<td>
				<c:choose>
					<c:when test="${q.status == 'success'}"><span class="color-green">${l.l('Успешно')}</span></c:when>
					<c:when test="${q.status == 'failed'}"><span class="color-red">${l.l('Ошибка')}</span></c:when>
					<c:when test="${q.status == 'cancelled'}"><span class="color-gray">${l.l('Отменено')}</span></c:when>
					<c:when test="${q.status == 'reversed'}"><span class="color-blue">${l.l('Возвращено')}</span></c:when>
					<c:otherwise><span class="color-gray">${l.l('Выполняется')}</span></c:otherwise>
				</c:choose>
			</td>
			<td>${q.attempts}</td>
			<td><small>${q.lastError}</small></td>
			<td class="nowrap">${tu.format(q.createdAt, 'ymdhms')}</td>
		</tr>
	</c:forEach>
	<c:if test="${empty frd.queue}">
		<tr><td colspan="8" class="center">${l.l('Нет данных')}</td></tr>
	</c:if>
</table>

<shell:title text="${l.l('Очередь экспорта')}"/>
