<?php

namespace App\Admin\Controllers\Log;

use App\Admin\Renderable\Log\InData;
use App\Admin\Renderable\Log\OutData;
use App\Models\Log;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;

class IndexController extends AdminController
{

    protected $title = 'api接口日志';

    /**
     * Notes:
     * @Author: <C.Jason>
     * @Date  : 2019/9/18 14:50
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Log);
        $grid->model()->orderBy('id', 'desc');

        $grid->disableActions();

        $grid->filter(function ($filter) {
            $filter->equal('type', '接口类型')->select(Log::TYPES);
            $filter->where(function ($query) {
                $query->where("out_source->data->profitOfferItemVersion", $this->input);
            }, '券类型')->select([
                1 => '新版',
                0 => '老版',
            ]);
            $filter->where(function ($query) {
                $query->where("in_source->query->redemptionCode", $this->input)
                      ->orWhere("in_source->jiemi->redemptionCode", $this->input);
            }, '券码');
            $filter->where(function ($query) {
                $query->Where("in_source->jiemi->outletId", $this->input);
            }, '网点编号');

            $filter->between('created_at', '提交时间')->datetime();

            // $filter->equal('created_at', '提交时间')->date();
        });

        $grid->column('id', '#ID#');

        // $grid->column('path', '请求地址');
        $grid->column('请求地址')->display(function ($title, $column) {
            return '展开';
        })->expand(function ($model) {
            $header = ['请求地址'];
            $array  = [[$model->path]];

            return new Table($header, $array);
        });

        $grid->column('created_at', '提交时间');

        $grid->column('method', '模式');
        //        $grid->column('in_source', '请求参数')
        //             ->display(function ($title, $column) {
        //                 return '点击展开';
        //             })->modal(InData::class);
        //
        //        $grid->column('out_source', '返回参数')
        //             ->display(function ($title, $column) {
        //                 return '点击展开';
        //             })->modal(OutData::class);
        $grid->column('请求参数')->display(function () {
            $in_source = $this->in_source;
            unset($in_source['merchantSign']);
            unset($in_source['merchantCert']);
            unset($in_source['sign']);
            unset($in_source['data']);
            unset($in_source['json']);
            unset($in_source['query']['merchantSign']);
            unset($in_source['query']['merchantCert']);

            return $in_source;
        });
        $grid->column('返回信息')->display(function () {
            $out_source = $this->out_source;

            if (!is_array($out_source)) {
                return $out_source;
            }
            unset($out_source['merchantSign']);
            unset($out_source['merchantCert']);
            unset($out_source['serverCert']);
            unset($out_source['serverSign']);
            if (isset($out_source['data']) && is_string($out_source['data'])) {
                unset($out_source['data']);
            }

            return $out_source;
        });

        return $grid;
    }

}
