<?php

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

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

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

    public function __construct($opt = [])
    {
        if (is_array($opt)) {
            $this->options = array_merge($this->options, $opt);
        }
    }

    public function column($value = '')
    {
        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 . '"/>';
    }

}
