<?php

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

/**
 * Set view_order
 *
 * @author Igor Ratsun
 */
class Rtgc_view_order extends \Abstract_rtgc_callback implements \Rtgc_filtrable
{

    protected $options = [
        'field' => 'view_order',
    ];

    public function __construct($opt = [])
    {
        if (is_array($opt)) {
            $this->options = array_merge($this->options, $opt);
        }
        if (!empty($this->options['crud'])) {
            $action = new rtgrid_action('change_view_order', '', [$this, 'change_view_order']);
            $action->set_template(' ');
            $this->options['crud']->add_action($action);
            $ci = get_instance();
            $ci->js_css->add_css_string('.view_order_container{width:50px!important}.view_order_label{padding:5px 7px;display:inline-block}.view_order_input{display:none!important; width:50px}.main_table td:hover .view_order_input{display:inline-block!important; margin-bottom:0!important}.main_table td:hover .view_order_label{display:none!important}');
            $ci->js_css->add_js_string(
                    '$(function(){
                    $(".main_table").on("change", ".view_order_input", function(){
                        var el = $(this); 
                        var form = el.parents("form:first");
                        form.find("input[type=checkbox]").prop("checked", false);
                        el.parents("tr:first").find("input[type=checkbox]").prop("checked", true);
                        if (form.find("input[type=checkbox]:checked").length > 0) {
                            var action = form.find("input[name=action]");
                            action.val("change_view_order");
                            form.append(\'<input name="sent_view_order" type="hidden" value="\' + el.val() + \'" />\');
                            formSubmit(form);
                            action.val("");
                            form.find("input[name=sent_view_order]").remove();
                            form.find("input[type=checkbox]").prop("checked", false);
                        }
                    });
                });'
            );
        }
    }

    public function column($value = '', $row = [])
    {
        if (!empty($this->options['crud'])) {
            $id = reset($row);
            $name = $this->options['field'] . '[' . $id . ']';
            return '<div class="view_order_container"><span class="view_order_label">' . $value . '</span>'
                    . '<input type="number" class="view_order_input" name="' . $name . '" value="' . $value . '"/></div>';
        }

        return $value;
    }

    public function field($name = '', $value = NULL)
    {
        if (empty($value)) {
            $max = 0;
            if (!empty($this->options['crud'])) {
                $table = $this->options['crud']->get_table();
                $res = get_instance()->db
                                ->select('MAX(' . $this->options['field'] . ') as value', false)
                                ->get($table)->row_array();
                $max = $res['value'];
            }
            $value = floor(($max + 10) / 10) * 10;
        }

        return '<input name="' . $name . '" type="number" min="0" value ="' . (int) $value . '"/>';
    }

    public function change_view_order($ids = [])
    {
        if (!empty($this->options['crud'])) {
            $ci = get_instance();
            $table = $this->options['crud']->get_table();
            $pkey = isset($this->options['primary_key']) ? $this->options['primary_key'] : 'id_' . $table;
            $ci->db
                    ->where_in($pkey, $ids)
                    ->update($table, [
                        $this->options['field'] => (int) $ci->input->post('sent_view_order')
            ]);
        }

        return true;
    }

    public function filter($name, $value)
    {
        return '<input name="' . $name . '" type="number" min="0" value ="' . $value . '"/>';
    }

    public function filter_by($column, $value)
    {
        if ($value != '') {
            get_instance()->db->where($column, $value);
        }
    }

}
