<?php

namespace web\admin\cemap;

class pathglb {
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $csql = new \ciy\sql('zc_cemap_pglb');
        $csql->order('csort desc,id');
        $rows = $db->get($csql);
        $ret = array('list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_cemap_pglb');
            $fshow = fieldadd($fshow, $field, -1, '_btn', '操作');
            $field['csort']['thwidth'] = '6em';
            $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;"'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }

    public static function json_update() {
        global $db;
        $rsuser = verifyfast();
        $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');
        $csql = new \ciy\sql('zc_cemap_pglb');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        if (!is_array($datarow))
            return errjson('数据不存在');

        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['upid'] = $upid;
            $updata['csort'] = $csort;
            $csql = new \ciy\sql('zc_cemap_pglb');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $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();
        $post = new \ciy\post();
        $csql = new \ciy\sql('zc_cemap_pglb');
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                delcheck($db, $delid, 'zc_cemap_glb', 'cpath', '资源');
                delme($db, $delid, 'zc_cemap_pglb');
                $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();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $newupid = $post->getint('newupid');
        $csql = new \ciy\sql('zc_cemap_pglb');
        $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('zc_cemap_pglb');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('操作数据库失败:' . $db->error);
            $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();
        $post = new \ciy\post();
        $upid = $post->getint('upid');
        $multi = explode("\n", $post->get('multi'));

        $cnt = 0;
        try {
            $db->begin();
            foreach ($multi as $m) {
                $m = trim($m);
                if (empty($m))
                    continue;
                $updata = array();
                $updata['name'] = $m;
                $updata['upid'] = $upid;
                $updata['csort'] = 10;
                $csql = new \ciy\sql('zc_cemap_pglb');
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $cnt++;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        if ($cnt == 0)
            return errjson('没有任何新增');
        return succjson();
    }
}
