<?php

namespace App\Http\Controllers;

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

class HousekeeperOrderController extends Controller
{
    public $classify_map = [];
    public $classify_name = [];

    public function __construct() {
        $classifies = Classify::all();
        foreach ($classifies as $classify) {
            $this->classify_map[$classify->id] = $classify;
            $this->classify_name[$classify->name] = $classify;
        }
    }

    /**
     * 创建保姆订单
     * @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');
        $notes = $request->input('notes');
        $status = $request->input('status');
        $price = $request->input('price');
        $photo = $request->input('photo');
        $classify = $request->input('type');
        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->notes = $notes;
            $housekeeperOrder->phone = $phone;
            if($classify) {
                $classify2 = $this->classify_name[$classify]->name;
                $pid = $this->classify_name[$classify]->pid;
                $classify1 = $this->classify_map[$pid]->name;
                $housekeeperOrder->classify1 = $classify1;
                $housekeeperOrder->classify2 = $classify2;
                $housekeeperOrder->classify = $this->classify_name[$classify]->id;
            }
            $housekeeperOrder->wechat_id = $wechat_id;
            $housekeeperOrder->photo = $photo;
            $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)
    {
        $classify = $request->input("classify");
        if($classify) {
            return HousekeeperOrder::where("classify", $classify)->paginate(20);
        }
        return HousekeeperOrder::paginate(20);
    }

    public function classify() {

    }
}
