<?php

namespace app\product\controller;

use app\product\model\Product as PoductModel;
use app\product\model\ProductAccount as AccountModel;
use app\product\model\User as UserModel;
use app\agent\model\ProductPackage as PoductPackageModel;

class OpenProduct
{
    /**
     * @description: 获取数据
     * @param {*}
     * @return {*}
     */
    public function getOpenList()
    {
        $product = new PoductModel;
        $list = $product->getList(['OnLine' => 1], 'Id,Name', 'Sort asc');
        \result($list);
    }


    public function getProductPackageList()
    {
        $product = new PoductModel;
        $package = new PoductPackageModel;
        $product_list = $product->getList(['OnLine' => 1], 'Id,Name', 'Sort asc');
        $package_list = $package->getList(['Status' => 1], 'Id,ProductId,Name', 'ProductId asc');

        $product_id_name = [];
        foreach ($product_list as $v) {
            if ($v['Id'] == 28)  {
                continue;
            }
            $product_id_name[$v['Id']] = $v['Name'];
        }

        foreach ($package_list as $k => &$pakg) {
            if (isset($product_id_name[$pakg['ProductId']])) {
                $pakg['ProductName'] = $product_id_name[$pakg['ProductId']];
            } else {
                unset($package_list[$k]);
            }
        }
        $list = [
            "product_list" => $product_list,
            "package_list" => $package_list
        ];
        \result($list);
    }

    //根据账号搜索用户是否实名
    public function getVerify()
    {
        $account = json_decode(file_get_contents("php://input"), true)['account'];
        if(trim($account)){
            $account_model = new AccountModel;
            $user = new UserModel;

            $user_id = $account_model->getOne(['Account' => $account],'UserId,Account,ProductName');

            $user_info = $user->getOne(['Id'=>$user_id['UserId']],'Name,Phone,id_code,is_verify');
            
            $return_indo = [
                'product'=>$user_id['ProductName'],
                'account'=>$account,
                'name'=>mb_substr($user_info['Name'], 0, 1) . '**',
                'id_code'=>mb_substr($user_info['id_code'], 0, 3) . '***',
                'phone'=>mb_substr($user_info['Phone'], 0, 3) . '***',
                'verify'=> $user_info['is_verify'] == 1?'已通过支付宝扫脸认证':'',
            ];

            echo json_encode($return_indo);
        }
    }
}
