<?php

namespace App\Http\Controllers;

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

class MaternityMatronOrderController extends Controller
{

    public $classify6;

    public function __construct() {
        $this->classify6 = Classify::find(6);
    }

    /**
     * 创建保姆订单
     * @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");
        }
        $level = $request->input('options');
        $expected_date = $request->input('expected_date');
        $special_demand = $request->input('special_demand');
        $server_time = $request->input('server_time');
        $address = $request->input('address');
        $contact = $request->input('contact');
        $phone = $request->input('phone');
        $status = $request->input('status');
        $price = $request->input('price');
        $remark = $request->input('remark');
        if ($id) {
            $maternityMatronOrder = MaternityMatronOrder::find($id);
            $maternityMatronOrder->status = $status;
            $maternityMatronOrder->price = $price;

        } else {
            $maternityMatronOrder = new MaternityMatronOrder;
            $maternityMatronOrder->status = 0;
            $maternityMatronOrder->price = 0;

            $maternityMatronOrder->classify1 = "家政";
            $maternityMatronOrder->classify2 = "月嫂";
            $maternityMatronOrder->classify = 6;
        }
        if ($wechat_id && $level && $expected_date && $server_time && $special_demand && $address && $contact && $phone) {
            $maternityMatronOrder->level = $level;
            $maternityMatronOrder->expected_date = $expected_date;
            $maternityMatronOrder->special_demand = $special_demand;
            $maternityMatronOrder->remark = $remark;
            $maternityMatronOrder->server_time = $server_time;
            $maternityMatronOrder->address = $address;
            $maternityMatronOrder->contact = $contact;
            $maternityMatronOrder->phone = $phone;
            $maternityMatronOrder->wechat_id = $wechat_id;
            $maternityMatronOrder->photo = $this->classify6->photo;
            $maternityMatronOrder->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!",
                "data" => $maternityMatronOrder
            ]);
        } 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 = MaternityMatronOrder::where("special_demand", "like",  "%{$keyword}%")
                ->orWhere("contact", "like",  "%{$keyword}%")
                ->orWhere("phone", "like",  "%{$keyword}%")
                ->orWhere("remark", "like",  "%{$keyword}%")
                ->paginate();
        } else {
            $data = MaternityMatronOrder::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 = MaternityMatronOrder::find($id);
            $record->remark = $remark;
            $record->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!",
                "data" => $record
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }
}
