<?php

namespace addons\csmtable;

use addons\csmtable\library\xcore\xcore\utils\XcAdminSessionUtils;
use addons\csmtable\library\xcore\xcore\utils\XcRequestUtils;
use think\Addons;
use app\common\library\Menu;
use addons\csmtable\library\xcore\xcore\utils\XcConfigUtils;
use addons\csmtable\library\xcore\xcore\utils\XcResponseUtils;

/**
 * 插件
 */
class Csmtable extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu = [
            [
                'name' => 'csmtable',
                'title' => 'Table功能增强',
                'sublist' => [
                    [
                        'name' => 'csmtable/test',
                        'title' => '使用示例',
                        'icon' => 'fa fa-meetup',
                        'sublist' => [
                            [
                                'name' => 'csmtable/test/index',
                                'title' => '查询'
                            ],
                            [
                                'name' => 'csmtable/test/recyclebin',
                                'title' => '回收站'
                            ],
                            [
                                'name' => 'csmtable/test/add',
                                'title' => '添加'
                            ],
                            [
                                'name' => 'csmtable/test/edit',
                                'title' => '修改'
                            ],
                            [
                                'name' => 'csmtable/test/del',
                                'title' => '删除'
                            ],
                            [
                                'name' => 'csmtable/test/destroy',
                                'title' => '真实删除'
                            ],
                            [
                                'name' => 'csmtable/test/restore',
                                'title' => '还原'
                            ],
                            [
                                'name' => 'csmtable/test/multi',
                                'title' => '批量更新'
                            ],
                            [
                                'name' => 'csmtable/test/import',
                                'title' => '导入'
                            ],
                            [
                                'name' => 'csmtable/datasource/admin',
                                'title' => '人员信息读取'
                            ],
                            [
                                'name' => 'csmtable/cligenerateexcel/index',
                                'title' => '下载Excel'
                            ]
                        ]
                    ],
                    [
                        'name' => 'csmtable/xlstask',
                        'title' => '下载任务',
                        'icon' => 'fa fa-meetup',
                        'sublist' => [
                            [
                                'name' => 'csmtable/xlstask/index',
                                'title' => '查询'
                            ],
                            [
                                'name' => 'csmtable/xlstask/add',
                                'title' => '添加'
                            ],
                            [
                                'name' => 'csmtable/xlstask/edit',
                                'title' => '修改'
                            ],
                            [
                                'name' => 'csmtable/xlstask/del',
                                'title' => '删除'
                            ],
                            [
                                'name' => 'csmtable/xlstask/multi',
                                'title' => '批量'
                            ],
                            [
                                'name' => 'csmtable/csmgeneratesub/index',
                                'title' => '重新执行'
                            ],
                            [
                                'name' => 'csmtable/csmgenerate/generate',
                                'title' => '生成文件'
                            ],
                        ]
                    ]
                ]
            ]
        ];
        Menu::create($menu);
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete(XcConfigUtils::getAddonCode());
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable(XcConfigUtils::getAddonCode());
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable(XcConfigUtils::getAddonCode());
        return true;
    }

    /**
     * JS 获取配置的方式 Config.csmtable.cloginwxmp
     */
    public function configInit(&$params)
    {
        $config = $this->getConfig();
        $addons = XcConfigUtils::xpconfig('addons_code');
        $params[$addons] = [
        ];
    }

    public function actionBegin($call)
    {

        $request = XcRequestUtils::getRequest();

        if (true) {
            // 判断是否安装了插件:表格无刷新行内编辑
            $path = $request->path();
            if ($path == 'csmtable/test') {
                $editable = get_addon_info('editable');
                if (! $editable || ! $editable['state']) {
                    XcResponseUtils::error("为更好的演示本功能，请安装【表格无刷新行内编辑】插件", null, null, 60);
                }
            }
        }
        if (true) {
            // 异步下载用
            $method = $request->request('csmtable_method');
            $filesource = $request->request('csmtable_filesource');
            if ($method == 'download_excel') {
                set_time_limit(0);

                $dao = new \app\admin\model\csmtable\Xlstask();
                $admin_id = XcAdminSessionUtils::getUserId();

                // 限制下载
                if (true) {
                    $row = $dao->where("admin_id", "=", $admin_id)
                        ->where("progress", "<", "100")
                        ->where("createtime", ">", time() - 1800)
                        ->where("iserror", "<>", "Y")
                        ->find();
                    if ($row) {
                        XcResponseUtils::error("当前有下载任务，请任务结束后再尝试下载。");
                    }
                }

                // 生成任务记录
                $dao->where("admin_id", "=", $admin_id)
                    ->where("filesource", '=', $filesource)
                    ->where("status", "=", "normal")
                    ->update([
                    "status" => "hidden"
                ]);

                // 触发异步生成Excel任务
                $classname = get_class($call[0]);
                $getparams = [
                    'search' => $request->request('search'),
                    'filter' => $request->request('filter'),
                    'op' => $request->request('op'),
                    'sort' => $request->request('sort'),
                    'order' => $request->request('order'),
                    'offset' => $request->request('offset'),
                    'limit' => $request->request('limit'),
                    'csmtable_classname' => str_replace('\\', '/', $classname),
                    'csmtable_methodname' => 'index',
                    'csmtable_columns' => $request->request('csmtable_columns')
                ];

                $param = [
                    'admin_id' => $admin_id,
                    'filesource' => $filesource,
                    'param' => json_encode($getparams),
                    'createtime' => time()
                ];
                $row = $dao->create($param);

                $url = XcRequestUtils::urlBase("/addons/csmtable/csmgenerate/index?id={$row->id}&clogintoken=".XcAdminSessionUtils::getToken());
                $this->callremote2($url);
            }
        }
    }

    private function callremote2($url)
    {
        \fast\Http::sendRequest($url);
    }
}
