<?php


namespace App\Http\Controllers\Admin;


// 单控配置
use App\Facade\TableName;
use App\Http\helper\NumConfig;
use App\Http\helper\Redis;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ControlController
{
    // 单控配置列表
    public function ControlConfig(Request $request)
    {
        $list = DB::connection('write')->table('agent.dbo.ControlConfig')->get();
        $data = compact('list');
        return view('admin.control.config', $data);
    }

    // 单控配置添加
    public function ControlConfig_add(Request $request)
    {
        if ($request->isMethod('post')) {

            $post = $request->post();
            $data['Name']=$post['Name'];
            unset($post['Name']);
            $data['Data']=json_encode($post);
            $admin = session('admin');
            $data['AdminID'] = $admin->id;

            DB::connection('write')->table('agent.dbo.ControlConfig')->insert($data);
            return apiReturnSuc();

        } else {
            $ControlModel = new \App\Models\Control();
            $GameDataText=$ControlModel->GameDataText;
            return view('admin.control.config_add',compact('GameDataText'));
        }
    }

    // 单控配置修改
    public function ControlConfig_update(Request $request, $ID)
    {
        if ($request->isMethod('post')) {

            $post = $request->post();
            $data['Name']=$post['Name'];
            unset($post['Name']);
            $data['Data']=json_encode($post);
            $admin = session('admin');
            $data['AdminID'] = $admin->id;
            DB::connection('write')->table('agent.dbo.ControlConfig')->where('ID', $ID)->update($data);
            return apiReturnSuc();

        } else {

            $ControlModel = new \App\Models\Control();
            $GameDataText=$ControlModel->GameDataText;

            $info=$this->getConfigByID($ID);
//            var_dump($info);
            $data = compact('info','GameDataText');
            return view('admin.control.config_update', $data);
        }
    }

    // 获取配置
    public function getConfig()
    {
        $list = $this->getList();
        return apiReturnSuc($list);
    }

    // 获取配置
    public function getList()
    {
        $list = DB::connection('write')->table('agent.dbo.ControlConfig')->get();

        foreach ($list as &$info){
            $infoData=json_decode($info->Data);
            foreach ($infoData as $k=>$v){
                $info->$k=$v;
            }
        }
        return $list;
    }

    public function getConfigByID( $ID)
    {
        $info = DB::connection('write')->table('agent.dbo.ControlConfig')->where('ID', $ID)->first();

        $infoData=json_decode($info->Data);
        foreach ($infoData as $k=>$v){
            $info->$k=$v;
        }
        return $info;
    }


    // 选中模板给页面赋值
    public function gradeChange(Request $request, $ID)
    {
        $info =$this->getConfigByID($ID);
        return apiReturnSuc($info);
    }

    public function ControlConfigGear(Request $request)
    {
        $KindID = $request->KindID ?: '';
        $list = DB::connection('write')->table('agent.dbo.ControlGear')->where('KindID', $KindID)->get();
        return view('admin.control.control_config_gear', [
            'list' => $list
        ]);
    }

    public function ControlConfigGearAdd(Request $request)
    {
        $Value = $request->Value ?: '';
        $KindID = $request->KindID ?: '';

        DB::connection('write')->table('agent.dbo.ControlGear')->where('KindID', $KindID)->delete();

        if (!empty($Value)) {
            $explode = explode(',', $Value);
            $data = [];
            foreach ($explode as $val) {
                $data[] = ['Value' => $val, 'KindID' => $KindID];
            }
            DB::connection('write')->table('agent.dbo.ControlGear')->insert($data);
        }
        return apiReturnSuc();
    }

    // 捕鱼配置
    public function fish_add(Request $request, $UserID)
    {

        $redis = Redis::getInstance();

        $redis = $redis->redis_content();

        if ($request->isMethod('post')) {
            $post = $request->post();

            $post['controlTarget'] *= NumConfig::NUM_VALUE;

            $redis->hMSet('userControl_' . $UserID, ['controlLevel' => $post['controlLevel'], 'controlTarget' => $post['controlTarget'], 'curControlValue' => 0]);

            // 当前余额
            $score = DB::connection('read')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->select('Score')->first()->Score ?? 0;

            $content = '捕鱼控制：';
            $content .= $post['controlLevel'] == 0 ? '不控制' : ($post['controlLevel'] > 0 ? '赢' : '输');
            $content .= '控制分数' . $post['controlTarget'];

            // 添加控制记录
            $data = [
                'user_id' => $UserID,
                'admin_id' => session('admin')->id ?? '',
                'contents' => $content,
                'before_config' => '',
                'created_at' => date('Y-m-d H:i:s'),
                'score' => $score
            ];

            DB::connection('write')->table('agent.dbo.control_record')->insert($data);

            return apiReturnSuc();
        }

        $info = $redis->hmGet("userControl_$UserID", ['controlLevel', 'controlTarget', 'curControlValue']);

        $info['controlTarget'] /= NumConfig::NUM_VALUE;
        $info['curControlValue'] /= NumConfig::NUM_VALUE;
        return view('admin.control.fish_add', compact('info', 'UserID'));
    }

    // 全局控制配置
    public function control_config_info()
    {

        $list = DB::connection('write')->table(TableName::QPPlatformDB() . 'ControlConfigInfo')->get();

        return view('admin.control.control_config_info', compact('list'));
    }

    public function control_config_info_update(Request $request, $ID)
    {
        if ($request->isMethod('post')) {
            $post = $request->post();
            $post = array_map(function ($val) {
                return (int)$val;
            }, $post);
            DB::connection('write')->table(TableName::QPPlatformDB() . 'ControlConfigInfo')
                ->where('ID', $ID)
                ->update($post);
            return apiReturnSuc();
        } else {
            $info = DB::connection('write')->table(TableName::QPPlatformDB() . 'ControlConfigInfo')->where('ID', $ID)->first();
            return view('admin.control.control_config_info_update', compact('info'));
        }
    }
}
