<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\SendOrder;
use App\Models\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->with(['users:id,name,avatar,mobile', 'goods:id,img,name', 'goods_attr:id,title,integral'])->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('user_id')->display(function () {
                $str = "";
                if($this->users){
                    $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                    $str .= '<img data-action="preview-img" src="' . $this->users->avatar . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                    $str .= '<div>';
                    $str .= '<p style="margin-bottom: 5px">用户名：' . $this->users->name . '</p>';
                    $str .= '<p style="margin-bottom: 0px">手机号：' . $this->users->mobile . '</p>';
                    $str .= "</div>";
                    $str .= "</div>";
                }
                return $str;
            });
            $grid->column('order_no');
            $grid->column('integral');
            $grid->column('goods_id')->display(function (){
                $str = "";
                if($this->goods){
                    $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                    $str .= '<img data-action="preview-img" src="' . $this->goods->img . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                    $str .= '<div>';
                    $str .= '<p style="margin-bottom: 5px">商品名：' . $this->goods->name . '</p>';
                    $str .= "</div>";
                    $str .= "</div>";
                }
                return $str;
            });
            $grid->column('goods_attr_id')->display(function (){
                $str = "";
                if($this->goods_attr){
                    $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                    $str .= '<p style="margin-bottom: 5px">' . $this->goods_attr->title . '</p>';
                    $str .= '<p style="margin-bottom: 0px">：' . $this->goods_attr->integral . '</p>';
                    $str .= "</div>";
                }else{
                    $str .= '无';
                }
                return $str;
            });

            $grid->column('num');
            $grid->column('express_price');
            $grid->column('is_send')->using(Order::SEND_STATUS)->label([0=>'yellow', 1=>'success']);
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(4);
                $filter->like('users.name', '用户')->width(4);
                $filter->like('order_no', '订单号')->width(4);
                $filter->like('goods.name', '商品名')->width(4);
                $filter->between('created_at')->date()->width(4);
            });

            //操作管理
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($actions->row->is_send == 0) {
                    $actions->append(new SendOrder(Order::class, $actions->row->id));
                }
            });

            $grid->disableEditButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $order = Order::query()->where('id', $id)->first()->toArray();

        return Show::make($id, Order::with(['users', 'goods']), function (Show $show) use ($order) {
            $show->row(function (Show\Row $show) use ($order) {
                //下单人
                $show->width(12)->user_id->unescape()->as(function () {
                    $user = $this->users;
                    $user['name'] = $user['name'] ?: '未知';
                    $html = '';
                    $html .= '<div style="text-align:left">';
                    $html .= '<img data-action="preview-img" src="' . $user['avatar'] . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                    $html .= '<div style="margin-top:5px">姓名：' . $user['name'] . '</div>';
                    $html .= '<div style="margin-top:5px">电话：' . $user['mobile'] . '</div>';
                    $html .= '</div>';
                    return $html;
                });

                //商品信息
                $show->width(12)->goods_info->unescape()->as(function () {
                    $goods = $this->goods;
                    $html = '';
                    $html .= '<div style="text-align:left">';
                    $html .= '<img data-action="preview-img" src="' . $goods['img'] . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                    $html .= '<div style="margin-top:5px">商品：' . $goods['name'] . '</div>';
                    $html .= '<div style="margin-top:5px">详情：' . '<a href=http://' . $_SERVER['HTTP_HOST'] . '/admin/goods/' . $goods->id . '/edit' . '>查看更多>></a></div>';
                    $html .= '</div>';
                    return $html;
                });

                //收件信息
                $show->width(12)->name->unescape()->as(function () {
                    $html = '';
                    $html .= '<div style="text-align:left">';
                    $html .= '<div style="margin-top:5px">姓名：' . $this->receiver_name . '</div>';
                    $html .= '<div style="margin-top:5px">电话：' . $this->receiver_mobile . '</div>';
                    $html .= '<div style="margin-top:5px">地址：' . $this->receiver_address . '</div>';
                    $html .= '</div>';
                    return $html;
                });

                //订单信息
                $show->width(12)->order_info->unescape()->as(function () {
                    $html = '';
                    $html .= '<div style="text-align:left">';
                    $html .= '<div style="margin-top:5px">订单单号：' . $this->order_no . '</div>';
                    $html .= '<div style="margin-top:5px">兑换积分：' . $this->unit_price . '</div>';
                    $html .= '<div style="margin-top:5px">商品数量：' . $this->num . '</div>';
                    $html .= '<div style="margin-top:5px">订单状态：' . Order::STATUS[$this->status] . '</div>';
                    $html .= '<div style="margin-top:5px">发货状态：' . Order::SEND_STATUS[$this->is_send] . '</div>';
                    $html .= '<div style="margin-top:5px">下单时间：' . $this->created_at . '</div>';
                    $html .= '<div style="margin-top:5px">更新时间：' . $this->updated_at . '</div>';
                    $html .= '</div>';
                    return $html;
                });

                //物流信息
                $show->width(12)->express_info->unescape()->as(function () {
                    $html = '';
                    $html .= '<div style="text-align:left">';
                    $html .= '<div style="margin-top:5px">单号：' . $this->order_no . '</div>';
                    $html .= '<div style="margin-top:5px">运费：' . $this->express_price . '</div>';
                    $html .= '</div>';
                    return $html;
                });
            });

            $show->disableEditButton();
        });
    }
}
