<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\controller\Crud;
use app\model\BalanceLog;
use support\exception\BusinessException;
use Webman\Http\Request;
use support\Response;
use Exception;

/**
 * 用户管理 
 */
class BalanceLogController extends Crud
{
    
    /**
     * @var BalanceLog
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    function __construct()
    {
        $this->model = new BalanceLog;
    }
    /**
     * 浏览
     * @return Response
     * @throws Exception
     */
    public function index(Request $request): Response
    {
        return view('balance_log/index');
    }
    protected function formatNormal($items, $total): Response
    {
        $formattedItems = array_map(function ($item) {
            if(!isset($item['user']))
            {
                $item['user'] = \app\model\User::where('id',$item->user_id)->find();
            }
            if (isset($item['user']['username'])) {
                $item['username'] = $item['user']['username'];
            } else {
                $item['username'] = '';
            }
            unset($item['user']); // 移除user数组，因为我们已经提取了username
            return $item;
        }, $items);
        //echo gettype($items);
        // $formattedItems = $items->each(function ($item) {
        //     $item->username = $item->user->username ?? '';
        // });
        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        if(!isset($where['currency'])){
            return $this->success('',[]);
        }
        $currency = $where['currency']['value1'];
        unset($where['currency']);
        $this->model = $this->model->setSuffix('_'.$currency);
        if(isset($where['username'])){
            $User = new \app\model\User();
            $User = $this->parseOneWhere($User,'username',$where['username']);
            $ids = $User->column('id');
            $where['user_id'] = [
                'symbol'    =>  'in',
                'value1'    =>  $ids
            ];
            unset($where['username']);
        }
        if(isset($where['created_at'])){
            if(isset($where['created_at']['value1']) && $where['created_at']['value1']){
                $where['created_at']['value1']= strtotime($where['created_at']['value1']).'';
            }
            if(isset($where['created_at']['value2']) && $where['created_at']['value2']){
                $where['created_at']['value2']= strtotime($where['created_at']['value2']).'';
            }
        }
        if(isset($where['type'])){
            $where['type']['value1']= intval($where['type']['value1']);
        }
        if(isset($where['amount'])){
            $where['amount']['value1']= floatval($where['amount']['value1']);
            if(isset($where['amount']['value2'])){
                $where['amount']['value2']= floatval($where['amount']['value2']);
            }
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }
    public function __before_select__(Request $request){
        $this->assignconfig('balanceTypeList',\app\enum\BalanceType::toArray());
    }
    public function __before_index__(Request $request){
        $this->__before_select__($request);
    }

    /**
     * 执行删除
     * @param array $ids
     * @return void
     */
    protected function doDelete(array $ids)
    {
        if (!$ids) {
            return;
        }
        $primary_key = $this->model->getPk();
        $currency = Input('currency');
        $this->model->setSuffix('_'.strtolower($currency))->whereIn($primary_key, $ids)->delete();
    }

}
