<?php

namespace App\Http\Controllers;

use App\Company;
use App\User;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function check($id)
    {
        $exists = Company::where('user_id', $id)->exists();
        return response()->json(['exists' => $exists]);
    }

    public function config($id)
    {
        $company = User::where('id', $id)->get();
        return response()->json(['company' => $company]);
    }

    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)
    {
        $imageCompany = null;
        $imageMap = null;
        if ($request->get('imageCompany') !== 'https://asianinteriorservices.com/wp-content/uploads/2018/04/noImg.png') {
            $image = $request->get('imageCompany');
            $name = 'Company'.time().'.'.explode('/', explode(':', substr($image, 0, strpos($image, ';')))[1])[1];
            \Image::make($request->get('imageCompany'))->save(storage_path('uploads/').$name);
            $imageCompany = $name;
        }
        if ($request->get('imageMap')) {
            $image = $request->get('imageMap');
            $name = 'Map'.time().'.'.explode('/', explode(':', substr($image, 0, strpos($image, ';')))[1])[1];
            \Image::make($request->get('imageMap'))->save(storage_path('uploads/').$name);
            $imageMap = $name;
        }
        $company = new Company();
        $company->imageCompany = $imageCompany;
        $company->company_name = $request->get('company_name');
        $company->company_owner = $request->get('company_owner');
        $company->phone_no = $request->get('phone_no');
        $company->email = $request->get('email');
        $company->fax = $request->get('fax');
        $company->website = $request->get('website');
        $company->addr = $request->get('addr');
        $company->tambon = $request->get('tumbon');
        $company->amphoe = $request->get('amphoe');
        $company->province = $request->get('province');
        $company->zipcode = $request->get('zip_code');
        $company->imageMap = $imageMap;
        $company->lat = $request->get('lat');
        $company->long = $request->get('lng');
        $company->about = $request->get('about');
        $company->bts = $request->get('bts');
        $company->mrt = $request->get('mrt');
        $company->arl = $request->get('arl');
        $company->bus = $request->get('bus');
        $company->another = $request->get('another');
        $company->user_id = $request->get('user_id');
        $company->save();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $exists = Company::where('user_id', $id)->exists();
        if ($exists) {
            $data = Company::where('user_id', $id)->get();
        } else {
            $data = User::where('id', $id)->get();
        }
        return response()->json(['check' => $exists, 'data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
