<?php

namespace App\Http\Controllers;

use App\Models\Branch;
use App\Models\Country;
use App\Models\CarVariant;
use App\Models\ShowroomPrice;
use App\Models\Slot;
use App\Models\StateList;
use App\Models\CityList;
use App\Models\InsuranceCompany;
use App\Models\NCB;
use App\Models\SlotCompanyPercentage;
use App\Models\ODDiscount;
use App\Models\TariffRate;
use App\Models\Department;
use App\Models\Designation;
use App\Models\Event;
use App\Models\Language;
use App\Models\School;
use App\Models\ShowType;
use App\Models\Subject;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AjaxController extends Controller
{
    
    public function subjects(Request $request){
        $perPage = 15;
        $offset = 0;
        $page = $request->has("page")?$request->page:1;

        if($page > 1){
            $offset = ($page - 1)*$perPage;
        }

        $search = $request->has("search")?$request->search:1;
        $where = array(
            ["id", ">", 0],
        );
        if($search != ""){
            array_push($where,["name","LIKE", "%$search%"]);
        }

        $data = Subject::select("id","name")->where($where)->offset($offset)->limit($perPage)->get();
        return response()->json(["status"=>1,"message"=>"Success","items"=>$data]);
    }
    public function schools(Request $request){
        $perPage = 15;
        $offset = 0;
        $page = $request->has("page")?$request->page:1;

        if($page > 1){
            $offset = ($page - 1)*$perPage;
        }

        $search = $request->has("search")?$request->search:1;
        $where = array(
            ["id", ">", 0],
        );
        if($search != ""){
            array_push($where,["name","LIKE", "%$search%"]);
        }

        $data = School::select("id","name")->where($where)->offset($offset)->limit($perPage)->get();
        return response()->json(["status"=>1,"message"=>"Success","items"=>$data]);
    }
    public function country(Request $request){
        $perPage = 15;
        $offset = 0;
        $page = $request->has("page")?$request->page:1;

        if($page > 1){
            $offset = ($page - 1)*$perPage;
        }

        $search = $request->has("search")?$request->search:1;
        $where = array(
            ["country_id", ">", 0],
        );
        if($search != ""){
            array_push($where,["country_name","LIKE", "%$search%"]);
        }

        $country = Country::select("country_id as id","country_name as name")->where($where)->offset($offset)->limit($perPage)->get();
        return response()->json(["status"=>1,"message"=>"Success","items"=>$country]);
    }
    public function state(Request $request){
        $perPage = 15;
        $offset = 0;
        $page = $request->has("page")?$request->page:1;
        $country_id = $request->has("country_id")?$request->country_id:0;

        if($page > 1){
            $offset = ($page - 1)*$perPage;
        }

        $search = $request->has("search")?$request->search:1;
        $where = array(
            ["country_id", $country_id],
        );
        if($search != ""){
            array_push($where,["state_name","LIKE", "%$search%"]);
        }

        $state = StateList::select("state_id as id","state_name as name")->where($where)->offset($offset)->limit($perPage)->get();
        return response()->json(["status"=>1,"message"=>"Success","items"=>$state]);
    }
    public function city(Request $request){
        $perPage = 15;
        $offset = 0;
        $page = $request->has("page")?$request->page:1;
        $state_id = $request->has("state_id")?$request->state_id:0;

        if($page > 1){
            $offset = ($page - 1)*$perPage;
        }

        $search = $request->has("search")?$request->search:1;
        $where = array(
            ["state_id", $state_id],
        );
        if($search != ""){
            array_push($where,["city_name","LIKE", "%$search%"]);
        }

        $city = CityList::select("city_id as id","city_name as name")->where($where)->offset($offset)->limit($perPage)->get();
        return response()->json(["status"=>1,"message"=>"Success","items"=>$city]);
    }

    //Reporting User
    public function reporting_user(Request $request){
        $perPage = 15;
        $offset = 0;
        $page = $request->has("page")?$request->page:1;

        if($page > 1){
            $offset = ($page - 1)*$perPage;
        }

        $search = $request->has("search")?$request->search:1;
        $where = array(
            ["status", 1],
        );
        if($search != ""){
            array_push($where,["name","LIKE", "%$search%"]);
        }


        $data = User::select("id as id","name as name")->where($where)->offset($offset)->limit($perPage)->get();
        return response()->json(["status"=>1,"message"=>"Success","items"=>$data]);
    }
}
