<?php

namespace App\Http\Controllers\Admin;

use App\Models\Attribute;
use App\Models\ProductAttribute;
use App\Http\Requests\AttributeRequest;
use App\Models\AttributeDescription as Description;

class AttributeController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Attribute::with(['description' => function($query) {
            $query->where('lang', config('setting.lang'));
        }])->oldest('orders')->get();

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

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

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

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

        if ($result) {
            Description::attachDescription($request->input('description'), $result->id, 'attribute_id', true);
            event(new \App\Events\TranslateEvent('attribute', $result->id));
            \App\Models\SystemLog::set("发布属性成功[id=". $result->id ."]");
            return redirect('admin/attribute')->with('success', '添加属性成功');
        }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data         = Attribute::find($id);
        $descriptions = Description::where('attribute_id', $id)->get()->groupBy('lang');

        if (!isset($descriptions[config('managerLang')])) $descriptions[config('managerLang')] = $descriptions[config('setting.lang')];

        return view('admin.attribute.attribute_edit', compact('data', 'descriptions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param AttributeRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(AttributeRequest $request, $id)
    {
        $result  = Attribute::find($id)->update($request->all());
        Description::attachDescription($request->input('description'), $id, 'attribute_id');

        if ($result) {
            \App\Models\SystemLog::set("编辑属性成功[id=". $id ."]");
            event(new \App\Events\TranslateEvent('attribute', $id, true));
            return redirect('admin/attribute')->with('success', '修改属性成功');
        }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = Attribute::deletes($id, 'attribute_id');

        if ($result) {
            $ids = array_str($id);
            ProductAttribute::whereIn('attribute_id', $ids)->delete();
            return response()->json(['status' => 'sucess']);
        }

        return response()->json(['status' => 'error']);
    }
}
