<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Identitas;

class IdentityController extends Controller
{
	public function index()
	{
		$data = Identitas::all();
		return view('identity.daftar_identitas')->with('data',$data);
	}

	public function create()
	{
		return view('identity.buat_identitas');
	}

	public function store(Request $request)
	{
		Identitas::create($request->all());
		return redirect('identity');
	}
	public function edit($id)
	{
		$data = Identitas::find($id);
		return view('identity.edit_identitas')->with('data',$data);
	}
	public function update(Request $request, $id)
	{
		Identitas::find($id)->update($request->all());
		return redirect('identity');
	}
	public function destroy($id)
	{
		Identitas::find($id)->delete();
		return redirect('identity');
	}
	public function search(Request $request)
	{
		if($request->ajax())
		{
			$outputHtml = "";
			$a = Identitas::where('fullnames','LIKE','%'.$request->search.'%')->get();
			if($a)
			{
				foreach ($a as $key => $a) {
					$outputHtml.='<tr>'.
					'<td>'.$a->id.'</td>'.
					'<td>'.'<img src="images/'.$a->image.'"style="max-width: 10em; max-height: 10em;">'.'</td>'.
					'<td>'.$a->fullnames.'</td>'.
					'<td>'.$a->sex.'</td>'.
					'<td>'.$a->idcard.'</td>'.
					'<td>'.$a->birthdate.'</td>'.
					'<td>'.$a->province.'</td>'.
					'<td>'.$a->district.'</td>'.
					'<td>'.$a->sector.'</td>'.
					'<td>'.$a->cellar.'</td>'.
					'<td>'.'<a href="identity/'.$a->id.'/edit" class="btn btn-success" title="edit '.$a->fullnames.'"><i class="glyphicon glyphicon-edit"></i></a>'.
					'<form action="identity/'.$a->id.'" method="post">'.
					csrf_field().
					'<br/><input type="hidden" name="_method" value="delete"><button type="submit" class="btn btn-danger" title="delete '.$a->fullnames.'"><i class="glyphicon glyphicon-trash"></i></button></form>'.
					'</td>'.
					'<tr>';
				}
				return Response($outputHtml);
			}
		}
	}
}
