<?php

namespace App\Http\Controllers\v2019;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use App\Rules\ShowLessType;
use Illuminate\Http\Request;
use Validator;

class ShowMoreLess extends Controller
{

    public function saveShow(Request $request)
    {
        $memId = \App\Helper::getMemId();
        //$data = json_decode($request->getContent(), true);
        $data["resourceType"] = $request->input("resourceType");
         $data["memId"] = $memId;
        $data["itemID"] = $request->input("itemID");
        $data["type"] = $request->input("type");
        $data["section"] = $request->input("section");
        $validator = Validator::make($data, [
            'memId' => 'int',
            'itemID' => 'required',
            'type' => 'required',
            'section' => 'required',
            'resourceType' => 'required'
        ]);
        if ($validator->fails()) {
            $messages = $validator->messages();
            return response()->json([
                'status' => [
                    "code" => \App\RestApiResponseCodes::mandatoryParamsMissingCode,
                    'message' => $messages
                ]
            ]);
        }
        $hasRec = \App\ShowMoreLess::select('*')
            ->where('memid', $memId)
            ->where('item_id', $data['itemID'])
            ->where('section', $data['section'])
            ->where('resourceType', $data['resourceType'])
            ->first();
        if (empty($hasRec)) {
            \App\ShowMoreLess::insert([
                'memid' => $memId,
                'resourceType' => $data['resourceType'],
                'section' => $data['section'],
                'item_id' => $data['itemID'],
                'action' => $data['type'],
                'f_date' => date("Y-m-d"),
                'f_time' => date("h:i:s"),
            ]);
            return response()->json([
                'status' => [
                    "code" => \App\RestApiResponseCodes::RequestSentSuccessfullyCode,
                    'message' => 'Successfully added .'
                ]
            ]);
        }
        \App\ShowMoreLess::where('id', $hasRec->id)->update([
            'action' => $data['type'],
        ]);
        return response()->json([
            'status' => [
                "code" => \App\RestApiResponseCodes::RequestSentSuccessfullyCode,
                'message' => 'Successfully updated .'
            ]
        ]);

    }
}
