<?php

namespace App\Http\Controllers\Admin;

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

class Priv_codeController extends Controller {

	function add(Request $req) {
		if (!$req->isMethod('post')) {
			return view('admin.Priv_code/add');
		} else {
			$model = new \App\Models\Priv_codeModel();

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

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

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

			$model->create_at = date('Y-m-d H:i:s');
			$model->save();
			return responseToJson(1);
		}
	}
	function getEditColsModel() {
		return 'Priv_codeModel';
	}
	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_codeModel::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();
			return view('admin.Priv_code/view', array('priv_code' => $data));
		} else {
			$model = \App\Models\Priv_codeModel::where('id', $id)->first();

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

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

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

			$model->update_at = date('Y-m-d H:i:s');

			$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_codeModel::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id错误
		}
		return responseToJson(1, trans('priv_code.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';

		$model = \App\Models\Priv_codeModel::offset(($page - 1) * $limit)
			->limit($limit);
		if (!empty($field) && !empty($order)) {
			$model = $model->orderBy($field, $order);
		}

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

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

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

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

		$data = $model->get();
		if (!$data) {
			return;
		}
		$data = $data->toArray();
		$count = \App\Models\Priv_codeModel::count();
		return \App\Lib\DataTable\DataTable::init()->toJson($data, $count);
	}

	public function index(Request $req) {
		$request=array();
		$request['priv_name']=isset($req->priv_name)?trim($req->priv_name):null;
		$request['priv_code'] = isset($req->priv_code) ? trim($req->priv_code) : null;
		$request['remark'] = isset($req->remark) ? trim($req->remark) : null;


		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setLang('priv_code');
		$dt->addColsFields('id', array('width' => 80));
		$dt->addColsFields('priv_name', array('sort'=>false));
		$dt->addColsFields('priv_code', array('sort'=>false));
		$dt->addColsFields('remark', array('sort'=>false));
		$dt->addColsFields('create_at');
		$dt->addColsFields('update_at');
		$dt->enableCheckBox();
		$arr=array();
		$arr[]='view';
		if (checkRriv('/admin/Priv_code/edit')) {
			$arr[]= 'edit';
		}
		$dt->setToolBar($arr, array('width' => 140));
		return view('admin.Priv_code/index', $dt->render($request));
	}
}
