public function save_page_data()
    {
        // Get JSON POST data
        $json = file_get_contents('php://input');
        $data = json_decode($json, true);

        // Set response headers
        header('Content-Type: application/json');

        // Validate input data - now including new required fields
        if (
            !isset($data['app_slug']) ||
            !isset($data['pageData']) ||
            !isset($data['configFile']) ||
            !isset($data['globalCss'])
        ) {
            echo json_encode([
                'status' => 'error',
                'message' => 'Invalid input data format. Missing required fields.'
            ]);
            return;
        }

        $app_slug = $data['app_slug'];
        $response = [
            'status' => 'success',
            'message' => 'Pages saved successfully',
            'app_slug' => $app_slug,
            'details' => []
        ];

        // Create app directory if it doesn't exist
        $dir = FCPATH . 'uploads/apps/' . $this->active_app->id;
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }

        // Save config.js
        $config_path = $dir . '/config.js';
        try {
            if (file_put_contents($config_path, $data['configFile']) !== false) {
                $response['details'][] = [
                    'file' => 'config.js',
                    'status' => 'success',
                    'message' => 'Config file saved'
                ];
            } else {
                throw new Exception('Failed to write config file');
            }
        } catch (Exception $e) {
            $response['details'][] = [
                'file' => 'config.js',
                'status' => 'error',
                'message' => $e->getMessage()
            ];
        }

        // Save global.css
        $css_path = $dir . '/global.css';
        try {
            if (file_put_contents($css_path, $data['globalCss']) !== false) {
                $response['details'][] = [
                    'file' => 'global.css',
                    'status' => 'success',
                    'message' => 'Global CSS saved'
                ];
            } else {
                throw new Exception('Failed to write CSS file');
            }
        } catch (Exception $e) {
            $response['details'][] = [
                'file' => 'global.css',
                'status' => 'error',
                'message' => $e->getMessage()
            ];
        }

        // Process each page
        $project_data = [];
        foreach ($data['pageData'] as $key => $page) {
            if (!isset($page['page_name'])) {
                $response['details'][] = [
                    'page_name' => 'unknown',
                    'status' => 'error',
                    'message' => 'Missing page name'
                ];
                continue;
            }

            $project_data[$key]['page_name'] = $page['page_name'];
            // Save HTML file
            if (isset($page['page_html'])) {
                $html_filepath = $dir . '/' . $page['page_name'] . '.php';
                try {
                    if (file_put_contents($html_filepath, $page['page_html']) !== false) {
                        $project_data[$key]['page_html'] = $html_filepath;
                        $response['details'][] = [
                            'page_name' => $page['page_name'],
                            'status' => 'success',
                        ];
                    } else {
                        throw new Exception('Failed to write HTML file');
                    }
                } catch (Exception $e) {
                    $response['details'][] = [
                        'page_name' => $page['page_name'],
                        'status' => 'error',
                        'message' => $e->getMessage()
                    ];
                }
            }
        }

        // Save the root-level projectData as a single file
        $project_data_filepath = null;
        if (isset($data['projectData'])) {
            $project_data_filepath = $dir . '/project_data.json';
            try {
                if (file_put_contents($project_data_filepath, json_encode($data['projectData'], JSON_PRETTY_PRINT)) !== false) {
                    $response['project_data_file'] = 'uploads/apps/' . $this->active_app->id . '/project_data.json';
                } else {
                    throw new Exception('Failed to write project data file');
                }
            } catch (Exception $e) {
                $response['status'] = 'error';
                $response['message'] = 'Project data save failed: ' . $e->getMessage();
            }
        }

        // Check for any errors in the entire process
        $hasErrors = false;
        foreach ($response['details'] as $detail) {
            if ($detail['status'] === 'error') {
                $hasErrors = true;
                break;
            }
        }

        if ($hasErrors) {
            $response['status'] = 'partial';
            $response['message'] = 'Some files failed to save';
        }

        // Save homepage to database (default to 'home' if not specified)
        $this->db->set('site_url', base_url() . 'uploads/apps/' . $this->active_app->id . '/index.php');
        $this->db->set('project_data_file', $project_data_filepath ? 'uploads/apps/' . $this->active_app->id . '/project_data.json' : null);
        $this->db->set('project_data', json_encode($project_data));
        $this->db->where('ids', $this->active_app->ids);
        if (!$this->db->update('apps')) {
            $response['status'] = 'error';
            $response['message'] = 'Database update failed: ' . $this->db->error()['message'];
        }

        echo json_encode($response);
    }
some pages failed to save when this error comes