<?php

namespace App\Http\Controllers;

use Dingo\Api\Routing\Helpers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests ,Helpers;


    public function success($data,$msg="ok"){
        $this->parseNull($data);
        $result = [
            "code"=>200,
            "message"=>$msg,
            "data"=>$data,
        ];
        return response()->json($result,200);
    }


    public function success_list($data,$msg="ok" ,$total=0,$top=0,$tj=0){
        $this->parseNull($data);
        $result = [
            "code"=>200,
            "message"=>$msg,
            "data"=>[
                'total'=>$total,
                'top'=>$top,
                'tj'=>$tj,
                'list'=>$data
            ],
        ];
        return response()->json($result,200);
    }


    public function success_list_one($data,$msg="ok" ,$total=0,$top=0,$tj=0,$list1,$list2,$list3,$status,$list4,$list5=null){
        $this->parseNull($data);
        $result = [
            "code"=>200,
            "message"=>$msg,
            "data"=>[
                'total'=>$total,
                'top'=>$top,
                'tj'=>$tj,
                'list'=>$data,
                'list1'=>$list1,
                'list2'=>$list2,
                'list3'=>$list3,
                'list4'=>$list4,
                'list5'=>$list5,
                'status'=>$status,
            ],
        ];
        return response()->json($result,200);
    }

    public function success_list_two($data,$msg="ok" ,$total=0,$hard=0,$simple=0,$old=0,$list1,$list2,$list3,$list4,$status,$money,$yes_hard,$yes_simple,$yes_old,$list5=[],$list6=[],$list7=[]){
        $this->parseNull($data);
        $result = [
            "code"=>200,
            "message"=>$msg,
            "data"=>[
                'total'=>$total,
                'money'=>$money,
                'hard'=>$hard,
                'simple'=>$simple,
                'old'=>$old,
                'yes_hard'=>$yes_hard,
                'yes_simple'=>$yes_simple,
                'yes_old'=>$yes_old,
                'list'=>$data,
                'list1'=>$list1,
                'list2'=>$list2,
                'list3'=>$list3,
                'list4'=>$list4,
                'list5'=>$list5,
                'list6'=>$list6,
                'list7'=>$list7,
                'status'=>$status,
            ],
        ];
        return response()->json($result,200);
    }

    //失败返回
    public function error($code=422,$data="",$msg="fail"){
        $result = [
            "code"=>$code,
            "message"=>$msg,
            "data"=>$data
        ];
        return response()->json($result,200);
    }

    //如果返回的数据中有 null 则那其值修改为空 （安卓和IOS 对null型的数据不友好，会报错）
    private function parseNull(&$data){
        if(is_array($data)){
            foreach($data as &$v){
                $this->parseNull($v);
            }
        }else{
            if(is_null($data)){
                $data = [];
            }
        }
    }


}
