<?php
/**
 *
 * User: anyluck
 * Date: 2020/6/3
 * Time: 13:13
 */

namespace app\web\controller;


use app\common\controller\Orde;
use app\common\model\About;
use app\common\model\Bond;
use app\common\model\BondFinance;
use app\common\model\CommissionFinance;
use app\common\model\Feedback;
use app\common\model\GeneralFinance;
use app\common\model\GeneralTransfer;
use app\common\model\Message;
use app\common\model\Notice;
use app\common\model\Problem;
use app\common\model\System;
use app\common\model\User;
use app\common\model\UserLevel;
use app\common\model\UserMessage;
use app\common\model\VendibilityFinance;
use app\web\model\Orders;
use app\web\model\Team;
use app\web\model\Tripartite;
use think\facade\Db;


class My extends Base
{
    // todo 个人中心

    // 获取个人信息
    public function index()
    {

        // 给用户加活跃度
        $where["id"]=$this->user_id;
        $update["update_time"]=time();
        User::update($update,$where);

        json_result(200, "", $this->user);
    }


    // 更新头像
    public function update_image()
    {
        $image = input("image");
        if (!$image) json_result(400, "请上传图片路径");
        $where["id"] = $this->user_id;
        $update["image"] = $image;
        User::update($update, $where);
        json_result(200, "设置成功");
    }


    // 更新用户名
    public function update_name()
    {
        $name = input("name");
        $where["id"] = $this->user_id;
        $update["name"] = $name;
        User::update($update, $where);
        json_result(200, "设置成功");
    }

// 更新手机号码
    public function update_phone()
    {
        $phone = trim(input("phone"));
        if (!$phone) json_result(400, "请输入手机号");
        $code = trim(input("code"));
        if (!$code) json_result(400, "请输入验证码");

        $wheres["phone|mail"] = $phone;
        $user = User::where($wheres)->count();
        $phone_email= Orders::phone_email();
        if ($user>=$phone_email) {
            json_result(400, "账号手机号或邮箱已经注册超过".$phone_email);
        }

        $rest = Tripartite::push_code($phone, 0);
        if ($rest != $code) {
            json_result(400, "验证码错误");
        }
        $where["id"] = $this->user_id;
        $update["phone"] = $phone;
        User::update($update, $where);
        json_result(200, "设置成功");
    }

// 更新邮箱
    public function update_mail()
    {
        $phone = trim(input("mail"));
        if (!$phone) json_result(400, "请输入邮箱");
        $code =trim(input("code"));
        if (!$code) json_result(400, "请输入验证码");

        $wheres["phone|mail"] = $phone;
        $user = User::where($wheres)->count();
        $phone_email= Orders::phone_email();
        if ($user>=$phone_email) {
            json_result(400, "账号手机号或邮箱已经注册超过".$phone_email);
        }

        $rest = Tripartite::push_code($phone, 1);
        if ($rest != $code) {
            json_result(400, "验证码错误");
        }

        $where["id"] = $this->user_id;
        $update["mail"] = $phone;
        User::update($update, $where);
        json_result(200, "设置成功");
    }

    // 显示收账信息
    public function receivable_show()
    {

        $where["user_id"] = $this->user_id;
        $user_message = UserMessage::where($where)->find();
        if (!$user_message) {// 没有数据就添加数据
            $add["user_id"] = $this->user_id;
            UserMessage::create($add);
            $user_message = UserMessage::where($where)->find();
        }
        json_result(200,"",$user_message);

    }

    // 修改收账信息
    public function update_receivable()
    {
        $id = input("id");
        if (!$id) json_result(400, "请上传更新id");
        $data["phone"] = trim(input("phone"));
        $data["name"] = trim(input("name"));
        $data["bank_name"] =trim(input("bank_name"));
        $data["bank_number"] = trim(input("bank_number"));
        $data["bank_branch"] = trim(input("bank_branch"));
        $data["ali_pay"] = trim(input("ali_pay"));
        $data["wx_pay"] =trim( input("wx_pay"));
        $data["ali_pay_image"] = input("ali_pay_image");
        $data["wx_pay_image"] = input("wx_pay_image");
        $where["id"] = $id;
        UserMessage::update($data, $where);
        json_result(200, "操作成功");
    }


    // 消息 获取最新的三条数据
    public function notice()
    {

        $notice = Message::where(["type" => 0,"user_id"=>$this->user_id])->order("id desc")->find(); // 公告

        $message_jiaoyi = Message::where(["type" => 2,"user_id"=>$this->user_id])->order("id desc")->find();// 交易
        $message_zijin = Message::where(["type" => 1,"user_id"=>$this->user_id])->order("id desc")->find();// 资金
        $data["notice"] = $notice?:new \ArrayObject();
        $data["message_jiaoyi"] = $message_jiaoyi?:new \ArrayObject();
        $data["message_zijin"] = $message_zijin?:new \ArrayObject();
        $notice_count=Message::where(["type" => 0,"user_id"=>$this->user_id])->count();
        $message_jiaoyi_count=Message::where(["type" => 2,"user_id"=>$this->user_id])->count();
        $message_zijin_count=Message::where(["type" =>1,"user_id"=>$this->user_id])->count();

        $data["notice_count"]=$notice_count;
        $data["message_jiaoyi_count"]=$message_jiaoyi_count;
        $data["message_zijin_count"]=$message_zijin_count;
        json_result(200, "", $data);
    }


    // 消息列表
    public function notice_list()
    {
        $type = input("type") ?: 0;
        $page = input("page") ?: 1;
        $limit = input("limit") ?: 10;

        $where["type"] = $type;
        $where["user_id"] = $this->user_id;
        $list = Message::where($where)->order("id desc,status asc")->paginate(['list_rows' => $limit, 'page' => $page])->each(function ($item) {
            $item["add_time"] = date("Y-m-d H:i", $item["add_time"]);
            return $item;
        });

        json_result(200, "", $list);
    }

    // 更新消息是否已读
    public function notice_status()
    {
        $id = input("id");
        $where["id"] = $id;
        $update["status"] = 1;
        Message::update($update, $where);
        json_result(200);
    }


// 反馈意见
    public function feedback()
    {
        $content = input("content");
        $image = input("image");
        $add["user_id"] = $this->user_id;
        $add["content"] = $content;
        $add["image"] = $image;
        $add["add_time"] = time();
        Feedback::create($add);
        json_result(200, "反馈成功");
    }

    // 常见问题
    public function problem()
    {
        $page = input("page") ?: 1;
        $limit = input("limit") ?: 10;

        $list = Problem::order("id desc")->field("id,name")->paginate(['list_rows' => $limit, 'page' => $page]);

        json_result(200, "", $list);
    }

    // 常见问题详情
    public function problem_show()
    {
        $id = input("id");
        $where["id"] = $id;
        $data = Problem::where($where)->find();
        json_result(200, "", $data);
    }


    // 修改密码
    public function update_pwd()
    {
//        $phone=input("phone");// 系统内置手机号或邮件
        $yuan = trim(input("ypwd"));
        if (!$yuan) json_result(400, "请输入原密码");
        $pwd =trim( input("pwd"));
        if (!$pwd) json_result(400, "请输入新密码");
        $where["id"] = $this->user_id;
        $update["pwd"] = password_hash($pwd, PASSWORD_DEFAULT);;
        $user = User::where($where)->find();

        // 查看原密码是否错误
        if (!password_verify($yuan, $user->pwd)) {
            json_result(400, "原密码错误");
        }


//        if ($user->pwd == $update["pwd"]) {
//            json_result(400, "原密码不能和新密码一样");
//        }

        User::update($update, $where);
        json_result(200, "设置成功");
    }

    // 关于我们
    public function about()
    {
        $where["id"] = 1;
        $about = About::where($where)->find();
        json_result(200, "", $about);
    }

    // 我的推广显示页面
    public function extension()
    {
        $where["pid"] = $this->user_id;
        $count = User::where($where)->count();
        $data["push"] = $count;
        $data["team"] = Team::all_tem($this->user_id);
        json_result(200, "", $data);

    }

    // 直推列表
    public function extension_list()
    {
        $pid = input("pid");
        $page = input("page") ?: 1;
        $limit = input("limit") ?: 10;
        $where = [];
        if ($pid) {
            $where["pid"] = $pid;
        } else {
            $where["pid"] = $this->user_id;
        }
        $list = User::where($where)->order("id desc")->field("id,image,phone,name,mail,add_time")->paginate(['list_rows' => $limit, 'page' => $page])->each(function ($item) {
            // 统计人数
            $where["pid"] = $item["id"];
            $count = User::where($where)->count();
            $item["team"] = $count;
            if ($item["add_time"]) {
                $item["add_time"] = date("Y-m-d H:i", $item["add_time"]);
            }
            return $item;
        });
        json_result(200,"",$list);
    }

    // 可售额度
    public function vendibility_list()
    {//lm_vendibility_finance
        $page = input("page") ?: 1;
        $limit = input("limit") ?: 10;
        $where["user_id"]=$this->user_id;
        $list = VendibilityFinance::where($where)->order("id desc")->paginate(['list_rows' => $limit, 'page' => $page])->each(function ($item) {
            if ($item["add_time"]) {
                $item["add_time"] = date("Y-m-d H:i", $item["add_time"]);
            }
            return $item;
        });
        json_result(200,"",$list);
    }

    // 保证金交易明细
    public function bond_list()
    {
        $page = input("page") ?: 1;
        $limit = input("limit") ?: 10;
        $where["user_id"]=$this->user_id;
        $list = BondFinance::where($where)->order("id desc")->paginate(['list_rows' => $limit, 'page' => $page])->each(function ($item) {
            if ($item["add_time"]) {
                $item["add_time"] = date("Y-m-d H:i", $item["add_time"]);
            }
            return $item;
        });
        json_result(200,"",$list);
    }

    //佣金记录
    public function commission_list()
    {
        $page = input("page") ?: 1;
        $limit = input("limit") ?: 10;
        $where["user_id"]=$this->user_id;
        $list = CommissionFinance::where($where)->order("id desc")->paginate(['list_rows' => $limit, 'page' => $page])->each(function ($item) {
            if ($item["add_time"]) {
                $item["add_time"] = date("Y-m-d H:i", $item["add_time"]);
            }
            return $item;
        });
        json_result(200,"",$list);
    }

    // 充值和提现保证金
    public function bond()
    {
        $money=input("money");if (!$money)json_result(400,"请输入提现金额");
        $type=input("type")?:0;//0 充值 1 提现

        // 直接处理转账成功
        if ($type==0){
            if ($this->user->money<$money){
                json_result(400,"当前账号通证余额不足");
            }
        }else{
            // 查看账号余额
            if ($this->user->ensure_money<$money){
                json_result(400,"账号保证金不足");
            }
        }
        // 直接转账操作
        Orde::bond_transfer($this->user,$this->user_id,$money,$type);
        // 添加记录
        $add["money"]=$money;
        $add["type"]=$type;
        $add["status"]=0;
        $add["add_time"]=time();
        $add["user_id"]=$this->user_id;
        Bond::create($add);
        json_result(200,"申请成功");
    }

    // 通证充值
    public function general()
    {
        $money=input("money");
        $actual_moneys=input("actual_money");
        $charge_moneys=input("charge_money");
        $image=input("image");

        $where["level"]=$this->user["vip_level"];
        $level=UserLevel::where($where)->find();

        $vip_bili=$level->bili;
        $actual_money=$money;
        $charge_money=0;
        if ($vip_bili){
            $charge_money=$money*($vip_bili/100);
            $actual_money-=$charge_money;
        }

        if ($actual_money!=$actual_moneys){
            json_result(400,"实际到账金额计算错误");
        }
        if ($charge_money!=$charge_moneys){
            json_result(400,"优惠金额计算错误");
        }

        $add["money"]=$money;
        $add["user_id"]=$this->user_id;
        $add["actual_money"]=$actual_money;
        $add["charge_money"]=$charge_money;
        $add["status"]=0;
        $add["image"]=$image;
        $add["add_time"]=time();
        GeneralFinance::create($add);
        // 添加转账操作

        json_result(200,"申请成功");
    }



    //通证转账
    public function general_transfer()
    {
        $account=input("account");
        $money=input("money");
        if ($money>$this->user->money){
            json_result(400,"账号通证余额不足");
        }
        $where["name|uuid"]=$account;
        $user=User::where($where)->find();
        if (!$user){
            json_result(200,"账号不存在");
        }
        // 查找无限级
        $team=Team::father($this->user_id,$user->id);
        if (!$team){
            json_result(400,"非同一个直推链不能转账");
        }
        $add["user_id"]=$this->user_id;
        $add["puser_id"]=$user->id;
        $add["account"]=$account;
        $add["money"]=$money;
        $add["status"]=0;
        $add["add_time"]=time();
        GeneralTransfer::create($add);
        $jmoney=$this->user["money"]-$money;
        $name=$this->user["name"];
        Orde::transfer($this->user_id,$user->id,$money,$jmoney,$name);
        \app\common\controller\Message::add_message($user->id,1,"好友转账","您的好友".$this->user['name'].",已为您转账".$money."通证");
        json_result(200,"转账成功");
    }


    // 转账验证密码
    public function transfer_pwd()
    {
        $pwd=input("pwd");if (!$pwd)json_result(400,"请输入密码");

        if (!password_verify($pwd,$this->user->pwd)){
            json_result(400,"密码错误");
        }
        json_result(200,"密码正确");

    }


    // 购买通证显示页面
    public function general_show()
    {
        $where["level"]=$this->user["vip_level"];
        $level=UserLevel::where($where)->find();

        $data["vip_level"]=$this->user["vip_level"];
        $data["vip_bili"]=$level->bili;
        $data["tong_money"]=$this->user->money;
        $system=System::where(["type"=>"shouk"])->field("name,value")->select();
        foreach ($system as $v){
            if ($v->name=="usdt"){
                $data["usdt"]=$v->value?:"";
            }else{
                $data["code"]=$v->value?:"";
            }
        }
        json_result(200,"",$data);
    }

    // 设置性别
    public function update_sex()
    {
        $sex=input("sex");
        $where["id"]=$this->user_id;
        $update["sex"]=$sex;
        User::update($update,$where);
        json_result(200,"设置成功");
    }

    // 获取二维码和推广码
    public function code_url()
    {
        $where["name"]="register_url";
        $url=System::where($where)->value("value");
        $data["code"]=$this->user["code"];
        $data["url"]=$url;
        json_result(200,"",$data);
    }

    // 更新紧急联系人信息
    public function urgent_user()
    {
        $urgent_name=trim(input("urgent_name"));
        $urgent_phone=trim(input("urgent_phone"));
        $where["id"]=$this->user_id;
        $update["urgent_name"]=$urgent_name;
        $update["urgent_phone"]=$urgent_phone;
        User::update($update,$where);
        json_result(200,"操作成功");
    }


}