<?php

namespace Modules\Linksehat\Http\Controllers\Api;
use App\Helpers\Helper;
use App\Models\OLDLMS\User;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Modules\Linksehat\Transformers\User\ShowProfileResource;
use Illuminate\Support\Facades\DB;


class AutocompleteController extends Controller {

    public function maritalStatus(){
        $maritalStatus = DB::connection('oldlms')->table('tm_status_pernikahan')->get()->toArray();

        $data = [];

        if ($maritalStatus){
            $temp = [];
            foreach($maritalStatus as $d){
                $temp['id'] = $d->nID;
                $temp['value'] = $d->sStatusPernikahan;

                array_push($data, $temp);
            }

        }
        return Helper::responseJson($data);
    } 

    public function bloodType() {
        $bloodType = DB::connection('oldlms')->table('tm_golongan_darah')->get()->toArray();

        $data = [];

        if ($bloodType){
            $temp = [];
            foreach($bloodType as $d){
                $temp['id'] = $d->nID;
                $temp['value'] = $d->sGolonganDarah;

                array_push($data, $temp);
            }

        }
        return Helper::responseJson($data);
    }

    public function relationship() {
        $relation = DB::connection('oldlms')->table('tm_hubungan_keluarga')->get()->toArray();

        $data = [];

        if ($relation){
            $temp = [];
            foreach($relation as $d){
                $temp['id'] = $d->nID;
                $temp['value'] = $d->sHubunganKeluarga;

                array_push($data, $temp);
            }

        }
        return Helper::responseJson($data);
    }

    public function corporate(Request $request) {
        $search = $request->search;
        $corporate = DB::table('corporates')
                        ->where('active', 1)
                        ->where(function ($query) use ($search) {
                            $query->where('name', 'like', "%$search%")
                                ->orWhere('code', 'like', "%$search%");
                        })
                        ->get()
                        ->toArray();

        $data['company'] = [];

        if ($corporate){
            $temp = [];
            foreach($corporate as $d){
                $temp['id'] = $d->id;
                $temp['name'] = $d->name;
                $temp['linking_rule'] = json_decode($d->linking_rules);

                array_push($data['company'], $temp);
            }

        }
        return Helper::responseJson($data);
    }
}

