<?php

namespace App\Http\Controllers\Admin;

use App\Models\PhotoGroupCate;
use Illuminate\Http\Request;

class PhotoGroupCateController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = PhotoGroupCate::get();

        return view('admin.photo_group.cate', compact('data'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if (config('managerLang') != config('setting.lang')) {
            return back()->withInput()->with('error', '请在主语言添加内容！');
        }

        $this->validate($request,
            ['name' => 'required'],
            ['name.required' => '标题不能为空！']
        );

        $result  = PhotoGroupCate::create($request->all());

        if ($result) {
            \App\Models\SystemLog::set("发布图文分类成功[id=". $result->id ."]");
            return redirect('admin/photo-group-cate')->with('success', '添加分类成功');
        }

        return back()->withInput()->with('error', '添加分类失败');
    }

    /**
     * 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)
    {
        $data  = PhotoGroupCate::find($id);

        return view('admin.photo_group.cate_edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,
            ['name' => 'required'],
            ['name.required' => '标题不能为空！']
        );

        $result  = PhotoGroupCate::find($id)->update($request->all());

        if ($result) {
            \App\Models\SystemLog::set("编辑图文分类成功[id=". $id ."]");
            return redirect('admin/photo-group-cate')->with('success', '修改成功');
        }

        return back()->withInput()->with('error', '修改失败');
    }

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

        foreach ($ids as $val) {
            if (PhotoGroupCate::find($val)->pages->count() > 0 ) {
                return response()->json(['status' => 'error', 'message' => '所删分类中含有内容，请先转移或删除内容！']);
            }
        }

        $res = PhotoGroupCate::whereIn('id', $ids)->delete();

        if ($res) {
            \App\Models\SystemLog::set("删除图文分类成功[id=". implode(',', $ids) ."]");
            return response()->json(['status' => 'sucess', 'message' => '分类删除成功！']);
        }

        return response()->json(['status' => 'error', 'message' => '分类删除失败！']);
    }
}
