<?php

namespace App\Http\Controllers\Configuration;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Controllers\Utils\PaginatorController;
use stdClass;

class CompanyController extends Controller
{

    protected  $route = 'configurations.company';
    public function index() {

        $exampledata = [
            [
                'id' => 1,
                'company_thainame' => 'บริษัทเน็ตเบย์ จำกัด มหาชน',
                'company_engname' => 'Netbay'
            ]
            ,  
            [
                'id' => 2,
                'company_thainame' => 'บริษัทเน็ตเบย์ จำกัด มหาชน3',
                'company_engname' => 'Netbay2'
            ],
            [
                'id' => 3,
                'company_thainame' => 'บริษัทเน็ตเบย์ จำกัด มหาชน3',
                'company_engname' => 'Netbay3'
            ],
            [
                'id' => 4,
                'company_thainame' => 'บริษัทเน็ตเบย์ จำกัด มหาชน4',
                'company_engname' => 'Netbay4'
            ],
            [
                'id' => 5,
                'company_thainame' => 'บริษัทเน็ตเบย์ จำกัด มหาชน5',
                'company_engname' => 'Netbay5'
            ],
            ];
        
            $companies = PaginatorController::paginate($exampledata, 3);
            $url = '/configurations/company';
            $route = $this->route;
            $companies->withPath(  $url );

            $searchBy = [ 
                "tax" => "Tax No.",
                "phone" => "Phone",
                "company_thainame" => "Thai Name", 
                "company_engname" => "English Name", 
            ];
            return view('configurations.companies.index', compact('companies', 'url', 'route', 'searchBy'));
    }


    public function create()
    {
        $route = $this->route;
        return view('configurations.companies.create' , compact('route'));
    }

    public function store(Request $request)
    {
        $rules = array(
            'name'       => 'required',
            'password'       => 'required',
            'email'      => 'required|email',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        } else {
          
            $user = new User;
            $user->password = Hash::make($request->input('password'));
            $user->name = $request->input('name');
            $user->email = $request->input('email');
            $user->save();

        return redirect()->route('configurations.company')
            ->withSuccess(__('Company created successfully.'));
        }
    }

    public function show(User $user)
    {
        return view('configurations.companies.show', [
            'user' => $user
        ]);
    }

    public function edit(User $user)
    {
        $company =  [
            'id' => 4,
            'companyname' => 'Netbay3',
            'groupname' => 'Admin4',
            'admingroup' => 'Y'
        ];
        
        return view('configurations.companies.edit', [
            'company' => $company,
            'route' => $this->route
        ]);
    }

    public function update(Request $request, User $user)
    {
        $rules = array(
            'name'       => 'required',
            'email'      => 'required|email',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        } else {
            $user->name = $request->input('name');
            $user->email = $request->input('email');
            $user->save();

        return redirect()->route('configurations.company')
            ->withSuccess(__('Company updated successfully.'));
        }
    }
    public function destroy(User $user)
    {
        $user->delete();

        return redirect()->route('configurations.company')
            ->withSuccess(__('Company deleted successfully.'));
    }
}
