<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use Session;
use Illuminate\Support\Arr;


class ServicesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $service_arr = new Models\Service;
        $table_columns = \DB::select(\DB::raw("show full columns from services"));
        $exclude_arr = [
            'updated_at', 'id'
        ];
        $columns = [];
        $queries = [];
        foreach ($table_columns as $key) {
          $columns[$key->Field] = $key->Field;
        }
        foreach ($columns as $col) {
            if (request('filter') == $col) {
                $filter = request('filter');
                $keyword = request('keyword');
                $table_arr = ['staff_id'];
                if (in_array($col, $table_arr)) {
                    $key = $this->get_filter_ids($filter, $keyword);
                    if (!empty($key)) {
                        $keyword = $key;
                    }
                }
                $service_arr = $service_arr->where($col, 'like', '%'.$keyword.'%');
                $queries[$col] = request('keyword');
            }
        }

        $service_arr = $service_arr->orderBy('created_at', 'DESC')->paginate(40)->appends($queries);

        $data = [
            'page_title' => 'Services',
            'columns' => Arr::except($columns, $exclude_arr),
            'service_arr' => $service_arr
        ];

        return view('service.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   
        $service_type = ['regular' => 'Regular', 'advanced' => 'Advanced'];
        $data = [
            'page_title' => 'Create Service',
            'service_type' => $service_type
        ];

        return view('service.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([
            'name' => 'required',
            'type' => 'required',
        ]);

        $service_arr = [
          'name' => $request->name,
          'type' => $request->type
        ];
        $saved = Models\Service::create($service_arr);
        Session::flash('success_message', 'Service successfully added');
        return redirect(url('services'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
        $showservice = Models\Service::find($id);
        $data = [
            'page_title' => 'Show Service',
            'showservice' => $showservice
        ];

        return view('service.show', $data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $service_arr = Models\Service::findOrFail($id);
        $service_type = ['regular' => 'Regular', 'advanced' => 'Advanced'];

        $data = [
            'page_title' => 'Edit Service',
            'service_arr' => $service_arr,
            'service_type' => $service_type
        ];

        return view('service.edit', $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([
            'name' => 'required',
            'type' => 'required',
        ]);

        $service_update = Models\Service::find($id);
        $service_update->name = $request->name;
        $operator_update->type = $request->type;
        $result = $service_update->save();

        Session::flash('success_message', 'Service successfully Updated');
        return redirect(url('services'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = Models\Service::destroy($id);
        if (request()->ajax()) {
            $result_arr = ['code' => 1];
            return response()->json($result_arr);
        }
        Session::flash('success_message', 'Service successfully deleted!');
        return redirect(route('services.index'));
    }


    public function get_filter_ids($filter, $keyword)
    {
        switch ($filter) {
            case 'name':
              $id = Models\Service::where('name', 'like', "%$keyword%")->get(['id']);
              if ($id->isEmpty()) {
                return '';
              }
              $step = json_decode($id);
              $the_id = $step[0]->id;
              return (count($step) > 0 ) ? $step[0]->id : "";
              break;
            case 'type':
              $id = Models\Service::where('type', 'like', "%$keyword%")->get(['id']);
              if ($id->isEmpty()) {
                return '';
              }
              $step = json_decode($id);
              $the_id = $step[0]->id;
              return (count($step) > 0 ) ? $step[0]->id : "";
              break;
            
            default:
              return '';
              break;
        }
    }
}
