<?php

namespace web\admin\rigger;

class power {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_power');
        $csql->where('groupname like', objstr($query, 'groupname'));
        $csql->where('name like', objstr($query, 'name'));
        $csql->where('scope like', objstr($query, 'scope'));
        $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);
        $rows[] = array('id' => 0, 'groupname' => '', 'name' => '', 'scope' => '', 'csort' => 10, 'powers' => '');
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = '';
            $fshow = fieldadd($fshow, $field, -1, 'groupname', '分组名');
            $fshow = fieldadd($fshow, $field, -1, 'name', '模块名称');
            $fshow = fieldadd($fshow, $field, -1, 'scope', '模块代码');
            $fshow = fieldadd($fshow, $field, -1, 'powers', '权限项');
            $fshow = fieldadd($fshow, $field, -1, 'csort', '排序');
            $fshow = fieldadd($fshow, $field, -1, '_btn', '操作');
            $field['groupname']['thwidth'] = '8em';
            $field['name']['thwidth'] = '8em';
            $field['scope']['thwidth'] = '6em';
            $field['powers']['thwidth'] = '31em';
            $field['csort']['thwidth'] = '5em';
            $ret['fshow'] = $fshow;
            $ret['field'] = $field;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'input', 'form' => 'groupname', 'name' => '分组名', 'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'name', 'name' => '模块名称', 'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'scope', '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'], 'p990u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $groupname = $post->get('groupname');
        $name = $post->get('name');
        $scope = $post->get('scope');
        $powers = $post->get('powers');
        $csort = $post->getint('csort');
        if (empty($groupname))
            return errjson('请填写分组名');
        if (empty($name))
            return errjson('请填写模块名称');
        if (empty($scope))
            return errjson('请填写模块代码');
        if (empty($powers))
            return errjson('请填写权限项');

        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('zc_power');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $csql = new \ciy\sql('zc_power');
            $csql->where('scope', $scope);
            $csql->column('id');
            $chkid = toint($db->get1($csql));
            if ($chkid > 0 && (($id > 0 && $chkid != $id) || $id == 0))
                throw new \Exception('CIYIGN模块代码重复');

            $updata = array();
            $updata['groupname'] = $groupname;
            $updata['name'] = $name;
            $updata['scope'] = $scope;
            $updata['powers'] = $powers;
            $updata['csort'] = $csort;
            $csql = new \ciy\sql('zc_power');
            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_power', $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'], 'p990u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_power');
        $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_power');
                savelogdb($db, $rsuser['id'], 'zc_power', $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);
    }
}
