# !/bin/bash
# ████████╗░█████╗░██╗░░██╗░█████╗░██╗░░░██╗██████╗░███╗░░██╗
# ╚══██╔══╝██╔══██╗██║░██╔╝██╔══██╗██║░░░██║██╔══██╗████╗░██║
# ░░░██║░░░██║░░██║█████═╝░██║░░██║╚██╗░██╔╝██████╔╝██╔██╗██║
# ░░░██║░░░██║░░██║██╔═██╗░██║░░██║░╚████╔╝░██╔═══╝░██║╚████║
# ░░░██║░░░╚█████╔╝██║░╚██╗╚█████╔╝░░╚██╔╝░░██║░░░░░██║░╚███║
# ░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝░╚════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝░░╚══╝

dateFromServer=$(curl -v --insecure --silent https://google.com/ 2>&1 | grep Date | sed -e 's/< Date: //')
biji=$(date +"%Y-%m-%d" -d "$dateFromServer")
RED='\033[0;31m'
NC='\033[0m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHT='\033[0;37m'
red='\e[1;31m'
green='\e[0;32m'
NC='\e[0m'
BGWHITE='\e[0;100;37m'
green() { echo -e "\\033[32;1m${*}\\033[0m"; }
red() { echo -e "\\033[31;1m${*}\\033[0m"; }

clear
NUMBER_OF_CLIENTS=$(grep -c -E "^### " "/etc/xray/config.json")

if [[ ${NUMBER_OF_CLIENTS} == '0' ]]; then
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo -e "\E[40;1;37m          LIST MEMBER V2RAY VMESS         \E[0m"
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo -e ""
    echo -e "You Don't Have a Vmess Account!"
    echo -e ""
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    read -n 1 -s -r -p "Press any key to back"
    m-vmess
fi

clear
echo -e "\e[33m──────────────────────────────────────────\033[0m"
echo -e "\E[40;1;37m          LIST MEMBER V2RAY VMESS         \E[0m"
echo -e "\e[33m──────────────────────────────────────────\033[0m"
echo -e "\E[40;1;37mUSERNAME          EXP DATE          STATUS\E[0m"
echo -e "\e[33m──────────────────────────────────────────\033[0m"

# Mengambil dan mengurutkan berdasarkan username (alphabetically)
declare -A user_map
declare -A user_status

current_date=$(date +%Y-%m-%d)  # Mendapatkan tanggal saat ini

while read -r line; do
    user=$(echo "$line" | cut -d ' ' -f 2)
    exp=$(echo "$line" | cut -d ' ' -f 3)
    
    # Cek status dengan mencari kunci "LOCKED" dalam konfigurasi
    status=$(grep -w "^### $user" -A 1 "/etc/xray/config.json" | sort | uniq | grep -o "LOCKED" || echo "UNLOCKED")
    
    user_map["$user"]="$exp"
    user_status["$user"]="$status"
done < <(grep -E "^### " "/etc/xray/config.json" | sort | uniq)

# Menampilkan username, expiration date, dan status secara terurut
for user in $(echo "${!user_map[@]}" | tr ' ' '\n' | sort); do
    exp="${user_map[$user]}"
    status="${user_status[$user]}"
    
    # Menghitung selisih hari antara tanggal sekarang dan tanggal exp
    exp_timestamp=$(date -d "$exp" +%s)  # Tanggal exp dalam format timestamp
    current_timestamp=$(date -d "$current_date" +%s)  # Tanggal sekarang dalam format timestamp
    diff_days=$(( (exp_timestamp - current_timestamp) / 86400 ))  # Menghitung selisih dalam hari
    
    if [[ $diff_days -le 1 ]]; then
        # Jika exp kurang dari atau sama dengan 1 hari, warnai merah
        printf "${RED}%-15s %-10s %-10s${NC}\n" "$user" " $exp" "      $status"
    else
        # Tampilkan dengan warna normal
        printf "%-15s %-10s %-10s\n" "$user" " $exp" "      $status"
    fi
done

echo -e "\e[33m──────────────────────────────────────────\033[0m"
echo -e "\E[1;37mTOTAL MEMBER VMESS : ${#user_map[@]} USERS\E[0m"
echo -e "\e[33m──────────────────────────────────────────\033[0m"
echo -e ""
read -n 1 -s -r -p "Press Any Key To Back"
m-vmess
