<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Country;
use App\Models\State;
use Illuminate\Http\Request;
use Yajra\DataTables\DataTables;

class StateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = "All States";
        if ($request->ajax()) {
            $states = State::with(['country'])->orderBy('state_name', 'asc');
            return Datatables::of($states)
                ->addIndexColumn()
                ->setRowId(function ($data) {
                    return "row_" . $data->id;
                })
                ->editColumn('status', function ($data) {
                    if ($data->status == 1)
                        $status = '<div class="badge badge-success">Active</div>';
                    else
                        $status = '<div class="badge badge-danger">In-Active</div>';

                    return $status;
                })
                ->addColumn('action', function ($row) {
                    $btn = '<a href="' . route("admin.state.edit", $row->id) . '" title="Edit" class="btn-success btn-sm"><i class="fa fa-pencil"></i></a>&nbsp;&nbsp;';
                    $btn .= '<a href="javascript:void(0)" data-id="' . $row->id . '" title="Delete" class="delete btn-danger btn-sm"><i class="fa fa-trash"></i></a>';
                    return $btn;
                })
                ->rawColumns(['action', 'status', 'featured_image'])
                ->make(true);
        }
        return view('admin.state.view-all', compact('title'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data['title'] = "Create New";
        $data['countries'] = Country::whereStatus('1')->pluck('country_name', 'id');
        return view('admin.state.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'state_name'        => 'required|min:1|max:150|unique:states,state_name',
            'country_id'        =>  'required|exists:countries,id',
            'state_code'        => 'required|min:1|max:150|unique:states,state_code',
            'status'            => 'required|integer|between:0,1',
        ]);

        $input = $request->all();
        State::create($input);
        session()->flash('success', 'State Created Successfully');
        return redirect()->route('admin.state.index');
    }

    /**
     * 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)
    {
        $data['title'] = "Edit State";
        $data['editValue'] =  State::findOrFail($id);
        $data['countries'] = Country::whereStatus('1')->pluck('country_name', 'id');
        return view('admin.state.create', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'state_name'        => 'required|min:1|max:150|unique:states,state_name,' . $id . '',
            'country_id'        => 'required|exists:countries,id',
            'state_code'        => 'required|min:1|max:150|unique:states,state_code,' . $id . '',
            'status'            => 'required|integer|between:0,1',
        ]);

        $data = State::findOrFail($id);
        $data->country_id = $request->country_id;
        $data->state_name = $request->state_name;
        $data->state_code = $request->state_code;
        $data->status     = $request->status;
        $data->save();

        session()->flash('success', 'State Updated Successfully');
        return redirect()->route('admin.state.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(State $state)
    {
        $state->delete();
        return response()->json(['status' => 200, 'message' => 'Record deleted successfully!']);
    }
}
