<?php
/*
 * @Descripttion:
 * @version:
 * @Author: kangkang
 * @Date: 2020-11-06 14:32:57
 * @LastEditors: “wanyongkang” “937888580@qq.com”
 * @LastEditTime: 2024-01-20 17:16:47
 */
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: *");
header('Access-Control-Allow-Headers:*');
header("Access-Control-Allow-Credentials: true");
$id = $_GET['product'];
switch ($id) {
    case 0:
        get_all_product(__DIR__ . '/data/download/all.csv');
        break;
    case 8:
        get_product(__DIR__ . '/data/download/xingxing.csv');
        break;
    case 6:
        get_product(__DIR__ . '/data/download/xianfeng.csv');
        break;
    case 13:
        get_product(__DIR__ . '/data/download/jike.csv');
        break;
    case 1:
        get_product(__DIR__ . '/data/download/qiangzi.csv');
        break;
    case 2:
        get_product(__DIR__ . '/data/download/xunlian.csv');
        break;
    case 14:
        get_product(__DIR__ . '/data/download/tiantian.csv');
        break;
    case 5:
        get_product(__DIR__ . '/data/download/laoying.csv');
        break;
    case 4:
        get_product(__DIR__ . '/data/download/jinrui.csv');
        break;
    case 10:
        get_product(__DIR__ . '/data/download/wuxian.csv');
        break;
    case 16:
        get_product(__DIR__ . '/data/download/shihui.csv');
        break;
    case 15:
        get_product(__DIR__ . '/data/download/gongxiang.csv');
        break;
    case 17:
        get_product(__DIR__ . '/data/download/xigua.csv');
        break;
    case 18:
        get_product(__DIR__ . '/data/download/mogu.csv');
        break;
    case 29:
        get_product(__DIR__ . '/data/download/jihu.csv');
        break;
    case 3:
        get_product(__DIR__ . '/data/download/jiguang.csv');
        break;
    case 19:
        get_product(__DIR__ . '/data/download/huohu.csv');
        break;
    
    case 22:
        get_product(__DIR__ . '/data/download/huohu.csv');
        break;
    
    case 23:
        get_product(__DIR__ . '/data/download/yunniu.csv');
        break;
        
    case 25:
        get_product(__DIR__ . '/data/download/qiangzijt.csv');
        break;
    case 26:
        get_product(__DIR__ . '/data/download/liebao.csv');
        break;
    case 27:
        get_product(__DIR__ . '/data/download/qilin.csv');
        break;
    
    case 28:
        get_product(__DIR__ . '/data/download/shuidi.csv');
        break;
}

function get_all_product($file_path)
{
    $list = [];
    $record = [];
    $user_count = 0;
    $count = 0;
    $file = fopen($file_path, 'r');
    while ($info = fgetcsv($file)) {
        $data['name'] = $info[0];
        $city = $info[1];
        $data['city'] = $city;
        $data['supply'] = $info[2]??'';
        $data['ip'] = $info[3]??'';
        $data['daikuan'] = $info[4]??'';
        $data['onlineuser'] = '';
        $data['maxuser'] = '';
        $data['online'] = $info[7]??'';
        $data['status'] = $info[8] ?? '';
        if (!empty($info[9])) {
            $user_count += $data['online'] == '正常' ? 1 : 0;
            $count++;
        }
        $data['nasname'] = $info[9]??'';
        $list[] = $data;
    }
    fclose($file);
    echo json_encode(['data' => $list, 'count' => $count, 'use_count' => $user_count]);
}

function get_product($file_path, $dns = '')
{
    $list1 = [];
    $list = [];
    $user_count = 0;
    $count = 0;
    $key = '';
    $file = fopen($file_path, 'r');
    $record = [];
    while ($info = fgetcsv($file)) {
        if (in_array($info['1'], ['曾', '龙'])) {
            continue;
        }
        if (empty($info[9])) {
            $key = $info[1];
        }
        $data['name'] = $info[0];
        $city = $info[1];
        $data['city'] = $city;
        $data['supply'] = $info[2]??'';
        $data['ip'] = $info[3]??'';
        $data['daikuan'] = $info[4]??'';
        $data['onlineuser'] = '';
        $data['maxuser'] = '';
        $data['online'] = $info[7]??'';
        $data['status'] = $info[8] ?? '';
        if (!empty($info[9])) {
            $user_count += $data['online'] == '正常' ? 1 : 0;
            $count++;
        }
        $data['nasname'] = $info[9]??'';
        $list1[$key][] = $data;
    }

    foreach ($list1 as $k => $val) {
        foreach ($val as $net_data) {
            $list[] = $net_data;
        }
    }
    fclose($file);
    echo json_encode(['data' => $list, 'count' => $count, 'use_count' => $user_count]);
}
