<?php
namespace addons\csmtable\library\xapp\csmtable\utils;

use addons\csmtable\library\xcore\xcore\utils\XcAdminSessionUtils;
use think\App;
use think\Loader;
use think\Request;
use addons\csmtable\library\xapp\csmtable\model\CsmRequest;

class CsmTableUtils
{

    /**
     *
     * @usaged
     *        var_dump(CsmTableUtils::getInstanceAndMethod("fa/test"));
     *        var_dump(CsmTableUtils::getInstanceAndMethod("fa/test/test"));
     *        array (size=2)
     *          0 => string '\app\admin\controller\fa\Test' (length=29)
     *          1 => string 'index' (length=5)
     *
     *        var_dump(CsmTableUtils::getInstanceAndMethod("category"));
     *        var_dump(CsmTableUtils::getInstanceAndMethod("category/test"));
     *        array (size=2)
     *          0 => string '\app\admin\controller\Category' (length=30)
     *          1 => string 'index' (length=5)
     */
    public static function getInstanceAndMethod(&$url)
    {
        if(stripos($url,"?")!==false){
            $url = substr($url,0,stripos($url,"?"));
        }
        $im = static::_isClassExists($url);
        if ($im == null) {
            $im = static::_isClassExists($url . "/index");
        }
        if ($im == null) {
            $im = static::_isClassExists($url . "index");
        }
        return $im;
    }

    private static function _isClassExists($url)
    {
        $sr = null;
        $clsname = "\\app\\admin\\controller";
        $patharr = explode("/", $url);
        foreach ($patharr as $k => $v) {
            if ($k == (count($patharr) - 2)) {
                $clsname .= "\\" . ucfirst($v);
                break;
            } else {
                $clsname .= "\\" . $v;
            }
        }
        $b = class_exists($clsname);
        if ($b === true) {
            $methodname = $patharr[count($patharr) - 1];
            if (count($patharr) == 1) {
                $methodname = 'index';
            }
            $sr = [
                $clsname,
                $methodname
            ];
        }
        return $sr;
    }

 
    
    public static function getParamValue(&$params, $key, $defaultvalue = null)
    {
        $sr = null;
        if (isset($params[$key])) {
            $sr = $params[$key];
        }
        $sr = ($sr == null) ? $defaultvalue : $sr;
        return $sr;
    }
    public static function callRemoteControl(&$params, $offset, $limit)
    {
        XcAdminSessionUtils::directApplicationAdminLogin();

        $classname = str_replace('/', '\\', static::getParamValue($params, 'csmtable_classname'));
        $methodname = static::getParamValue($params, 'csmtable_methodname');
 
        $controller = null;
        if(true){
            $request = CsmRequest::instance(); 
            $onlyClassname = Loader::parseName(substr($classname,strrpos($classname,"\\")+1),0);
            $tt = substr($classname,0,strrpos($classname,"\\"));
            $tt = Loader::parseName(substr($tt,strrpos($tt,"\\")+1),1);
            $controller = "{$tt}.{$onlyClassname}";
        }
        $request->controller($controller);
        $request->action($methodname);

         $instance = new $classname($request);
        $request->set('search', static::getParamValue($params, 'search'));
        $request->set('filter', static::getParamValue($params, 'filter'));
        $request->set('op', static::getParamValue($params, 'op'));
        $request->set('sort', static::getParamValue($params, 'sort'));
        $request->set('order', static::getParamValue($params, 'order'));
        $request->set("offset", $offset);
        $request->set("offset/d", $offset);//v2.1.8 修复在1.2.0.20201008_beta版本下导出数据记录数错误的问题
        $request->set('limit', $limit);
        $request->set('limit/d', $limit);//v2.1.8 修复在1.2.0.20201008_beta版本下导出数据记录数错误的问题
        $request->setMethodReturn("isAjax", true);

        //2.2.3 适配1.3.3版本，修复导出数据错乱的问题
        //修复了control分页使用paginate方法造成导出分页信息无法获取，原因是Paginator#getCurrentPage获取page是直接通过Request，而不是control的Request方法
        $page = $limit ? intval($offset / $limit) + 1 : 1;
        Request::instance()->get([config('paginate.var_page') => $page]);
         
        return App::invokeMethod([
            $instance,
            $methodname
        ], null);
    }
    
}
