<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\GoldBond;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class BondController extends Controller
{



    // public function bond_details()
    // {
    //     return view('Admin.bond_details');
    // }

    public function bond_details($roi_id)
    {
        $bond = DB::table('fin_roi_users')
            ->where('roi_id', $roi_id)
            ->first();

        if (!$bond) {
            abort(404, 'Bond Not Found');
        }

        $user = User::where('user_id', $bond->user_id)->first();

        $added_bond = GoldBond::where('roi_id', $roi_id)->first();

        return view(
            'Admin.bond_details',
            compact(
                'bond',
                'user',
                'added_bond'
            )
        );
    }
 

public function save_gold_bond(Request $request)
{
    $request->validate([

        'user_id' => 'required',
        'roi_id' => 'required',

        'bond_date' => 'required',
        'alphabetic_date' => 'required',

        'salutation' => 'required',
        'name' => 'required',

        'pincode' => 'required|digits:6',
        'address' => 'required',

        'product_details' => 'required',

        'total_grams' => 'required|numeric',
        'total_value' => 'required|numeric',

        'labour_percent' => 'required|numeric',
        'labour_amount' => 'required|numeric',

        'gst_percent' => 'required|numeric',
        'gst_amount' => 'required|numeric',

        'subtotal' => 'required|numeric',

        'loaded_fund' => 'required|numeric|min:1',
        'loaded_date' => 'required',

        'balance' => 'required|numeric'
    ]);

    DB::beginTransaction();

    try {

        GoldBond::updateOrCreate(

            [
                'roi_id' => $request->roi_id
            ],

            [
                'user_id' => $request->user_id,

                'bond_date' => $request->bond_date,
                'bond_date_text' => $request->alphabetic_date,

                'salutation' => $request->salutation,
                'name' => $request->name,
                'pincode' => $request->pincode,
                'address' => $request->address,

                'product_details' => $request->product_details,

                'total_grams' => $request->total_grams,
                'total_value' => $request->total_value,

                'labour_percent' => $request->labour_percent,
                'labour_amount' => $request->labour_amount,

                'gst_percent' => $request->gst_percent,
                'gst_amount' => $request->gst_amount,

                'subtotal' => $request->subtotal,

                'loaded_fund' => $request->loaded_fund,
                'loaded_date' => $request->loaded_date,

                'balance' => $request->balance,
            ]
        );

        DB::commit();

        return redirect()
            ->back()
            ->with('success', 'Bond Saved Successfully');
    } catch (\Exception $e) {

        DB::rollBack();

        return redirect()
            ->back()
            ->with('error', $e->getMessage());
    }
}

    public function ipl_bond()
    {
        return view('Admin.ipl_bond');
    }
    public function print_bond()
    {
        return view('Admin.print_bond');
    }
}
