<?php

namespace App\Http\Controllers\Admin;

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

/****
 * Class LabelController 用户分组标签
 * @package App\Http\Controllers\Admin
 */
class LabelController extends Controller
{
    //首页
    public function index(){
        $dt=\App\Lib\DataTable\DataTable::init();
        $dt->setLang('label');
        $dt->enableCheckBox();
        $dt->addColsFields('id');
        $dt->addColsFields('group_name');
        $dt->addColsFields('group_code');
        $dt->addColsFields('sort');
        $dt->addColsFields('create_at');
//        $dt->setToolBar(array('edit'));
        return  view('admin.Label/index',$dt->render());
    }
    //数据源
    public function dataSource(Request $req){
        $group_name = isset($req->group_name) ? trim($req->group_name) : null;
        $group_code = isset($req->group_code) ? trim($req->group_code) : null;
        $limit = isset($req->limit) ? trim($req->limit) : 20;
        $page = isset($req->page) ? trim($req->page) : 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;
        $where=array();
        if (!empty($group_name)) {
            $where[] = array('group_name', 'like', '%' . $group_name . '%');
        }
        if (!empty($group_code)) {
            $where[] = array('group_code', '=',$group_code );
        }

//        var_dump($name);

        $data = \App\Models\Account_group::getList($where,$limit);

        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);

    }
   //添加
   function add(Request $req){
        if(!$req->isMethod('post')){
            return view('admin.Label/add');
        }else{
            $group_name=$req->group_name?trim($req->group_name):'';
            $group_code=$req->group_code?trim($req->group_code):'';
            $sort=$req->sort?intval($req->sort):1;
            if(empty($group_code)){
                return responseToJson(-240005);
            }
            if(empty($group_name)){
                return responseToJson(-240006);
            }
            $data=array(
                'group_name'=>$group_name,
                'group_code'=>$group_code,
                'sort'=>$sort,
            );
            $model=\App\Models\Account_group::addGroup($data);
            $log = array(
               session('adminInfo.admin_name'),
               $group_name,
               $group_code,
               $sort,
            );
            OperationLog(session('adminInfo.admin_id'), 'addLabel', $log);
            return responseToJson($model);
        }        
    }
	//查看
    function view(Request $req){
       return $this->edit($req);
    }

    //删除
    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]);
                }
            }
        }
        if(in_array(1,$ids)){
            return responseToJson(-240003);
        }

//        if (!$rows) {
//            return responseToJson(-2003);
//        }
        /*******修改 改标签会员成为初级会员********/
        $groupList=\App\Models\Account_group::getGroups($ids);//获取删除信息
        $groups=array_column($groupList,'group_code');
        $group_names=array_column($groupList,'group_name');
        $putong=\App\Models\Account_group::getInfoByID(1);//普通会员信息

        $data=array(
            'group_name'=>'|'.$putong['group_name'].'|',
            'group_code'=>'0,'.$putong['group_code'].','
        );
        foreach ($groups as $v){
            $res=\App\Models\Account_detailed::changeToleve($v,$data);
            if($res<0){
                return responseToJson($res);
            }
        }
        //修改银行卡显示==
        \App\Models\System_bank::changeShow($groups,$group_names);
        $rows = \App\Models\Account_group::whereIn('id', $ids)->delete();

        /*************************************/
        /**********删除权限相关信息*********/
        $log = array(
            session('adminInfo.admin_name')
        );
        $res = OperationLog(session('adminInfo.admin_id'), 'detaleLabel', $log);
        return responseToJson(1);

    }

    //修改
    function edit(Request $req){
        $id=$req->id;
        if(intval($id)<1){
            return -1;
        }
        if(!$req->isMethod('post')){

            $data=\App\Models\User_grade::where('id',$id)->first();
            if(!$data){
                return -2;
            }
            $data=$data->toArray();
            return view('admin.Usergrade/add',array('admin'=>$data));
        }else{
            $model=\App\Models\User_grade::where('id',$id)->first();
            $model->grade=trim($req->input('grade'));
            $model->save();
            return responseToJson(1);
        }
    }

}


