<?php
namespace TourOperatorProAdmin\app\models;
defined('_TOUROPERATOR_EXEC') or die;

use TourOperatorPro\Factory;
use TourOperatorPro\Mvc\Model\ModelForm;
use TourOperatorPro\Form\Form;
use TourOperatorPro\Registry\Registry;
class ConfigModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "config";
    /**
     * @var string
     */
    public $table_name = "config";
    /**
     * @var array
     */
    public static $list_menu_by_xml = array();

    /**
     * @param array $data
     * @return array|bool|object|void
     * @throws Exception
     */
    public function save($data = array())
    {
        $table = $this->getTable('config');
        $table->load(1);
        $param = $data['params'];
        $reg = new Registry();
        $reg->loadArray($param);
        $table->params = $reg->toString();
        $table->store();
    }

    /**
     * @param int $id
     * @return mixed
     */
    public function getItem($id = 0)
    {
        $item = parent::getItem(1); // TODO: Change the autogenerated stub
        $param = $item->params;
        $reg = new Registry();
        $reg->loadString($param);
        $item->params = $reg;
        return $item;
    }

    /**
     * @return array|mixed
     */
    public function get_list_view_publish()
    {
        $db = Factory::getDBO();
        $query_check_table = $db->getQuery(true);
        $query_check_table->setQuery("SHOW TABLES LIKE '" . $this->getTableName('view') . "'");
        $tables = $db->setQuery($query_check_table)->loadColumn();
        if (count($tables)) {
            $query = $db->getQuery(true);
            $query->select('view.name')
                ->from($this->getTableName('view') . ' AS view')
                ->where('view.published=1');
            $listview = $db->setQuery($query)->loadColumn();
        } else {
            $listview = self::get_list_view_xml();
        }
        return $listview;
    }

    /**
     * @return array
     */
    public static function get_list_view_xml()
    {
        if (empty(self::$list_menu_by_xml)) {
            $file_xml_path_app = TOUROPERATORPRO_APP_BACKEND_PATH . "/views.xml";
            $xml = simplexml_load_file($file_xml_path_app);
            $list_menu_by_xml = [];
            foreach ($xml->view as $view) {
                $is_system = (string)$view->attributes()['is_system'];
                if ($is_system == 1) {
                    $list_menu_by_xml[] = (object)[
                        'id' => (string)$view->attributes()['id'],
                        'menu_slug' => (string)$view->attributes()['menu_slug'],
                        'label' => (string)$view->attributes()['label'],
                        'page_title' => (string)$view->attributes()['page_title'],
                        'capability' => (string)$view->attributes()['capability'],
                        'icon' => (string)$view->attributes()['icon'],
                        'is_system' => (string)$view->attributes()['is_system'],
                    ];
                }
            }
            self::$list_menu_by_xml = $list_menu_by_xml;
        }
        return self::$list_menu_by_xml;
    }

    /**
     * @return bool|Form
     */
    public function getFormCreateView()
    {
        Form::addFieldPath(TOUROPERATOR_PLUGIN_DIR_URL . '/lib/Form/Field');
        Form::addFieldPath(TOUROPERATOR_PLUGIN_DIR_URL . '/lib/fieldss');
        Form::addFormPath(TOUROPERATORPRO_APP_PATH . '/models/forms');
        try {
            $data = [
                "views" => $this->get_list_view_publish()
            ];
            $form = Form::getInstance("create_view");
            $form->bind($data);
        } catch (\Exception $e) {
            $this->setError($e->getMessage());
            return false;
        }
        return $form;
    }
}
