<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\ProductPage;

class ProductController extends Controller
{
    /**
     * 创建活动
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {

        $id = $request->input('id');
        $name = $request->input('name');
        $info = $request->input('info');
        $classify = $request->input('classify');
        $color = $request->input('color');
        $price = $request->input('price');
        $status = $request->input('status');
        $show = $request->input('show');
        $photo = $request->input('photo');
        if ($id) {
            $product = Product::find($id);
            $product->status = $status;
            $product->show = 0;
        } else {
            $product = new Product;
            $product->status = 0;
            $product->collect_num = 0;
            $product->sell_num = 0;
        }
        if ($name && $info && $classify && $color) {
            $product->name = $name;
            $product->info = $info;
            $product->color = $color;
            $product->price = $price;
            $product->classify = $classify;
            $product->show = $show;
            $product->photo = $photo;
            $product->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!"
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    /**
     * 返回给后台列表
     * @param Request $request
     * @return mixed
     */
    public function manage_list(Request $request) {
        return Product::paginate(20);
    }

    /**
     * 按分类返回给小程序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        $products = Product::where("status", 0)->get()->toArray();
        $datas = array_map(function ($product) {
            return [
                "id" => $product["id"],
                "classify" => $product["classify"],
                "name" => $product["name"],
                "price" => $product["price"],
                "photo" => $product["photo"]
            ];
        }, $products);

        $data = [];
        foreach ($datas as $d) {
            if(!array_key_exists($d["classify"], $data)) {
                $data[$d["classify"]] = [];
            }
            array_push($data[$d["classify"]], $d);
        }
        return response()->json([
            "status" => 0,
            "data" => $data
        ]);
    }

    /**
     * 每页20进行分页
     * @param Request $request
     * @return mixed
     */
    public function home(Request $request)
    {
        $top = Product::where("show", 1)->orderBy("updated_at", "desc")->limit(5)->get()->toArray();

        $tops = array_map(function ($t) {
            return [
                "id" => $t["id"],
                "name" => $t["name"],
                "photo" => $t["photo"]
            ];
        }, $top);

        return Response()->json([
            "data" => $tops,
            "status" => 0,
        ]);
    }

    public function setHome(Request $request) {
        $top1 = $request->input("top1");
        $top2 = $request->input("top2");
        $top3 = $request->input("top3");

        if($top1 && $top2 && $top3 ) {
            $homePage = ProductPage::find(1);
            $homePage->top1 = $top1;
            $homePage->top2 = $top2;
            $homePage->top3 = $top3;
            $homePage->save();

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