File size: 15,194 Bytes
d70361b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/** Same-area pairing: oldest = Before, newest = After, with manual override. */

const areaPairsState = { groups: [], unpaired: [], overrides: {}, autoApplied: false };

function _pairDateLabel(img) {
  if (!img) return '';
  if (img.captureDate) return String(img.captureDate).slice(0, 10);
  return '';
}

function _pairOptionLabel(img) {
  const date = _pairDateLabel(img);
  const name = img.filename || img.imageName || img.path || '';
  return date ? `${date} · ${name}` : name;
}

function _pairChoice(group) {
  const ov = areaPairsState.overrides[group.id];
  const images = group.images || [];
  const beforePath = ov?.beforePath || group.beforePath || group.suggestedBefore?.path;
  const afterPath = ov?.afterPath || group.afterPath || group.suggestedAfter?.path;
  const before = images.find((i) => i.path === beforePath) || group.suggestedBefore;
  const after = images.find((i) => i.path === afterPath) || group.suggestedAfter;
  return { before, after, overridden: !!ov };
}

function renderAreaPairRows(container, groups, { compact } = {}) {
  if (!container) return;
  if (!groups.length) {
    container.innerHTML = '<p class="dim">No same-area pairs yet. Upload two dates of the same place (or add capture dates / georef) and refresh.</p>';
    return;
  }
  container.innerHTML = groups.map((g) => {
    const { before, after, overridden } = _pairChoice(g);
    const opts = (g.images || []).map((img) => {
      const enc = encodeURIComponent(img.path);
      return `<option value="${enc}">${escapeHtml(_pairOptionLabel(img))}</option>`;
    }).join('');
    const match = g.match === 'bounds' ? 'overlapping map extent' : 'matching name / grid';
    const n = (g.images || []).length;
    return `
      <article class="dda-area-pair" data-group-id="${escapeHtml(g.id)}">
        <div class="dda-area-pair-head">
          <p class="dda-area-pair-title">${escapeHtml(g.label)}</p>
          <p class="dda-area-pair-meta dim">${n} image${n === 1 ? '' : 's'} · ${escapeHtml(match)}${overridden ? ' · <span class="dda-area-pair-overridden">overridden</span>' : ''}</p>
        </div>
        <label>Before (oldest)
          <select data-role="before">${opts}</select>
        </label>
        <label>After (newest)
          <select data-role="after">${opts}</select>
        </label>
        <div class="dda-area-pair-actions">
          <button type="button" class="btn btn-primary btn-sm" data-role="compare">Compare</button>
        </div>
      </article>`;
  }).join('');

  container.querySelectorAll('.dda-area-pair').forEach((row) => {
    const group = groups.find((g) => g.id === row.dataset.groupId);
    if (!group) return;
    const { before, after } = _pairChoice(group);
    const beforeSel = row.querySelector('select[data-role="before"]');
    const afterSel = row.querySelector('select[data-role="after"]');
    if (beforeSel && before) beforeSel.value = encodeURIComponent(before.path);
    if (afterSel && after) afterSel.value = encodeURIComponent(after.path);

    const saveOverride = () => {
      areaPairsState.overrides[group.id] = {
        beforePath: decodeURIComponent(beforeSel.value || ''),
        afterPath: decodeURIComponent(afterSel.value || ''),
      };
      renderAreaPairRows(document.getElementById('dda-area-pairs'), areaPairsState.groups);
      const cmp = document.getElementById('dda-compare-pairs');
      if (cmp && areaPairsState.groups.length) {
        renderAreaPairRows(cmp, areaPairsState.groups, { compact: true });
      }
    };
    beforeSel?.addEventListener('change', saveOverride);
    afterSel?.addEventListener('change', saveOverride);
    row.querySelector('[data-role="compare"]')?.addEventListener('click', () => {
      // Library page has no compare slots in its DOM — hand off to the
      // Detect page instead of trying to apply the pair locally.
      if (!document.getElementById('slot-t1')) {
        const mode = _pairChoice(group).overridden ? 'manual' : 'automatic';
        window.location.href = `/detect?pair=${encodeURIComponent(group.id)}&mode=${mode}`;
        return;
      }
      const choice = _pairChoice(group);
      areaPairsState.selectedGroupId = group.id;
      if (choice.overridden) {
        if (typeof setDetectMode === 'function') setDetectMode('manual', { applyPair: false });
        if (typeof applyAreaPairToCompare === 'function') {
          applyAreaPairToCompare(choice.before, choice.after, { silent: false });
        }
      } else {
        if (typeof setDetectMode === 'function') setDetectMode('automatic', { applyPair: false });
        const areaSel = document.getElementById('dda-auto-area');
        if (areaSel) areaSel.value = group.id;
        applySelectedAutoPair();
      }
    });
  });
}

function applySelectedAutoPair() {
  const sel = document.getElementById('dda-auto-area');
  const status = document.getElementById('dda-auto-status');
  const groupId = sel?.value || areaPairsState.selectedGroupId;
  const group = (areaPairsState.groups || []).find((g) => g.id === groupId);
  areaPairsState.selectedGroupId = group?.id || '';
  if (typeof getDetectMode === 'function' && getDetectMode() !== 'automatic') return;
  if (!group) {
    if (status) status.textContent = 'No same-area pair found. Upload two dates of the same place, or switch to Manual.';
    if (typeof updateAutoScheduleLine === 'function') updateAutoScheduleLine();
    return;
  }
  const before = group.suggestedBefore;
  const after = group.suggestedAfter;
  if (typeof applyAreaPairToCompare === 'function') {
    applyAreaPairToCompare(before, after, { silent: true });
  }
  const bDate = group.beforeDate || _pairDateLabel(before) || 'oldest';
  const aDate = group.afterDate || _pairDateLabel(after) || 'newest';
  if (status) {
    status.textContent = `Selected automatically: Before = ${before?.filename || '—'} (${bDate}) → After = ${after?.filename || '—'} (${aDate}). Use Run for a one-off detection. The schedule only runs after you click Start.`;
  }
  if (typeof updateAutoScheduleLine === 'function') updateAutoScheduleLine();
}

function populateAutoAreaSelect() {
  const sel = document.getElementById('dda-auto-area');
  if (!sel) return;
  const groups = areaPairsState.groups || [];
  const prev = sel.value || areaPairsState.selectedGroupId || '';
  if (!groups.length) {
    sel.innerHTML = '<option value="">No same-area pairs found</option>';
    areaPairsState.selectedGroupId = '';
    applySelectedAutoPair();
    return;
  }
  sel.innerHTML = groups.map((g) => {
    const n = (g.images || []).length;
    return `<option value="${escapeHtml(g.id)}">${escapeHtml(g.label)} · ${n} image${n === 1 ? '' : 's'}</option>`;
  }).join('');
  if (prev && groups.some((g) => g.id === prev)) sel.value = prev;
  else sel.value = groups[0].id;
  areaPairsState.selectedGroupId = sel.value;
  applySelectedAutoPair();
}

// Consumes ?pair=<groupId>&mode=automatic|manual once, set by the Library
// page's "Compare" button when it navigates to /detect (see renderAreaPairRows).
function applyPairFromUrl() {
  if (areaPairsState._urlPairApplied) return;
  const params = new URLSearchParams(window.location.search);
  const wantPair = params.get('pair');
  if (!wantPair) return;
  areaPairsState._urlPairApplied = true;
  const group = areaPairsState.groups.find((g) => g.id === wantPair);
  if (!group) return;
  areaPairsState.selectedGroupId = group.id;
  if (params.get('mode') === 'manual') {
    if (typeof setDetectMode === 'function') setDetectMode('manual', { applyPair: false });
    const choice = _pairChoice(group);
    if (typeof applyAreaPairToCompare === 'function') {
      applyAreaPairToCompare(choice.before, choice.after, { silent: false });
    }
  } else {
    if (typeof setDetectMode === 'function') setDetectMode('automatic', { applyPair: false });
    const areaSel = document.getElementById('dda-auto-area');
    if (areaSel) areaSel.value = group.id;
    applySelectedAutoPair();
  }
}

async function loadAreaPairs({ nodeId, autoFillCompare } = {}) {
  const libBox = document.getElementById('dda-area-pairs');
  const hint = document.getElementById('dda-area-pairs-hint');
  const cmpBox = document.getElementById('dda-compare-pairs');
  const params = new URLSearchParams();
  if (nodeId) params.set('node_id', String(nodeId));
  try {
    const data = await ddaApi('GET', '/api/dda/local/area-groups' + (params.toString() ? `?${params}` : ''));
    areaPairsState.groups = data.groups || [];
    areaPairsState.unpaired = data.unpaired || [];
    window.ddaState = window.ddaState || {};
    window.ddaState.areaGroups = areaPairsState.groups;
    const n = areaPairsState.groups.length;
    const unpaired = areaPairsState.unpaired.length;
    if (hint) {
      hint.textContent = n
        ? `${n} pair${n === 1 ? '' : 's'} · ${unpaired} unpaired`
        : (unpaired ? `${unpaired} image(s) not yet paired` : '');
    }
    renderAreaPairRows(libBox, areaPairsState.groups);
    if (cmpBox) {
      if (!areaPairsState.groups.length) cmpBox.innerHTML = '';
      else renderAreaPairRows(cmpBox, areaPairsState.groups, { compact: true });
    }
    populateAutoAreaSelect();
    applyPairFromUrl();
    if (typeof populateSelects === 'function' && typeof selectSourceItems === 'function') {
      populateSelects(selectSourceItems((window.ddaState && window.ddaState.libraryItems) || []));
    }
    if (typeof loadAutoDetectStatus === 'function') loadAutoDetectStatus();
    if (autoFillCompare && !areaPairsState.autoApplied && areaPairsState.groups.length) {
      if (typeof getDetectMode !== 'function' || getDetectMode() !== 'automatic') {
        const first = areaPairsState.groups[0];
        const choice = _pairChoice(first);
        if (typeof applyAreaPairToCompare === 'function') {
          applyAreaPairToCompare(choice.before, choice.after, { silent: true, onlyIfEmpty: true });
        }
      }
      areaPairsState.autoApplied = true;
    }
  } catch (err) {
    if (libBox) libBox.innerHTML = `<p class="dim">Could not load same-area pairs: ${escapeHtml(err.message || 'error')}</p>`;
  }
}

window.loadAreaPairs = loadAreaPairs;
window.areaPairsState = areaPairsState;
window.applySelectedAutoPair = applySelectedAutoPair;
window.loadAutoDetectStatus = loadAutoDetectStatus;
window.updateAutoScheduleLine = updateAutoScheduleLine;
window.initAutoScheduleControls = initAutoScheduleControls;

function _scheduleWhen(iso) {
  if (!iso) return 'not yet';
  try {
    return new Date(iso).toLocaleString();
  } catch (_) {
    return iso;
  }
}

function fillAutoScheduleForm(data) {
  const interval = document.getElementById('dda-auto-interval');
  const runAt = document.getElementById('dda-auto-run-at');
  if (interval && document.activeElement !== interval) {
    interval.value = String(data.intervalDays || 10);
  }
  if (runAt && document.activeElement !== runAt) {
    runAt.value = data.runAt || '02:00';
  }
  const start = document.getElementById('btn-auto-schedule-start');
  const stop = document.getElementById('btn-auto-schedule-stop');
  const canStart = typeof hasPermission === 'function' && window.ddaPermissions
    ? hasPermission(window.ddaPermissions, 'detect', 'create')
    : false;
  if (start) {
    start.textContent = data.running ? 'Update schedule' : 'Start schedule';
    if (!canStart) start.disabled = true;
  }
  if (stop) stop.disabled = !data.running || !canStart;
}

function updateAutoScheduleLine() {
  const box = document.getElementById('dda-auto-schedule');
  if (!box) return;
  const data = (window.ddaState && window.ddaState.autoDetect) || null;
  if (!data) {
    box.textContent = '';
    return;
  }
  if (!data.enabled) {
    box.textContent = 'Background schedule is turned off on the server.';
    fillAutoScheduleForm(data);
    return;
  }
  fillAutoScheduleForm(data);
  const days = data.intervalDays || 10;
  if (!data.running) {
    box.textContent = `Schedule is stopped. It does not start when the app launches. Set interval and time, then click Start. First queue will be at that time (IST), then every ${days} day${days === 1 ? '' : 's'}.`;
    return;
  }
  const groupId = document.getElementById('dda-auto-area')?.value;
  const group = (areaPairsState.groups || []).find((g) => g.id === groupId);
  const before = group?.suggestedBefore?.path || group?.beforePath;
  const after = group?.suggestedAfter?.path || group?.afterPath;
  const pair = (data.pairs || []).find((p) => p.beforePath === before && p.afterPath === after);
  const next = _scheduleWhen(data.nextRunAt);
  const last = pair?.lastCompletedAt ? _scheduleWhen(pair.lastCompletedAt) : 'not yet';
  let line = `Schedule running · every ${days} days at ${data.runAt || '02:00'} IST · next queue ${next} · last report ${last}.`;
  if (pair?.lastJobStatus === 'queued' || pair?.lastJobStatus === 'running') {
    line += ` Job #${pair.lastJobId} ${pair.lastJobStatus}.`;
  }
  if (pair?.lastError) line += ` Last error: ${pair.lastError}`;
  box.textContent = line;
}

async function loadAutoDetectStatus() {
  try {
    const data = await ddaApi('GET', '/api/dda/auto-detect');
    window.ddaState = window.ddaState || {};
    window.ddaState.autoDetect = data;
    updateAutoScheduleLine();
    if (typeof setDetectMode === 'function' && typeof getDetectMode === 'function' && getDetectMode() === 'automatic') {
      setDetectMode('automatic', { applyPair: false });
    }
  } catch (_) {
    const box = document.getElementById('dda-auto-schedule');
    if (box) box.textContent = '';
  }
}

function initAutoScheduleControls() {
  const start = document.getElementById('btn-auto-schedule-start');
  const stop = document.getElementById('btn-auto-schedule-stop');
  if (!start || start.dataset.bound) return;
  start.dataset.bound = '1';
  start.addEventListener('click', async () => {
    const interval = Number(document.getElementById('dda-auto-interval')?.value || 10);
    const runAt = document.getElementById('dda-auto-run-at')?.value || '02:00';
    start.disabled = true;
    try {
      const data = await ddaApi('POST', '/api/dda/auto-detect/start', {
        body: JSON.stringify({ intervalDays: interval, runAt }),
      });
      window.ddaState = window.ddaState || {};
      window.ddaState.autoDetect = data;
      updateAutoScheduleLine();
      if (typeof showDdaSuccess === 'function') {
        showDdaSuccess(`Schedule started. First automatic queue at ${_scheduleWhen(data.nextRunAt)} (not now).`);
      }
    } catch (err) {
      if (typeof showDdaError === 'function') showDdaError(err.message || 'Could not start schedule');
    } finally {
      start.disabled = false;
    }
  });
  stop?.addEventListener('click', async () => {
    stop.disabled = true;
    try {
      const data = await ddaApi('POST', '/api/dda/auto-detect/stop');
      window.ddaState = window.ddaState || {};
      window.ddaState.autoDetect = data;
      updateAutoScheduleLine();
      if (typeof showDdaSuccess === 'function') showDdaSuccess('Automatic schedule stopped.');
    } catch (err) {
      if (typeof showDdaError === 'function') showDdaError(err.message || 'Could not stop schedule');
      stop.disabled = false;
    }
  });
}