<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Level;
use App\Models\Plan;
use Illuminate\Http\Request;
use App\Rules\FileTypeValidate;

class PlanController extends Controller
{
    public function index()
    {
        $pageTitle = "All Plan List";
        $plans = Plan::orderBy('id','asc')->paginate(getPaginate());
        return view('admin.plan.index', compact('pageTitle', 'plans'));
    }

    public function create()
    {
        $pageTitle = "Plan Create";
        return view('admin.plan.create', compact('pageTitle'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'title' => 'required',
            'price' => 'required|numeric|gt:0',
            'discount_amount' => 'sometimes|required|numeric|gt:0',
            'discount_type' => 'sometimes|required|in:INR,%',
            'uplink_payment' => 'sometimes|required|numeric|gt:0',
            'uplink_payment_type' => 'sometimes|required|in:INR,%',
            'referral_bonus' => 'required|numeric|gt:0',
            'level' => 'required|array',
            'level.*' => 'numeric|gt:0'
        ]);

        $plan = new Plan();
        $plan->name = $request->name;
        $plan->title = $request->title;
        $plan->price = $request->price;
        $plan->referral_bonus = $request->referral_bonus;
        $plan->discount_amount = $request->discount_amount;
        $plan->discount_type = $request->discount_type;
        $plan->uplink_payment = $request->uplink_payment;
        $plan->uplink_payment_type = $request->uplink_payment_type;
        $plan->save();

        $this->levelUpdate($request, $plan);

    	$notify[] = ['success', 'Plan added Successfully'];
        return back()->withNotify($notify);
    }

    public function edit($id)
    {
    	$plan = Plan::findOrFail($id);
    	$pageTitle = "Product Details";

    	return view('admin.plan.edit', compact('pageTitle', 'plan'));
    }

    public function update(Request $request, $id)
    {
        // dd($request->all());
    	$request->validate([
    	 	'name' => 'required',
    	 	'title' => 'required',
    	 	'price' => 'required|numeric|gt:0',
            'plan_image' => ['nullable', 'image', new FileTypeValidate(['jpg', 'jpeg', 'png'])],
    	 	// 'referral_bonus' => 'required|numeric|gt:0',
            'discount' => 'required|numeric|gt:0',
            'discounted_plan_price' => 'required|numeric|gt:0',
            'description'   => 'required',
            'app_link' => 'nullable',
            'donation' => 'required|numeric|gt:0',
            'receivers_count' => 'required|numeric|gt:0',
    	]);

    	$plan = Plan::findOrFail($id);

        if ($request->hasFile('plan_image')) {
            try {
                $plan->plan_image = fileUploader($request->plan_image, getFilePath('withdrawMethod'), getFileSize('withdrawMethod'), $plan->plan_image);
            } catch (\Exception $exp) {
                $notify[] = ['error', 'Couldn\'t upload your QR code'];
                return back()->withNotify($notify);
            }
        }

    	$plan->name = $request->name;
    	$plan->title = $request->title;
    	$plan->price = $request->price;
    	$plan->discount = $request->discount;
    	$plan->discounted_plan_price = $request->discounted_plan_price;
        $plan->app_link = $request->app_link;
        $plan->description = $request->description;
    	$plan->donation = $request->donation;
    	$plan->receivers_count = $request->receivers_count;
    	// $plan->referral_bonus = $request->referral_bonus;
    	$plan->save();

    	$notify[] = ['success', 'Plan updated successfully'];
    	return back()->withNotify($notify);
    }

    private function levelUpdate($request, $plan){

        $level = Level::where('plan_id', $plan->id)->delete();
        foreach($request->level as $l=>$a)
        {
            $level = new Level();
            $level->plan_id = $plan->id;
            $level->level = $l;
            $level->amount = $a;
            $level->save();
        }
    }

    public function status($id)
    {
        return Plan::changeStatus($id);
    }

    public function matrixSetting(Request $request)
    {
        $request->validate([
            'matrix_height' => 'required|integer|gt:0',
            'matrix_width' => 'required|integer|gt:0'
        ]);
        $general = gs();
        $general->matrix_height = $request->matrix_height;
        $general->matrix_width = $request->matrix_width;
        $general->save();
        $notify[] = ['success', 'Matrix setting has been updated'];
        return back()->withNotify($notify);
    }

}
