<?php

namespace web\admin\demo;

class ultable {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_role');
        $csql->where('name like', objstr($query, 'name'));
        $csql->where('memo like', objstr($query, 'memo'));
        $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('where' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_role');
            foreach ($field as $fr => $v) {
                if ($post->is('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $field['memo']['thwidth'] = '25em';
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }

        if ($post->getbool('once')) {
            $ret['once'] = array();
            $input = array();
            $input[] = array(
                'type' => 'input', 'form' => 'name', 'name' => '角色名称', 'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'memo', 'name' => '角色说明', 'prop' => ' style="width:8em;"'
            );
            $ret['once']['input'] = $input;
        }
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();
        if (nopower($db, $rsuser['id'], 'p502d'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_role');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                delcheck($db, $delid, 'zc_admin', 'roleid', '用户');
                delme($db, $delid, 'zc_role');
                savelogdb($db, $rsuser['id'], 'zc_role', $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);
    }
}
