<?php

namespace App\Http\Controllers;

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

class ProductOrderController extends Controller
{

    /**
     * 创建产品订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $id = $request->input('id');
        $products = $request->input('products');

        $wechat_id = $request->header("openid");
        $remark = $request->input('remark');
        $info = $request->input('info');
        $address = $request->input('address');
        $contact = $request->input('contact');
        $phone = $request->input('phone');
        $status = $request->input('status');
        if ($id) {
            $productOrder = ProductOrder::find($id);
            $productOrder->status = $status;

        } else {
            $productOrder = new ProductOrder;
            $productOrder->status = 1;
        }
        if ($wechat_id && $products && $address && $contact && $phone) {

            $productOrder->wechat_id = $wechat_id;
            $productOrder->remark = $remark;
            $productOrder->price = 0;
            $productOrder->info = $info;
            $productOrder->address = $address;
            $productOrder->contact = $contact;
            $productOrder->phone = $phone;
            $productOrder->wechat_id = $wechat_id;
            $productOrder->save();

            $total = 0;
            foreach ($products as $pid) {
                $id = $pid["id"];
                $amount = $pid["amount"];
                $product = Product::find($id);
                $total += $product->price;
                $productOrder->products()->attach($id,
                    [
                        "product_name" =>$product->name,
                        "product_color" => $product->color,
                        "product_amount" => $amount,
                        "product_price" => $product->price,
                        "product_photo" => $product->photo
                    ]);
                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
                $shopcar->delete();

            }
            $productOrder->price = $total;
            $productOrder->save();



            return Response()->json([
                "status" => 0,
                "message" => "保存成功!",
                "data" => $productOrder
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    /**
     * 每页20进行分页
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $keyword = $request->input('keyword');
        if($keyword) {
            $data = ProductOrder::with("products")->Where("contact", "like",  "%{$keyword}%")
                ->orWhere("remark", "like",  "%{$keyword}%")
                ->orWhere("info", "like",  "%{$keyword}%")
                ->orWhere("phone", "like",  "%{$keyword}%")
                ->paginate();
        } else {
            $data = ProductOrder::with("products")->paginate();
        }
        $data->appends(["keyword" => $keyword]);
        return $data;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function remark(Request $request)
    {
        $remark = $request->input('remark');
        $id = $request->input('id');
        if($id && $remark) {
            $record = ProductOrder::with("products")->find($id);
            $record->info = $remark;
            $record->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!",
                "data" => $record
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    /**
     * 删除产品订单
     * @param Request $request
     * @param $order_type
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request, $id)
    {
        $id = $request->route("id");
        if ($id) {
            $record = ProductOrder::find($id);
            if ($record) {
                $record->delete();
                DB::delete('delete from orders_products_mapping where order_id = ?', [$id]);
                return Response()->json([
                    "status" => 0,
                    "message" => "删除成功!"
                ]);
            }
            return Response()->json([
                "status" => -2,
                "message" => "不存在的记录！"
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }
}
