<?php

namespace App\Http\Controllers;

use App\Models\VipLog;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Crypt;

class CryptController extends Controller
{
    public function getCryptData(Request $request)
    {
        $store_id = $request->input('store_id');
        $mobiles = $request->input('mobiles',[]);
        $uids = $request->input('list',[]);
        $phones = [];
        $list = User::whereIn('id',$uids)->get()->toArray();
        foreach($list as $k=>$v){
            if(array_key_exists("phone",$v) && $v['phone']){
                if(strstr($v['phone'],'****' )){
                    if(array_key_exists("phone_code",$v) && $v['phone_code']){
                        $phone = Crypt::decryptString($v['phone_code']);
                    }else{
                        $end = substr($v['openId1'], -4);
                        $phone = str_replace('****',$end,$v['phone']);
                    }
                }else {
                    $phone = $v['phone'];
                }
                if(!in_array($phone, $phones)){
                    $phones[] = $phone;
                    if(in_array($phone, $mobiles)){
                        unset($list[$k]);
                    }else{
                        $vip_log=VipLog::where('user_id',$v['id'])->where('store_id',$store_id)->first();
                        if($vip_log){
                            $list[$k]['is_vip']=1;
                        }else{
                            $list[$k]['is_vip']=0;
                        }
                    }
                }else{
                    unset($list[$k]);
                }
            }else{
                unset($list[$k]);
            }
        }
        $ids = array_column($list, 'id');
        return $ids;
    }

    public function getCryptDataStore(Request $request){
        $users = $request->input('users',[]);
        $phone = $request->input('phone');
        $user = [];
        Log::info('----------------start------------------');
        Log::info($phone);
        foreach($users as $val){
            if(array_key_exists('phone', $val) && $val['phone']){
                if(strstr($val['phone'],'****' )){
                    if(array_key_exists('phone_code', $val) && $val['phone_code']){
                        $pho = Crypt::decryptString($val['phone_code']);
                    }else{
                        $end = substr($val['openId1'], -4);
                        $pho = str_replace('****',$end,$val['phone']);
                    }
                }else {
                    $pho = $val['phone'];
                }
            }
            if($phone == $pho){
                $user = $val;
                break;
            }
        }
        Log::info('----------------end------------------');
        return $user;
    }

}
