<?php

namespace app\admin\controller\user;

use app\admin\controller\AuthController;
use think\facade\Route as Url;
use crmeb\traits\CurdControllerTrait;
use app\admin\model\user\User as UserModel;
use app\admin\model\user\UserBill as UserBillAdmin;
use app\admin\model\system\{SystemUserLevel,SystemUserTask};
use crmeb\services\{UtilService,JsonService,FormBuilder as Form};
/**
 * 会员设置
 * Class UserLevel
 * @package app\admin\controller\user
 */
class UserAuth extends AuthController
{
    use CurdControllerTrait;

    /*
     * 审核展示
     * */
    public function index()
    {
        return $this->fetch();
    }

    /*
     * 创建form表单
     * */
    public function create($uid =0)
    {
		if ($uid) $vipinfo = UserModel::get($uid);
        $field[] = Form::input('off', '拒绝理由', isset($vipinfo) ? $vipinfo->off: '')->col(Form::col(24));
        $form = Form::make_post_form('添加拒绝理由', $field, Url::buildUrl('save', ['uid' => $uid]), 2);
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

    /*
     * 审核/拒绝
     * @param $uid 审核的用户uid
     * @return json
     * */
    public function save($uid = 0)
    {
        $data = UtilService::postMore([
            ['off', ''],
        ]);
        if (!$data['off']) return JsonService::fail('请输入拒绝理由');
		if($data['off']){
			// 查询是否已审核
			$res1=UserModel::where(['uid' => $uid])->find();
			if($res1['is_auth']==2||$res1['is_auth']==3){
				 return JsonService::fail('该信息已审核！不可操作');
			}
			$res=UserModel::where(['uid' => $uid])->update(['off' =>$data['off'],'is_auth'=>"3"]);
			if($res>0){
				return JsonService::successful('审核成功');
			}else{
				 return JsonService::fail('该信息已审核!不可重复');
			}
		}
    }
	
	/*
	 * 审核通过
	 * @param int $id
	 * */
	public function pass($uid = 0){
		 if($uid){
			// 查询是否已审核
			$res1=UserModel::where(['uid' => $uid])->find();
			if($res1['is_auth']==2||$res1['is_auth']==3){
				 return JsonService::fail('该信息已审核！不可操作');
			}
			$res=UserModel::where(['uid' => $uid])->update(['is_auth' =>2,'f_integral'=>3000,'off'=>"", 'is_new' => 1]);
			if($res>0){
				// 添加积分充值记录
				$res2=UserBillAdmin::income('实名认证赠送',$uid, 'integral', 'top_add', 3000, 0, 3000, '首次实名赠送(竞拍消费满30000释放)');
				return JsonService::successful('审核成功');
				
			}else{
				 return JsonService::fail('审核失败');
			} 
		 }else{
			 return JsonService::fail('参数错误！'); 
		 }
		
		
		
		
		
	}
    /*
     * 获取列表
     * @param int page
     * @param int limit
     * */
    public function get_system_user_list()
    {
       $where =UtilService::getMore([
           ['page', 1],
           ['limit', 20],
		   ['is_auth','<>',0]
       ]);
        return JsonService::successlayui(UserModel::authlist($where));
    }
  
}