<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * CRUD model
 *
 * @author CIEXT skeleton
 */
class Text_crud extends \Ciext\Model\AbstractEventModel
{

    protected $crud;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('rtgrid/rtgrid_api');
        $this->crud = new rtgrid_api();

        // Access check
        if (!$this->rt_user->isAllowed('inlineeditor', 'edit_inlineeditor')) {
            $this->admin_lib->show_denied();
        }

        if ($this->input->get('referer')) {
            $this->session->set_userdata('inline_referer', $this->input->get('referer'));
        }
    }

    public function action_save($table, $primary_key, $primary_key_value, $post_data = [])
    {
        $this->rtgrid_edit_model->redirect_url = $this->session->userdata('inline_referer');
        return $this->rtgrid_edit_action_model->save($table, $primary_key, $primary_key_value, $post_data);
    }

    public function action_delete($table, $primary_key, $primary_key_value, $post_data = [])
    {
        $this->load->model('rtgrid/rtgrid_action_model');
        $this->rtgrid_edit_model->redirect_url = $this->session->userdata('inline_referer');
        $this->rtgrid_action_model->table = $table;
        $this->rtgrid_action_model->primery_key = $primary_key;

        // Clear file
        if ($this->uri->get_next_segment('inline') == 'img') {
            $this->load->model('inlineeditor/inlineeditor_text_tmodel');
            $file = $this->inlineeditor_text_tmodel->fetch($primary_key_value);
            (new \Rtgc_upload_image())->remove_file($file['value']);
        }

        return $this->rtgrid_action_model->delete([$primary_key_value]);
    }

    public function action_cancel($table, $primary_key, $primary_key_value, $post_data = [])
    {
        $this->rtgrid_edit_model->redirect_url = $this->session->userdata('inline_referer');
        return true;
    }

    public function render()
    {
        $this->crud->set_table('inlineeditor_text');
        $this->crud->columns('i_id, value');
        $this->crud->fields('value');

        $this->crud->callback('i_id', 'rtgc_edit_link');
        $this->crud->readonly('i_id');
        if ($this->crud->is_grid()) {
            $this->crud->callback('value', 'rtgc_html');
        }

        $this->crud->display_as('i_id', $this->lang->line('Inline ID'));

        $this->crud->unset_action('add');
        $this->crud->unset_action('clone');
        $this->crud->unset_action('save');
        $this->crud->unset_action('save_and_back_to_list');
        $this->crud->unset_action('back_to_list');

        if (!$this->crud->is_grid()) {
            $btn_template = '<input type="submit" class="btn %s" name="{action_name}" value="{action_text}" />';
            $this->crud->add_action(
                new rtgrid_edit_action(
                    'save',
                    $this->lang->line('Save changes'),
                    [$this, 'action_save'],
                    '',
                    sprintf($btn_template, 'btn-success')
                )
            );
            $this->crud->add_action(
                new rtgrid_edit_action(
                    'delete',
                    $this->lang->line('Return default value'),
                    [$this, 'action_delete'],
                    '',
                    sprintf($btn_template, 'btn-danger')
                )
            );
            $this->crud->add_action(
                new rtgrid_edit_action(
                    'cancel',
                    $this->lang->line('Cancel'),
                    [$this, 'action_cancel']
                )
            );
        }

        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->crud->render();
    }

    public function render_text()
    {
        $this->crud->display_as('value', $this->lang->line('Enter description'));
        $this->crud->callback('value', 'rtgc_texteditor');

        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->render();
    }

    public function render_html()
    {
        $this->crud->display_as('value', $this->lang->line('Enter HTML'));
        $this->crud->callback('value', 'rtgc_textarea', [
            'maxlength' => 21844,
        ]);

        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->render();
    }

    public function render_label()
    {
        $this->crud->display_as('value', $this->lang->line('Enter short description'));
        $this->crud->callback('value', 'rtgc_textarea', [
            'maxlength' => 255,
            'rows'      => 4,
        ]);

        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->render();
    }

    public function render_img()
    {
        $this->crud->set_table('inlineeditor_text');
        $this->crud->display_as('value', $this->lang->line('Select image'));
        $this->crud->set_image_upload(new image_upload('value'));

        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->render();
    }

    public function render_yt_video()
    {
        $this->crud->display_as('value', $this->lang->line('YouTube link'));
        $this->crud->callback('value', new \Rtgc_yt_video());

        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->render();
    }
}
