<?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 User extends Controller {
    // private $url_root = 'https://sandbox.sgxz.cn';
    private $url_root = 'https://merchant.sgxz.cn';

    private $http_user_info = [];

    public function __construct() {
        parent::__construct('', '');
        if (empty($this->userinfo['UserId'])) {
            die;
        }

        $user_model = new UserModel;
        $this->http_user_info = $user_model->get_http_user($this->userinfo['UserId']);
    }

    //获取用户的token
    public function get_user_token() {
        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
        ];
        $url = $this->url_root . '/user/detail';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));

        $user_data = [
            'uid' => $this->http_user_info['sgUserId'],
            'token' => $data->d->apiToken,
        ];
        $return_data = [
            'code' => 1,
            'data' => $user_data,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //获取聚币余额
    public function get_balance() {
        $data = $this->http_user_info['ju_money'];
        echo json_encode($data);
    }

    //获取所有充值订单
    public function charge_list() {
        $recharge_model = new RechargeModel;
        $where = [
            'user_id' => $this->userinfo['UserId'],
            'pay_status' => 1,
        ];

        $list = $recharge_model->getListPage($where);

        $pay_type = [
            '2' => '支付宝',
            '3' => '微信',
        ];

        foreach ($list as &$info) {
            $info['pay_type'] = $pay_type[$info['pay_type']];
        }

        echo json_encode($list);
    }

    //短效全部套餐
    public function dx_package() {
        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
        ];
        $url = $this->url_root . '/user/short/pack/list';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $list = array_merge($data->d->packs->period, $data->d->packs->unlimited, $data->d->packs->usage);

        foreach ($list as &$info) {
            $info->name = HttpOrderEnum::$package_type[$info->planType];
            $info->durationType2 = $info->durationType;
            $info->durationType = HttpOrderEnum::$durationType[$info->durationType];
            $info->expireTime = date('Y-m-d H:i:s', $info->expireTime);
            $info->isExpired = $info->isExpired ? '已过期' : '未过期';
            $info->ipAmount = $info->ipAmount ?: '无限量';
            $info->remainingAmount = $info->remainingAmount ?: '无限量';
        }

        $return_data = [
            'code' => 1,
            'data' => $list,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //短效全部套餐
    public function dx_order() {
        $order_model = new OrderModel();
        $order_where = [
            'package_type' => ['in', HttpOrderEnum::$package_list],
            'order_status' => HttpOrderEnum::$pay_yes,
            'user_id' => $this->userinfo['UserId']
        ];
        $order_list = $order_model->getListPage($order_where);

        foreach ($order_list as &$info) {
            $info['order_type'] = HttpOrderEnum::$order_type[$info['order_type']];
            $info['package_type'] = HttpOrderEnum::$package_type[$info['package_type']];
            $info['durationType'] = HttpOrderEnum::$durationType[$info['durationType']];
        }

        $return_data = [
            'code' => 1,
            'data' => $order_list,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //用户短效每日使用情况查询
    public function dx_used() {
        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
        ];
        $url = $this->url_root . '/user/short/ip/used/summary';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $return_data = [
            'code' => 1,
            'data' => $data->d,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }
    //用户短效每日使用情况查询
    public function dx_used_log() {

        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
            'page' => isset($request_data['page']) ? $request_data['page'] : 1,
            'limit' => 50,
            'packType' => $request_data['packType'],
        ];
        $url = $this->url_root . '/user/short/ip/used/list';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));

        foreach ($data->d->items as &$info) {
            $info->packType = HttpOrderEnum::$package_type[$info->packType];
            $info->createTime = date('Y-m-d H:i:s', $info->createTime);
        }
        $return_data = [
            'code' => 1,
            'data' => $data,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //长效ip列表
    public function lang_ip_list() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];
        $request_data['sgUserId'] =  $this->http_user_info['sgUserId'];
        $url = $this->url_root . '/user/long/ip/list';

        foreach ($request_data as $key => &$info) {
            if (strlen($info) == 0) {
                unset($request_data[$key]);
            }
        }

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));

        $line_status = [
            '1' => '正常',
            '0' => '异常',
            '9' => '禁用',
        ];

        foreach ($data->d->items as &$info) {
            $info->lineType2 = $info->lineType;
            $info->lineType = ($info->lineType == 1) ? '普通' : '优质';
            $info->expireTime = date('Y-m-d H:i:s', $info->expireTime);
            $info->shareType2 = $info->shareType;
            $info->shareType = ($info->shareType == 1) ? '共享' : '独享';
            $info->state = $line_status[$info->state];
        }

        $return_data = [
            'code' => 1,
            'data' => $data,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //长效全部套餐
    public function cx_package() {
        $list = [];
        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
        ];
        $url = $this->url_root . '/user/long/pack/list';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $list = array_merge($data->d->packs);

        foreach ($list as &$info) {
            $info->isAbroad2 = $info->isAbroad;
            $info->isAbroad = HttpOrderEnum::$game_type[$info->isAbroad];
            $info->expireTime = date('Y-m-d H:i:s', $info->expireTime);
            $info->isExpired = $info->isExpired ? '已过期' : '未过期';
            $info->shareType = ($info->shareType == 1) ? '共享' : '独享';
        }

        $return_data = [
            'code' => 1,
            'data' => $list,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //长效全部订单
    public function cx_order() {
        $order_list = [];
        $order_model = new OrderModel();
        $order_where = [
            'package_type' => HttpOrderEnum::$cxyx,
            'order_status' => HttpOrderEnum::$pay_yes,
            'user_id' => $this->userinfo['UserId']
        ];
        $order_list = $order_model->getListPage($order_where);

        foreach ($order_list as &$info) {
            $info['order_type'] = HttpOrderEnum::$order_type[$info['order_type']];
            $info['package_type'] = HttpOrderEnum::$package_type[$info['package_type']];
            $info['game_type'] = HttpOrderEnum::$game_type[$info['game_type']];
        }

        $return_data = [
            'code' => 1,
            'data' => $order_list,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //长效ip切换记录
    public function cx_switch() {
        ini_set('display_errors', 'off');
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
            'page' => isset($request_data['page']) ? $request_data['page'] : 1,
            'limit' => 50,
        ];
        $url = $this->url_root . '/user/long/switch/ip/log';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));

        //切换原因，0-系统自动切换，1-失效手动切换，2-免费手动切换，5-管理员免费切换，6-收费手动切换
        $reason = [
            0 => '系统自动切换，1',
            1 => '失效手动切换，2',
            2 => '免费手动切换',
            5 => '管理员免费切换',
            6 => '收费手动切换',
        ];

        foreach ($data->d->items as &$info) {
            $info->createTime = date('Y-m-d H:i:s', $info->createTime);
            $info->reason = $reason[$info->reason];
        }

        $return_data = [
            'code' => 1,
            'data' => $data,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //用户白名单列表
    public function white_list() {
        $list = [];
        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
        ];
        $url = $this->url_root . '/user/short/whitelist';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $list = $data->d->items;

        foreach ($list as &$info) {
            $info->isLocked = $info->isLocked ? '已锁定' : '未锁定';
        }

        $return_data = [
            'code' => 1,
            'data' => $list,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //用户创建白名单
    public function create_white_ip() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];

        $url = $this->url_root . '/user/short/whitelist/create';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $return_data = [
            'code' => 1,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //用户锁定ip
    public function lock_ip() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];

        $url = $this->url_root . '/user/short/whitelist/lock';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $return_data = [
            'code' => 1,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //用户删除ip
    public function delete_ip() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];

        $url = $this->url_root . '/user/short/whitelist/delete';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $return_data = [
            'code' => 1,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    //提取长效ip
    public function get_lang_ip() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];
        $request_data['ids'] = trim($request_data['ids'], ',');

        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];
        $url = $this->url_root . '/user/long/ip/batch/get';

        $request_data = formatHttpRequert($request_data);

        echo (linkcurl($url, 'GET', $request_data, [], 0));
    }

    // 短效无限量白名单列表
    public function white_list_su() {
        $list = [];
        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
        ];
        $url = $this->url_root . '/user/short/unlimited/whitelist';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        if ($data->s < 0) {
            echo json_encode([
                'code' => 0,
                'msg' => $data->m,
            ]);
            return;
        }

        $list = $data->d->items;
        foreach ($list as &$info) {
            $info->isLocked = $info->isLocked ? '已锁定' : '未锁定';
        }

        $return_data = [
            'code' => 1,
            'data' => $list,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    // 短效无限量白名单创建
    public function create_white_ip_su() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];

        $url = $this->url_root . '/user/short/unlimited/whitelist/create';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $return_data = [
            'code' => 1,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    // 短效无限量白名单删除
    public function delete_ip_su() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];

        $url = $this->url_root . '/user/short/unlimited/whitelist/delete';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $return_data = [
            'code' => 1,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    // 短效无限量白名单切换锁定
    public function lock_ip_su() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];

        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];

        $url = $this->url_root . '/user/short/unlimited/whitelist/lock';

        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        $return_data = [
            'code' => 1,
            'msg' => '',
        ];

        echo json_encode($return_data);
    }

    // 短效无限量白名单查询上限
    public function white_limit_get_su() {
        $request_data = [
            'sgUserId' => $this->http_user_info['sgUserId'],
        ];

        $url = $this->url_root . '/user/short/unlimited/whitelist/maxAmount';
        $request_data = formatHttpRequert($request_data);

        $data = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        echo json_encode([
            'code' => 1,
            'data' => $data->d->maxAmount,
            'msg' => '',
        ]);
    }

    // 短效无限量白名单修改上限
    public function white_limit_set_su() {
        $request_data = json_decode(file_get_contents("php://input"), true)['data'];
        $request_data['sgUserId'] = $this->http_user_info['sgUserId'];

        $url = $this->url_root . '/user/short/unlimited/whitelist/edit/maxAmount';
        $request_data = formatHttpRequert($request_data);

        $resp = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        if ($resp->s < 0) {
            echo json_encode([
                'code' => -1,
                'msg' => $resp->m,
            ]);
            return;
        }

        echo json_encode([
            'code' => 1,
            'data' => $resp,
            'msg' => '',
        ]);
    }

    // 用户短效当前可用的无限量套餐详情
    public function get_unlimited_available() {
        $url = $this->url_root . '/user/short/pack/unlimited/available';
        $request_data = formatHttpRequert([
            'sgUserId' => $this->http_user_info['sgUserId'],
        ]);

        $resp = json_decode(linkcurl($url, 'POST', $request_data, [], 0));
        if ($resp->s < 0) {
            echo json_encode([
                'code' => -1,
                'msg' => $resp->m,
            ]);
            return;
        }

        echo json_encode([
            'code' => 1,
            'data' => $resp->d,
        ]);
    }
}
