<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Traits\MediaUploadingTrait;
use App\Http\Requests\MassDestroyKegiatanpkapkRequest;
use App\Http\Requests\StoreKegiatanpkapkRequest;
use App\Http\Requests\UpdateKegiatanpkapkRequest;
use App\IdentitasKlien;
use App\Kegiatanpkapk;
use App\Referensi;
use App\Referensipkapk;

class KegiatanpkapkController extends Controller
{
    use MediaUploadingTrait;

    public function index()
    {
        abort_unless(\Gate::allows('kegiatanpkapk_access'), 403);

        $kegiatanpkapks = Kegiatanpkapk::all();

        return view('admin.kegiatanpkapks.index', compact('kegiatanpkapks'));
    }

    public function create()
    {
        abort_unless(\Gate::allows('kegiatanpkapk_create'), 403);

        $namas = Referensipkapk::all()->pluck('nama', 'id')->prepend(trans('global.pleaseSelect'), '');

        $kegiatans = Referensi::all()->pluck('detail_kegiatan', 'id')->prepend(trans('global.pleaseSelect'), '');

        $nama_kliens = IdentitasKlien::all()->pluck('nama', 'id')->prepend(trans('global.pleaseSelect'), '');

        return view('admin.kegiatanpkapks.create', compact('namas', 'kegiatans', 'nama_kliens'));
    }

    public function store(StoreKegiatanpkapkRequest $request)
    {
        abort_unless(\Gate::allows('kegiatanpkapk_create'), 403);

        $kegiatanpkapk = Kegiatanpkapk::create($request->all());

        foreach ($request->input('dokumen', []) as $file) {
            $kegiatanpkapk->addMedia(storage_path('tmp/uploads/' . $file))->toMediaCollection('dokumen');
        }

        return redirect()->route('admin.kegiatanpkapks.index');
    }

    public function edit(Kegiatanpkapk $kegiatanpkapk)
    {
        abort_unless(\Gate::allows('kegiatanpkapk_edit'), 403);

        $namas = Referensipkapk::all()->pluck('nama', 'id')->prepend(trans('global.pleaseSelect'), '');

        $kegiatans = Referensi::all()->pluck('detail_kegiatan', 'id')->prepend(trans('global.pleaseSelect'), '');

        $nama_kliens = IdentitasKlien::all()->pluck('nama', 'id')->prepend(trans('global.pleaseSelect'), '');

        $kegiatanpkapk->load('nama', 'kegiatan', 'nama_klien', 'team');

        return view('admin.kegiatanpkapks.edit', compact('namas', 'kegiatans', 'nama_kliens', 'kegiatanpkapk'));
    }

    public function update(UpdateKegiatanpkapkRequest $request, Kegiatanpkapk $kegiatanpkapk)
    {
        abort_unless(\Gate::allows('kegiatanpkapk_edit'), 403);

        $kegiatanpkapk->update($request->all());

        if (count($kegiatanpkapk->dokumen) > 0) {
            foreach ($kegiatanpkapk->dokumen as $media) {
                if (!in_array($media->file_name, $request->input('dokumen', []))) {
                    $media->delete();
                }
            }
        }

        $media = $kegiatanpkapk->dokumen->pluck('file_name')->toArray();

        foreach ($request->input('dokumen', []) as $file) {
            if (count($media) === 0 || !in_array($file, $media)) {
                $kegiatanpkapk->addMedia(storage_path('tmp/uploads/' . $file))->toMediaCollection('dokumen');
            }
        }

        return redirect()->route('admin.kegiatanpkapks.index');
    }

    public function show(Kegiatanpkapk $kegiatanpkapk)
    {
        abort_unless(\Gate::allows('kegiatanpkapk_show'), 403);

        $kegiatanpkapk->load('nama', 'kegiatan', 'nama_klien', 'team');

        return view('admin.kegiatanpkapks.show', compact('kegiatanpkapk'));
    }

    public function destroy(Kegiatanpkapk $kegiatanpkapk)
    {
        abort_unless(\Gate::allows('kegiatanpkapk_delete'), 403);

        $kegiatanpkapk->delete();

        return back();
    }

    public function massDestroy(MassDestroyKegiatanpkapkRequest $request)
    {
        Kegiatanpkapk::whereIn('id', request('ids'))->delete();

        return response(null, 204);
    }
}
