<?php

namespace App\Http\Controllers;

use App\Church;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Municipio;


class ChurchController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if($request->ajax()){
            return response()->json(Church::with(['member','estado','municipio'])->get());
        }else{
            return redirect()->route('dashboard');
        }
    }

    public function store(Request $request){
        //try{
            $postData = $request->all();
            $current = 0;
            if(array_key_exists("current",$postData) && $postData['current']!="0"){
                \DB::table('churchs')->update(array('current' => 0));
                $current = 1;
            }
            $municipio = null;
            if(array_key_exists("otro",$postData) && $postData['otro']!=""){
                $tmp = ucfirst(strtolower(trim($postData['otro'])));
                $existe = Municipio::where('name',$tmp)->get();
                if(count($existe)==0){
                    $m = new Municipio();
                    $m->name = $tmp;
                    $m->save();
                    $municipio = $m->id;
                }else{
                    $municipio = $existe->first()->id;
                }
            }else{
                $municipio = $postData['city'];
            }
            $c = new Church();
            $c->name = $postData['name'];
            $c->address = $postData['address'];
            $c->colony = $postData['colony'];
            $c->municipio_id = $municipio;
            $c->estado_id = $postData['state'];
            $c->country = "Mexico";
            $c->postal_code = $postData['postal_code'];
            $c->member_id = $postData['member'];
            $c->current = $current;
            $c->save();
            return response()->json($c);
        //}catch (\Exception $e) {
    	//	return response()->json("Error no se pudo guardar", 400);
    	//}
    }

    public function update(Request $request,$id){
    	//try{
            $postData = $request->all();
            $current = 0;
            if(array_key_exists("current",$postData) && $postData['current']!="0"){
                \DB::table('churchs')->update(array('current' => 0));
                $current = 1;
            }
            $municipio = null;
            if(array_key_exists("otro",$postData) && $postData['otro']!=""){
                $tmp = ucfirst(strtolower(trim($postData['otro'])));
                $existe = Municipio::where('name',$tmp)->get();
                if(count($existe)==0){
                    $m = new Municipio();
                    $m->name = $tmp;
                    $m->save();
                    $municipio = $m->id;
                }else{
                    $municipio = $existe->first()->id;
                }
            }else{
                $municipio = $postData['city'];
            }
            $c = Church::findOrFail($id);
            $c->name = $postData['name'];
            $c->address = $postData['address'];
            $c->colony = $postData['colony'];
            $c->municipio_id = $municipio;
            $c->estado_id = $postData['state'];
            $c->postal_code = $postData['postal_code'];
            $c->member_id = $postData['member'];
            $c->current = $current;
            $c->save();
            return response()->json($c);
        //}catch (\Exception $e) {
    	//	return response()->json(null, 400);
    	//}
    }

    public function destroy($id){
    	try{
            $c = Church::findOrFail($id);
            $c->delete();
            return response()->json(true);
        }catch(\Exception $e){
            return response()->json(null, 400);
        }
    }

}