<?php

namespace web\admin\demo;

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

        // if (nopower($db, $rsuser['id'], 'p501u'))
        //     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');
        $leaderuser = $post->getint('leaderuser');
        $csql = new \ciy\sql('zc_depart');
        $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['upid'] = $upid;
            $updata['csort'] = $csort;
            $updata['leaderuser'] = $leaderuser;
            $csql = new \ciy\sql('zc_depart');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_depart', $datarow, $updata);
            $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();

        // if (nopower($db, $rsuser['id'], 'p501d'))
        //     return errjson('您未被授权操作');
        $post = new \ciy\post();
        $csql = new \ciy\sql('zc_depart');
        $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_admin', 'departid', '人员');
                delcheck($db, $delid, 'zc_depart', 'upid', '下级');
                delme($db, $delid, 'zc_depart');
                savelogdb($db, $rsuser['id'], 'zc_depart', $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'], 'p501u'))
        //     return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $newupid = $post->getint('newupid');
        $csql = new \ciy\sql('zc_depart');
        $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_depart');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('操作数据库失败:' . $db->error);
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_depart', $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'], 'p501u'))
        //     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;
                if ($lastdeep < $deep)
                    throw new \Exception('不能跳跃层级:' . $m);
                $updata = array();
                $updata['name'] = $m;
                $updata['isuse'] = 1;
                $updata['upid'] = $deepids[$deep];
                $updata['csort'] = 10;
                $csql = new \ciy\sql('zc_depart');
                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_depart', null, $updata);
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        if ($cnt == 0)
            return errjson('没有任何新增');
        return succjson();
    }
}
