<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class PagibigLoanTest extends TestCase
{
    use DatabaseTransactions;

    protected $indexUrl = "/wages/pagibigloan";
    protected $item = "pagibig loan";
    protected $model = "App\Models\Wages\PagibigLoan";
    protected $table = "wages_ph_pagibig_loans";

    public function testUserCanGotoManagementPage()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->see($this->indexTitle())
            ->see($item->employee->dipslayName);
    }

    public function testUserCanCreateItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->make();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->click($this->createButton())
            ->see($this->createTitle())

            ->select($item->employee_id, 'employee_id')
            ->type($item->loan_start_date, 'loan_start_date')
            ->type($item->loan_end_date, 'loan_end_date')
            ->type($item->amount_per_day, 'amount_per_day')
            ->type($item->amount_total, 'amount_total')
            ->type($item->amount_missing, 'amount_missing')

            ->press($this->createButton())
            ->see($this->indexTitle())
            ->see($item->name);

        $this->seeInDatabase($this->table ?? $this->item, [
            'employee_id' => $item->employee_id,
            'loan_start_date' => $item->loan_start_date,
            'loan_end_date' => $item->loan_end_date,
            'amount_per_day' => $item->amount_per_day,
            'amount_total' => $item->amount_total,
            'amount_missing' => $item->amount_missing
        ]);
    }

    public function testUserCanEditItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();
        $newItem = factory($this->model)->make();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->within("tr:contains('$item->name')", function() { $this->click('edit'); })
            ->see($this->editTitle(""))

            ->seeIsSelected('employee_id', $item->employee_id)
            ->seeInField('loan_start_date', $item->loan_start_date)
            ->seeInField('loan_end_date', $item->loan_end_date)
            ->seeInField('amount_per_day', $item->amount_per_day)
            ->seeInField('amount_total', $item->amount_total)
            ->seeInField('amount_missing', $item->amount_missing)

            ->select($newItem->employee_id, 'employee_id')
            ->type($newItem->loan_start_date, 'loan_start_date')
            ->type($newItem->loan_end_date, 'loan_end_date')
            ->type($newItem->amount_per_day, 'amount_per_day')
            ->type($newItem->amount_total, 'amount_total')
            ->type($newItem->amount_missing, 'amount_missing')

            ->press($this->updateButton())
            ->see($this->indexTitle())
            ->see($newItem->name);

        $this->seeInDatabase($this->table ?? $this->item, [
            'employee_id' => $newItem->employee_id,
            'loan_start_date' => $newItem->loan_start_date,
            'loan_end_date' => $newItem->loan_end_date,
            'amount_per_day' => $newItem->amount_per_day,
            'amount_total' => $newItem->amount_total,
            'amount_missing' => $newItem->amount_missing
        ]);
    }

    public function testUserCanDeleteItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->within("tr:contains('{$item->employee->displayName}')", function() { $this->press('delete'); })
            ->see($this->indexTitle())
            ->dontSee($item->employee->displayName);

        $this->dontSeeInDatabase($this->table ?? $this->item, [
            'id' => $item->id
        ]);
    }
}
