<?php

namespace App\Http\Controllers\Masters;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\SalesPoc;
use App\DataTables\SalesPocDataTable;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;

class SalesPocController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(SalesPocDataTable $dataTable)
    {
        return $dataTable->render('master.salespoc.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('master.salespoc.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'remark' => 'nullable'
        ]);
        $input = $request->all();
        $salespoc = new SalesPoc($input);
        $salespoc->save();
        return redirect('sales/poc')->with('success', 'Sales POC Created Successfully');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $salespoc = SalesPoc::where('id','=',$id)->get();
        return view('master.salespoc.show', compact('salespoc'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $salespoc = SalesPoc::where('id','=',$id)->get();
        return view('master.salespoc.edit', compact('salespoc'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'remark' => 'nullable'
        ]);
        $input = $request->all();
        $salespoc = SalesPoc::find($request->id);
        $salespoc->update($input);
        return redirect('sales/poc')->with('success', 'Sales POC Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Request $request)
    {
        $salespoc_delete = SalesPoc::find($request->id);
        $salespoc_delete->delete();
        Session::flash('success', __('Sales POC Data Delete Succesfully'));
        $data            = array();
        return Response::json([
            'success' => true,
            'data'    => $data,
        ]);
    }
}
