<?php

namespace App\Http\Controllers;

use App\Models\OptionOrder;
use App\Models\OptionValue;
use App\Models\Classify;
use Illuminate\Http\Request;


class OptionOrderController 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;
        }
    }

    public function store(Request $request) {

        $wechat_id = $request->header("openid");

        $options = $request->input("options");
        $id = $request->input("id");
        $address = $request->input('address');
        $contact = $request->input('contact');
        $phone = $request->input('phone');
        $notes = $request->input('notes');
        $classify = $request->input('type');
        $price = $request->input('price');
        $status = $request->input('status');
        $remark = $request->input('remark');

        if($options && $address && $contact && $phone && $classify) {
            if(!$id) {
                $option_order = new OptionOrder;
                $option_order->status = 0;
                $option_order->price = 0;
            } else {
                $option_order = OptinOrder::find($id);
                $option_order->status = $status;
                $option_order->price = $price;
            }
            $option_order->address = $address;
            $option_order->contact = $contact;
            $option_order->phone = $phone;
            $option_order->notes = $notes;
            $option_order->remark = $remark;
            $option_order->classify = $classify;
            $option_order->options = json_encode($options, JSON_UNESCAPED_UNICODE);
            if($classify) {
                $classify2 = $this->classify_name[$classify]->name;
                $pid = $this->classify_name[$classify]->pid;
                $classify1 = $this->classify_map[$pid]->name;
                $option_order->classify1 = $classify1;
                $option_order->classify2 = $classify2;
                $option_order->classify = $this->classify_name[$classify]->id;
                $option_order->photo = $this->classify_name[$classify]->photo;
            }
            $option_order->wechat_id = $wechat_id;

            $option_order->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功"
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!",
            ]);
        }
    }


    /**
     * 每页20进行分页
     * @param Request $request
     * @return mixed
     */
    /**
     * 每页20进行分页
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $keyword = $request->input('keyword');
        if($keyword) {
            $data = OptionOrder::where("notes", "like",  "%{$keyword}%")
                ->orWhere("contact", "like",  "%{$keyword}%")
                ->orWhere("classify", "like",  "%{$keyword}%")
                ->orWhere("phone", "like",  "%{$keyword}%")
                ->orWhere("remark", "like",  "%{$keyword}%")
                ->paginate();
        } else {
            $data = OptionOrder::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 = OptionOrder::find($id);
            $record->remark = $remark;
            $record->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!",
                "data" => $record
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }
}
