<?php

namespace App\Http\Controllers;

use App\Models\loanProduct;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class LoanProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        
        
        $data = DB::table('loanproduct')->get();

        return view("layouts.LoanProd",["loanProd"=>$data]);

    }


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

         $BankArray  = DB::table("bank")->get();
         $BankArray =  json_decode(json_encode($BankArray),true);
      //  echo "<pre>"; print_r( $BankArray); die();
         return view("layouts.AddLoanProd",["BankArray" => $BankArray]);
    
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

         $prod       = new loanProduct;
         $prod->name = $request->input("name");
        // $prod->bk   = $request->input("bk");
         $prod->status   = 'active';
         $prod->save();

         return redirect("LoanProd")->with("status","added successfully");
    }

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

    /**
     * 
     * 
     * Show the form for editing the specified resource.
     *
     * 
     * @param  \App\Models\loanProduct  $loanProduct
     * @return \Illuminate\Http\Response
     */

    public function edit(loanProduct $loanProduct,$id){

        $data = DB::table("loanproduct")->where("id",$id)->first();
        $data= json_decode(json_encode($data),true);

        $data2 = DB::table("bank")->get();
        $BankArray= json_decode(json_encode($data2),true);

        return view("layouts.editLoanProduct",["data"=>$data,"BankArray"=>$BankArray]);
    
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\loanProduct  $loanProduct
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id= $request->input("id");

      
        $uparray = array(
            
            "name"    => $request->input("name"),
        //    "bk"      => $request->input("bk"),
            "status"  => $request->input("status")

        );

        DB::table("loanproduct")->where("id",$id)->update($uparray);
        return redirect("LoanProd")->with("status","update successfully"); 


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\loanProduct  $loanProduct
     * @return \Illuminate\Http\Response
     */

    public function destroy(loanProduct $loanProduct,$id){
            //echo $id;
            DB::table("loanproduct")->where("id",$id)->delete();
            return redirect("LoanProd")->with("status","deleted successfully");
    }
}
