<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\CompanyScheduleRequest;
use App\Models\Category;
use App\Models\CompanySchedule;
use App\Models\ScheduleYear;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AdminCompanySchedulesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $company_schedules = CompanySchedule::all();
        return view('admin.company-schedules.index', compact('company_schedules'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $years = ScheduleYear::pluck('date', 'id')->all();
        return view('admin.company-schedules.create', compact('years'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CompanyScheduleRequest $request)
    {

        $inputs = $request->except('photo', 'keyword_ar', 'keyword_en');
        $inputs['photo'] = $request->photo ? $request->photo : 'http://127.0.0.1:8000/storage/1/download.jpeg';
        $user = Auth::user();

        $company_schedule = $user->company_schedules()->create($inputs);

        if ($request->keyword_ar) {
            foreach ($request->keyword_ar as $key => $keyword_ar) {
                $company_schedule->keywords()->create([
                    'body_ar' => $keyword_ar,
                    'body_en' => $request->keyword_en[$key],
                ]);
            }
        }

        session()->flash('company_schedule_action_msg', awtTrans('تم الاضافة بنجاح'));
        return redirect('/admin/company-schedules');
    }

    /**
     * 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)
    {
        //
        $years = ScheduleYear::pluck('date', 'id')->all();
        $company_schedule = CompanySchedule::findOrFail($id);
        $categories = Category::pluck('name_ar', 'id')->all();
        return view('admin.company-schedules.edit', compact('company_schedule', 'categories' , 'years'));
    }

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

        $inputs = $request->all();
        $user = Auth::user();

        $user->company_schedules()->whereId($id)->first()->update($inputs);

        $company_schedule = CompanySchedule::find($id);

        if ($request->keyword_ar) {
            $company_schedule->keywords()->delete();
            foreach ($request->keyword_ar as $key => $keyword_ar) {
                $company_schedule->keywords()->create([
                    'body_ar' => $keyword_ar,
                    'body_en' => $request->keyword_en[$key],
                ]);
            }
        }

        session()->flash('company_schedule_update_msg',  awtTrans('تم التعديل بنجاح'));
        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $company_schedule = CompanySchedule::findOrFail($id);
        $company_schedule->delete();
        return response()->json();
    }

    // bluckDelete
    public function bluckDelete(Request $request)
    {

        $company_schedules = CompanySchedule::findOrFail($request->ids);

        foreach ($company_schedules as $company_schedule) {
            $company_schedule->delete();
        } // end foeach

        session()->flash('company_schedule_action_success_msg', awtTrans('تم الحذف بنجاح'));
        return redirect()->back();
    }

    // addYear
    public function addYear(Request $request)
    {

        $year_current = ScheduleYear::create($request->all());

        $years = ScheduleYear::all();

        $type = $request->type;

        return view('admin.company-schedules.years', compact('years' , 'year_current' , 'type'))->render();

    }
}
