<?php

namespace web\admin\rigger;

class tokthd {
    static function setwhere($db, $post, $rsuser) {
        $query = $post->get('query', array());
        $csql = new \ciy\sql('zc_tokthd');
        $csql->where('name like', objstr($query, 'name'));
        $csql->wheredaterange('exptimes', objstr($query, 'exptimes'));
        $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_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post, $rsuser);
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $mainrowcount = $post->getint('count');
        $mrows = $db->get($csql, $mainrowcount);
        if ($mrows === false)
            return errjson($db->error);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $mrows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_tokthd');
            foreach ($field as $fr => $v) {
                if (get('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array('type' => 'input', 'form' => 'name', 'name' => '三方平台名称', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'daterange', 'form' => 'exptimes', 'name' => '过期时间');
            $input[] = array('type' => 'input', 'form' => 'memo', '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'], 'p u'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        //$db->trace($post->get('_pf'), $rsuser);
        $id = $post->getint('id');
        $name = $post->get('name');
        $cfgtoken = $post->get('cfgtoken');
        $memo = $post->get('memo');
        if (empty($name))
            return errjson('请填写三方平台名称');
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('zc_tokthd');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['cfgtoken'] = $cfgtoken;
            $updata['memo'] = $memo;
            $csql = new \ciy\sql('zc_tokthd'); //auto
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['accesstoken'] = '';
                $updata['exptimes'] = 0;
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            //savelogdb($db, $rsuser['id'], 'zc_tokthd', $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'], 'p d'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        //$db->trace($post->get('_pf'), $rsuser);
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_tokthd');
        $csql->where('id in', $ids);
        $mrows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($mrows as $mrow) {
                $delid = $mrow['id'];
                //delcheck($db, $delid, 'tablexx', 'xxid', '管理员');
                //delall($db, $delid, 'tablexx', 'xxid', '运动员');  //deltimeall
                delme($db, $delid, 'zc_tokthd');
                savelogdb($db, $rsuser['id'], 'zc_tokthd', $mrow, 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);
    }
}
