<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\CustomerType;
use Auth;

class CustomerTypesController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    
	public function create()
	{	
		$type = CustomerType::all();
		return view('customer_type/create',compact('type'));
	}

	public function store(Request $request)
	{	
		
		
		if($request->get('type_id') != ''){
			$update = CustomerType::where('id','=',$request->get('type_id'))
								->update([
											'name' => strtoupper(trim($request->get('type'))),
											'is_active' => ($request->get('active')) ? 'Y' : 'N'
										]);
			return redirect()->route('customer_type.create')->with(['update' => true]);
		}
		else{
			$exist = CustomerType::where('name','=',strtoupper(trim($request->get('type'))))->count();
			
			if($exist != 0){
				return redirect()->route('customer_type.create')->with(['exist' => true]);
			}

			$insert = CustomerType::create([
											'name' => strtoupper(trim($request->get('type'))),
											'is_active' => ($request->get('active')) ? 'Y' : 'N'
										]);

			return redirect()->route('customer_type.create')->with(['message' => true]);
		}
		
	}

	public function edit(Request $request)
	{
		$id = $request->get('option');
		$type = CustomerType::find($id);

		return $type;
	}
}