<?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\allwin\controller\info;
use app\allwin\controller\Common;
use app\allwin\model\AllwinInfoMp;
use app\common\model\SystemUser;
use think\facade\Validate;
use think\facade\Request;
use think\helper\Time;

class Mp extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'城市号','url'=>url("info.mp/index")]]);
    }

    /**
     * 列表
     */
    public function index(int $types = 0){
        $condition  = [];
        $time      = Request::param('time/d',0);
        $starttime = Request::param('starttime/s');
        $endtime   = Request::param('endtime/s');
        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[] = ['reg_time','>=',$start];
            $condition[] = ['reg_time','<=',$end];
        }else{
            if($starttime){
                $condition[] = ['reg_time','>=',strtotime($starttime)];
            }
            if($endtime){
                $condition[] = ['reg_time','<=',strtotime($endtime)];
            }
        }
        $view['num']  = AllwinInfoMp::where($this->mini_program)->where($condition)->count();
        $view['vip_num']   = AllwinInfoMp::where($this->mini_program)->where($condition)->where(['is_vip' => 1])->count();
        $view['notvip_num'] = AllwinInfoMp::where($this->mini_program)->where($condition)->where(['is_vip' => 0])->count();
        $condition[] = ['is_lock','=',$types];
        $lists = AllwinInfoMp::where($this->mini_program)->where($condition)->order('id desc')->paginate(20,false,['query' => ['types' => $types,'starttime' => $starttime,'endtime' => $endtime,'time'=>$time]]); 
        $view['types']     = $types;
        $view['lists']      = $lists;
        $view['time']       = $time;
        $view['starttime']  = $starttime;
        $view['endtime']    = $endtime;
        return view()->assign($view);
    }


    //规格编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'member_miniapp_id' => $this->member_miniapp_id,
                'id'                => $this->request->param('id/d',0),
                'uid'               => $this->request->param('uid/d'),
                'logo'              => $this->request->param('logo/s'),
                'title'             => $this->request->param('title/s'),
                'note'              => $this->request->param('note/s'),
                'is_vip'            => $this->request->param('is_vip/d',0),
                'vip_title'         => $this->request->param('vip_title/s'),
                'vip_about'         => $this->request->param('vip_about/s'),
                'vip_time'          => $this->request->param('vip_time/s'),
            ];
            $validate = $this->validate($data,$data['is_vip'] ? 'Info.mpVip':'Info.mpEdit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = AllwinInfoMp::edit($data);
            if(empty($result)){
                return enjson(403,'操作失败');
            }
            return enjson(200,'操作成功',['url'=>url('info.mp/index')]);
        }else{
            $view['info'] = AllwinInfoMp::where($this->mini_program)->where(['id' => $this->request->param('id/d',0)])->find();
            return view()->assign($view);
        }
    }
    
    /**
     * 预览信息
     * @return void
     */
    public function review(){
        $view['info'] = AllwinInfoMp::where($this->mini_program)->where(['id' => $this->request->param('id/d',0)])->find();
        return view()->assign($view);
    }

    /**
     * 选择所属用户
     */
    public function selectUid(){
        $keyword = Request::param('keyword');
        $input   = Request::param('input');
        $condition = [];
        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)->whereNotIn('id',AllwinInfoMp::where($this->mini_program)->column('uid'))->order('id desc')->paginate(10,false,['query' => ['input' => $input,'keyword' => $keyword]]);
        $view['keyword'] = $keyword;
        $view['input']   = $input;
        $view['id']      = $this->member_miniapp_id;
        return view()->assign($view);
    }

    /**
     * 认证
     */
    public function vip(int $id){
        $result = AllwinInfoMp::where($this->mini_program)->where(['id' => $id])->field('is_vip')->find();
        if(!$result){
            return enjson(0);
        }else{
            $result->is_vip = $result->is_vip ? 0: 1;
            $result->save();
            return enjson(200);
        }
    } 

    /**
     * 锁定
     */
    public function lock(int $id){
        $result = AllwinInfoMp::where($this->mini_program)->where(['id' => $id])->field('is_lock')->find();
        if(!$result){
            return enjson(0);
        }else{
            $result->is_lock = $result->is_lock ? 0: 1;
            $result->save();
            return enjson(200);
        }
    } 
}