<?php

namespace Modules\Mall\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Support\MessageBag;
use Modules\Mall\Facades\Item;
use Modules\Mall\Http\Controllers\Admin\Action\Order\Delivered;
use Modules\Mall\Http\Controllers\Admin\Action\Order\Pay;
use Modules\Mall\Http\Exporter\OrderExporter;
use Modules\Mall\Models\Address;
use Modules\Mall\Models\Express;
use Modules\Mall\Models\GoodsSku;
use Modules\Mall\Models\Order;
use Modules\Mall\Models\Shop;
use Modules\User\Models\User;
use Modules\Mall\Facades\Order as OrderFacade;

class StockOrderController extends AdminController
{

    protected $title = '会员线上提货';

    protected function grid(): Grid
    {
        $grid = new Grid(new Order());

        $grid->model()
            ->where('type', Order::TYPE_SAMPLE)
            ->where('channel', Order::CHANNEL_USER)
            ->with(['shop', 'user', 'items'])
            ->withCount('versions');

        $grid->disableCreateButton();
        $grid->quickSearch('order_no')->placeholder('搜索订单编号');

        $grid->filter(function (Grid\Filter $filter) {
            $filter->scope('unPay', '未付款')->unPay();
            $filter->scope('paid', '待发货')->paid();
            $filter->scope('delivered', '已发货')->delivered();
            $filter->scope('signed', '已签收')->signed();
            $filter->scope('completed', '已完成')->completed();

            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->like('order_no', '订单编号');
//                $filter->equal('shop_id', '所属店铺')->select(Shop::pluck('name', 'id'));
                $filter->equal('user_id', '提货用户')->select()->ajax(route('admin.user.users.ajax'));
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->between('created_at', '下单时间')->datetime();
                $filter->equal('state', '订单状态')->select([
                    Order::STATUS_CANCEL    => '已取消',
                    Order::STATUS_PAID      => '待发货',
                    Order::STATUS_DELIVERED => '已发货',
                    Order::STATUS_SIGNED    => '已签收',
                ]);

//                $filter->between('paid_at', '付款时间')->datetime();
//                $filter->between('expired_at', '过期时间')->datetime();
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableEdit();
            $actions->disableDelete();
            $actions->disableView();
            if ($actions->row->can('pay') && $this->row->type == Order::TYPE_NORMAL) {
                $actions->add(new Pay());
            }
            if ($actions->row->can('deliver')) {
                $actions->add(new Delivered);
            }
        });

        $grid->model()->with(['shop', 'user.info']);

        $grid->column('order_no', '订单编号')->display(function () {
            return sprintf('<a href="%s">%s</a>', route('admin.mall.stock_orders.show', $this), $this->order_no);
        });
//        $grid->column('shop.name', '所属店铺');
        $grid->column('user.username', '提货用户');
//        $grid->column('amount', '商品金额');
//        $grid->column('freight', '运费');
        $grid->column('qty', '提货数量')
            ->display(function () {
                return $this->items()->sum('qty');
            });
//        $grid->column('订单金额')->display(function () {
//            return $this->total;
//        });
        $grid->column('state', '订单状态')
            ->display(function () {
                return $this->state_text;
            })
            ->label();
//        $grid->column('type', '订单类型')->using(Order::TYPE_MAP)->label();
//        $grid->column('paid_at', '支付时间')->sortable();
//        $grid->column('expired_at', '过期时间')->sortable();
        $grid->column('versions_count', '操作日志')->link(function () {
            return route('admin.mall.versions', [
                'model' => get_class($this),
                'key'   => $this->id,
            ]);
        }, '_self');
        $grid->column('created_at', '下单时间')->sortable();

        $grid->disableExport(false);
        $grid->export(function ($export) {
            $export->column('order_no', function ($value, $original) {
                return strip_tags($value)."\n";
            });
            $export->column('type', function ($value, $original) {
                return strip_tags($value);
            });
            $export->column('state', function ($value, $original) {
                return strip_tags($value);
            });

            $export->except(['versions_count', 'paid_at', 'expired_at']);

            $export->filename($this->title.date("YmdHis"));
        });
        return $grid;
    }

    /**
     * Notes   : 订单详情
     *
     * @Date   : 2021/3/16 1:46 下午
     * @Author : < Jason.C >
     * @param  string  $orderNo  订单的 order_no
     */
    protected function detail(string $orderNo)
    {
        $order = Order::query()->firstWhere(['order_no' => $orderNo]);

        return view('mall::admin.stock_order.detail', compact('order'));
    }

}
