<?php

namespace App\Http\Controllers;

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


class DomainSaleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(isset($_GET['domain']) and $_GET['domain'] != null){
            $domains = DomainSales::orderBy('id' , 'DESC')->where('name' , $_GET['domain'])->paginate(10); 
        }else{
            $domains = DomainSales::orderBy('id' , 'DESC')->paginate(10); 
        }
        return view('Admin.DomainSale.index' , compact('domains'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request , [
            'name' => 'required',
            'short_description' => 'required',
            'full_description' => 'required',
            'price' => 'required',
            'registrar' => 'required',
            'registrar_date' => 'required',
            'status' => 'required',
            'url' => 'required'
        ]);

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

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $domain = DomainSales::find($id);
        return view('Admin.DomainSale.show' , compact('domain'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $domain = DomainSales::find($id);
        return view('Admin.DomainSale.update', compact('domain'));
    }

    /**
     * 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 , [
            'name' => 'required',
            'short_description' => 'required',
            'full_description' => 'required',
            'price' => 'required',
            'registrar' => 'required',
            'registrar_date' => 'required',
            'status' => 'required',
            'url' => 'required',
        ]);

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

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