<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request; 
use App\Member;
use DB;
 
 class LivesearchController extends Controller
 { 
    function index()
    { 
        return view('livesearch.index'); 
    } 
 
    function action(Request $request) 
    { 
        if($request->ajax()) 
        { 
			$ada_data = true;
            $query = $request->get('query'); 
			
            if($query != '') 
            { 
		
				//cari mbr_no terlebih dulu
				$member = DB::table('member')->where('mbr_no', '=', $query)->get();
				$total_mbr = $member->count();

				if ($total_mbr>0)
				{ 
				
					$row = array();
					foreach($member as $list) 
					{ 
						$a = $list->tl_point;
						$b = $list->tl_tukar;
						$c = $a - $b;
						

						$row[0] =$list->mbr_no;
						$row[1] =$list->mbr_nama;
						$row[2] =$list->mbr_nohp;
						$row[3] =format_uang($c);
					}
				}
				else
					{ 
						$row[0]='';
						$row[1]= 'No Data Found ';
						$row[2]='';
						$row[3]=0;
					} 
				
			}
			
			$data = array( 'mbr_no' => $row[0], 'mbr_nama' => $row[1], 'mbr_nohp' => $row[2], 'mbr_point' =>$row[3] ); 
			echo json_encode($data); 
				
		}
	} 
}