<?php

namespace App\Http\Controllers\Admin;

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

class PrivController extends Controller {

	function addTo(Request $req) {

		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setLang('priv_code');
		$dt->setdataSource('/admin/priv/dataSource?role_id=' . $req->role_id . '&priv_code=' . $req->priv_code);
		$dt->addColsFields('priv_name');
		$dt->addColsFields('priv_code');
		$dt->addColsFields('remark');
		$dt->addColsFields('create_at');
		$dt->addColsFields('update_at');
		$dt->enableCheckBox();
		return view('admin.Priv/addTo', $dt->render());

	}

	public function insert(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]);
				}
			}
		}
        //获取权限详情
        $code_data=\App\Models\Priv_codeModel::whereIn('id',$ids)->get();
        $code_data=$code_data->toArray();
        //获取角色信息
        $role_info=\App\Models\System_root::where('id',$req->role_id)->first();
        $role_info=$role_info->toArray();
        //组装权限角色关联数组
        $data=array();
        foreach ($code_data as $k=>$v){
            $data[$k]['role_id']=$req->role_id;
            $data[$k]['priv_id']=$v['id'];
            $data[$k]['role_name']=$role_info['name'];
//            $data[$k]['priv_code']=$v['priv_code'];
            $data[$k]['create_at']=date('Y-m-d H:i:s');
        }
        $rows = \App\Models\Priv_roleModel::insert($data);
		return responseToJson(1, trans('priv_role.insert_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->role_name ? $req->role_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->priv_id ? $req->priv_id : '';
		if (!empty($remark)) {
			$model = $model->where('remark','like', '%' . $remark . '%');
		}
		
		$ids = \App\Models\Priv_roleModel::where('role_id', $req->role_id)->select('priv_id')->get();
		if ($ids) {
			$ids = $ids->toArray();
			if (is_array($ids) && count($ids)) {
				$ids2 = array();
				foreach ($ids as $v) {
					$ids2[] = $v['priv_id'];
				}
				$model = $model->whereNotIn('id', $ids2);
			}

		}

		$data = $model->get();

		if (!$data) {
			return;
		}
		$data = $data->toArray();
		$count = $model->count();

		return \App\Lib\DataTable\DataTable::init()->toJson($data, $count);
	}
}