<?php


namespace App\Http\Controllers\Admin;

use App\Http\helper\NumConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RechargeControlConfigController
{
    public function index()
    {
        $list = DB::connection('read')->table('QPTreasureDB.dbo.RechargeControlConfig')
            ->paginate(10);

        foreach ($list as &$value) {
            $value->ProbBase /= NumConfig::NUM_VALUE;
            $value->ProbAdd /= NumConfig::NUM_VALUE;
            $value->ProbMax /= NumConfig::NUM_VALUE;
            $value->RechargeMin /= NumConfig::NUM_VALUE;
            $value->RechargeMax /= NumConfig::NUM_VALUE;
            $value->ScoreBase /= NumConfig::NUM_VALUE;
            $value->ScoreWin /= NumConfig::NUM_VALUE;
        }

        return view('admin.recharge_control_config.index', [
            'list' => $list
        ]);
    }

    // 添加
    public function add(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('admin.recharge_control_config.add', [

            ]);
        } else {
            $post = $request->post();
            $RechargeMin = $post['RechargeMin'] * NumConfig::NUM_VALUE;
            $RechargeMax = $post['RechargeMax'] * NumConfig::NUM_VALUE;
            if ($RechargeMin > $RechargeMax) {
                return apiReturnFail(__('messages.admin.recharge.limit_error'));
            }
            if ($RechargeMin > 0 && $RechargeMax > 0) {
                $first = DB::connection('read')->table('QPTreasureDB.dbo.RechargeControlConfig')
                    ->where('RechargeMax', '>=', $RechargeMin)
                    ->first();
                if ($first) {
                    return apiReturnFail(__('messages.admin.recharge.level_exists'));
                }
            }

            $post['ScoreType'] = 1;
            $post['Status'] = 1;
            $post['RechargeMin'] *= NumConfig::NUM_VALUE;
            $post['RechargeMax'] *= NumConfig::NUM_VALUE;
            $post['ScoreBase']  *= NumConfig::NUM_VALUE;
            $post['ScoreWin']  *= NumConfig::NUM_VALUE;
            DB::connection('write')->table('QPTreasureDB.dbo.RechargeControlConfig')
                ->insert($post);
            return apiReturnSuc();
        }
    }

    // 修改
    public function update(Request $request, $id)
    {
        if ($request->isMethod('get')) {
            $first = DB::connection('read')->table('QPTreasureDB.dbo.RechargeControlConfig')
                ->where('ID', $id)
                ->first();
            $first->RechargeMin /= NumConfig::NUM_VALUE;
            $first->RechargeMax /= NumConfig::NUM_VALUE;
            $first->ScoreBase /= NumConfig::NUM_VALUE;
            $first->ScoreWin /= NumConfig::NUM_VALUE;
            return view('admin.recharge_control_config.update', [
                'info' => $first
            ]);
        } else {
            $post = $request->post();
            $RechargeMin = $post['RechargeMin'] * NumConfig::NUM_VALUE;
            $RechargeMax = $post['RechargeMax'] * NumConfig::NUM_VALUE;
            if ($RechargeMin > $RechargeMax) {
                return apiReturnFail(__('messages.admin.recharge.limit_error'));
            }

            if ($RechargeMin > 0 && $RechargeMax > 0) {
                $first = DB::connection('read')->table('QPTreasureDB.dbo.RechargeControlConfig')
                    ->where('RechargeMin', '>', $RechargeMin)
                    ->where('RechargeMax', '<', $RechargeMax)
                    ->where('ID', '<>', $id)
                    ->first();
                if ($first) {
                    return apiReturnFail(__('messages.admin.recharge.level_exists'));
                }
            }

            $post['ScoreType'] = 1;
            $post['Status'] = 1;
            $post['RechargeMin'] *= NumConfig::NUM_VALUE;
            $post['RechargeMax'] *= NumConfig::NUM_VALUE;
            $post['ScoreBase']  *= NumConfig::NUM_VALUE;
            $post['ScoreWin']  *= NumConfig::NUM_VALUE;
            DB::connection('write')->table('QPTreasureDB.dbo.RechargeControlConfig')
                ->where('ID', $id)
                ->update($post);
            return apiReturnSuc();
        }
    }

    // 删除
    public function del($id)
    {
        DB::connection('write')->table('QPTreasureDB.dbo.RechargeControlConfig')
            ->where('ID', $id)
            ->delete();
        return apiReturnSuc();
    }

    // 开关
    public function switch(Request $request, $id)
    {
        $type = $request->type ?: '';
        $data = [];
        switch ($type) {
            case 'off':
                $data['Status'] = 2;
                break;
            case 'on':
                $data['Status'] = 1;
                break;
        }
        DB::connection('write')->table('QPTreasureDB.dbo.RechargeControlConfig')
            ->where('ID', $id)
            ->update($data);
        return apiReturnSuc();
    }
}
