<?php

namespace App\Http\Controllers\Admin;

use App\Models\Option;
use App\Models\Order;
use App\Models\OrderDevice;
use App\Models\Project;
use Illuminate\Http\Request;

class OrderController extends BaseController
{
    protected $model;

    protected $project;

    protected $model_name = '订单';

    protected $pre_uri = '/admin/Order/';

    protected $view_path = 'admin.orders.';

    protected $redirect_index = '/admin/Order/index';

    public function __construct()
    {
        parent::__construct();
        $this->model = new Order();
        $this->project = new Project();
    }

    public function index()
    {
        $project_options = $this->project->getOptions();
        $status_options = Option::get('orders', 'status');
        $type_options = [['name' => '外部租赁', 'id' => 1], ['name' => '内部调用', 'id' => 2]];
        list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
        return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'project_options', 'status_options', 'type_options'));
    }

    public function get(Request $request)
    {
        $items = $this->model->where('id', '>', 0)->with('project', 'user');

        $tmp_items = collect(['order_number']);
        foreach($tmp_items as $tmp_item) {
            if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
                $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
            }
        }

        $select_items = collect(['status', 'type', 'project_id']);
        foreach($select_items as $select_item) {
            if($request->has($select_item) && !empty($request->input($select_item))) {
                $items = $items->where($select_item, '=', $request->input($select_item));
            }
        }
        if ($request->input('order_id'))
        {
            $items->where('id','like','%'.request('order_id').'%');
        }
        $items = $items->paginate();

        foreach($items as $item) {
            $item->order_id = $item->id ? $item->id : '';
            $item->project_name = $item->project ? $item->project->name : '';
            $item->work_point = $item->work_point_id ? $item->workpoints?($item->workpoints->name ? $item->workpoints->name : '') :'': '';
//            dd($item->workpoints);
            //方法区
            $order = OrderDevice::where('order_id',$item->order_id)->get()->toArray();
            $order_num = count($order);
            $total_price = 0;
            if ($item->type == 1)
            {
                foreach ($order as $value)
                {
                    $one_price = ($value['quantity']*$value['price'])/100;
                    $total_price += $one_price;
                }
            }
            $item->order_num = $item->id ? $order_num : '';
            $item->total_price = $item->id ? $total_price : '';
            $item->remark = $item->remark ? $item->remark : '';
            $item->user_name = $item->user ? $item->user->name : '';
            $item->status_name = $item->getStatusName();
            $item->option = Option::find($item->status);
            $color = $item->option ? $item->option->color : '';
            $item->status_name = '<span style="color: ' . $color . '">' . $item->status_name . '</span>';
            $item->type_name = $item->type == 1 ? '<span class="layui-badge layui-bg-green">外部租赁</span>' : '<span class="layui-badge layui-bg-blue">内部调用</span>';
        }

        return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
    }

    public function create()
    {
        list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
        return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
    }

    public function store(Request $request)
    {
        if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
        $validator = $this->model->getValidator($request, 'store');
        if($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }
        $data = $request->input('data');
        $res = $this->model->create($data);
        if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
        return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
    }

    public function edit(Request $request)
    {
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
        list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
        return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
    }

    public function update(Request $request)
    {
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
        if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
        $validator = $this->model->getValidator($request, 'update');
        if($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }
        $data = $request->input('data');
        $res = $this->model->where('id', $request->input('id'))->update($data);
        if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败！']);
        return back()->with(['sg_success_info' => '编辑成功']);
    }

    public function delete(Request $request)
    {
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
        $res = $item->delete();
        if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
        return response()->json(['status' => 'success', 'info' => '操作成功']);
    }

}
