<?php

namespace App\Http\Controllers\Admin;

use App\Game\RePayConfig;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class RePayConfigController extends Controller
{
    public function __construct() {


    }

    // Display a listing of the resource.
    public function index()
    {
        $repayConfigs = RePayConfig::all();
        return view('admin.repay_config.index', compact('repayConfigs'));
    }

    // Show the form for creating a new resource.
    public function create()
    {
        return view('admin.repay_config.create');
    }

    // Store a newly created resource in storage.
    public function store(Request $request)
    {
//        dd($request->all());
        $validator = Validator::make($request->all(), [
            'PayTotal'      => 'required|integer|min:0',
            'PayTimes'      => 'required|integer|min:0',
            'WithdrawTotal' => 'required|integer|min:0',
            'LessThan'      => 'required|integer|min:0',
            'Condition'     => 'required|integer|min:0',
            'Price'         => 'required|integer|min:0',
            'Amount'        => 'required|integer|min:0',
            'Gift'          => 'required|integer|min:0',
            'TimeLimit'     => 'required|integer|min:0',
            'Status'        => 'required|boolean',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }


        RePayConfig::create($request->all());
        return redirect()->route('repay_config.index')->with('success',__('repay_config.RePayConfig created successfully.'));
    }

    // Display the specified resource.
    public function show($id)
    {
        $repayConfig = RePayConfig::findOrFail($id);
        return view('admin.repay_config.show', compact('repayConfig'));
    }

    // Show the form for editing the specified resource.
    public function edit($id)
    {
        $repayConfig = RePayConfig::findOrFail($id);
        return view('admin.repay_config.edit', compact('repayConfig'));
    }

    // Update the specified resource in storage.
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'PayTotal'      => 'required|integer|min:0',
            'PayTimes'      => 'required|integer|min:0',
            'WithdrawTotal' => 'required|integer|min:0',
            'LessThan'      => 'required|integer|min:0',
            'Condition'     => 'required|integer|min:0',
            'Price'         => 'required|integer|min:0',
            'Amount'        => 'required|integer|min:0',
            'Gift'          => 'required|integer|min:0',
            'TimeLimit'     => 'required|integer|min:0',
            'Status'        => 'required|boolean',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
        $repayConfig = RePayConfig::findOrFail($id);
        $repayConfig->update($request->all());
        $repayConfig->save();
        $key='RePayConfig';
        Redis::del($key);
//        dd($id,$repayConfig,$request->all());
        return redirect()->route('repay_config.index')->with('success',__('repay_config.RePayConfig updated successfully.'));
    }

    // Remove the specified resource from storage.
    public function destroy($id)
    {
        $repayConfig = RePayConfig::findOrFail($id);
        $repayConfig->delete();
        return redirect()->route('repay_config.index')->with('success',__('repay_config.RePayConfig deleted successfully.'));
    }
}
