<?php

namespace App\Http\Controllers;

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

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

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