<?php

namespace app\home\service;

use app\model\CountryModel;
use app\model\FileModel;
use app\model\UserModel;
use app\model\UserVerifyLogModel;
use app\utility\UnqId;

class UserVerifyService extends BaseHomeService
{

    public function add($params, $userId)
    {
        try {
            // 参数校验
            if(empty($params['name']) || !is_string($params['name'])){
                return $this->toData('100400','Invalid name');
            }

            if(empty($params['code']) || !is_string($params['code'])){
                return $this->toData('100400','Invalid ID number');
            }

            if(empty($params['country']) || !is_numeric($params['country'])){
                return $this->toData('100400','Invalid country');
            }

            if(empty($params['front_img']) || !is_numeric($params['front_img'])){
                return $this->toData('100400','Invalid front_img');
            }

            if(empty($params['back_img']) || !is_numeric($params['back_img'])){
                //return $this->toData('100400','Invalid back_img');
            }

            // 判断用户状态
            $user = UserModel::where('user_id', $userId)->find();
            if(empty($user)){
                return $this->toData('100400','param error');
            }

            // 判断是是否已经认证
            if($user->is_real == 1){
                return $this->toData('100400','Already real-name authenticated');
            }

            // 是否是未认证或者认证失败状态
            if($user->real_status == 2 || $user->real_status == 3){
                return $this->toData('100400','status error');
            }

            $country = CountryModel::where('id',$params['country'])->find();
            if(empty($country)){
                return $this->toData('100400','country error');
            }

            // 图片记录
            $front = FileModel::where('id',$params['front_img'])->find();
            if(empty($front)){
                return $this->toData('100400','front_img error');
            }

            $back = FileModel::where('id',$params['back_img'])->find();
            if(empty($back)){
                //return $this->toData('100400','back_img error');
            }

            //锁屏密码
            $lockPassword = '';
            if (!empty($params['lock_password'])) {
                $lockPassword = $params['lock_password'];
//                $salt = env('ENCRYPT.SALT');
//                $enLockPassword = (new UnqId())->encryptPassword($lockPassword, $salt);
            }


            // 写入数据库
            $userVerify = new UserVerifyLogModel;
            $userVerify->user_id = $userId;
            $userVerify->country = $params['country'];
            $userVerify->code = $params['code'];
            $userVerify->name = $params['name'];
            $userVerify->front_img = $params['front_img'];
            $userVerify->back_img = isset($params['back_img']) ? $params['back_img'] :'';
            if (!empty($lockPassword)) $userVerify->lock_password = $lockPassword;
            $userVerify->create_time = date('Y-m-d H:i:s');
            $userVerify->update_time = date('Y-m-d H:i:s');
            $userVerify->save();

            // 更改用户状态
            $user->real_status = 2;
            $user->save();

            return $this->toData('0','SUCCESS');

        }catch (\Exception $exception){
            return $this->toData('100500','The system is busy.');
        }
    }

    public function detail($userId)
    {
        try {
            $user = UserModel::where('user_id', $userId)->find();
            if(empty($user)){
                return $this->toData('100400','param error');
            }

            $log = UserVerifyLogModel::where('user_id', $userId)->order('id', 'desc')->find();

            $data = [];
            if(!empty($log)){
                $front = FileModel::where('id', $log->front_img)->value('path');
                $back = FileModel::where('id', $log->back_img)->value('path');
                $country = CountryModel::where('id', $log->country)->find();
                $data = [
                    'name' => $log->name,
                    'code' => $log->code,
                    'country_data' => $country,
                    'front_img' => $front,
                    'back_img' => $back,
                    'status' => $log->status,
                ];
            }

            return $this->toData('0', 'SUCCESS', $data);
        }catch (\Exception $exception){
            return $this->toData('100500','The system is busy.');
        }
    }
}