<?php

namespace App\Http\Controllers\Admin\Base;

use App\Http\Controllers\Admin\Controller;
use App\Models\AlbumManufacturerModel;
use App\Services\Base\Attachment;
use App\Models\BaseAttachmentModel;
use App\Models\BaseSettingsModel;
use App\Models\ClassModel;
use Illuminate\Http\Request as HttpRequest;
use Request;
use Storage;
use File;
use Auth;

class PhotosController extends Controller
{
    private $_serviceAttachment;

    public function __construct()
    {
        if( !$this->_serviceAttachment ) $this->_serviceAttachment = new Attachment();
    }

    public function index(HttpRequest $request)
    {
        $userAuth = Auth::guard('admin')->user();

        $a_class = $request->has('class') ? $request->input('class') : null;

        if ($userAuth->is_root == 1) {
            $classes = ClassModel::all();
            if (($a_class = ClassModel::find($a_class)) == null) {
                $a_class = ClassModel::first();
            }
        } else {
            $classes = ClassModel::where('store_id',$this->getStoreId())->get();
            if (($a_class = ClassModel::find($a_class)) == null) {
                $a_class = ClassModel::where('store_id', $this->getStoreId())->first();

            }
        }
        if (!$a_class) {
            $sys = AlbumManufacturerModel::where('store_id', $this->getStoreId())->first();
            $create['store_id'] = $this->getStoreId();
            $create['class'] = $sys->name ?? '未分类';
            //dd($create);
            $a_class = ClassModel::create($create);
        }
            $photos = null;
        $status = 'normal';

        if ($request->has('search')) {
            $name = '%' . $request->input('search') . '%';
            if ($userAuth->is_root == 1) {
                $photos = BaseAttachmentModel::where([['name', 'like', $name]])->paginate(24);
            } else {
                $photos = BaseAttachmentModel::where([['name', 'like', $name],['store_id', $this->getStoreId()]])->paginate(24);
            }

            $status = 'search';
        } else {
            if ($a_class) {
                if ($userAuth->is_root == 1) {
                    $photos = BaseAttachmentModel::where([['class', $a_class->class]])->paginate(24);
                } else {

                    $photos = BaseAttachmentModel::where([['class', $a_class->class],['store_id', $this->getStoreId()]])->paginate(24);
                }
            }
        }
    	return view('admin.base.photos.index', compact('photos', 'classes', 'a_class', 'photos', 'status', 'photo_compress_quality'));
    }

    public function edit(HttpRequest $request)
    {
        if(Request::method() != 'POST') {
            return back();
        }

        if($request->has('img-name') && $request->has('img-id')) {
            $photo = BaseAttachmentModel::find($request->input('img-id'));
            $photo->name = $request->input('img-name');
            $photo->save();
        }

        return back();
    }

    public function move(HttpRequest $request)
    {
        if(Request::method() != 'POST') {
            return back();
        }

        $ids = explode(',', $request->input('ids'));
        $class = ClassModel::find($request->input('class'));
        BaseAttachmentModel::whereIn('id', $ids)->update(['class' => $class->class]);
        return back();
    }

    public function delete(HttpRequest $request)
    {
        if(Request::method() != 'POST') {
            return back();
        }

        $ids = explode(',', $request->input('ids'));
        $photos = BaseAttachmentModel::find($ids);
        foreach($photos as $photo) {
            File::delete($photo->path);
            $photo->delete();
        }
        return back();
    }

    public function crop(HttpRequest $request)
    {
        if(Request::method() != 'POST') {
            return back();
        }

        $this->_serviceAttachment->localUpload('file', $request->all());

        return response()->json(['status' => 'ok'], 200);
    }
}
