<?php

namespace App\Web\Components\Form;

use App\Exceptions\ApiException;
use App\Form\Controllers\FormController;
use App\Form\Facades\FormItemFacade;
use App\Form\Services\FormInfoService;
use App\Web\Facades\WebFacade;
use App\Web\Services\WebService;
use Illuminate\View\Component;

class FormInfo extends Component
{
    protected $formId; //这是参数
    protected $viewFile;
    protected $router_path;
    protected $element_code;


    /**
     * @throws ApiException
     */
    public function __construct($viewFile = '', $formId = '', $routerPath = '', $elementCode = '')
    {
        if (empty($viewFile)) {
            throw new ApiException('common.page_none', '您访问的地址不存在~');
        }
        $this->formId = $formId;
        $this->viewFile = $viewFile;
        $this->router_path = $params['router_path'] ?? $routerPath;
        $this->element_code = $elementCode ?? '';
    }

    /**
     * 表单组件
     */
    public function render()
    {
        $retData = [];
        $retData['form_id'] = $this->formId;
        $retData['form_items'] = FormItemFacade::getData($this->formId);
        $layoutContent = WebFacade::getStaticPageInfo(['route_path' => WebService::LAYOUTS_ROUTE]);
        if (!empty($this->router_path)) {
            $pageContent = WebFacade::getStaticPageInfo(['route_path' => $this->router_path]);
            $retData['page_content'] = $pageContent['page_content'];
            if (!empty($layoutContent['page_content'])) {
                foreach ($layoutContent['page_content'] as $key => $content_item) {
                    if (empty($retData['page_content'][$key])) {
                        $retData['page_content'][$key] = $content_item;
                    }
                }
            }
        }
        $retData['menu_data'] = WebFacade::getWebMenu();
        if (!empty($this->element_code)) {
            $retData['element_code'] = $this->element_code;
        }
        //dd($retData);
        return view($this->viewFile, $retData);
    }

}
