<?php

namespace App\Http\Controllers\Site;

use App\Http\Controllers\Controller;
use App\Models\Site\NeedHome;
use App\Models\Site\NeedHomePerson;
use Illuminate\Http\Request;

class NeedHomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $needHome = new NeedHome();
        $needHome->no = rand(10000000,99999999);
        $needHome->name = $request->name;
        $needHome->telephone = $request->telephone;
        $needHome->city_id = $request->city_id;
        $needHome->town_id = $request->town_id;
        $needHome->district_id = $request->district_id;
        $needHome->street_id = $request->street_id;
        $needHome->apartment_no = $request->apartment_no;
        $needHome->floor = $request->floor;
        $needHome->apartment = $request->apartment;
        $needHome->save();

        if (isset($request->person_name)){
            foreach ($request->person_name as $key => $item){
                if ($item){
                    $needHomePerson = new NeedHomePerson();
                    $needHomePerson->need_home_id = $needHome->id;
                    $needHomePerson->name = $request->person_name[$key];
                    $needHomePerson->telephone = $request->person_telephone[$key];
                    $needHomePerson->save();
                }
            }
        }
        if ($needHome)
            return redirect()->route('site.applicant_completed',['no'=>$needHome->no]);
        else
            return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Site\NeedHome  $needHome
     * @return \Illuminate\Http\Response
     */
    public function show(NeedHome $needHome)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Site\NeedHome  $needHome
     * @return \Illuminate\Http\Response
     */
    public function edit(NeedHome $needHome)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Site\NeedHome  $needHome
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, NeedHome $needHome)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Site\NeedHome  $needHome
     * @return \Illuminate\Http\Response
     */
    public function destroy(NeedHome $needHome)
    {
        //
    }
}
