<?php

namespace web\admin\rigger;

class online {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_online');
        $csql->where('ip like', objstr($query, 'ip'));
        $csql->where('user', objstr($query, 'user'));
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $order = objstr($query, 'order', 'id desc');
        $csql->order($order);
        $query['order'] = $order;
        return [$query, $csql];
    }
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post);
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $mainrowcount = $post->getint('count');
        $rows = $db->get($csql, $mainrowcount);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_online');
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }

        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'daterange',
                'form' => 'addtimes',
                'name' => '登录时间'
            );
            $input[] = array(
                'type' => 'select',
                'form' => 'user',
                'name' => '用户',
                'select' => 'adminuser',
                'all' => '全部'
            );
            $input[] = array(
                'type' => 'input',
                'form' => 'ip',
                'name' => '登录IP'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p505d'))
            return errjson('您未被授权操作');

        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_online');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                delme($db, $delid, 'zc_online');
                savelogdb($db, $rsuser['id'], 'zc_online', $row, null);
                $vids[] = $delid;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['ids'] = $vids;
        return succjson($ret);
    }
    public static function json_logout() {
        global $db;
        $rsuser = verifyfast();
        if (nopower($db, $rsuser['id'], 'p505o'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('zc_online');
        $csql->where('id', $id);
        $row = $db->getone($csql);
        if (!is_array($row))
            return errjson('记录不存在');
        if ($rsuser['id'] == $row['user'])
            return errjson('不能下线自己');
        $db->execute('update zc_online set sid=\'\' where id=?', array($id));
        $ret['data'] = array('id' => $id, 'sid' => '');
        return succjson($ret);
    }
}
