<?php

namespace web\admin\rigger;

class menu {
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_icon');
        $csql->where('icontarget', 10);
        $csql->column('targetid as id,icon');
        $iconrows = $db->get($csql);
        $icons = mapid2data($iconrows);
        $csql = new \ciy\sql('zc_menu');
        $csql->order('csort desc,id');
        $rows = $db->get($csql);

        for ($i = 0; $i < count($rows); $i++) {
            if (isset($icons[$rows[$i]['id']]))
                $rows[$i]['icon'] = $icons[$rows[$i]['id']]['icon'];
        }
        $ret = array('list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_menu');
            $fshow = fieldadd($fshow, $field, -1, '_btn', '操作');
            $fshow = fieldadd($fshow, $field, 3, 'demo', '原型');
            $fshow = fieldadd($fshow, $field, 2, 'icon', '|图标|');
            $field['url']['thwidth'] = '20em';
            $field['csort']['thwidth'] = '5em';
            $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' => 'input',
                'form' => 'url',
                'name' => '链接',
                'prop' => ' style="width:12em;"'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }

    public static function json_update() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p980u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $updata = array();
        $id = $post->getint('id');
        $name = $post->get('name');
        if ($name == '')
            return errjson('请填写菜单名称');
        $upid = $post->getint('upid');
        $csort = $post->getint('csort');
        $isuse = $post->getint('isuse');
        $csql = new \ciy\sql('zc_menu');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        if (!is_array($datarow))
            return errjson('数据不存在');
        $url = '';
        $urldb = '';
        $pow = '';
        $demo = '';
        $csql = new \ciy\sql('zc_menu');
        $csql->where('upid', $id);
        $downcnt = toint($db->get1($csql));
        if ($downcnt == 0) {
            $url = $post->get('url', '', 'all');
            $urldb = $url;
            $demo = $post->get('demo');
            if (!empty($demo)) {
                if ($demo[0] != ':' && $demo[0] != 'a' && $demo[0] != 'm' && $demo[0] != 'c')
                    return errjson('原型前缀错误，支持[:acm]');
                $urldb .= '~' . $demo;
            }
            $pow = $post->get('pow');
        }

        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['isuse'] = $isuse;
            $updata['upid'] = $upid;
            $updata['csort'] = $csort;
            $updata['url'] = $urldb;
            $updata['pow'] = $pow;
            $csql = new \ciy\sql('zc_menu');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_menu', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret = array();
        $ret['url'] = $url;
        $ret['demo'] = $demo;
        $ret['pow'] = $pow;
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p980d'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_menu');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                if ($delid >= 10) {
                    delcheck($db, $delid, 'zc_menu', 'upid', '子菜单');
                }
                delme($db, $delid, 'zc_menu');
                savelogdb($db, $rsuser['id'], 'zc_menu', $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_modifyupid() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p980u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $newupid = $post->getint('newupid');
        $csql = new \ciy\sql('zc_menu');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        if (!is_array($datarow))
            return errjson('数据不存在');
        try {
            $db->begin();
            $updata = array();
            $updata['url'] = '';
            $updata['pow'] = '';
            $csql = new \ciy\sql('zc_menu');
            $csql->where('id', $newupid);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新new失败:' . $db->error);
            $updata = array();
            $updata['upid'] = $newupid;
            $csql = new \ciy\sql('zc_menu');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新id失败:' . $db->error);
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_menu', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_multiadd() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p980u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $upid = $post->getint('upid');
        $multi = explode("\n", $post->get('multi'));

        $cnt = 0;
        $deepids = array();
        $deepids[0] = $upid;
        $lastdeep = 0;
        try {
            $db->begin();
            foreach ($multi as $m) {
                $m = trim($m);
                if (empty($m))
                    continue;
                $deep = 0;
                while (true) {
                    if (substr($m, 0, 2) != '--')
                        break;
                    $deep++;
                    $m = trim(substr($m, 2));
                }
                if (empty($m))
                    continue;
                $ms = explode('~', $m);
                $name = trim($ms[0]);
                if (empty($name))
                    continue;
                if ($lastdeep < $deep)
                    throw new \Exception('不能跳跃层级:' . $m);
                $updata = array();
                $updata['name'] = $name;
                if (count($ms) > 1)
                    $updata['url'] = trim($ms[1]);
                if (count($ms) > 2)
                    $updata['pow'] = trim($ms[2]);
                $updata['isuse'] = 1;
                $updata['upid'] = $deepids[$deep];
                $updata['csort'] = 10;
                $csql = new \ciy\sql('zc_menu');
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('操作数据库失败.' . $db->error);
                $updata['id'] = $db->insert_id();
                $deepids[$deep + 1] = $updata['id'];
                $lastdeep = $deep + 1;
                $cnt++;
                savelogdb($db, $rsuser['id'], 'zc_menu', null, $updata);
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        if ($cnt == 0)
            return errjson('没有任何新增');
        $updata = array();
        $updata['url'] = '';
        $updata['pow'] = '';
        $csql = new \ciy\sql('zc_menu');
        $csql->where('id', $upid);
        $db->update($csql, $updata);
        return succjson();
    }

    public static function json_seticon() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p980u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $svg = $post->get('svg', '', 'all');
        if (!empty($svg) && substr($svg, 0, 5) != '<svg ')
            return errjson('svg格式错误');
        $csql = new \ciy\sql('zc_icon');
        $csql->where('icontarget', 10);
        $csql->where('targetid', $id);
        $datarow = $db->getone($csql);
        if (empty($svg) && !is_array($datarow))
            return succjson();
        try {
            $db->begin();
            if (empty($svg)) {
                $csql = new \ciy\sql('zc_icon');
                $csql->where('id', $datarow['id']);
                $execute = $db->delete($csql);
                $msg = '删除图标';
            } else {
                $msg = '更新图标';
                $updata = array();
                $updata['icon'] = $svg;
                if (is_array($datarow)) {
                    $csql = new \ciy\sql('zc_icon');
                    $csql->where('id', $datarow['id']);
                    if ($db->update($csql, $updata) === false)
                        throw new \Exception('更新失败:' . $db->error);
                } else {
                    $updata['icontarget'] = 10;
                    $updata['targetid'] = $id;
                    $csql = new \ciy\sql('zc_icon');
                    if ($db->insert($csql, $updata) === false)
                        throw new \Exception('新增失败:' . $db->error);
                }
            }
            savelog($db, $rsuser['id'], 'zc_menu', "Upd=" . $id . "_|@|_icon=" . $msg);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        return succjson();
    }
}
