<?php

namespace app\admin\controller;

use think\Validate;
use think\Lang;
use app\admin\model\Userinfo;
use app\common\model\User as UserModel;
use  app\admin\model\CapitalMovements;

class User extends AdminControl
{

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

    /**
     * 用户列表
     * @return mixed
     */
    public function index()
    {
        $model_user = new UserModel();
        $title = input('post.title');
        $time = input('post.timeRang');
        if (!empty($time)) {
            $gap = explode(' - ', $time);
            $begin = strtotime($gap[0]);
            $end = strtotime($gap[1]);
        }
        $condition = array();
        if ($title) {
            $condition['user_email|user_phone'] = $title;
        }
        if ($time && $begin && $end) {
            $condition['user_addTime'] = array('between', array($begin, $end));
        }
        $user_list = $model_user->getUserList($condition, '*', 10);
        for ($i = 0; $i < count($user_list); $i++) {
            $user_list[$i]['user_addTime'] = date("Y-m-d H:i:s", $user_list[$i]['user_addTime']);

            if ($user_list[$i]['user_status'] == -1) {
                $user_list[$i]['user_status'] = '未实名认证';
            }
            if ($user_list[$i]['user_status'] == 2) {
                $user_list[$i]['user_status'] = '实名认证中';
            }
            if ($user_list[$i]['user_status'] == 1) {
                $user_list[$i]['user_status'] = '已实名认证';
            }
            if ($user_list[$i]['user_status'] == 3){
                $user_list[$i]['user_status'] = '实名认证失败';
            }

            if ($user_list[$i]['user_type'] == 1) {
                $user_list[$i]['user_type'] = '个人用户';
            }
            if ($user_list[$i]['user_type'] == 2) {
                $user_list[$i]['user_type'] = '企业用户';
            }
        }
        $allpower = $this->qxhans();
        $this->assign('allpower', $allpower);
        $this->assign('user_list', $user_list);
        $this->assign('show_page', $model_user->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    public function userinfo()
    {
        $uid = intval(input("uid", 0));
        $model = (new Userinfo())->findOne($uid, 1);
        $this->assign('userinfo', $model);
        return $this->fetch();
    }

    public function  usercapitalmovements(){
        $uid = intval(input('uid'),0);
        $model_user = new CapitalMovements();
        $list = $model_user->where(['user_id'=>$uid])->order('capitalMovements_id','desc')->paginate(10);

        $this->assign('list', $list);
        $this->assign('show_page', $list->render());

        return $this->fetch();
    }


    public function recharge()
    {
        $money = input('post.money');
        $user_id = input('post.user_id');
        $type = input('post.type');
        if ($money && $user_id) {
            $userInfo = Model('userinfo');
            $userMessage = Model('userMessage');
            $capitalMovements = Model('CapitalMovements');
            $getUserInfo = $userInfo->getUserInfo(['user_id' => $user_id]);
            $userInfo->startTrans();
            $userMessage->startTrans();
            $capitalMovements->startTrans();
            try {
                if ($type == 1) {
                    $balance = $getUserInfo->userInfo_money + $money;
                    $msg = '充值';
                } else {
                    $balance = $getUserInfo->userInfo_money - $money;
                    $msg = '扣款';
                }
                $userInfo->updateUserInfo(['user_id' => $user_id], ['userInfo_money' => $balance]);
                $nowDate = date('Y-m-d H:i:s');
                $userMessageData = [
                    'user_id' => $user_id,
                    'userMessage_title' => '充值消息',
                    'userMessage_content' => '后台管理员' . $msg . $money . '元',
                    'userMessage_create' => $nowDate,
                    'userMessage_update' => $nowDate,
                ];
                $userMessage->addMessage($userMessageData);
                $orderID = OrderID();
                $admin = session('admin_id');
                $cplMvtData = [
                    'user_id' => $user_id,
                    'capitalMovements_describe' => '后台管理员' . $msg . $money . '元',
                    'capitalMovements_identity' => $orderID,
                    'capitalMovements_create' => $nowDate,
                    'capitalMovements_update' => $nowDate,
                    'capitalMovements_money' => $money,
                    'capitalMovements_type' => $type,
                    'capitalMovements_cash' => $balance,
                    'capitalMovements_operator' => $admin,
                ];
                $capitalMovements->add($cplMvtData);
                $userInfo->commit();
                $userMessage->commit();
                $capitalMovements->commit();
                return ["msg" => $msg . "成功"];
            } catch (\Exception $e) {
                // 回滚事务
                $userInfo->rollBack();
                $userMessage->rollBack();
                $capitalMovements->rollBack();
                return ["msg" => "操作失败"];
            }
        } else {
            return ["msg" => "错误：请正确填写充值金额"];
        }
    }

}
