<?php

namespace App\Http\Controllers;

use App\DataTables\OfficesDataTable;
use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\Office;
use Illuminate\Http\Request;

class OfficeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(OfficesDataTable $dataTable)
    {
        if(auth()->user()->can('show_offices')){
            
            return $dataTable->render('offices.index');
        }
        else{
             return abort(404);

        }
    }

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

   /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$this->validate($request,[
            'name'=>'nullable|string|max:191',
            'address'=>'required|string|max:2000',
            'email'=>'required|email|unique:offices',
            'phone'=>'required|string|max:191',
            'booking_commission'=>'required|numeric',
            'exchange_commission'=>'required|numeric',
            'canellation_commission'=>'required|numeric',
            'employers_number'=>'required|numeric',
            'image' => 'image',
            'currency_code'=>'required'
        ]);
        $data['status']=$request->status?true:false;
        if($request->image){
            $data['image'] = time().'.'.$request->image->extension();  
            $request->image->move(public_path('uploads'), $data['image']);
        }
        $data['is_admin']=$request->is_admin;

        $office = Office::create($data);

        Account::create([
            'number' => time(),
            'balance' => 0,
            'currency_code'=>$request->currency_code??config('currency.default'),
            'office_id' => $office->id,
            'state' => true
        ]);

        return redirect()->route('offices.index')->with('success', __('Task was successful'));

    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Office  $office
     * @return \Illuminate\Http\Response
     */
    public function edit(Office $office)
    {
        return view('offices.edit',['office'=>$office]);
    }

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

        $data=$this->validate($request,[
            'name'=>'nullable|string|max:191',
            'address'=>'required|string|max:2000',
            'email'=>'required|email|unique:offices,email,'.$office->id,
            'phone'=>'required|string|max:191',
            'booking_commission'=>'required|numeric',
            'exchange_commission'=>'required|numeric',
            'canellation_commission'=>'required|numeric',
            'employers_number'=>'required|numeric',
            'image' => 'image',
        ]);
        $data['status']=$request->status?true:false;
        if($request->image){
            $data['image'] = time().'.'.$request->image->extension();  
            $request->image->move(public_path('uploads'), $data['image']);
            $this->destroyImage($office->image);
        }
        
        $data['is_admin']=$request->is_admin;
     
     
        $office->update($data);
        return redirect()->route('offices.index')->with('success', __('Task was successful'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Office  $office
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        
       $office= Office::find($id);
       if($office->is_admin==1 || $office->account()->exists() ){
           return back();
       }
      else{
            $image_name = $office->image;
            $office->delete();
            $this->destroyImage($image_name);
            return ['status'=>'success','message'=>__('Task was successful')];
      }
    }

    public function destroyImage($name)
    {
        $image_path = public_path() . '/uploads/' . $name;
        return @unlink($image_path);
    }
}
