<?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\ais\controller\Common;
use app\ais\model\AisVip;
use app\ais\model\AisVipUser;

class Index extends Common{

    public $mini_program   = []; 

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

    /**
     * 列表
     */
    public function index(){
        $view['lists'] = AisVip::where($this->mini_program)->order('sort desc,id desc')->paginate(20);
        return view()->assign($view);
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $param = [
                'name'              => $this->request->param('name/s'),
                'price'             => $this->request->param('price/f'),
                'rule'              => $this->request->param('rule'),
                'fund'              => $this->request->param('fund/d',0),
                'open_queue_reward' => $this->request->param('open_queue_reward/f',0),
                'open_share_reward' => $this->request->param('open_share_reward/f',0),
                'pay_queue_reward'  => $this->request->param('pay_queue_reward/d',0),
                'pay_share_reward'  => $this->request->param('pay_share_reward/d',0),
                'open_store_num'    => $this->request->param('open_store_num/d',0),
                'open_vip_num'      => $this->request->param('open_vip_num/d',0),
                'is_auto_up'        => $this->request->param('is_auto_up/d',0),
                'member_miniapp_id' => $this->member_miniapp_id
            ];
            $validate = $this->validate($param,'Vip.edit');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $id = $this->request->param('id/d',0);
            if($id){
                $result = AisVip::where(['id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->update($param);
            }else{
                $param['update_time']       = time();
                $param['member_miniapp_id'] = $this->member_miniapp_id;
                $result = AisVip::create($param);
            }
            if($result){
                return enjson(200,['url' => url("vip.index/index")]);
            }
            return enjson(0);
        }else{
            $view['info'] = AisVip::where(['id' => $this->request->param('id/d',0)])->find();
            return view()->assign($view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'Category.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = AisVip::where(['id' => $data['id'],'member_miniapp_id' => $this->member_miniapp_id])->update(['sort'=>$data['sort']]);
            if($result){
                return enjson(200);
            }
            return enjson(0);
        }
    }

    /**
     * 是否自读升级
     * @param integer $id 用户ID
     */
    public function isAutoUp(int $id){
        AisVip::where(['member_miniapp_id' => $this->member_miniapp_id])->update(['is_auto_up' => 1]);
        if(AisVip::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->update(['is_auto_up' => 0])){
            return enjson(200);
        }
        return enjson(0);
    } 

    /**
     * 是否自读升级
     * @param integer $id 用户ID
     */
    public function isLock(int $id){
        $rel = AisVip::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->find();
        $rel->is_lock = !$rel->is_lock;
        $rel->save();
        return enjson(200);
    } 

    //删除
    public function delete(int $id){
        $rel = AisVipUser::where($this->mini_program)->where(['vip_id' => $id])->find();
        if($rel){
            return enjson(0,'会员组已有会员开通,建议禁止');
        }
        $result = AisVip::where($this->mini_program)->where(['id' => $id])->delete();
        if($result){
            return enjson(200);
        }
        return enjson(0);
    }
}