<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\HousekeeperOrder;

class HousekeeperOrderController extends Controller
{

    /**
     * 创建保姆订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $id = $request->input('id');
        //$user = session('wechat.oauth_user.default');
        //$wechat_id = $user->getId();
        $wechat_id = $request->header("openid");
        if(!$wechat_id) {
            $wechat_id = $request->input("openid");
        }
        $contents = $request->input('contents');
        $people_num = $request->input('people_num');
        $server_time = $request->input('server_time');
        $rest = $request->input('rest');
        $area = $request->input('area');
        $address = $request->input('address');
        $contact = $request->input('contact');
        $phone = $request->input('phone');
        $status = $request->input('status');
        $price = $request->input('price');
        //$photo = $request->input('photo');
        if ($id) {
            $housekeeperOrder = HousekeeperOrder::find($id);
            $housekeeperOrder->status = $status;
            $housekeeperOrder->price = $price;

        } else {
            $housekeeperOrder = new HousekeeperOrder;
            $housekeeperOrder->status = 0;
            $housekeeperOrder->price = 0;
        }
        if ($wechat_id && $contents && $people_num && $server_time && $rest && $area && $address && $contact && $phone) {
            $housekeeperOrder->contents = $contents;
            $housekeeperOrder->people_num = $people_num;
            $housekeeperOrder->server_time = $server_time;
            $housekeeperOrder->rest = $rest;
            $housekeeperOrder->area = $area;
            $housekeeperOrder->address = $address;
            $housekeeperOrder->contact = $contact;
            $housekeeperOrder->phone = $phone;
            $housekeeperOrder->wechat_id = $wechat_id;
            $housekeeperOrder->photo = "https://wechat.sotype.com/storage/gOtuHM3GvKva6x68N340DJuN6cX1oIfaAhWi8yoq.jpeg";
            $housekeeperOrder->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!",
                "data" => $housekeeperOrder
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    /**
     * 每页20进行分页
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        return HousekeeperOrder::paginate(20);
    }
}
