<?php
namespace PortalPeople\documentdesigner\controllers;

use Yii;
use yii\web\Controller;


use PortalPeople\documentdesigner\models\DocumentLayout;

class DesignerController extends Controller
{
    /**
     * Show the document designer UI
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Save a layout securely (AJAX)
     */
    public function actionSave()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = new DocumentLayout();
        $model->name = Yii::$app->request->post('name');
        $model->content = Yii::$app->request->post('content');
        $model->type = Yii::$app->request->post('type', 'html');
        if ($model->validate() && $model->save()) {
            return ['success' => true, 'id' => $model->id];
        }
        return ['success' => false, 'errors' => $model->errors];
    }

    /**
     * Load a layout securely (AJAX)
     */
    public function actionLoad($id)
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = DocumentLayout::findOne($id);
        if ($model) {
            return ['success' => true, 'layout' => $model];
        }
        return ['success' => false, 'error' => 'Not found'];
    }

    /**
     * Export layout to PDF using chrome-php (stub)
     */
    public function actionExport($id)
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        // TODO: Implement chrome-php PDF rendering
        // Load layout by $id, render PDF in sandbox
        return ['success' => true, 'pdfUrl' => '/path/to/pdf'];
    }
}
