<?php

namespace App\Http\Controllers\Admin;

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


class GradeController extends Controller
{
   //添加
   function add(Request $req){
        if(!$req->isMethod('post')){
            return view('admin.Usergrade/add');
        }else{           
            $model=new \App\Models\User_grade();
            $time=time();
            $model->time=date('Y-m-d H:i:s', $time);
            $model->name=trim($req->input('name'));
            $model->grade=trim($req->input('grade'));
            $model->rule=trim($req->input('rule'));
            $time_type = trim($req->input('time_type'));
            $rechargemoney = trim($req->input('rechargemoney'));
            $bettingmoney = trim($req->input('bettingmoney'));
            $day = trim($req->input('day'));
            if($time_type =='day' || $time_type =='ymd'){
                $model->rule_details=$time_type.','.$rechargemoney.','.$bettingmoney.','.$day;
            }else{
                $model->rule_details=$time_type.','.$rechargemoney.','.$bettingmoney;
            }
            $model->save();
            $log = array(
                session('adminInfo.admin_name'),
                $req->input('name'),
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'add_grade', $log);
            return responseToJson($res);
        }        
    }
	//查看
    function view(Request $req){
       return $this->edit($req);
    }
    //修改
    function edit(Request $req){
        $id=$req->id;
        if(intval($id)<1){
            return responseToJson(-6000001);
        }
        if(!$req->isMethod('post')){
           
            $data=\App\Models\User_grade::where('id',$id)->first();
            if(!$data){
               return responseToJson(-6000002);
            }
            $data=$data->toArray();
            $data['rule_details'] =explode(',',$data['rule_details']);
            return view('admin.Usergrade/add',array('admin'=>$data));
        }else{
            $model=\App\Models\User_grade::where('id',$id)->first();
            $model->name=trim($req->input('name'));
			$model->grade=trim($req->input('grade'));
            $model->rule=trim($req->input('rule'));
            $time_type = trim($req->input('time_type'));
            $rechargemoney = trim($req->input('rechargemoney'));
            $bettingmoney = trim($req->input('bettingmoney'));
            $day = trim($req->input('day'));
            if($time_type =='day' || $time_type =='ymd'){
                $model->rule_details=$time_type.','.$rechargemoney.','.$bettingmoney.','.$day;
            }else{
                $model->rule_details=$time_type.','.$rechargemoney.','.$bettingmoney;
            }
            $model->save();
            $log = array(
                session('adminInfo.admin_name'),
                $req->input('name'),
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'edit_grade', $log);
            return responseToJson($res);
        }
    }
    //删除
    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]);
                }
            }
        }
        //获取待删除等级信息
        $gradeInfo = \App\Models\User_grade::whereIn('id',$ids)->get();
        if(empty($gradeInfo)){
            return responseToJson(-2002);//id错误
        }
        $gradeInfo = $gradeInfo->toArray();
        $grades = array();
        foreach ($gradeInfo as $key => $value) {
            if($value['grade'] == 0){
                return responseToJson(-2000100102);//id错误
            }
            $grades[] = $value['grade'];
        }

        $rows=\App\Models\User_grade::whereIn('id',$ids)->delete();
        if(!$rows){
             return responseToJson(-2003);//id错误
        }
        $updateGrade = \App\Models\Account_detailed::whereIn('grade',$grades)->update(['grade'=>'0']);
        $log = array(
            session('adminInfo.admin_name')
        );
        $res = OperationLog(session('adminInfo.admin_id'), 'del_grade', $log);
        return responseToJson(1);

    }


    public function index(){
       $dt=\App\Lib\DataTable\DataTable::init();
       $dt->setLang('grade');
       $dt->enableCheckBox();
	   $dt->addColsFields('id');
	   $dt->addColsFields('name');
       $dt->addColsFields('grade');
       //$dt->addColsFields('is_water');
       $dt->addColsFields('time');
       $dt->setToolBar(array('edit'));
       return  view('admin.Usergrade/index',$dt->render());
    }

    public function dataSource(Request $req){
        $name = isset($req->name) ? trim($req->name) : null;
        $limit = isset($req->limit) ? trim($req->limit) : null;
        $page = isset($req->page) ? trim($req->page) : null;
        $grade = isset($req->grade) ? trim($req->grade) : null;
        //$is_water = isset($req->is_water) ? trim($req->is_water) : null;
        $regist_endtime = isset($req->create_at_start) ? trim($req->create_at_start) : null;
        $regist_startime = isset($req->create_at_end) ? trim($req->create_at_end) : null;


//        var_dump($name);

        $model = \App\Models\User_grade::offset(($page - 1) * $limit)
            ->limit($limit);
        
        if (!empty($name)) {
            $model = $model->where('name', 'like', '%' . $name . '%');
        }
        /*if (!empty($is_water)) {
            $model = $model->where('is_water', '=', $is_water);
        }*/
        if (!empty($grade)) {
            if ($grade == -1) {
                $model = $model->where('grade', '=', '0');
            } else {
                $model = $model->where('grade', '=', $grade);
            }
        }
        if (!empty($regist_endtime)) {
            $regist_endtime = date('Y-m-d H:i:s', strtotime($regist_endtime));
            $model =$model->where('time', '<=', $regist_endtime);
        }
        if (!empty($regist_startime)) {
            $regist_startime = date('Y-m-d H:i:s', strtotime($regist_startime));
            $model =$model->where('time', '>=', $regist_startime);
        }
        $model=$model->orderBy('id', 'desc');
        $data = $model->get();
        if (!$data) {
            return;
        }
        $data = $data->toArray();
        if (!($data < 0)) {
            $langinfo = trans('grade.manager');
            foreach ($data as $k => $v) {
                $data[$k]['is_water'] = $langinfo[$v['is_water']];
            }
        }


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

    }
}


