<?php
namespace WpBookingProAdmin\app\models;


defined('_WPBOOKINGPRO_EXEC') or die;


use Exception;
use WpBookingPro\Factory;
use WpBookingPro\Language\Text;
use WpBookingPro\Mvc\Model\ModelForm;
use WpBookingPro\Filesystem\File as FileAlias;
use WpBookingPro\Mvc\Model\Model;
use WpBookingPro\Form\Form;
use WpBookingPro\Mvc\Model\ModelList;
use WpBookingPro\Registry\Registry;
use WpBookingPro\Utilities\Utility;

class GroupuserModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "groupuser";
    /**
     * @var string
     */
    public $table_name = "groupuser";

    public function save($data = array())
    {

        $role_user = isset($data['role_user'])?$data['role_user']:array();
        $role_user = json_decode($role_user);
        $permission = array();

        foreach ($role_user as $key => &$item) {
            $permission[$key] = json_encode($item);
        }
        
        $group_user =  parent::save($data); // TODO: Change the autogenerated stub
        $modelPermission = Model::getInstance('permission');

        $permission['group_user_id'] = $group_user->id;
        $db = Factory::getDBO();
        $query = $db->getQuery(true);
        $query->clear()
            ->delete($this->getTableName('permission'))
            ->where('group_user_id='.(int)$group_user->id)
        ;
        $db->setQuery($query)->execute();

        $modelPermission->save($permission);
        return $group_user;
    }

}
