<?php

namespace App\Http\Controllers;

use App\Models\HomePage;
use Illuminate\Http\Request;
use App\Models\Classify;

class ClassifyController extends Controller
{

    public function list(Request $request)
    {
        return Classify::paginate(100);
    }

    public function setHome(Request $request) {
        $introduce = $request->input("introduce");

        if($introduce) {
            $homePage = HomePage::find(1);
            $homePage->introduce = $introduce;
            $homePage->save();

            return response()->json([
                "status" => 0,
                "message" => '修改成功'
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    /**
     * 每页20进行分页
     * @param Request $request
     * @return mixed
     */
    public function home(Request $request)
    {
        $top = Classify::where("location", "top")->orderBy("updated_at", "desc")->limit(5)->get()->toArray();
        //$bottom = Classify::where("location", "bottom")->orderBy("updated_at", "desc")->first();
        $homePage = HomePage::find(1);
        $tops = array_map(function ($t) {
            return [
                "id" => $t["id"],
                "server" => $t["server"],
                "content" => $t["content"],
                "photo" => $t["photo"],
                "location" => $t["location"]
            ];
        }, $top);

        return Response()->json([
            "top" => $tops,
            "introduce" => $homePage->bottom_introduce,
            "bottom" => [
                "id" => $homePage->id,
                "server" => "",
                "content" => $homePage->content,
                "photo" => $homePage->photo,
                "location" => "bottom"
            ]
        ]);
    }


    /**
     * 获取某个记录
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request,  $id)
    {
        $id = $request->route("id");

        $data = [];
        if ($id) {
            $record = Classify::find($id);
            if ($record) {
                $data["api"] = $record->api;
                $data["fields"] = json_decode($record->fields_json, true);
                return Response()->json([
                    "status" => 0,
                    "data" => [
                        "id" => $record->id,
                        "pid" => $record->pid,
                        "name" => $record->name,
                        "info" => $record->info,
                        "content" => $record->summary,
                        "photo" => $record->photo,
                        "api" => $data["api"],
                        "fields" => $data["fields"],
                        "type" => $record->name,
                    ]
                ]);
            }
            return Response()->json([
                "status" => -2,
                "message" => "不存在的记录！"
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "id不能为空!"
            ]);
        }
    }

    /**
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function server_content(Request $request, $id) {

        $id = $request->route("id");
        if ($id) {
            $record = Classify::find($id);
            if ($record) {
                return Response()->json([
                    "status" => 0,
                    "data" => [
                        "id" => $record->id,
                        "server" => $record->server,
                        "content" => $record->content,
                        "photo" => $record->photo,
                        "location" => $record->location,
                    ]
                ]);
            }
            return Response()->json([
                "status" => -2,
                "message" => "不存在的记录！"
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "id不能为空!"
            ]);
        }
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $id = $request->input('id');
        $pid = $request->input('pid');
        $name = $request->input('name');
        $info = $request->input('info');
        $summary = $request->input('summary');
        $server = $request->input('server');
        $content = $request->input('content');
        $photo = $request->input('photo');
        $location = $request->input('location');
        $fields_json = $request->input('fields_json');
        $api = $request->input('api');
        if ($id) {
            $classify = Classify::find($id);
        } else {
            $classify = new Classify;
            if(!$api) {
                $classify->api = "option_order/store";
            }
        }
        if ($pid && $name && $info && $summary && $server && $content && $photo && $location) {
            $classify->pid = $pid;
            $classify->name = $name;
            $classify->info = $info;
            $classify->summary = $summary;
            $classify->server = $server;
            $classify->content = $content;
            $classify->photo = $photo;
            $classify->location = $location;
            $classify->fields_json = $fields_json;
            $classify->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!"
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    public function parent(Request $request) {
        $classifies = Classify::where("pid", 0)->get();
        return Response()->json([
            "status" => 0,
            "data" => $classifies
        ]);
    }

    public function list2(Request $request) {
        return Classify::where("pid", ">", 0)->paginate(100);;
    }
}
