<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class RoutesCategoryController extends Controller {
	/**
	 * @return 管理员日志管理
	 */
	function index(Request $req) {
		$request=array();
		$request['name'] = isset($req->name) ? trim($req->name) : null;
		$request['rdesc'] = isset($req->rdesc) ? trim($req->rdesc) : null;
		$request['remark'] = isset($req->remark) ? trim($req->remark) : null;
        $request['sort'] = isset($req->sort) ? trim($req->sort) :null;
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/RoutesCategory/data');
		$dt->setLang('routes');
		$dt->addColsFields('id');
		$dt->addColsFields('name');
		$dt->addColsFields('rdesc');
		$dt->addColsFields('remark');
		$dt->addColsFields('sort');
		$dt->setToolBar(array('edit'),array('sort' => false));
		$dt->enableCheckBox();
		return view('admin.RoutesCategory/index', $dt->render($request));
	}

	function data() {
		$db = new \App\Models\RoutesCategory();
		$data =  $db->getCode();
		return \App\Lib\DataTable\DataTable::init()->toJson($data);
	}
	function add(Request $req) {
		$model = new \App\Models\RoutesCategory();
		if (!$req->isMethod('post')) {
			return view('admin.RoutesCategory/add', array('data' => $model));
		} else {
			$model = new \App\Models\RoutesCategory();
			$model->name = trim($req->input('name'));
			$model->rdesc = trim($req->input('rdesc'));
			$model->remark = trim($req->input('remark'));
			$model->sort = trim($req->input('sort'));
			$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\RoutesCategory::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id错误
		}
		return responseToJson(1); //id错误
	}

	function edit(Request $req) {
		$id = $req->id;
		if (intval($id) < 1) {
			return -1;
		}
		if (!$req->isMethod('post')) {
			$data = \App\Models\RoutesCategory::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();
			return view('admin.RoutesCategory/add', array('data' => $data));
		} else {
			$model = \App\Models\RoutesCategory::where('id', $id)->first();
			$model->name = trim($req->input('name'));
			$model->rdesc = trim($req->input('rdesc'));
			$model->remark = trim($req->input('remark'));
			$model->sort = trim($req->input('sort'));
			$model->save();
			return responseToJson(1);
		}

	}

	

}