<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\admin\controller\v1;


use app\BaseController;
use app\model\admin\Admin;
use app\model\admin\Member as MemberModel;
use app\model\admin\MemberLevel;
use app\model\api\MemberDetail;
use library\services\UtilService;
use library\utils\IpLocation;
use app\Request;

class Member extends  BaseController
{

    public function getMember(Request $request)
    {
        $pageSize = 50;
        $post = UtilService::getMore([
            ['page',1],
            ['keyword',''],
            ['i_uid',''],
            ['mobile',''],
            ['level',''],
            ['uid','']
        ]);
        $post['sassid'] = $request->site['sassid'];
        list($pageCount,$data) = (new MemberModel())
            ->getList($post['page'], $post,$pageSize,'*','uid desc');
        $result = UtilService::getParam([
            'uid',
            'mobile',
            'weixin_name',
            'nickname',
            'sex',
            'avatar',
            'address',
            'money',
            'name',
            "tx_money",
            "level_name",
            "site_name",
            'status',
            'levelid',
            'order_count',
            'recharge_count',
            'order_info_count',
            'i_mobile',
            'i_nickname',
            'i_uid',
            'sassid',
            'regip',
            ['regtime','regtime','date("Y-m-d H:i:s",$1)'],
            ['lasttime','lasttime','date("Y-m-d H:i:s",$1)'],
            ['ipara','regip',function($item){
                return  (new IpLocation())->getlocation($item)['country'];
            }]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }


    /**
     * 获取会员数据
     * @param Request $request
     * @return mixed
     */
    public function getItem(Request $request)
    {
        $pageSize = 50;
        $post = UtilService::getMore([
            ['uid',1]
        ]);
        $data = (new MemberModel())->getItem($post['uid'],$request->site['sassid']);
        $result = UtilService::getParam([
            'uid',
            'mobile',
            'nickname',
            'sex',
            'avatar',
            'address',
            'money',
            'name',
            "tx_money",
            "level_name",
            "site_name",
            'status',
            'levelid',
            'order_count',
            'recharge_count',
            'order_info_count',
            'regip',
            ['regtime','regtime','date("Y-m-d H:i:s",$1)'],
            ['lasttime','lasttime','date("Y-m-d H:i:s",$1)'],
            ['ipara','regip',function($item){
                return  (new IpLocation())->getlocation($item)['country'];
            }]
        ],[$data]);
        return app('json')->success(empty($result) ? [] : $result[0]);
    }


    /**
     * 修改会员数据
     * @param Request $request
     */
    public function memberUpdate(Request $request) {
        $post = UtilService::getMore([
            ['levelid','0'],
            ['mobile','','isMobile','请输入手机号码'],
            ['nickname',''],
            ['status','0'],
            ['name',''],
            ['password',''],
            ['avatar',''],
            ['sex','0'],
            ['uid','0','empty','参数错误']
        ], $request);
        $save = [
            'mobile'    => $post['mobile'],
            'nickname'  => $post['nickname'],
            'sex'       => $post['sex'],
            'name'      => $post['name'],
            'levelid'   => $post['levelid'],
            'status'    => $post['status'],
            'avatar'    => $post['avatar']
        ];

        if(!empty($post['password'])) {
            $save['password'] = md5($post['password']);
        }
        //
        (new \app\model\admin\Member)->where('uid',$post['uid'])->where('sassid',$request->site['sassid'])->save($save);
        return app('json')->success('修改成功');
    }


    public function getMemberLevel(Request $request)
    {
        $pageSize = 50;
        $post = UtilService::getMore([
            ['page',1]
        ]);
        $where = ['sassid'=>$request->site['sassid']];
        list($pageCount,$data) = (new MemberLevel())
            ->getList($post['page'],$where,$pageSize,'*','id desc');
        $result = UtilService::getParam([
            'id',
            'name',
            "is_show",
            'code',
            ['update_money','update_money',function($val) { return empty($val) ? '0.00' : $val; }]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }


    /**
     * 操作明细
     * @param Request $request
     */
    public function detailList(Request $request) {
        $pageSize = 5;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['uid','0'],
                ['time',[]]
            ],$request
        );
        $where['uid'] = $post['uid'];
        $where['sassid'] = $request->site['sassid'];
        if(!empty($post['tabType']) && $post['tabType'] != 'all') {
            $where['code'] = $post['tabType'];
        }
        if(!empty($post['time'])) {
            $where['time'] = [[strtotime($post['time'][0]),strtotime($post['time'][1])],'whereBetween'];
        }
        $detail = new MemberDetail();
        list($pageCount,$data) = $detail->getList($post['page'],$where,$pageSize,"*","time desc");
        $result = UtilService::getParam([
            "id",
            "code",
            "v",
            'title',
            'content',
            "type",
            ["time","time","date('Y-m-d H:i:s',$1)"]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

    /**
     * 修改推荐人
     * @param Request $request
     */
    public function editTj(Request $request) {
        $post =  UtilService::getMore(
            [
                ['uid','','empty','参数错误'],
                ['i_uid','0']
            ],$request
        );
        if($post['uid'] == $post['i_uid']) {
            return app('json')->fail('自己无法挂载自己下面');
        }

        if(!empty($post['i_uid'])) {
            $count = (new \app\model\admin\Member)
                ->where('sassid',$request->site['sassid'])
                ->where('uid',$post['i_uid'])
                ->count();
            if($count <= 0) {
                return app('json')->fail('当前推荐用户不存在!');
            }
        }
        (new \app\model\admin\Member)
                        ->where('sassid',$request->site['sassid'])
                        ->where('uid',$post['uid'])
                        ->save(['i_uid'=>$post['i_uid']]);
        return app('json')->success('操作成功');
    }


    /**
     * 获取等级
     * @param Request $request
     * @return mixed
     */
    public function levelInfo(Request $request)
    {
        [$id] = UtilService::getMore([
            ['id','0']
        ],$request,true);
        $data = MemberLevel::alias("ml")
                    ->field("ml.name,ml.id,sl.update_money")
                    ->leftJoin("site_level sl","sl.level_id = ml.id AND sass_id=".$request->site['sassid'])
                    ->where('ml.id',$id)
                    ->find();
        if(empty($data)) return app('json')->fail('数据错误');
        $tAr = [];
        $tAr['name'] = $data['name'];
        $tAr['id'] = $data['id'];
        $tAr['update_money'] = empty($data['update_money']) ? '0.00' : $data['update_money'];
        return app('json')->success($tAr);

    }

    /**
     * 保存等级设置
     * @param Request $request
     */
    public function levelSave(Request $request) {
        [$id,$update_money] = UtilService::getMore([
            ['id','0'],
            ['update_money','0'],
        ],$request,true);
        $siteLevel = new \app\model\admin\SiteLevel;
        $count = $siteLevel
                    ->where('sass_id',$request->site['sassid'])
                    ->where('level_id',$id)
                    ->count();
        if($count > 0) {
            $siteLevel
                ->where('sass_id',$request->site['sassid'])
                ->where('level_id',$id)
                ->save(['update_money'=>$update_money]);
        } else {
            $siteLevel->insert([
                'sass_id'       =>  $request->site['sassid'],
                'level_id'      =>  $id,
                'update_money'  =>  $update_money
            ]);
        }
        return app('json')->success("操作成功");
    }


}