with open('app/Controllers/Admin.php', 'r', encoding='utf-8') as f:
    content = f.read()

old_block = """                    if ($field['type'] === 'text' || $field['type'] === 'date') {
                        $value = $this->request->getPost('tpl_' . $fieldId) ?? '';
                        $templateData[$fieldId] = ['type' => $field['type'], 'value' => $value];
                        $field['value'] = $value;
                        $photoFile = $this->request->getFile('tpl_' . $fieldId);
                        if ($photoFile && $photoFile->isValid() && !$photoFile->hasMoved()) {
                            $ext = strtolower($photoFile->getExtension());
                            $photoName = uniqid('photo_') . '.' . $ext;
                            if (!is_dir(WRITEPATH . 'uploads/template_photos')) {
                                mkdir(WRITEPATH . 'uploads/template_photos', 0755, true);
                            }
                            $photoFile->move(WRITEPATH . 'uploads/template_photos', $photoName);
                            $templateData[$fieldId] = ['type' => 'photo', 'file' => $photoName];
                            $field['photoFile'] = WRITEPATH . 'uploads/template_photos/' . $photoName;
                        }
                    }"""

new_block = """                    if ($field['type'] === 'text' || $field['type'] === 'date') {
                        $value = $this->request->getPost('tpl_' . $fieldId) ?? '';
                        $templateData[$fieldId] = ['type' => $field['type'], 'value' => $value];
                        $field['value'] = $value;
                    } elseif ($field['type'] === 'photo') {
                        $photoFile = $this->request->getFile('tpl_' . $fieldId);
                        if ($photoFile && $photoFile->isValid() && !$photoFile->hasMoved()) {
                            $ext = strtolower($photoFile->getExtension());
                            $photoName = uniqid('photo_') . '.' . $ext;
                            if (!is_dir(WRITEPATH . 'uploads/template_photos')) {
                                mkdir(WRITEPATH . 'uploads/template_photos', 0755, true);
                            }
                            $photoFile->move(WRITEPATH . 'uploads/template_photos', $photoName);
                            $templateData[$fieldId] = ['type' => 'photo', 'file' => $photoName];
                            $field['photoFile'] = WRITEPATH . 'uploads/template_photos/' . $photoName;
                        }
                    }"""

if old_block in content:
    content = content.replace(old_block, new_block)
    with open('app/Controllers/Admin.php', 'w', encoding='utf-8') as f:
        f.write(content)
    print("Fixed Admin.php")
else:
    print("Block not found!")
