<?php

namespace app\user\controller;

use think\Validate;
use think\Lang;

class Certification extends UserControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/member.lang.php');
    }

    /**
     * 用户信息
     * @return mixed
     */
    public function index()
    {
        $user_info = $this->getAdminInfo();
        $user = db('user')->where('user_email',$user_info['user_email'])->find();
        if($user['user_status'] ==1){
            $user['user_status'] = '已实名认证';
        }
        if($user['user_status'] ==-1){
            $user['user_status'] = '未实名认证';
        }
        if($user['user_status'] ==2){
            $user['user_status'] = '实名认证中';
        }

        $this->assign('user', $user);
        return $this->fetch();
    }

    /**
     * 个人实名认证
     * @return mixed
     */
    public function personal()
    {
        $user_info = $this->getAdminInfo();
        if (request()->isPost()) {
            $name = input('post.name');
            $identity = input('post.identity');
            $bank_card = input('post.bank_card');
            $debit_card = input('post.debit_card');
            $ch_box = input('post.ch_box');

            // 协议 校验
            if ($ch_box == false) {
                //验证失败
                $this->error('未勾选协议');
            }

            $update_info = array(
                'userInfo_name' => $name,
                'userInfo_identity' => $identity,
                'userInfo_bank_card' => $bank_card,
                'userInfo_debit_card' => $debit_card
            );

            $imgurl = DS_THEME_STYLE_URL . 'images/certification';
            $numurl = '/static/user/images/certification';
            $img_front = request()->file('img_front');
            $img_back = request()->file('img_back');
            if($img_front){
                $imgname = time().'_1.jpg';
                $file = $img_front->setSaveName($imgname);
                $imgo = $file->move($imgurl, $savename = $imgname, $replace = true);
                if($imgo){
                    $update_info['img_front'] =$numurl.'/'.$imgname;
                }
            }
            if($img_back){
                $imgname = time().'_2.jpg';
                $file = $img_back->setSaveName($imgname);
                $imgo = $file->move($imgurl, $savename = $imgname, $replace = true);
                if($imgo){
                    $update_info['img_back'] =$numurl.'/'.$imgname;
                }
            }
            $res = db('userinfo')->where('user_id', $user_info['user_id'])->update($update_info);
            if($res == 1){
                $date = array(
                    'user_status' => 0
                );
                $result = db('user')->where('user_id', $user_info['user_id'])->update($date);
                if($result == 1){
                    return $this->redirect('User/Certification/index');
                }
            }

        }else{
            $user = db('user')->where('user_email',$user_info['user_email'])->find();
            $userinfo = db('userinfo')->where('user_id',$user_info['user_id'])->find();
            if($user['user_status'] ==1){
                $user['user_status_cn'] = '已实名认证';
            }
            if($user['user_status'] ==-1){
                $user['user_status_cn'] = '未实名认证';
            }
            if($user['user_status'] ==0){
                $user['user_status_cn'] = '实名认证中';
            }

            if($user['user_type'] ==1){
                $user['user_type_cn'] = '个人用户';
            }
            if($user['user_type'] ==2){
                $user['user_type_cn'] = '企业用户';
            }
            $this->assign('user', $user);
            $this->assign('userinfo', $userinfo);
            return $this->fetch();
        }
    }


    /**
     * 企业实名认证
     * @return mixed
     */
    public function enterprise()
    {
        $user_info = $this->getAdminInfo();
        if (request()->isPost()) {
            $enterprise = input('post.enterprise');
            $area = input('post.area');
            $enterprise_location = input('post.enterprise_location');
            $enterprise_address = input('post.enterprise_address');
            $contact_name = input('post.contact_name');
            $whether = input('post.whether');
            $credit_code = input('post.credit_code');
            $bank_location = input('post.bank_location');
            $bank = input('post.bank');
            $bankbranch = input('post.bankbranch');
            $debit_card = input('post.debit_card');
            $ch_box = input('post.ch_box');

            // 协议 校验
            if ($ch_box == false) {
                //验证失败
                $this->error('未勾选协议');
            }

            $update_info = array(
                'enterprise' => $enterprise,
                'area' => $area,
                'enterprise_location' => $enterprise_location,
                'enterprise_address' => $enterprise_address,
                'contact_name' => $contact_name,
                'whether' => $whether,
                'credit_code' => $credit_code,
                'bank_location' => $bank_location,
                'bank' => $bank,
                'bankbranch' => $bankbranch,
                'userInfo_debit_card' => $debit_card
            );

            $imgurl = DS_THEME_STYLE_URL . 'images/certification';
            $numurl = '/static/user/images/certification';
            $file = request()->file('images');
//            print_r($file->getInfo());
//            echo $file->getInfo()['name'];
            if($file){
                $imgname = $file->getInfo()['name'];
                $file = $file->setSaveName($imgname);
                $imgo = $file->move($imgurl, $savename = $imgname, $replace = true);
                if($imgo){
                    $update_info['businesslicense'] = $numurl.'/'.$imgname;
                }
            }
//            print_r($update_info);
            $date = array(
                'user_status' => 0
            );
            $res = db('userinfo')->where('user_id', $user_info['user_id'])->update($update_info);
            $result = db('user')->where('user_id', $user_info['user_id'])->update($date);
            return $this->redirect('User/Certification/index');

        }else{
            $user = db('user')->where('user_email',$user_info['user_email'])->find();
            $userinfo = db('userinfo')->where('user_id',$user_info['user_id'])->find();
            if($user['user_status'] ==1){
                $user['user_status_cn'] = '已实名认证';
            }
            if($user['user_status'] ==-1){
                $user['user_status_cn'] = '未实名认证';
            }
            if($user['user_status'] ==0){
                $user['user_status_cn'] = '实名认证中';
            }

            if($user['user_type'] ==1){
                $user['user_type_cn'] = '个人用户';
            }
            if($user['user_type'] ==2){
                $user['user_type_cn'] = '企业用户';
            }
            $this->assign('user', $user);
            $this->assign('userinfo', $userinfo);
            return $this->fetch();
        }
    }


}
