<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\ParentsLogin;
// use App\Models\AboutUs;
use App\Repositories\ResponseRepository;

class ParentsLoginController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'student_id' => 'required',
            'parent_name' => 'required',
            'password' => 'required',
            're_password' => 'required',
        ];
    }
    public function indexParentsLogin() {

        return $this->response->jsonResponse(false,"ParentsLogin Details Fetched Successfully", ParentsLogin::orderBy('id', 'desc')->get(), 100);
    }


    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('ParentsLogin', 'store'), ParentsLogin::create($request->all()), 301);
        } else {
            return $validate;
        }
    }

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('ParentsLogin', 'show'), $this->findTestimony($id), 302);
    }

    public function updateParentsLogin(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'student_id' => ['required'],
            'parent_name' => ['required'],
            'password' => ['required'],
            're_password' => ['required'],
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('ParentsLogin', 'update'), $this->findTestimony($request->id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function destroy($id) {
        $size = $this->findTestimony($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('ParentsLogin', 'destroy'), $size->delete(), 304);
        }
        return $this->response->jsonResponse(true, 'ParentsLogin Not Exists', [], 305);
    }

    public function findTestimony($id) {
        return ParentsLogin::find($id);
    }

    public function parentsLoginSwitch($id) {
        $size = $this->findTestimony($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'ParentsLogin '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'ParentsLogin Not Exists', [], 307);
    }

    public function searchParentsLogin($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('ParentsLogin', 'search'), [], 910);
        }
        return $this->response->jsonResponse(false, $this->response->message('ParentsLogin', 'search'), ParentsLogin::where('parent_name', 'LIKE', '%'.$search.'%')->get(), 911);
    }

}
