<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class MenuController extends Controller {
	//
	public function top() {
		$menu = \App\Models\NavsModel::getTopMenu();
		$menus = array('data' => array());
		if ($menu > 0) {
			$menus['data'] = $menu;
		}
		return json_encode($menus);
	}
	public function left() {
		$menu = \App\Models\NavsModel::getLeftMenu();
		$menus = array('data' => array());
		if ($menu > 0) {
			$menus['data'] = $menu;
		}
		return json_encode($menus);
	}
	public function quick() {
		$menu = \App\Models\NavsModel::getQuickMenu();
		$menus = array('data' => array());
		if ($menu > 0) {
			$menus['data'] = $menu;
		}
		return json_encode($menus);
	}

	function getParent() {
		$data = \App\Models\NavsModel::where('parent_id', '0')->get();
		if (!$data) {
			return;
		}
		return $data->toArray();
	}
	function add(Request $req) {
		if (!$req->isMethod('post')) {
			$data = $this->getParent();
			$lange = trans('menu');
			foreach ($data as $k => $v) {
				$arr = trim($data[$k]['name']);
				if (isset($lange[$arr])) {
					$data[$k]['name']=$lange[$arr];
				}
			}
			return view('vip.menu_form', array('parents' => $data));
		} else {
			$model = new \App\Models\NavsModel();
			$model->name = $req->input('menu_name');
			$model->href = $req->input('menu_url');
			$model->icon = $req->input('menu_icon');
			$model->sort = $req->input('menu_sort');
			$model->parent_id = $req->input('parent_id');
			$model->target = $req->input('target');

			$model->type = $req->input('type');
			$model->save();
			return responseToJson(1);
		}

	}
	function view(Request $req) {
		return $this->edit($req);
	}
	function edit(Request $req) {
		$id = $req->id;
		if (intval($id) < 1) {
			return -1;
		}
		if (!$req->isMethod('post')) {

			$data = \App\Models\NavsModel::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();
			$data['parents'] = $this->getParent();


			$lange = trans('menu');
			foreach ($data['parents'] as $k => $v) {
				$arr = trim($data['parents'][$k]['name']);
				if (isset($lange[$arr])) {
					$data['parents'][$k]['name']=$lange[$arr];
				}
			}

			return view('vip.menu_form', $data);
		} else {
			$model = \App\Models\NavsModel::where('id', $id)->first();
			$model->name = $req->input('menu_name');
			$model->href = $req->input('menu_url');
			$model->icon = $req->input('menu_icon');
			$model->sort = $req->input('menu_sort');
			$model->type = $req->input('type');
			$model->parent_id = $req->input('parent_id');
			$model->target = $req->input('target');
			$model->save();
			return responseToJson(1);
		}

	}
	public function delete(Request $req) {
		$id = $req->id;
		if (empty($id)) {
			return responseToJson(-2001); //id����Ϊ��
		}
		$ids = explode(',', $id);
		if (!is_array($ids) && intval($ids) < 0) {
			return responseToJson(-2002); //id����
		}
		if (is_array($ids) && count($ids) > 0) {
			foreach ($ids as $k => $v) {
				if (intval($v) < 1) {
					unset($ids[$k]);
				}
			}
		}

		$rows = \App\Models\NavsModel::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1, trans('menu.delete_success')); //id����

	}

	public function dataSource(Request $req) {
		$limit = intval($req->limit) > 0 ? $req->limit : 10;
		$page = intval($req->page) > 0 ? $req->page : 1;
		$field = empty($req->field) ? $req->field : '';
		$order = empty($req->order) ? $req->order : '';

		$name = $req->name ? $req->name : '';
		$url = $req->href ? $req->href : '';
		$type = $req->type ? $req->type : '';
		$target = $req->target ? $req->target : '';
		$model = \App\Models\NavsModel::offset(($page - 1) * $limit)
			->limit($limit);
		if (!empty($field) && !empty($order)) {
			$model = $model->orderBy($field, $order);
		}
		if (!empty($name)) {
			$model = $model->where('name', 'like', '%' . $name . '%');
		}
		if (!empty($url)) {
			$model = $model->where('href', 'like', '%' . $url . '%');
		}
		if (!empty($type)) {
			if ($type == -1) {
				$model = $model->where('type', '=', '0');
			} else {
				$model = $model->where('type', '=', $type);
			}
		}
		if (!empty($target)) {
			if ($target == -1) {
				$model = $model->where('target', '=', '0');
			} else {
				$model = $model->where('target', '=', $target);
			}
		}
		$data = $model->get();
		if (!$data) {
			return;
		}
		$data = $data->toArray();
		$parent = $this->getParent();
		$parentgroup = array();
		if (count($parent) > 0) {
			foreach ($parent as $v) {
				$parentgroup[$v['id']] = $v['name'];
			}
		}
		if (!empty($data) && is_array($data)) {
			$langinfo = trans('status.dc_navs');
			$menu = trans('menu');
			foreach ($data as $k => $v) {
				$data[$k]['name'] = isset($menu[trim($v['name'])]) ? $menu[trim($v['name'])] : trim($v['name']);
                $pname = ($v['parent_id'] == 0 || !isset($parentgroup[$v['parent_id']])) ? 'noparent' : $parentgroup[$v['parent_id']];
                $data[$k]['parent_name'] = isset($menu[trim($pname)])?$menu[trim($pname)]:trim($pname);
				$data[$k]['type'] = ($v['type'] == 0) ? $langinfo['type']['-1'] : $langinfo['type'][$v['type']];
				$data[$k]['target'] = ($v['target'] == 0) ? $langinfo['target']['-1'] : $langinfo['target'][$v['target']];
			}
		}
		$count = \App\Models\NavsModel::count();
		return \App\Lib\DataTable\DataTable::init()->toJson($data, $count);
	}

	public function index(Request $req) {
		$request['name'] = isset($req->name) ? trim($req->name) : null;
		$request['href'] = isset($req->href) ? trim($req->href) : null;
		$request['type'] = isset($req->type) ? trim($req->type) : null;
		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setLang('menu');
		$dt->addColsFields('id', array('sort' => false, 'width' => 100));
		$dt->addColsFields('name', array('sort' => false, 'width' => 250, 'templet' => '#menudetail'));
		//    $dt->addColsFields('icon');
		$dt->addColsFields('href', array('sort' => false));
		$dt->addColsFields('type', array('sort' => false, 'width' => 150));
		$dt->addColsFields('sort', array('width' => 85));
		$dt->addColsFields('parent_id', array('sort' => false, 'width' => 100));
		$dt->addColsFields('parent_name', array('sort' => false, 'width' => 150));
		$dt->addColsFields('target', array('sort' => false, 'width' => 150));
		$dt->enableCheckBox();
		$arr[] = 'view';
		if (checkRriv('/admin/Menu/edit')) {
			$arr[] = 'edit';
		}
		$dt->setToolBar($arr, array('width' => 200));
		return view('vip.system_menu', $dt->render($request));
	}
	function getEditColsModel() {
		return 'NavsModel';
	}

}
