<?php

namespace web\docs;

class manage {
    static function verify($pant) {
        $cfg = webini('mock');
        if($pant == $cfg['docpass'])
            return true;
        return false;
    }
    public static function json_init() {
        global $db;
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('doc_help');
        $csql->order('csort desc,id');
        $rows = $db->get($csql);

        $ret = array('list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'doc_help');
            $fshow = fieldadd($fshow, $field, -1, '_btn', '操作');
            $field['csort']['thwidth'] = '5em';
            $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;"'
            );
            $ret['once']['input'] = $input;
        }
        return succjson($ret);
    }

    public static function json_update() {
        global $db;
        $post = new \ciy\post();
        if (!self::verify($post->get('pant')))
            return errjson('您未被授权操作');
        $updata = array();
        $id = $post->getint('id');
        if($id < 10)
            return errjson('不能操作');
        $name = $post->get('name');
        if ($name == '')
            return errjson('请填写标题');
        $csort = $post->getint('csort');
        $isuse = $post->getint('isuse');
        $csql = new \ciy\sql('doc_help');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        if (!is_array($datarow))
            return errjson('数据不存在');

        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['isuse'] = $isuse;
            $updata['csort'] = $csort;
            $csql = new \ciy\sql('doc_help');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();
        return errjson('功能暂未开放');

        if (nopower($db, $rsuser['id'], 'p903d'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('doc_help');
        $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, 'doc_help', 'upid', '子菜单');
                }
                delme($db, $delid, 'doc_help');
                savelogdb($db, $rsuser['id'], 'doc_help', $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;
        $post = new \ciy\post();
        if (!self::verify($post->get('pant')))
            return errjson('您未被授权操作');
        $id = $post->getint('id');
        if($id < 10)
            return errjson('不能操作');
        $newupid = $post->getint('newupid');
        $csql = new \ciy\sql('doc_help');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        if (!is_array($datarow))
            return errjson('数据不存在');
        try {
            $db->begin();
            $updata = array();
            $updata['upid'] = $newupid;
            $csql = new \ciy\sql('doc_help');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新id失败:' . $db->error);
            $updata['id'] = $id;
            $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;
        $post = new \ciy\post();
        if (!self::verify($post->get('pant')))
            return errjson('您未被授权操作');
        $upid = $post->getint('upid');
        $multi = explode("\n", $post->get('multi'));

        $cnt = 0;
        try {
            $db->begin();
            foreach ($multi as $m) {
                if (empty(trim($m)))
                    continue;
                $ms = explode('~', $m);
                $name = trim($ms[0]);
                if (empty($name))
                    continue;
                $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'] = $upid;
                $updata['csort'] = 10;
                $updata['uptimes'] = 0;
                $csql = new \ciy\sql('doc_help');
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('操作数据库失败.' . $db->error);
                $updata['id'] = $db->insert_id();
                $cnt++;
            }
            $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('doc_help');
        $csql->where('id', $upid);
        $db->update($csql, $updata);
        return succjson();
    }
    public static function json_uptxt() {
        global $db;
        $post = new \ciy\post();
        if (!self::verify($post->get('pant')))
            return errjson('您未被授权操作');
        $updata = array();
        $id = $post->getint('id');
        $content = $post->get('content','','all');
        $csql = new \ciy\sql('doc_help');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        if (!is_array($datarow))
            return errjson('数据不存在');
        $uptimes = time();
        try {
            $db->begin();
            $updata = array();
            $updata['uptimes'] = $uptimes;
            $csql = new \ciy\sql('doc_help');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        file_put_contents(PATH_WEB . 'ud/docs/' . $id . '_' . $uptimes . '.txt', $content);
        $ret['uptimes'] = $uptimes;
        return succjson($ret);
    }
}
