<?php

namespace App\Admin\Controllers\Platform;

use App\Admin\Actions\LinkStockOrderDeliver;
use App\Admin\Actions\LinkVipOrderRefund;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Modules\Gout\Http\Controllers\Admin\Action\Audit;
use Modules\Gout\Models\GoutCase;
use Modules\Gout\Renderable\CaseData;
use Modules\Gout\Renderable\CaseSymptoms;
use Modules\User\Models\User;
use Modules\User\Models\UserStock;

class ExperienceController extends AdminController
{

    protected $title = '体验官审核';

    public function grid(): Grid
    {
        $grid = new Grid(new GoutCase());
        $grid->disableCreateButton();
        $grid->model()->with(['symptoms', 'user', 'user.sign', 'user.userStock'])
            ->withCount(['logs', 'surveys'])
            ->where('type', GoutCase::TYPE_TY);

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableEdit();
            $actions->disableDelete();
            $actions->disableView();

            if ($actions->row->canAudit()) {
                $actions->add(new Audit());
            }

            if ($actions->row->manage_status == GoutCase::MANAGE_STATUS_DELIVER) {
                $actions->add(new LinkStockOrderDeliver());
            }
            if ($actions->row->manage_status == GoutCase::MANAGE_STATUS_REFUND) {
                $actions->add(new LinkVipOrderRefund());
            }
        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->like('name', '姓名');
                $filter->equal('mobile', '手机号');
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select((new GoutCase())->status_map);
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('name', '姓名');
        $grid->column('mobile', '手机号');
        $grid->column('建档信息')
            ->display(function ($title, $column) {
                return '查看';
            })->modal('建档信息', CaseData::class);

        $grid->column('报告数据')
            ->display(function ($title, $column) {
                return '查看';
            })->modal('亚健康数据', CaseSymptoms::class);

        $grid->column('symptoms', '症状')
            ->display(function () {
                return $this->symptoms->pluck('title');
            })->label();
        $grid->column('是否关注')
            ->display(function () {
                return $this->user->isOfficialSubscribe();
            })
            ->bool();
//
        $grid->column('缴纳保证金')
            ->display(function () {
                return $this->user->isExperiencePrice() ? '已缴' : '待缴';
            })
            ->label([
                '已缴' => 'primary',
                '待缴' => 'success',
            ]);

        $grid->column('是否收货')
            ->display(function () {
                return $this->user->userStock->stock_order_status_text;
            })
            ->label(UserStock::STOCK_ORDER_STATUS_MAP);

        $grid->column('is_coupon', '是否发券')
            ->using(GoutCase::COUPONS)
            ->label(GoutCase::COUPONS_MAP);

        $grid->column('喝水打卡')
            ->display(function () {
                return $this->user->sign->counts;
            });
//
        $grid->column('完结报告')
            ->display(function () {
                return $this->logs_count > 1 ? '已上传' : '待上传';
            })
            ->label([
                '已上传' => 'primary',
                '待上传' => 'success',
            ]);
//
        $grid->column('是否退保')
            ->display(function () {
                return $this->user->isExperiencePriceRefund() ? '是' : '否';
            })
            ->label([
                '是' => 'primary',
                '否' => 'success',
            ]);
        $grid->column('好转反馈')
            ->display(function () {
                return $this->surveys_count;
            });

        $grid->column('manage_status', '状态')
            ->using((new GoutCase())->manage_status_map)
            ->label([
                GoutCase::MANAGE_STATUS_INIT    => 'primary',
                GoutCase::MANAGE_STATUS_DELIVER => 'success',
                GoutCase::MANAGE_STATUS_REFUND  => 'danger',
                GoutCase::MANAGE_STATUS_PASS    => 'info',
                GoutCase::MANAGE_STATUS_FINISH  => 'warning',
            ]);

        $grid->column('created_at', '创建时间');

        return $grid;
    }

}