<?php

namespace web\admin\rigger;

class debug_user {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_debug_user');
        $liid = objint($query, 'liid');
        $csql->where('targettype', objstr($query, 'targettype'));
        $csql->where('isuse', objstr($query, 'isuse'));
        $csql->where('name like', objstr($query, 'name'));
        $csql->where('user like', objstr($query, 'user'));
        $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_debug_user');
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }

        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'select', 'form' => 'targettype', 'name' => '子系统', 'select' => 'targettype', 'all' => '全部'
            );
            $vals = $db->getdbcodes('zc_debug_user', 'isuse');
            $input[] = array(
                'type' => 'select', 'form' => 'isuse', 'name' => '开关', 'select' => $vals, 'all' => '全部'
            );
            // $input[] = array(
            //     'type' => 'switch', 'form' => 'isuse', 'name' => '开关', 'value' => $vals
            // );
            $input[] = array(
                'type' => 'input', 'form' => 'name', 'name' => '显示名', 'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'user', 'name' => '用户名', 'prop' => ' style="width:8em;"'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }
    public static function json_update() {
        global $db;
        $rsuser = verifyfast();

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

        $post = new \ciy\post();
        $id = $post->getint('id');
        $targettype = $post->getint('targettype');
        if ($targettype <= 0)
            return errjson('请选择子系统');
        $isuse = $post->getint('isuse');
        $name = $post->get('name');
        if (empty($name))
            return errjson('请填写显示名');
        $user = $post->get('user');
        if (empty($user))
            return errjson('请填写用户名');
        $pass = $post->get('pass');
        if (empty($pass))
            return errjson('请填写密码');
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('zc_debug_user');

            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $csql = new \ciy\sql('zc_debug_user');
            $csql->where('targettype', $targettype);
            $csql->where('user', $user);
            $csql->column('id');
            $chkid = toint($db->get1($csql));
            if ($chkid > 0 && (($id > 0 && $chkid != $id) || $id == 0))
                throw new \Exception('CIYIGN数据已存在');

            $updata = array();
            $updata['targettype'] = $targettype;
            $updata['isuse'] = $isuse;
            $updata['name'] = $name;
            $updata['user'] = $user;
            $updata['pass'] = $pass;
            $csql = new \ciy\sql('zc_debug_user');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_debug_user', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

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

        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_debug_user');

        $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_debug_user');
                savelogdb($db, $rsuser['id'], $delid, $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_getlocal() {
        global $db;
        $rsuser = verifyfast();

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

        $post = new \ciy\post();
        $targettype = $post->getint('targettype');
        if($targettype == 0)
            return errjson('请选择子系统');
        $csql = new \ciy\sql('zc_debug_user');
        $csql->where('targettype', $targettype);
        $csql->where('isuse', 1);
        $csql->column('id,name,user,pass');
        $ret = array();
        $ret['list'] = $db->get($csql);
        return succjson($ret);
    }
}
