<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 创客管理
 */
namespace app\ais\controller\vip;
use app\common\model\SystemUser;
use app\common\facade\Inform;
use think\facade\Request;
use think\facade\Validate;
use think\helper\Time;
use app\ais\controller\Common;
use app\ais\model\AisVip;
use app\ais\model\AisVipUser;

class User extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'会员设置','url'=>url("vip.user/index")]]);
    }

    /**
     * 
     * @param int $types
     * @param int $vip_id
     * @return type
     */
    public function index(int $types = 0,int $vip_id = 0){
        $time      = Request::param('time/d',0);
        $starttime = Request::param('starttime/s');
        $endtime   = Request::param('endtime/s');
        $condition[] = ['is_lock','=',$types ? 1 : 0];
        if($time){
            switch ($time) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['update_time','>=',$start];
            $condition[] = ['update_time','<=',$end];
        }else{
            if($starttime){
                $condition[] = ['update_time','>=',strtotime($starttime)];
            }
            if($endtime){
                $condition[] = ['update_time','<=',strtotime($endtime)];
            }
        }
        //查询内容
        $view['vip']  = AisVip::where($this->mini_program)->order('sort desc,id desc')->select();
        $view['info'] = [];
        $view['pathMaps'][]   = ['name'=>' 会员列表','url'=>url('ais/vip.user/index')];
        if($vip_id){
            $condition[] = ['vip_id','=',$vip_id];
            $view['info'] = AisVip::where(['id' => $vip_id])->field('name,id')->find();
            $view['pathMaps'][] = ['name' => $view['info']->name,'url'=>url('ais/vip.user/index',['vip_id' => $view['info']->id])];
        }
        //用户精准查询
        $uid = Request::param('uid/d','');
        if($uid){
            $condition[] = ['uid','=',$uid];
        }
        $view['lists']  = AisVipUser::where($this->mini_program)->where($condition)->order('id desc')->paginate(20,false,['query'=> ['types' => $types,'vip_id' => $vip_id,'time' => $time]]);
        //模板赋值
        $view['pathMaps'][] = ['name' => $types ? '<span class="red fn-fb">锁定<span>' : '<span class="blue fn-fb">正常<span>','url'=>'javascript:;'];
        $view['types']      = $types;
        $view['vip_id']     = $vip_id;
        $view['time']       = $time;
        $view['starttime']  = $starttime;
        $view['endtime']    = $endtime;
        $view['uid']        = $uid;
        return view()->assign($view);
    }
    
    /**
     * 添加创客会员
     * @return type
     */
    public function winUser(){
        if(request()->isAjax()){
            $uids = ids($this->request->param('ids/s'),true);
            if(empty($uids)){
                return json(['code'=>0,'msg'=>'请选择要关联的用户']);
            }
            $vip_id  = $this->request->param('vip_id/d');
            $vipcard = AisVip::where($this->mini_program)->where(['id' => $vip_id])->find();
            if(empty($vipcard)){
                return json(['code'=>0,'msg'=>'未找到对应会员组']);
            }
            //读取并判断是否VIP
            $vipdata  = [];
            foreach ($uids as $key => $uid) {
                $vipdata[$key]['update_time'] = time();
                $vipdata[$key]['is_lock']     = 0;
                $vipdata[$key]['vip_id']    = $vipcard->id;
                $vipdata[$key]['uid']     = $uid;
                $vipdata[$key]['member_miniapp_id'] = $this->member_miniapp_id;
                //通知到申请者微信
                Inform::sms($uid,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为'.$vipcard['name']]);
            }
            //锁定以前的会员
            AisVipUser::where(['uid' => $uids])->update(['is_lock' => 1]);
            AisVipUser::insertAll($vipdata);
            return json(['code'=>200,'msg'=>'会员用户新增成功','data' => ['url'=>'']]);
        }else{
            $vip_id = $this->request->param('vip_id/d');
            $view['vip'] = AisVip::where($this->mini_program)->where(['id' => $vip_id])->find();
            if(empty($view['vip'])){
                $this->error('未找到会员类型');
            }
            $condition = [] ;
            $keyword = $this->request->param('keyword');
            if(!empty($keyword)){
                if(Validate::isMobile($keyword)){
                    $condition[] = ['phone_uid','=',$keyword] ;
                }else{
                    $condition[] = ['nickname','like','%'.$keyword.'%'];
                }
            }       
            $view['list'] = SystemUser::where($this->mini_program)->where($condition)->where('id', 'NOT IN', function ($query) use($vip_id){
                $query->table('ai_ais_vip_user')->where(['vip_id' => $vip_id,'is_lock' => 0])->field('uid');
            })->order('id desc')->paginate(20,false,['query' => ['keyword'=>$keyword,'vip_id' => $vip_id]]);
            $view['keyword'] = $keyword;
            $view['vip_id']  = $vip_id;
            return view()->assign($view);
        }
    }

    /**
     * 删除用户的优惠券
     * @return void
     */
    public function delete(){
        $result = AisVipUser::where($this->mini_program)->where(['uid' => $this->request->param('uid/d')])->delete();
        if($result){
            return enjson(200);
        }
        return enjson(0);
    }
}