<?php

namespace App\Http\Controllers;

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

class CompanyController 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)
    {
        $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(public_path('uploads/').$name);
            $imageCompany = '/uploads/'.$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(public_path('uploads/').$name);
            $imageMap = '/uploads/'.$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)
    {
        $exists = Company::where('user_id', $id)->exists();
        $count = Job::where('user_id', $id)->count();
        if ($exists) {
            return response()->json(['exists' => true, 'company' => Company::where('user_id', $id)->first(), 'count' => $count]);
        } else {
            return response()->json(['exists' => false, 'company' => User::where('id', $id)->first(), 'count' => $count]);
        }
    }

    /**
     * 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) {
            return response()->json(['exists' => true, 'company' => Company::where('user_id', $id)->first()]);
        } else {
            return response()->json(['exists' => false, 'company' => User::where('id', $id)->first()]);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $company = Company::find($id);
        if ($request->get('imageCompany') !== $company->imageCompany) {
            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(public_path('uploads/').$name);
                $company->imageCompany = '/uploads/'.$name;
            } else {
                $company->imageCompany = 'https://asianinteriorservices.com/wp-content/uploads/2018/04/noImg.png';
            }
        }
        if ($request->get('imageMap') !== $company->imageMap) {
            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(public_path('uploads/').$name);
                $company->imageMap = '/uploads/'.$name;;
            } else {
                $company->imageMap = null;
            }
        }
        $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->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->save();
    }

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