<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\PaymentModule;
use Validator;
use Alert;

class PaymentModuleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, PaymentModule $model)
    {
        //
        $modules = $model::latest()->get();
        return view('backend.payment_modules',compact(['modules']));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, PaymentModule $model)
    {
        //validating data
        $rules = [
            'operator' => 'required|unique:payment_modules',
            'number' => 'required|unique:payment_modules',
            'bank' => 'unique:payment_modules',
            'account' => 'unique:payment_modules',
            'swift_code' => 'unique:payment_modules',
        ];

        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['errors'=>"Recheck the fields they must be unique."]);
        }
        else{
            //getting the values
            $data_save = [
                'operator' => $request->operator,
                'number' => $request->number,
                'address' => $request->address,
                'bank' => $request->bank,
                'account' => $request->account,
                'account_name' => $request->account_name,
                'swift_code' => $request->swift_code,
                'publish_status' => $request->publish_status,
            ];

            $data = $model::create($data_save);

            if($data){
                return response()->json(['success'=>"Payment module created."]);
            }
            else{
                return response()->json(['errors'=>["Unable to create payment module."]]);
            }
        }
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = PaymentModule::findOrFail($id);
        return response()->json(['result'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, PaymentModule $model)
    {
        //validating data
        $rules = [
            'operator' => 'required',
            'number' => 'required',
        ];

        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['errors'=>"Crosscheck the fields they must be unique."]);
        }
        else{
            //getting the values
            $data_update = [
                'operator' => $request->operator,
                'number' => $request->number,
                'address' => $request->address,
                'bank' => $request->bank,
                'account' => $request->account,
                'account_name' => $request->account_name,
                'swift_code' => $request->swift_code,
                'publish_status' => $request->publish_status,
            ];

            $data = $model::whereId($request->hidden_id)->update($data_update);

            if($data){
                return response()->json(['success'=>"Payment module updated successfully."]);
            }
            else{
                return response()->json(['errors'=>["unable to update payment module."]]);
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        //
        $data = PaymentModule::findOrFail($id);

        if($data->delete()){
            return response()->json(['success'=>['Payment module deleted successfully.']]);
        }
        else{
            return response()->json(['errors'=>['unable to delete payment module.']]);
        }
    }
}
