<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Deposit;
use App\Models\Hostel;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * @api {get} /rent-status Tong hop hien trang thue
 * @apiName rent-status
 * @apiGroup Report
 *
 * @apiDescription Api Tong hop hien trang thue
 * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
 * @apiSuccess {String} message  Tin nhắn hệ thống.
 * @apiSuccess {String} data
 */
class ReportController extends BaseController
{
    //
    public function getRentStatus( Request $request ) {
        $ownerId = $this->user->id;

        if ( $this->user->type == User::STAFF ) {
            $ownerId = $this->user->staff_owner_id;
        }
        $hostels = Hostel::query()
            ->where( 'owner_id', $ownerId )
            ->with( 'rooms' )
            ->get();
        $api = true;
        return response( [
            'status' => 1,
            'data'   => view( 'admin2.report.rent_status_ajax', compact( 'hostels', 'api' ) )->render()
        ] );
    }

    public function getDeposit( Request $request ) {
       
        $ownerId = $this->user->id;

        if ($this->user->type == User::STAFF) {
            $ownerId = $this->user->staff_owner_id;
        }

        $hostels = Hostel::query()
            ->where('owner_id', $ownerId)
            ->with('rooms')
            ->get();

        $sumDeposits = Deposit::query()
            ->has('room')
            ->where(function($q) {
                $q->orHas('reserve');
                $q->orHas('contractValid');
            })
            ->whereIn('hostel_id', $hostels->pluck('id')->toArray())
            ->sum('amount');

        return response([
            'status' => 1,
            'data' => view('admin2.report.deposit_table', compact('hostels', 'sumDeposits'))->render()
        ]);
    }
}
