<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Shopcar;
use Illuminate\Support\Facades\DB;

class ShopcarController extends Controller
{
    /**
     * 添加购物车
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request) {
        $product_id = $request->input("product_id");
        $wechat_id = $request->header("openid");
        $amount = $request->input("product_amount");
        $id = $request->input("id");

        if(!$product_id) {
            return response()->json([
                "status" => -1,
                "error" => "请输入产品ID"
            ]);
        }
        if($id) {
            $shopcar = Shopcar::find($id);
        } else {
            $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
            if(!$shopcar) {
                $shopcar = new Shopcar;
            }
        }

        $shopcar->wechat_id = $wechat_id;
        $shopcar->product_id = $product_id;
        if($amount) {
            $shopcar->product_amount = $amount;
        } else {
            $shopcar->product_amount = $shopcar->product_amount + 1;
        }
        $shopcar->save();
        return response()->json([
            "status" => 0,
            "message" => "已加入购物车！"
        ]);

    }

    /**
     * 批量添加/修改购物车数量
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function pstore(Request $request) {

        $wechat_id = $request->header("openid");
        $products_prices = $request->input("data");
        foreach($products_prices as $data){
            if(isset($data["id"])) {
                $id = $data["id"];
            }
            $product_id = $data["product_id"];
            $amount = $data["product_amount"];
            if(isset($id)) {
                $shopcar = Shopcar::find($id);
            } else {
                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
                if(!$shopcar) {
                    $shopcar = new Shopcar;
                }
            }
            $shopcar->wechat_id = $wechat_id;
            $shopcar->product_id = $product_id;
            $shopcar->product_amount = $amount;

            $shopcar->save();
        }
        return response()->json([
            "status" => 0,
            "message" => "修改成功！"
        ]);

    }

    /**
     * 修改购物内产品数量
     * @param Request $request
     * @return mixed
     */
    public function amount(Request $request) {
        $id = $request->input("id");
        $product_id = $request->input("product_id");
        $wechat_id = $request->header("openid");
        $amount = $request->input("product_amount");
        if($id) {
            if($amount) {
                $shopcar = Shopcar::find($id);
                if($shopcar) {
                    $shopcar->product_amount = $amount;
                    $shopcar->save();

                    return response()->json([
                        "status" => 0,
                        "message" => "修改成功！"
                    ]);
                }
            } else {
                return response()->json([
                    "status" => -1,
                    "error" => "请输入产品数量"
                ]);
            }

        } else {
            if($product_id && $amount) {
                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
                if($shopcar) {
                    $shopcar->product_amount = $amount;
                    $shopcar->save();

                    return response()->json([
                        "status" => 0,
                        "message" => "修改成功！"
                    ]);
                }
            } else {
                return response()->json([
                    "status" => -1,
                    "error" => "请输入产品ID和数量"
                ]);
            }
        }

    }

    /**
     * 移除购物车
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request, $id) {
        $id = $request->route("id");
        $wechat_id = $request->header("openid");

        if($id) {
            $shopcar = Shopcar::find($id);
            if($shopcar) {
                $shopcar->delete();
                return response()->json([
                    "status" => 0,
                    "message" => "删除成功！"
                ]);
            }
        } else {
            return response()->json([
                "status" => -1,
                "error" => "请输入ID"
            ]);
        }
    }

    /**
     * 批量删除ids
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function pdelete(Request $request) {
        $ids = $request->input("ids");
        $wechat_id = $request->header("openid");

        if($ids) {
            foreach ($ids as $id) {
                $shopcar = Shopcar::find($id);
                if($shopcar) {
                    $shopcar->delete();
                }
            }
            return response()->json([
                "status" => 0,
                "message" => "批量删除成功！"
            ]);
        }
    }

    /**
     * 列出购物车列表
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request) {

        $wechat_id = $request->header("openid");

        $sql = 'select t1.*, t2.name, t2.price, t2.classify, t2.color, t2.status, t2.photo '
                . 'from shopcars t1 '
                . 'inner join '
                . 'products t2 '
                . 'on t1.product_id = t2.id '
                . 'where t1.wechat_id = ?';
        $shopcars = DB::select($sql, [$wechat_id]);

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

}
