<?php

namespace app\admin\controller\mall;

use app\admin\model\MallOrder;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\MiddlewareAnnotation;
use app\admin\service\annotation\NodeAnnotation;
use app\common\controller\AdminController;
use app\Request;
use think\App;
use think\facade\Db;
use think\response\Json;
use Wolfcode\Ai\Enum\AiType;
use Wolfcode\Ai\Service\AiChatService;

#[ControllerAnnotation(title: '支付订单管理')]
class Order extends AdminController
{

    #[NodeAnnotation(ignore: ['export'])] // 过滤不需要生成的权限节点 默认 CURD 中会自动生成部分节点 可以在此处过滤
    protected array $ignoreNode;

    public function __construct(App $app)
    {
        parent::__construct($app);
        self::$model = new MallOrder();
    }
    protected array $allowModifyFields = [
        'status',
        'sort',
        'remark',
        'is_delete',
        'is_auth',
        'title',
        'url',
    ];

    #[NodeAnnotation(title: '列表', auth: true)]
    public function index(Request $request): Json|string
    {
        if ($request->isAjax()) {
            if (input('selectFields')) return $this->selectList();
            list($page, $limit, $where) = $this->buildTableParams();
            $count = self::$model::where($where)->count();
            $list  = self::$model::where($where)->page($page, $limit)->order($this->sort)->select()->toArray();
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    #[NodeAnnotation(title: '确认支付成功', auth: true)]
    public function recharge(Request $request, $id): string
    {
        $row = self::$model::find($id);
        empty($row) && $this->error('数据不存在');
        if ($request->isPost()) {
            $post = $request->post();
            try {
                $post['status']       = 2;
                $save                = $row->save($post);
            }catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('确认支付成功') : $this->error('保存失败');
        }
    }

    #[NodeAnnotation(title: '编辑', auth: true)]
    public function edit(Request $request, $id = 0): string
    {
        $row = self::$model::find($id);
        empty($row) && $this->error('数据不存在');
        if ($request->isPost()) {
            $post = $request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                Db::transaction(function() use ($post, $row, &$save) {
                    $post['status']       = 1;
                    $save = $row->save($post);
                });
            }catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    #[NodeAnnotation(title: 'IP拉黑', auth: true)]
    public function blockip(Request $request, $id): string
    {
        $row = self::$model::find($id);
        empty($row) && $this->error('数据不存在');
        if ($request->isPost()) {
            try {
                $save  = (new \app\admin\model\BlackIp())->save([
                    'ip' => $row->ip,
                ]);
            }catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('IP拉黑成功') : $this->error('保存失败');
        }
    }
    #[NodeAnnotation(title: '开始操作', auth: true)]
    public function start_url(Request $request, $id): string
    {
        $row = self::$model::find($id);
        empty($row) && $this->error('数据不存在');
        if ($request->isPost()) {
            try {
                $save  = $row->save([
                    'type' => $row->type == 1?0:1,
                ]);
            }catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('操作处理成功') : $this->error('保存失败');
        }
    }

    #[MiddlewareAnnotation(ignore: MiddlewareAnnotation::IGNORE_LOGIN)]
    public function no_check_login(Request $request): string
    {
        return '这里演示方法不需要经过登录验证';
    }

}