<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\DiamondPricing;
// use Yajra\DataTables\Facades\DataTables;

class DiamondPricingController extends Controller
{
    /**
     * Display Page
     */
    public function index()
    {
        return view('admin.diamond.diamond_pricing');
    }

    /**
     * DataTables AJAX
     */
        public function list(Request $request)
        {
            if ($request->ajax()) {

                $data = DiamondPricing::orderBy('id', 'DESC')->get();

                foreach ($data as $row) {

                    $row->action = '
                        <button class="btn btn-primary btn-sm editBtn" data-id="'.$row->id.'">
                            Edit
                        </button>

                        <button class="btn btn-danger btn-sm deleteBtn" data-id="'.$row->id.'">
                            Delete
                        </button>
                    ';
                }

                return response()->json([
                    'data' => $data
                ]);
            }
        }

    /**
     * Store
     */
    public function store(Request $request)
    {
        $request->validate([
            'type'       => 'required',
            'shape'      => 'required',
            'size'       => 'required',
            'ppc'        => 'required|numeric',
            'handeling'  => 'required|numeric',
        ]);

        DiamondPricing::create([
            'type'       => $request->type,
            'shape'      => $request->shape,
            'size'       => $request->size,
            'ppc'        => $request->ppc,
            'handeling'  => $request->handeling,
            'status'     => $request->status,
        ]);

        return response()->json([
            'status' => true,
            'message' => 'Diamond Pricing Added Successfully'
        ]);
    }

    /**
     * Edit
     */
    public function edit($id)
    {
        $data = DiamondPricing::findOrFail($id);

        return response()->json($data);
    }

    /**
     * Update
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'type'       => 'required',
            'shape'      => 'required',
            'size'       => 'required',
            'ppc'        => 'required|numeric',
            'handeling'  => 'required|numeric',
        ]);

        $pricing = DiamondPricing::findOrFail($id);

        $pricing->update([
            'type'       => $request->type,
            'shape'      => $request->shape,
            'size'       => $request->size,
            'ppc'        => $request->ppc,
            'handeling'  => $request->handeling,
            'status'     => $request->status,
        ]);

        return response()->json([
            'status' => true,
            'message' => 'Updated Successfully'
        ]);
    }

    /**
     * Delete
     */
    public function destroy($id)
    {
        DiamondPricing::findOrFail($id)->delete();

        return response()->json([
            'status' => true,
            'message' => 'Deleted Successfully'
        ]);
    }
}