<?php

namespace App\Http\Controllers;

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

class HouseCleanOrderController 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");
        }
        $type = $request->input('type');
        $area = $request->input('area');
        $picture1 = $request->input('picture1');
        $picture2 = $request->input('picture2');
        $picture3 = $request->input('picture3');
        $picture4 = $request->input('picture4');
        $picture5 = $request->input('picture5');
        $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) {
            $houseCleanOrder = HouseCleanOrder::find($id);
            $houseCleanOrder->status = $status;
            $houseCleanOrder->price = $price;
        } else {
            $houseCleanOrder = new HouseCleanOrder;
            $houseCleanOrder->status = 0;
            $houseCleanOrder->price = 0;
        }
        if ($wechat_id && $type && $area && $address && $contact && $phone) {
            $houseCleanOrder->type = $type;
            $houseCleanOrder->area = $area;
            $houseCleanOrder->address = $address;
            $houseCleanOrder->contact = $contact;
            $houseCleanOrder->phone = $phone;
            $houseCleanOrder->picture1 = $picture1;
            $houseCleanOrder->picture2 = $picture2;
            $houseCleanOrder->picture3 = $picture3;
            $houseCleanOrder->picture4 = $picture4;
            $houseCleanOrder->picture5 = $picture5;
            $houseCleanOrder->wechat_id = $wechat_id;
            $houseCleanOrder->photo = "https://wechat.sotype.com/storage/gOtuHM3GvKva6x68N340DJuN6cX1oIfaAhWi8yoq.jpeg";
            $houseCleanOrder->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!",
                "data" => $houseCleanOrder
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

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