<?php

namespace App\Http\Controllers\Office;

use App\Models\PaymentMode;
use Illuminate\Http\Request;
use App\Models\PaymentGateway;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class PaymentModeController extends Controller
{

    public function index()
    {
        $route_active = 'paymentmode';
        $paymentModes = PaymentMode::get();
        $payment_gateways = PaymentGateway::get();
        $paymentmode_ids = response()->json($paymentModes->modelKeys());
        $default_currency = DB::table('currencies')->where('is_base_currency', 'yes')->first();
        return view('crm.office.paymentmode', compact(['route_active', 'paymentModes','paymentmode_ids', 'payment_gateways', 'default_currency']));
    }

    public function update(PaymentGateway $paymentGateway, Request $request){
        $paymentGateway->gateway_name =  $request->gateway_name;
        $paymentGateway->sandbox_username =  $request->sandbox_username;
        $paymentGateway->sandbox_password =  $request->sandbox_password;
        $paymentGateway->sandbox_api_key =  $request->sandbox_api_key;
        $paymentGateway->sandbox_license_key =  $request->sandbox_license_key;
        $paymentGateway->sandbox_secret_key =  $request->sandbox_secret_key;
        $paymentGateway->sandbox_client_id =  $request->sandbox_client_id;
        $paymentGateway->sandbox_auth_key =  $request->sandbox_auth_key;
        $paymentGateway->live_username =  $request->live_username;
        $paymentGateway->live_password =  $request->live_password;
        $paymentGateway->live_api_key =  $request->live_api_key;
        $paymentGateway->live_license_key =  $request->live_license_key;
        $paymentGateway->live_secret_key =  $request->live_secret_key;
        $paymentGateway->live_client_id =  $request->live_client_id;
        $paymentGateway->live_auth_key =  $request->live_auth_key;
        $paymentGateway->status =  $request->status;
        $paymentGateway->mode =  $request->mode ?? 'sandbox';
        $paymentGateway->description =  $request->description;
        if($paymentGateway->save()){
            $notification = array(
                'message' => __('Payment gateway updated successfully!'),
                'alert-type' => 'success'
            );
            return back()->with($notification);
        }else{
            $notification = array(
                'message' => __('Payment gateway not updated, Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);
        }
    }
}
