<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request; 
use App\Models\Financing;
use App\Models\DomainSales;

class FinancingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    { 
        $financings = Financing::orderBy('id' , 'DESC')->paginate(10);
        return view('Admin.Financing.index' , compact('financings'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $domains = DomainSales::orderBy('id' , 'DESC')->get();
        return view('Admin.Financing.add' , compact('domains'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request , [
            'domain_sales_id' => 'required',
            'from' => 'required',
            'term' => 'required'
        ]);

        Financing::create($request->all());
        return redirect()->route('Financing.index')->with('success_message' , 'Record Successfully Created');
    }

    /**
     * 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)
    {
        $financing = Financing::find($id);
        $domains = DomainSales::orderBy('id' , 'DESC')->get();
        return view('Admin.Financing.update' , compact('financing' , 'domains'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request , [
            'domain_sales_id' => 'required',
            'from' => 'required',
            'term' => 'required'
        ]);

        Financing::find($id)->update($request->all());
        return redirect()->route('Financing.index')->with('success_message' , 'Record Successfully Update');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Financing::find($id)->delete();
        return redirect()->route('Financing.index')->with('success_message' , 'Record Successfully Deleted');
    }
}
