<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\controller;
use admin\model\homemenu AS model;
use \temmoku\admin;
use \temmoku\db;
use admin\public_class\cache;
class homemenu extends admin{
	
	public function index(){
		if('post'===$_POST['step']){
			if(!$_POST['order']){
				$this->err('请先添加一个导航菜单');
			}
			foreach($_POST['order'] as $key=>$v){
				$arr=[
					'reorder'=>$v,
					'color'=>$_POST['color'][$key]
				];
				db::update(jab.'home_menu')->cols($arr)->where("id='$key'")->query();
			}
			$data=(new model)->all();
			cache::home_menu($data);
			$this->Success('操作成功');
		}
		$rows=db::select('*')->from(jab.'home_menu')->where("sup= '0'")->orderByDESC(['reorder'])->query();
		$data=[];
		foreach($rows AS $row){
			$_rows=db::select('*')->from(jab.'home_menu')->where("sup= '$row[id]'")->orderByDESC(['reorder'])->query();
			if($_rows){
				$row['list']=$_rows;
			}
			$data[]=$row;
		}
		$this->assign('data',$data);
		$this->display(tpl('homemenu/index'));
	}
	public function add(){
		if('post'===$_POST['step']){
			if(!$_POST['name']){
				$this->err('菜单名称必须填写');
			}
			$sup=intval($_POST['sup']);
			$target=intval($_POST['target']);
			$state=intval($_POST['state']);
			
			$arr=[
				'sup'	=>	$sup,
				'name'		=>$_POST['name'],
				'link'		=>$_POST['link'],
				'color'		=>$_POST['color'],
				'target'	=>$target,
				'state'		=>$state,
				'reorder'	=>(int)$_POST['reorder'],
			];
			db::insert(jab.'home_menu')->cols($arr)->query();
			$data=(new model)->all();
			cache::home_menu($data);
			$this->success('菜单添加成功');
		}
		$rows=db::select('*')->from(jab.'home_menu')->where("sup= '0'")->query();
		$this->assign('rows',$rows);
		$this->display(tpl('homemenu/add'));
	}
	public function state(){
		db::update(jab.'home_menu')->cols(['state'=>$_POST['state']])->where("id='$_POST[id]'")->query();
		$this->success('操作成功');
	}
	public function edit(){
		if('post'===$_POST['step']){
			if(!$_POST['name']){
				$this->err('菜单名称必须填写');
			}
			$sup=intval($_POST['sup']);
			if($_POST['id']===$_POST['sup']){
				$this->err('不可成为自己的下级');
			}
			$target=intval($_POST['target']);
			$state=intval($_POST['state']);
			$arr=[
				'sup'	=>	$sup,
				'name'		=>$_POST['name'],
				'link'		=>$_POST['link'],
				'color'		=>$_POST['color'],
				'target'	=>$target,
				'state'		=>$state,
				'reorder'	=>(int)$_POST['reorder'],
			];
			db::update(jab.'home_menu')->cols($arr)->where("id='$_POST[id]'")->query();
			$data=(new model)->all();
			cache::Home_Menu($data);
			$this->Success('菜单修改成功');
		}
		$rows=db::select('*')->from(jab.'home_menu')->where("sup= '0'")->query();
		$edit=db::select('*')->from(jab.'home_menu')->where("id= '$_GET[id]' ")->row();
		$this->assign('rows',$rows);
		$this->assign('edit',$edit);
		$this->display(tpl('homemenu/add'));
	}
	public function del(){
		$row=db::select('*')->from(jab.'home_menu')->where("id= '$_GET[id]' ")->row();
		if(!$row){
			$this->err('不存在的数据');
		}
		$_row=db::select('*')->from(jab.'home_menu')->where("sup= '$_GET[id]' ")->row();
		if($_row){
			$this->err('必须删除本菜单下的链接才可以删除本链接');
		}
		db::delete(jab.'home_menu')->where("id= '$_GET[id]' ")->query();
		
		cache::home_menu($data);
		$this->success('删除成功');
	}
}
?>
