<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\UserCreateRequest;
use App\Http\Requests\Dashboard\UserUpdateRequest;
use App\Models\Partner;
use App\Models\Photo;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;

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

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

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

        $inputs = $request->all();
        $inputs['password'] = bcrypt($request->password);

        if ($request->photo) {
            $inputs['photo'] = $request->photo;
        } else {
            $inputs['photo'] = url('storage/1/download (1).jpg');
        }

        $partner = Partner::create($inputs);

        session()->flash('partner_create_message', awtTrans('تم الاضافة بنجاح'));

        return redirect('admin/partners');
    }

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

        $partner = Partner::findOrFail($id);
        return view('admin.partners.show', compact('user'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $partner = Partner::findOrFail($id);
        return view('admin.partners.edit', compact('partner'));
    }

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

        if (trim($request->password) == '') {
            $inputs = $request->except('password');
        } else {
            $inputs = $request->all();
            $inputs['password'] = bcrypt($request->password);
        }

        if ($request->photo) {
            $inputs['photo'] = $request->photo;
        }

        $partner->update($inputs);
        session()->flash('partner_update_msg', awtTrans('تم التعديل بنجاح'));
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $partner = Partner::findOrFail($id);

        $partner->delete();

        return response()->json();
    }
    // bluckDelete
    public function bluckDelete(Request $request)
    {

        $partners = Partner::findOrFail($request->ids);

        foreach ($partners as $partner) {
            $partner->delete();
        } // end foeach

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