<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class CityController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    // done by me per Procedure te Qytetit  
    public function qyteti(Request $request)
    {

        $emr_qyteti = $request->emri_qyteti;
      
        $ekziston = DB::table('qyteti')->where('emri_qytetit', $emr_qyteti)->first();
        if ($ekziston != null) {
            $res['success'] = false;
            $res['message'] = "Qyteti ekziston";
            return response($res, 422);
        }


        try {
            $ins = DB::table('qyteti')->insert([
                'emri_qytetit' => $emr_qyteti
            ]);
            return response('sukses', 200);
        } catch (\Illuminate\Database\QueryException $ex) {
            $res['success'] = false;
            //  $ex->getMessage() per me shfaq qfar gabimi 
            $res['message'] =   "Error";
            return response($res, 500);
        }
    }

    public function getqytet()
    {

        $products = DB::table('qyteti')->where('statusi', 1)->get();
        if (empty($products)) {
            $arr = array();
            echo json_encode($arr);
        } else {
            $countallproducts = count($products);
            if ($countallproducts > 0) {
                return response()->json( $products
            );
            }
        }
    }
    public function qytetibyid(Request $request)
    {
        $id = $request->id;
        $products = DB::table('qyteti')->where('id', $id)->get();
        if (empty($products)) {
            $arr = array('code' => 404, 'data' => []);
            echo json_encode($arr);
        } else {
            $countallproducts = count($products);
            if ($countallproducts > 0) {
                return response()->json(array(
                    'code' => 200,
                    'data' => $products
                ), 200);
            }
        }
    }
    public function updqytet(Request $request)
    {
        $id = $request->id;
        $emri = $request->emri;


        if (empty($id) || empty($emri)) {
            $res['success'] = false;
            $res['message'] =   "Të plotësohen të gjitha fushat";
            return response($res, 422);
        }

        try {
            $crudclient = DB::table('qyteti')->where('id', $id)  // find your user by their email
                ->limit(1)  // optional - to ensure only one record is updated.
                ->update([
                    'emri_qytetit' => $emri,
                ]);

            return response($crudclient, 200);
        } catch (\Illuminate\Database\QueryException $ex) {
            $res['success'] = false;
            //  $ex->getMessage() per me shfaq qfar gabimi 
            $res['message'] =   "Error";
            return response($res, 500);
        }
    }
}
