<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\ProjectRequest;
use App\Models\Category;
use App\Models\ManufacturingCompany;
use App\Models\Project;
use App\Models\User;
use Illuminate\Http\Request;

class ProjectsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $projects = Project::all();
        return view('admin.projects.index', compact('projects'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $customers = User::whereRelation('roles', 'name', 'customer')->pluck('name', 'id')->all();
        $companies = ManufacturingCompany::pluck('name_en', 'id')->all();
        return view('admin.projects.create', compact('customers', 'companies'));
    }

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

        $inputs = $request->except('photo');

        $inputs['photo'] = $request->photo ? $request->photo : 'http://127.0.0.1:8000/storage/1/download.jpeg';

        $project = Project::create($inputs);

        if ($request->images > 0) {
            foreach (explode(',', $request->images) as $image) {
                $project->images()->create([
                    'image' => $image,
                ]);
            }
        }

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

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

    /**
     * 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)
    {

        $customers = User::whereRelation('roles', 'name', 'customer')->pluck('name', 'id')->all();
        $companies = ManufacturingCompany::pluck('name_en', 'id')->all();

        $project = Project::findOrFail($id);
        return view('admin.projects.edit', compact('project', 'companies', 'customers'));
    }

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

        $inputs = $request->all();

        $project = Project::findOrFail($id);
        $project->update($inputs);


        if ($request->images > 0) {
            $project->images()->delete();
            foreach (explode(',', $request->images) as $image) {
                $project->images()->create([
                    'image' => $image,
                ]);
            }
        }

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

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

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

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

        $projects = Project::findOrFail($request->ids);

        foreach ($projects as $project) {
            $project->delete();
        } // end foeach

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