<?php

namespace app\adminapi\controller\finance;

use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\finance\WithdrawLists;
use app\common\logic\finance\WithdrawLogic;
use app\common\model\user\Withdraw;
use think\Exception;

class WithdrawController extends BaseAdminController
{

    public function index()
    {
        return $this->dataLists(new WithdrawLists());
//        [$page, $limit] = $this->getPage();
//        $status = $this->request->param('status');
//        $query = Withdraw::with('user');
//        if ($status !== '') {
//            $query->where('status', $status);
//        }
//        $count = $query->count();
//        $list = $query->order('id', 'desc')->page($page)->limit($limit)->select()->toArray();
//        return $this->success('success', ['count' => $count, 'data' => $list]);
    }

    public function update($id)
    {
        $status = $this->request->param('status');
        if (!in_array($status, [1, 2, 3])) {
            return $this->fail('参数错误');
        }
        $data = Withdraw::find($id);
        if (empty($data)) {
            return $this->fail('数据不存在');
        }
        $data->status = $status;
        $data->udpate_time = time();
        $data->save();
        return $this->success('操作成功', [], 1, 1);
    }

    /**
     * 提现申请审核
     */
    public function audit()
    {
        try {
            $params = $this->request->param();
            $re = WithdrawLogic::audit($params);
            if (!$re) {
                return $this->fail(WithdrawLogic::getError());
            }
            return $this->success('操作成功', [], 1, 1);
        } catch (Exception $exception) {
            return $this->fail($exception->getMessage());
        }
    }
}