<?php
/*
 *	www.myucms.com
 *  作者：梦雨
 *  @ QQ50361804
*/
namespace app\admin\controller;
use think\Db;
use think\Controller;
use app\admin\model\Category as CategoryModel;
class Category extends Common
{
    protected $beforeActionList = ['delsoncate' => ['only' => 'dels']];
    public function index()
    {
        $category = new CategoryModel();
        $gzc = $category->catetree();
        foreach ($gzc as $k => $v) {
            $gzc[$k]['bauzhu'] = '';
            $gzc[$k]['uids'] = '';
            if ($v['tid'] > 0) {
                
                $uids = Db::name('banzhu')->where(array('cid' => $v['id']))->value('uids');
                if ($uids != '') {
                    $map['userid']        = ['in', $uids];
                    $username         = Db::name('member')->where($map)->column('username');
                    $gzc[$k]['uids'] = $uids;
                    $gzc[$k]['bauzhu'] = join(',', $username);
                }
            }
        }
        //超级版主
        $superbanzhu=Db::name('banzhu')->where(array( 'cid' => 0))->value('uids');
        $this->assign('superbanzhu', $superbanzhu);
        $this->assign('gzc', $gzc);
        return view();
    }
    public function add()
    {
        $category = new CategoryModel();
        if (request()->isPost()) {
            $data = input('post.');
            $data['time'] = time();
            if ($category->add($data)) {
                return json(array('code' => 200, 'msg' => '添加成功'));
            } else {
                return json(array('code' => 0, 'msg' => '添加失败'));
            }
        }
        $gzc = $category->catetree();
        $this->assign('gzc', $gzc);
        return view();
    }
    public function edit()
    {
        $category = new CategoryModel();
        if (request()->isPost()) {
            $data = input('post.');
            if ($category->edit($data)) {
                return json(array('code' => 200, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 0, 'msg' => '修改失败'));
            }
        }
        $gzc = $category->find(input('id'));
        $gzcs = $category->catetree();
        $this->assign(array('gzcs' => $gzcs, 'gzc' => $gzc));
        return view();
    }
    public function dels()
    {
        $dels = db('category')->delete(input('id'));
        
        $my = Db::name('forum')->where('tid',input('id'))->count();
        if ($my>0) {
        	 return json(array('code' => 0, 'msg' => '该板块存在主题'));
        } else {	
        if ($dels) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
        }
        
        
    }
    public function delsoncate()
    {
        $cateid = input('id');
        $category = new CategoryModel();
        $sonids = $category->getchilrenid($cateid);
        if ($sonids) {
            db('category')->delete($sonids);
        }
    }
    public function changeshow()
    {
        if (request()->isAjax()) {
            $change = input('change');
            $show = db('category')->field('show')->where('id', $change)->find();
            $show = $show['show'];
            if ($show == 1) {
                db('category')->where('id', $change)->update(['show' => 0]);
                echo 1;
            } else {
                db('category')->where('id', $change)->update(['show' => 1]);
                echo 2;
            }
        } else {
            $this->error('非法操作');
        }
    }
    public function changesidebar()
    {
        if (request()->isAjax()) {
            $change = input('change');
            $sidebar = db('category')->field('sidebar')->where('id', $change)->find();
            $sidebar = $sidebar['sidebar'];
            if ($sidebar == 1) {
                db('category')->where('id', $change)->update(['sidebar' => 0]);
                echo 1;
            } else {
                db('category')->where('id', $change)->update(['sidebar' => 1]);
                echo 2;
            }
        } else {
            $this->error('非法操作');
        }
    }
    /* 设置版主 */
    public function setbanzhu()
    {
        $data = $this->request->param();
        $cid  = $data['cid'];
        $flag=false;
        $res = Db::name('banzhu')->where(array('cid' => $cid))->find();
        if ($res) {
            $flag=Db::name('banzhu')->where('id', $res['id'])->setField($data);
        } else {
            $flag=Db::name('banzhu')->insert($data);
        }
        if($flag){
            return json(array('code' => 200, 'msg' => '设置成功'));
        } else {
            return json(array('code' => 0, 'msg' => '设置失败'));
        }

    }
    
}