<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Income;
use App\Models\Branch;
use App\Http\Requests\IncomeRequest;
use Illuminate\Support\Facades\Gate;

class IncomeController extends Controller
{
    protected $LogController;
    public function __construct(LogController $LogController)
    {
        $this->middleware('auth');
        $this->LogController = $LogController;
    }

    public function getIncome($month, $year)
    {
        if (Gate::allows('isSuperadmin')) {
            $incomes = Income::select('incomes.id as iid', 'incomes.*', 'users.username', 'branchs.*')
                            ->leftJoin('branchs', 'branchs.id', '=', 'incomes.branch_id')
                            ->leftJoin('users', 'users.id', '=', 'incomes.user_id')
                            ->whereMonth('date', '=', $month)
                            ->whereYear('date', '=', $year)
                            ->orderBy('date', 'desc')
                            ->orderBy('time', 'desc')
                            ->get();
        } else {
            $user = auth()->user()->id;
            $incomes = Income::select('incomes.id as iid', 'incomes.*', 'users.username', 'branchs.*')
                            ->leftJoin('branchs', 'branchs.id', '=', 'incomes.branch_id')
                            ->leftJoin('users', 'users.id', '=', 'incomes.user_id')
                            ->whereMonth('date', '=', $month)
                            ->whereYear('date', '=', $year)
                            ->where('user_id', '=', $user)
                            ->orderBy('date', 'desc')
                            ->orderBy('time', 'desc')
                            ->get();
        }
        $branchs = Branch::where('is_active', '=', '1')->get();

        return view('income.index')->with([
            'page_title' => 'รายรับอื่นๆ',
            'incomes' => $incomes,
            'date' => $month.'/'.$year,
            'branchs' => $branchs,
        ]);
    }
    public function index()
    {
        return $this->getIncome(date('m'), date('Y'));
    }

    public function submit(Request $request)
    {
        $request->validate([
            'date' => 'required|date_format:m/Y',
        ],[
            'date.required' => 'กรุณากรอกวันที่',
            'date.date_format:m/Y' => 'กรุณากรอกรูปแบบวันที่ให้ถูกต้อง'
        ]);

        $tmp = explode('/', $request->date);
        $month = $tmp[0];
        $year = $tmp[1];
        return $this->getIncome($month, $year);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Income $income)
    {
        $income->delete();
        // Log
        $description = "ลบรายรับ: ". $income->title. ' , จำนวน:'. $income->amount .' บาท ของวันที่  '.formatDateFromDB($income->date). ' เวลา '.$income->time ;
        $this->LogController->log('รายรับ', 'ลบข้อมูล', $description);

        return redirect()->route('income.index')->with('feedback','ลบข้อมูลรายรับเรียบร้อย');
    }

    public function income(IncomeRequest $request)
    {
        $income = new Income;
        $income->title = strip_tags($request->title);
        $income->amount = strip_tags($request->amount);
        $income->method = $request->method;
        $income->date = formatDateToDB($request->payDate);
        $income->time = $request->payTime;
        $income->branch_id = $request->branch;
        $income->user_id = auth()->user()->id;
        $income->save();

        $description = "เพิ่มรายรับ:". strip_tags($request->title). ", จำนวน:". strip_tags($request->amount). ", วันเวลา:".$request->payDate." ".$request->payTime;
        $this->LogController->log('รายรับ', 'เพิ่มข้อมูล', $description);

        return redirect()->back()->with('feedback','บันทึกรายรับเรียบร้อย');
    }
}
