<?php

namespace App\Admin\Controllers\OrdersManagement;

use App\Admin\Actions\backstage\Refund\RefundCompleteAction;
use App\Admin\Actions\backstage\Refund\RefundPartCompleteAction;
use App\Models\Order;
use App\Models\OrderPatient;
use App\Models\Organization;
use App\Models\Patient;
use App\Models\RefundApplication;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Auth\Permission;

class RefundApplicationController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '退款申请列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RefundApplication());
        $grid->disableCreateButton();
        $grid->disableBatchActions();
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->equal('orders.order_sn','订单号');
            $filter->equal('orders.product_type','产品类型')->select([3=>'门诊预约',5=>'儿保预约']);
            $filter->equal('orders.payment_status','支付状态')->select([3=>'退款中',4=>'已退款']);
            $filter->equal('orders.order_status','订单状态')->select([5=>'已取消',6=>'已超时']);
        });
        $grid->actions(function ($actions){
           $actions->disableView();
           $actions->disableEdit();
           $actions->disableDelete();
            //获取取消时间
            $cancel_time = Order::where('id',$actions->row->order_id)->value('cancel_time');
            //获取预约时间
            $appoint_start_time = OrderPatient::where('order_id',$actions->row->order_id)->value('appoint_start_time');
            //获取预约前一小时的时间
            $before_appoint_start_time = $appoint_start_time-60*60;
            //获取订单备注
            $notice = Order::where('id',$actions->row->order_id)->value('order_notes');
            //获取订单类型
            $order_type = Order::where('id',$actions->row->order_id)->value('product_type');
            if ($actions->row->status == 1)
           {
               if (\Admin::user()->isRole('financial_staff')){
                    if ($order_type == 3)
                    {
                        $actions->add(new RefundCompleteAction());
                    }
                    // 3. 儿保 大于一个小时和小于一个小时
                    //根据时间预约时间进行区分
                    if ($order_type == 5 && $notice != "医生拒绝接单")
                    {
                        if ($before_appoint_start_time>$cancel_time)
                        {
                            $actions->add(new RefundCompleteAction());
                        }
                        else
                        {
                            $actions->add(new RefundPartCompleteAction());
                        }
                    }
                    // 4. 儿保 医生取消订单
                    if ($order_type ==5 && $notice == "医生拒绝接单")
                    {
                        $actions->add(new RefundCompleteAction());
                    }
               }
           }
        });
        $grid->column('id', __('id'));
        $grid->column('order_id', __('订单id'));
        $grid->column('orders.order_sn', __('订单号'));
        $grid->column('orders.product_type','产品类型')->using([1=>'电话咨询',2=>'图文咨询',3=>'门诊预约',4=>'疫苗接种预约',5=>'儿保预约',6=>'服务包',7=>'充值',8=>'快速预约']);
        $grid->column('orders.user_id', __('用户名'))->display(function ($id){
            $name = User::where('id',$id)->value('nickname');
            return $name;
        });
        $grid->column('orders.patient_id', __('患者名'))->display(function ($id){
            $name = Patient::where('id',$id)->value('name');
            return $name;
        });
        $grid->column('orders.order_status','订单状态')->using([1=>'未支付',2=>'待接单',3=>'进行中',4=>'已完成',5=>'已取消',6=>'已超时',7=>'已预约']);
        $grid->column('orders.payment_status','支付状态')->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款',5=>'待退款']);
        $grid->column('orders.order_notes','订单备注');
        $grid->column('orders.payment_amount', __('订单金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('refund_amount', __('退款金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('refund_applicant', __('退款申请人员'))->display(function ($id){
            $name = Administrator::where('id',$id)->value('name');
            return $name;
        });
        $grid->column('financial_auditor', __('财务审核人员'))->display(function ($id){
            $name = Administrator::where('id',$id)->value('name');
            return $name;
        });
        $grid->column('created_at', __('退款发起时间'));
        $grid->column('end_time', __('退款完成时间'))->display(function ($time){
            if ($time == 0)
            {
                return '';
            }
            else
            {
                return $time;
            }
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RefundApplication::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('order_id', __('Order id'));
        $show->field('status', __('Status'));
        $show->field('refund_applicant', __('Refund applicant'));
        $show->field('financial_auditor', __('Financial auditor'));
        $show->field('end_time', __('End time'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RefundApplication());

        $form->number('order_id', __('Order id'));
        $form->switch('status', __('Status'));
        $form->number('refund_applicant', __('Refund applicant'));
        $form->number('financial_auditor', __('Financial auditor'));
        $form->datetime('end_time', __('End time'))->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
