# Debug: Classification "Keep old one" → duplicate 650 and fill

When you click **"Keep old one"** in the jConfirm modal (after selecting a second classification in 050/082), the flow should:
1. Duplicate the last 650 field (clone, replace IDs, append).
2. Call `getClassificationData` via AJAX.
3. In success: fill 050/082 and fill **only the new 650 clone** with `values.a`, `values.x`, `values.y`, `values.z`.

Use this list to set breakpoints or add `console.log` to trace where it fails.

---

## Files and lines linked to this flow

### 1. Front-end: "Keep old one" action and AJAX fill

| File | Lines | What to check |
|------|--------|----------------|
| `public_html/themes/main/views/items/biblios/create.blade.php` | **2272–2342** | Entire `keep_old_one` button action: clone 650, mark clone (`.js-650-clone-fill-target`, `data-650-fill-id`), append, then AJAX. |
| `public_html/themes/main/views/items/biblios/create.blade.php` | **2297–2338** | AJAX call and `success`: check `resp.status`, `resp.values`, then finding `$new650` and filling inputs. |
| `public_html/themes/main/views/items/biblios/create.blade.php` | **2317–2332** | Finding clone by `.js-650-clone-fill-target[data-650-fill-id="' + newId + '"]`, then `$new650.find('input[name^="tag_650_X_"]').first()` and `.val(values[code])`. |
| `public_html/themes/main/views/items/biblios/edit.blade.php` | Same structure as create | Same logic; use same line ranges as above for edit. |

**Debug tips (create.blade.php):**
- After `$last650.after($clone)`: log `$('.js-650-clone-fill-target').length` (should be 1).
- In `success`: log `resp`, `resp.values`, `values.a`, and `$new650.length` before the fill loop.
- Inside the loop: log `code`, `values[code]`, and `$inp.length` for each subfield.

---

### 2. Where the modal and "keep old one" are defined

| File | Lines | What to check |
|------|--------|----------------|
| `public_html/themes/main/views/items/biblios/create.blade.php` | **2244–2320** | `.select_classification` click handler: `valueExist650`, then `$.confirm({ ... keep_old_one: { action: function () { ... } } })`. |
| `public_html/themes/main/views/items/biblios/create.blade.php` | **2254–2262** | Condition that shows the modal: `valueExist650 > 0` (any 650@a/x/y has value). |

---

### 3. Back-end: classification data returned for the fill

| File | Lines | What to check |
|------|--------|----------------|
| `app/controllers/Admin/ClassificationController.php` | **265–328** | `getClassificationData()`: reads `classification_id`, `language`, `random`, loads Classification, builds `$fieldValues` (a, x, y, z, dewi_number, congress_number), returns JSON. |
| `app/controllers/Admin/ClassificationController.php` | **282–308** | Building `values.a`, `values.x`, `values.y`, `values.z` from classification (topical_term, general_subdivision, etc.). |
| `app/controllers/Admin/ClassificationController.php` | **319–326** | Return: `status`, `field`, `random`, `classification_id`, `values`. |

**Debug tips:**
- In browser Network tab: open the request to `getClassificationData` when you click "Keep old one" and confirm the response has `status: 'success'` and `values: { a: '...', ... }`.
- In controller: temporarily `\Log::info('getClassificationData', ['response' => $result]);` before returning.

---

### 4. DOM structure of 650 (so selectors are correct)

| File | Lines | What to check |
|------|--------|----------------|
| `public_html/themes/main/partials/marc/marc_form.blade.php` | **101–104** | Root of each tagfield: `<div class="collapser tagfield" data-identifier="{{ $id }}" tag-identifier="{{ $tagfield->tagfield }}">`. For 650, `tag-identifier="650"`. |
| `public_html/themes/main/partials/marc/marc_form.blade.php` | **180–201** | Panel that contains subfields: `<div class="col-md-12 panel-collapse collapse" id="tag_{{ $tagfield->tagfield }}_{{ $id }}">` and `<ul class="sortable_subfield">` then subfields_list. |
| `public_html/themes/main/partials/marc/subfields_list.blade.php` | **48** | Input name: `$name = 'tag_' . $tagfield->tagfield . '_' . $subfield->tagsubfield . '_' . $id . '_' . uniqid();` → e.g. `tag_650_a_<id>_<uniqid>`. |
| `public_html/themes/main/partials/marc/subfield_row.blade.php` | **125–126** | Actual input: `name="{{ $name }}"` (and id). So clone’s inputs have `name^="tag_650_a_"` etc. after ID replace. |

So inside the clone we find inputs with:
- `input[name^="tag_650_a_"]`, `input[name^="tag_650_x_"]`, etc., **scoped to the clone** (the div we found by `.js-650-clone-fill-target[data-650-fill-id="' + newId + '"]`).

---

### 5. Routes

| File | Lines | What to check |
|------|--------|----------------|
| `app/routes.php` | Search for `getClassificationData` | Route that points to `ClassificationController@getClassificationData`. |

---

## Quick checklist when "Keep old one" does nothing

1. **Modal appears?** If not, `valueExist650` is 0 → check selectors for `input[name^="tag_650_a_"]` etc. in create.blade.php (~2256–2259).
2. **After clicking "Keep old one", does a second 650 block appear?** If not, clone/append or selector for `$last650` is wrong (create ~2277–2295).
3. **Does the AJAX request run?** Network tab: request to `getClassificationData` with `classification_id`, `language`, `random`. If it doesn’t run, the button `action` may not be firing.
4. **Does the response have `status: 'success'` and `values.a` (and optionally x,y,z)?** If not, check ClassificationController and the classification record (topical_term, etc.).
5. **Is the clone found in success?** Log `$('.js-650-clone-fill-target[data-650-fill-id="' + newId + '"]').length` in success (create ~2318). Should be 1.
6. **Are inputs found inside the clone?** Log `$new650.find('input[name^="tag_650_a_"]').length` (create ~2325). Should be ≥ 1. If 0, the clone’s inner HTML or name pattern is wrong (see subfields_list name format above).

---

## Optional: temporary console logs (remove after debugging)

In `create.blade.php` inside the `keep_old_one` action, after `$last650.after($clone)`:

```javascript
console.log('650 clone appended', { newId: newId, cloneCount: $('.js-650-clone-fill-target').length });
```

Inside the AJAX `success` callback, right after `var values = resp.values`:

```javascript
console.log('getClassificationData success', { resp: resp, values: values, newId: newId });
```

Right after `var $new650 = $('.js-650-clone-fill-target...')`:

```javascript
console.log('$new650', { length: $new650.length, inputsA: $new650.find('input[name^="tag_650_a_"]').length });
```

Inside the `$.each(['a','x','y','z']` loop, before `if ($inp.length)`:

```javascript
console.log('fill 650', code, values[code], $inp.length);
```
