<?php

namespace Modules\Payment\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Modules\Payment\Models\Alipay;

class AlipayController extends AdminController
{

    protected $title = '支付宝';

    public function grid(): Grid
    {
        $grid = new Grid(new Alipay());
        $grid->disableFilter();

        $grid->column('id', '#ID#');
        $grid->column('name', '支付名称');
        $grid->column('app_id', '应用APPID');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new Alipay());

        $form->text('name', '支付名称')->required();

        $form->text('app_id', '应用APPID')->required();
        $form->url('notify_url', '通知地址');
        $form->url('return_url', '返回地址');

        $form->textarea('ali_public_key', '支付公钥');
        $form->textarea('private_key', '私钥');

        $form->text('app_cert_path', '应用公钥证书')->help('相对/storage/app下的路径如：certs/XXX.crt');
        $form->text('alipay_cert_path', '支付宝公钥证书')->help('相对/storage/app下的路径如：certs/XXX.crt');
        $form->text('alipay_root_cert_path', '支付宝根证书文件')->help('相对/storage/app下的路径如：certs/XXX.crt');

        $form->embeds('log', '日志配置', function (Form\EmbeddedForm $form) {
            $form->text('file', '日志文件名');
            $form->select('level')->options([
                'info'  => 'info',
                'debug' => 'debug',
            ])->default('info');
            $form->select('type', '记录方式')->options([
                'daily'  => '按日期',
                'single' => '单文件',
            ])->default('daily');
            $form->number('max_file')
                 ->default(30)
                 ->help('当 【记录方式】 为 【按日期】 时有效');
        });

        return $form;
    }

    public function ajax(Request $request)
    {
        $q = $request->q;

        return Alipay::where('name', 'like', "%$q%")
                     ->orWhere('app_id', 'like', "%$q%")
                     ->paginate(null, ['id', 'name as text']);
    }

}