<?php

namespace app\http\controller;

use fastphp\base\Controller;
use app\http\model\User as UserModel;
use app\http\model\Order as OrderModel;
use enum\http\Order as HttpOrderEnum;
use app\http\model\Recharge as RechargeModel;

class Admin extends Controller
{
    //获取充值记录
    public function get_recharge_list(){
        $get_data = json_decode(file_get_contents("php://input"), true);
        $page = 0;
        $user_model = new UserModel;
        
        $where = ['pay_status' => 1];

        if (isset($get_data['PageIndex'])) {
            $page = ($get_data['PageIndex'] - 1) * 50;
        }
        if (!empty($get_data['username'])) {
            $user_where_search['LoginCode'] = $get_data['username'];
            $user_info = $user_model->getOne( $user_where_search,'Id,LoginCode');
            $where['user_id'] = $user_info['Id'];
        }
        $recharge_model = new RechargeModel;
        $recharge_list = $recharge_model->getListPage($where, '*', 'id desc', "$page,50");

        $user_id_list = [];
        foreach ($recharge_list as $info) {
            $user_id_list[$info['user_id']] = $info['user_id'];
        }

        $user_where = ['Id' => ['IN',$user_id_list]];
        $user_list = $user_model->getList($user_where, 'Id,LoginCode');

        foreach ($recharge_list as &$recharge) {
            foreach ($user_list as $infos) {
                if ($recharge['user_id'] == $infos['Id']) {
                    $recharge['username'] = $infos['LoginCode'];
                    $recharge['pay_type'] = HttpOrderEnum::$pay_type[$recharge['pay_type']];
                }
            }
        }

        $data = [
            'Code' => 30000,
            'Data' => $recharge_list,
            'Message' => '',
            'TotalCount' => (int)$recharge_model->getCount($where)['count'],
        ];
        echo json_encode($data);


    }

    //获取用户的订单
    public function get_order_list() {
        $get_data = json_decode(file_get_contents("php://input"), true);
        $page = 0;
        $user_model = new UserModel;
        
        $where = ['order_status' => 1];

        if (isset($get_data['PageIndex'])) {
            $page = ($get_data['PageIndex'] - 1) * 50;
        }
        if (!empty($get_data['username'])) {
            $user_where_search['LoginCode'] = $get_data['username'];
            $user_info = $user_model->getOne( $user_where_search,'Id,LoginCode');
            $where['user_id'] = $user_info['Id'];
        }
        $order_model = new OrderModel;
        $order_list = $order_model->getListPage($where, '*', 'id desc', "$page,50");

        $user_id_list = [];
        foreach ($order_list as &$info) {
            $user_id_list[$info['user_id']] = $info['user_id'];
            $info['order_type'] = HttpOrderEnum::$order_type[$info['order_type']];
            $info['package_type'] = HttpOrderEnum::$package_type[$info['package_type']];
            $info['durationType'] = empty($info['durationType'])?'':HttpOrderEnum::$durationType[$info['durationType']];
            $info['payCash'] = $info['payCash'] / 100;
            $info['status'] = $info['status']?'成功':'失败';
        }
        
        $user_where = ['Id' => ['IN',$user_id_list]];
        $user_list = $user_model->getList($user_where, 'Id,LoginCode');
        
        foreach ($order_list as &$order) {
            foreach ($user_list as $infos) {
                if ($order['user_id'] == $infos['Id']) {
                    $order['username'] = $infos['LoginCode'];
                }
            }
        }

        $data = [
            'Code' => 30000,
            'Data' => $order_list,
            'Message' => '',
            'TotalCount' => (int)$order_model->getCount($where)['count'],
        ];
        echo json_encode($data);
    }

    // 获取原后台支付数据
    public function get_payments() {
        $get_data = json_decode(file_get_contents("php://input"), true);

        $request_data = [
            "page" => $get_data['PageIndex'], //必填，当前页数
            "limit" => 50 //必填，每页显示条数，建议10~20，不可超过100
        ];


        $request_data = formatHttpRequert($request_data);

        $url = 'https://merchant.sgxz.cn/admin/downline/payments';
        $data = json_decode(linkcurl($url,'GET',$request_data,[],0));

        $subtype = [
            '1' => '新购',
            '2' => '套餐整体续费',
            '3' => 'IP批量续费',
            '4' => '切换IP',
            '12' => '套餐升级中继',
        ];
        foreach ($data->d->items as &$info) {
            $info->orderType = HttpOrderEnum::$package_type[$info->orderType];
            $info->subtype = $subtype[$info->subtype];
        }

        $admin_url = "https://merchant.sgxz.cn/admin/get/coin";
        $money = json_decode((linkcurl($admin_url,'GET',formatHttpRequert([]),[],0)))->d->coin;
        $return_data = [
            'Code' => 30000,
            'money' => $money,
            'data' => $data
        ];
        echo json_encode($return_data);

    }
}