<?php

namespace web\admin\ap;

class usr_capcode {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_usr_capcode');
        $val = objstr($query, 'vuser');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('ap_user');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('vuser', $trow['id']);
                $query['vuser'] = $trow['name'];
            } else {
                $csql->where('vuser=0');
            }
        }
        $csql->where('account like', objstr($query, 'account'));
        $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_list() {
        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);
        if($rows === false)
            return errjson($db->error);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'ap_usr_capcode');
            foreach ($field as $fr => $v) {
                if (get('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array('type' => 'input', 'form' => 'vuser', 'name' => '所属用户', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'input', 'form' => 'account', 'name' => '账号', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'daterange', 'form' => 'addtimes', 'name' => '生成时间');
            $ret['searchinput'] = $input;
        }
        $ret['ap_user'] = getrelation($db, $rows, 'ap_user', 'vuser');
        return succjson($ret);
    }

    public static function json_getdata() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $act = $post->get('act');
        if ($id > 0) {
            $csql = new \ciy\sql('ap_usr_capcode');
            $csql->where('id', $id);
            $row = $db->getone($csql);
            if (!is_array($row))
                return errjson('数据不存在');
            if ($act == 'view' || $act == 'review') {
                $csql = (new \ciy\sql('ap_user'))->column('id,name');
                $csql->where('id', $row['vuser']);
                $ret['ap_user'] = $db->get($csql);
            }
        } else {
            $row = array();
        }
        $ret['data'] = $row;
        if ($act == 'edit') {
            $csql = (new \ciy\sql('ap_user'))->column('id,name');
            $ret['ap_user'] = $db->get($csql);
        }
        return succjson($ret);
    }


    public static function json_del() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p d'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('ap_usr_capcode');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                //delcheck($db, $delid, 'tablexx', 'xxid', '管理员');
                //delall($db, $delid, 'tablexx', 'xxid', '运动员');  //deltimeall
                delme($db, $delid, 'ap_usr_capcode');
                savelogdb($db, $rsuser['id'], 'ap_usr_capcode', $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);
    }
}
