<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\tradersadmin;
// use App\Models\Time;
use Illuminate\Http\Request;

class TradersadminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pageTitle = 'All Traders';

        $tradersadmin = Traders::latest()->paginate();

        return view('backend.tradersadmin.index', compact('pageTitle', 'tradersadmin'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       
        $pageTitle = 'Create Trader';

        $time=Time::all();
        return view('backend.tradersadmin.create', compact('pageTitle','time'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|unique:trader,trader_name',
            // 'amount_type' => 'required',
            'minimum' => '',
            // 'maximum' => 'required_if:amount_type,==,0',
            'percentage_gain' => 'required_if:amount_type,==,1',
            'followers' => 'required',
            'copied_trade' => 'required',
            // 'limit' => 'required|gte:0'
        ], [
            'percentage_gain.required_if' => 'input this traders gain ',
            'followers.required_if' => 'input this traders followers',
            'copied_trade.required_if' => 'Input numer of people who copied this trader',
            // 'repeat_time.required_if' => 'How Many Times  is required ',
        ]);

        tradersadmin::create([
            'trader_name' => $request->name,
            'followers' => $request->followers,
            // 'amount_type' => $request->amount_type,
            'minimum_amount' => $request->minimum,
            // 'maximum_amount' => $request->maximum,
            // 'amount' => $request->amount,
            // 'return_interest' => $request->interest,
            // 'every_time' => $request->times,
            // 'return_for' => $request->return_for,
            // 'how_many_time' => $request->repeat_time,
            // 'capital_back' => $request->capital_back,
            'status' => $request->status,           
            // 'interest_status'=>$request->interest_status,
            // 'invest_limit' => $request->limit

        ]);

        $notify[] = ['success', 'Trader created successfully'];

        return redirect()->route('admin.trader.index')->withNotify($notify);
    }

    public function edit(tradersadmin $tradersadmin)
    {
        $pageTitle = 'Edit Trader';     
        $time=Time::all();  
        return view('backend.trader.edit', compact('pageTitle', 'trader'));
    }

    public function update(Request $request, tradersadmin $tradersadmin)
    {       
        $request->validate([
            'name' => 'required|unique:tradersadmin,id,'.$tradersadmin->name,
            // 'amount_type' => 'required',
            'minimum' => 'required',
            'followers' => 'required',
            // 'maximum' => 'required_if:amount_type,==,0',
            // 'amount' => 'required_if:amount_type,==,1',
            // 'interest' => 'required',
            // 'repeat_time' => 'required_if:return_for,==,1',
            // 'limit' => 'required|gte:0'
        ], [
            'name.required' => 'trader name is required ',
            'followers.required' => 'followers is required ',
            'minimum' => 'Minimum Amount is required ',
                        // 'minimum.required_if' => 'Minimum Amount is required ',
            // 'maximum.required_if' => 'Maximum Amount is required ',
            // 'amount.required_if' => 'Amount is required',
            // 'repeat_time.required_if' => 'How Many Times  is required ',
        ]);

        

        $tradersadmin->update([
            'trader_name' => $request->name,
                        'followers' => $request->followers,
            // 'amount_type' => $request->amount_type,
            'minimum' => $request->minimum,
            // 'maximum_amount' => $request->maximum,
            // 'amount' => $request->amount,
            // 'return_interest' => $request->interest,
            // 'every_time' => $request->times,
            // 'return_for' => $request->return_for,
            // 'how_many_time' => $request->repeat_time,
            // 'capital_back' => $request->capital_back,
            'status' => $request->status,           
            // 'interest_status'=>$request->interest_status,
            // 'invest_limit' => $request->limit
        ]);

        $notify[] = ['success', 'Trader Updated Successfully'];

        return redirect()->route('admin.trader.index')->withNotify($notify);
    }

    public function tradersadminStatusChange(Request $request)
    {
        $trader = Trader::findOrFail($request->id);

        if ($request->status) {
            $trader->status = false;
        } else {
            $trader->status = true;
        }

        $trader->save();

        $notify = ['success' => 'Trader Status Changed Successfully'];

        return response($notify);
    }

   
}