<?php

namespace App\Http\Controllers\Backend;

use App\Models\Extra;
use App\Models\Hostel;
use App\Models\Notification;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\Datatables\Datatables;

class ExtraController extends AdminController
{
    //
    public function getExtraByAttributeView()
    {
        $hostels = Hostel::query();

        if(auth('backend')->check())
        {
            if(auth('backend')->user()->type == User::OWNER)
            {
                $hostels = Hostel::where('owner_id', auth('backend')->user()->id);
            }
        }
        $hostels = $hostels->get();
        return view('admin.extra.index', compact('hostels'));
    }

    public function getExtraByAttribute()
    {
        $items = Extra::query();


	    if(auth('backend')->check())
	    {
		    if(auth('backend')->user()->type == User::OWNER)
		    {
			    $hostels = Hostel::where('owner_id', auth('backend')->user()->id)->pluck('id')->toArray();
			    $items = $items->whereIn('hostel_id', $hostels);
		    }
	    }

	    $items = $items->orderBy('id', 'desc');

        return Datatables::of($items)->addColumn('hostel', function ($item) {
                if ($item->hostel) {
                    return $item->hostel->name;
                }
            })
            ->addColumn('room', function ($item) {
                if ($item->room) {
                    return $item->room->name;
                }
            })
            ->editColumn('amount', function ($item) {
                return number_format($item->amount, 0, '.', '.');
            })
            ->editColumn('pay', function ($item) {
                return number_format($item->pay, 0, '.', '.');
            })->addColumn('action', function ($item) {

            })->make(true);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $date = Carbon::now()->format('d/m/Y');

     //   $data['user_id'] = auth('backend')->user()->id;
        $dateAction = Carbon::createFromFormat('d/m/Y', '01/'.$data['date_action'])->toDateString();

        $data['date_action'] = $dateAction;

        Extra::create($data);

        $renters = RenterRoom::where('room_id', $data['room_id'])->get();

        if($data['amount'] == $data['pay'])
        {
            $content = 'Phát sinh tiền '.$data['name'].' ngày '.$date.' với số tiền '.number_format($data['amount'], 0, '.', '.').' đ. Đã thanh toán '.number_format($data['pay'], 0, '.', '.').' đ';
        } else {
            $content = 'Phát sinh tiền '.$data['name'].' ngày '.$date.' với số tiền '.number_format($data['amount'], 0, '.', '.').' đ. Đã thanh toán '.number_format($data['pay'], 0, '.', '.').' đ, còn lại '.number_format(($data['amount'] - $data['pay']), 0, '.', '.').' đ sẽ được thu vào tiền nhà cuối tháng';
        }


        foreach ($renters as $renter) {
            Notification::create( [
                'image' => auth('backend')->user()->image,
                'to_user'   => $renter->user_id,
                'hostel_id' => $data['hostel_id'],
                'room_id'   => $data['room_id'],
                'title'     => 'Thông báo từ itro.vn',
                'user_id'   => auth('backend')->user()->id,
                'content'   => $content
            ] );

        }

        if($request->ajax())
        {
            return response([
                'status' => 1,
                'message' => 'Thành công'
            ]);
        }
    }
}
