<?php

namespace App\Http\Controllers\Admin;

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

class Priv_roleController extends Controller {

	function add(Request $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$model = new \App\Models\Priv_roleModel();
		$data = $model->getRole($id);
		if (!$req->isMethod('post')) {
			return view('admin.Priv_role/add', ['priv_role' => $data]);
		} else {
			$model->role_id = trim($req->input('role_id'));

			$model->priv_code = trim($req->input('priv_code'));

			$model->priv_id = trim($req->input('priv_id'));

			$model->role_name = trim($req->input('role_name'));

			$model->create_at = date('Y-m-d H:i:s');
			$model->save();
			return responseToJson(1);
		}
	}

	function addTo(Request $req) {

		return view('admin.Priv_role/addTo');
	}

	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\Priv_roleModel::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();
			return view('admin.Priv_role/view', array('priv_role' => $data));
		} else {
			$model = \App\Models\Priv_roleModel::where('id', $id)->first();

			$model->role_id = trim($req->input('role_id'));

			$model->priv_code = trim($req->input('priv_code'));

			$model->priv_id = trim($req->input('priv_id'));

			$model->role_name = trim($req->input('role_name'));

			$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\Priv_roleModel::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id错误
		}
		return responseToJson(1, trans('priv_role.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 : 'asc';
		$id = intval($req->id) > 0 ? $req->id : '';
		DB::connection()->enableQueryLog();

		$model = \App\Models\Priv_roleModel::select('dc_priv_role.id','role_id','role_name','priv.priv_code','priv_id','priv.priv_name','dc_priv_role.create_at')->join('dc_priv_code as priv','priv.id','dc_priv_role.priv_id')->where('role_id', $id);
//		->offset(($page - 1) * $limit)
//			->limit($limit);
		if (!empty($field) && !empty($order)) {
			$model = $model->orderBy($field, $order);
		}

		$role_name = $req->role_name ? $req->role_name : '';
		if (!empty($role_name)) {
			$model = $model->where('role_name','like', '%' . $role_name . '%');
		}

		$priv_code = $req->priv_code ? $req->priv_code : '';
		if (!empty($priv_code)) {
			$model = $model->where('priv.priv_code','like', "%" . $priv_code . "%");
		}

		$priv_name = $req->priv_name ? $req->priv_name : '';
		if (!empty($priv_name)) {
			$model = $model->where('priv_name','like', "%" . $priv_name . "%");
		}

		/**
		$name=$req->name?$req->name:'';
		if(!empty($name)){
		$model=$model->where('name','like','%'.$name.'%');
		}
		 **/

		$data = $model->paginate($limit);

		if (!$data) {
			return;
		}
		$data = $data->toArray();
//		$count = $model->count();
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	public function index(Request $req) {
		$id = $req->id;
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setLang('priv_role');
		$dt->setdataSource('/admin/priv_role/dataSource?id=' . $id);
		$dt->addColsFields('id', array('width' => 120));
		$dt->addColsFields('priv_name', array('sort' => false));
		// $dt->addColsFields('priv_code');
		$dt->addColsFields('priv_code', array('sort' => false));
		$dt->addColsFields('role_name', array('sort' => false));
		$dt->addColsFields('create_at', array('width' => 240));
		$dt->enableCheckBox();

		$dt->setToolBar();
		return view('admin.Priv_role/index', $dt->render());
	}

}