<?php

namespace App\Admin\Controllers\OrdersManagement;

use App\Admin\Actions\backstage\Orders\Evaluate;
use App\Admin\Actions\backstage\Orders\ServicePack;
use App\Admin\Actions\backstage\Orders\ServicePackOrderList;
use App\Models\Order;
use App\Admin\Actions\backstage\Orders\OrderDetails;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\backstage\User\MapDepot;
use App\Admin\Actions\backstage\User\Suggests;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ServicePackOrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务包开通订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());
        $grid->disableCreateButton();
        $grid->model()->wherein('product_type',[6]);
        //筛选
        $grid->filter(function ($filter){
            $filter->disableIdFilter();

                $filter->like('user.nickname','用户姓名');
                $filter->like('orderPatient.name','患者姓名');
                $filter->equal('order_status','订单状态')->radio(
                    [
                        ''=>'不限',
                        1=>'未支付',
                        2=>'待接单',
                        3=>'进行中',
                        4=>'已完成',
                        5=>'已取消'
                    ]
                );
                $filter->timestampBetween('payment_time', "支付时间")->datetime();

            });
        //操作
        $grid->actions(function ($actions) {
            $actions->disableView();
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
            $actions->add(new ServicePack());
            $actions->add(new ServicePackOrderList());
        });
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'))->sortable();
        $grid->column('order_sn', __('订单编号'));
        $grid->column('user.nickname', __('用户姓名'));
        $grid->column('orderPatient.name', __('患者姓名'));
        $grid->column('orderPatient.patient_id', __('患者id'))->hide();
        $grid->column('user.phone', __('用户电话'));
        $grid->column('orderPack.service_pack_id', __('服务包id'));
        $grid->column('orderPack.pack_name', __('服务包名称'));
        $grid->column('orderPack.pack_price', __('服务包价格'))->display(function ($money){
            return $money/100;
        });
        $grid->column('product_type', __('产品类型'))->using([6=>'服务包订单']);
        $grid->column('payment_type', __('支付方式'))->using([1=>'微信支付',2=>'余额支付']);
        $grid->column('order_status', __('订单状态'))->using([1=>'未支付',2=>'待接单',3=>'进行中',4=>'已完成',5=>'已取消']);
        $grid->column('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款']);
        $grid->column('total_amount', __('订单总金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('payment_amount', __('用户实际支付的金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('discount_amount', __('折扣金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('order_notes', __('订单备注'));
        $grid->column('payment_time', __('支付时间'))->display(function ($time){
            return date('Y-m-d H:i:s',$time);
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('user_id', __('用户id'));
        $show->field('user.nickname', __('用户姓名'));
        $show->field('docter_id', __('医生id'));
        $show->field('docter.name', __('医生姓名'));
        $show->field('patient_id', __('患者id'));
        $show->field('orderPatient.name', __('患者姓名'));
        $show->field('organization_id', __('机构id'));
        $show->field('organization.name', __('机构名'));
        $show->field('order_sn', __('订单号'));
        $show->field('product_type', __('产品类型'))->using([6=>'服务包订单']);
        $show->field('payment_type', __('支付类型'))->using([1=>'微信支付',2=>'余额支付']);
        $show->field('order_status', __('订单状态'))->using([1=>'未支付',2=>'待接单',3=>'进行中',4=>'已完成',5=>'已取消']);
        $show->field('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款']);
        $show->field('total_amount', __('	订单总金额'))->as(function ($money){
            return $money/100;
        });
        $show->field('payment_amount', __('用户实际支付的金额'))->as(function ($money){
            return $money/100;
        });
        $show->field('discount_amount', __('折扣金额'))->as(function ($money){
            return $money/100;
        });
        $show->field('payment_time', __('支付时间'))->as(function ($time){
            if($time == 0)
            {
                return '';
            }
            else
            {
                return date('Y-m-d H:i:s',$time);
            }
        });
        $show->field('receiving_time', __('接单时间'))->as(function ($time){
            if($time == 0)
            {
                return '';
            }
            else
            {
                return date('Y-m-d H:i:s',$time);
            }
        });
        $show->field('end_time', __('订单完成时间'))->as(function ($time){
            if($time == 0)
            {
                return '';
            }
            else
            {
                return date('Y-m-d H:i:s',$time);
            }
        });
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }
}
