<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Models\Group;
use App\Models\Client;
use App\Helpers\Helper;
use App\Models\Commune;
use App\Models\Village;
use App\Models\Category;
use App\Models\District;
use App\Models\Province;
use App\Models\Collateral;
use App\Models\LoanStatus;
use Illuminate\Http\Request;
use App\Models\PaymentMethod;
use App\Models\CollateralDetail;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Resources\CollateralDetailCustomResource;
use App\Models\LoanCollateral;

class ComponentController extends Controller
{
    public function autoCompleteAddress(Request $request)
    {
        return Village::search($request->searchText)->take(10)->pluck('address_km')->toArray();
    }

    public function getProvince(Request $request)
    {
        $data = Province::all()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'full_name_km' => $value->full_name_km,
            ];
        });
        // dd($data);
        return response()->json($data);
    }

    public function getDistrict(Request $request)
    {
        return District::where('province_id', $request->province_id)->get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'full_name_km' => $value->full_name_km,
            ];
        });
    }

    public function getCommune(Request $request)
    {
        return Commune::where('district_id', $request->district_id)->get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'full_name_km' => $value->full_name_km,
            ];
        });
    }

    public function getVillage(Request $request)
    {
        return Village::where('commune_id', $request->commune_id)->get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'full_name_km' => $value->full_name_km,
            ];
        });
    }

    public function getVillageClient()
    {
        $client = Client::groupby('village_id')->get();
        $arr_id = [];
        foreach($client as $value){
            $arr_id[] = $value['village_id'];
        }
        return Village::whereIn('id', $arr_id)->get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'full_name_km' => $value->full_name_km,
            ];
        });
    }

    public function getClient()
    {
        return Client::whereIn('branch_id',Helper::byBranch())
            // ->where(function($query){
            //     if (Auth::user()->roles[0]->id!=1) {
            //         $query->where('created_by',Auth::user()->id);
            //     }
            // })
            ->where('status',1)
            ->orderby('id','desc')
            ->get()
            ->map(function($value){
                return [
                    'id'    => $value->id,
                    'value' => $value->id,
                    'name'  => $value->name,
                ];
            });
    }

    public function clientAndCode()
    {
        return Client::whereIn('branch_id',Helper::byBranch())
            // ->where(function($query){
            //     if (Auth::user()->roles[0]->id!=1) {
            //         $query->where('created_by',Auth::user()->id);
            //     }
            // })
            ->where('status',1)
            ->orderby('id','desc')
            ->get()
            ->map(function($value){
                return [
                    'id'    => $value->id,
                    'value' => $value->id,
                    'name'  => $value->name.'('.$value->code.')',
                ];
            });
    }

    public function getCO()
    {
        return User::whereIn('branch_id',Helper::byBranch())
            ->where('status',1)
            ->where('type',2)
            // ->whereIn('id',Helper::group())
            ->get()
            ->map(function($value){
                return [
                    'id'    => $value->id,
                    'value' => $value->id,
                    'name'  => $value->name,
                ];
            });
    }

    public function getCOLoan(Request $request)
    {
        return User::whereIn('branch_id',Helper::byBranch())->where('id','<>',$request->co_id)->where('status',1)->where('type',2)->get()->map(function($value){
            return [
                'id'    => $value->id,
                'value' => $value->id,
                'name'  => $value->name,
            ];
        });
    }

    public function getLoanStatus(Request $request)
    {
        return LoanStatus::where('id','<',4)->get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'title' => $value->title,
            ];
        });
    }

    public function getCategory(Request $request)
    {
        return Category::get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'title' => $value->title,
            ];
        });
    }

    public function getUser(Request $request)
    {
        return User::whereIn('branch_id',Helper::byBranch())
            ->where(function($query) use ($request){
                if(isset($request->type)){
                    $query->where('type',$request->type);
                }
            })
            ->where('status',1)
            ->get()
            ->map(function($value){
                return [
                    'id'    => $value->id,
                    'value' => $value->id,
                    'name'  => $value->name,
                ];
        });
    }

    public function getClientLoan(Request $request)
    {

     $data = CollateralDetail::where('loan_id',$request->loan_id)->where('status_type',$request->status_type)->first();
       if($data){
        $res = [
            'status' => 1,
            'message'=> 'Get success',
            'data'   => new CollateralDetailCustomResource($data),
        ];
    }else{
        $res = [
            'status'  => 0,
            'message' => 'Get not found',
            'data'    => [],
        ];
    }
    return response()->json($res,200);
    }

    public function getCollateral(Request $request)
    {
        return Collateral::get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'title' => $value->title,
            ];
        });
    }

    public function getCollateralCustom(Request $request)
    {
        return Collateral::where(function ($query) use ($request) {
            if ($request->cat_id) {
                $query->where('cat_id', $request->cat_id);
            }
        })
        ->get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'title' => $value->title,
            ];
        });
    }

    public function getDefaultCurrency(Request $request)
    {
        $data = Auth::user()->currency_id;
        return $data;
    }

    public function funFindCollateral(Request $request)
    {
       return CollateralDetail::where(function ($query) use ($request) {
            if ($request->client_id) {
                $query->where('client_id', $request->client_id);
            }
            if ($request->client_id) {
                $query->where('status', 1);
            }
        })
        ->get()->map(function($value){
            return [
                'id' => $value->id,
                'value' => $value->id,
                'collateral_id' => $value->id,
                'title' => $value->collateral_name,
            ];
        });
    }
    public function loanHasCollateral()
    {
       return LoanCollateral::get()->map(function($value){
            return [
                'id' => $value->collateral_id,
                'value' => $value->collateral_id,
                'collateral_id' => $value->collateral_id,
                'title' => $value->collateral_detail->collateral_name??"",
            ];
        });
    }
}
