<?php

namespace App\Http\Controllers\Admin;

use App\Models\Lang;
use App\Models\Link;
use Illuminate\Http\Request;
use App\Http\Requests\LinkRequest;
use App\Models\LinkDescription as Description;

class LinkController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = $request->except('page');
        $links = Link::with('langs')->where('lang', $request->input('lang', 'en'));
        $links = $request->has('title') ? $links->where('title', 'like', "%{$where['title']}%") : $links;
        $links = $links->oldest('orders')->paginate(20);
        $langs = Lang::where('buy', 1)->where('enabled', 1)->oldest('orders')->lists('name', 'slug')->toArray();

        return view('admin.link.link', compact('links', 'where', 'langs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $langs = Lang::where('slug', config('managerLang'))->lists('name', 'slug')->toArray();

        return view('admin.link.link_create', compact('langs'));
    }

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

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

        if ($result) {
            \App\Models\SystemLog::set("发布友情链接成功[id=". $result->id ."]");
            return redirect('admin/link')->with('success', '友情链接添加成功');
        }

        return back()->withInput($request->all())->with('error', '友情链接添加失败');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $langs = Lang::where('slug', config('managerLang'))->lists('name', 'slug')->toArray();
        $link = Link::find($id);

        return view('admin.link.link_edit', compact('link', 'langs'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param LinkRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(LinkRequest $request, $id)
    {
        $request->has('img') ?: $request->merge(['img' => null]);
        $request->has('nofollow') ?: $request->merge(['nofollow' => 0]);

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

        if ($result) {
            \App\Models\SystemLog::set("编辑友情链接成功[id=". $id ."]");
            return redirect('admin/link')->with('success', '友情链接修改成功');
        }

        return back()->withInput($request->all())->with('error', '友情链接修改失败');
    }

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

        return response()->json(['status' => $result ? 'sucess' : 'error']);
    }



}
