<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\love\controller;

use app\love\model\LotteryLogModel;
use app\love\model\LotteryPrizeModel;
use app\love\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

class AdminLotteryController extends AdminBaseController
{
    /**
     * 列表
     */
    public function prize()
    {
        $status = $this->request->param('status',1);
        $where = [];
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }
        $list = LotteryPrizeModel::where($where)->order('id asc')->paginate(10);

        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        // 渲染模板输出
        $this->assign('status', $status);
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加提交
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];

            if ($post['odds'] < 0 || $post['odds'] > 100) {
                $this->error('中奖概率需要0到100之间');
            }
            if ($post['number'] < 0) {
                $this->error('奖品数量不能小于0');
            }

            LotteryPrizeModel::create($post);

            $this->success('添加成功!', url('prize'));
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        $post = LotteryPrizeModel::get($id);

        $this->assign('post', $post);
        return $this->fetch();
    }

    /**
     * 编辑提交
     */
    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];

            if ($post['odds'] < 0 || $post['odds'] > 100) {
                $this->error('中奖概率需要0到100之间');
            }
            if ($post['number'] < 0) {
                $this->error('奖品数量不能小于0');
            }

            LotteryPrizeModel::update($post, ['id' => $post['id']]);

            $this->success('编辑成功!', url('prize'));
        }
    }

    /**
     * 调整状态
     */
    public function setStatus()
    {
        $id     = input('param.id', 0, 'intval');
        $status = input('param.status', 0, 'intval');
        if (empty($id) || empty($status)) {
            $this->error('数据有误');
        }

        LotteryPrizeModel::update(['status' => $status], ['id' => $id]);

        $this->success('操作成功!');

    }

    /**
     * 记录列表
     */
    public function log()
    {
        //搜索条件
        $param  = $this->request->param();
        $where = [];
        if (!empty($param['mobile'])) {
            $user = UserModel::where('mobile', $param['mobile'])->find();
            if (empty($user)) {
                $where[] = ['id', '=', 0];
            } else {
                $where[] = ['user_id', '=', $user['id']];
            }
        }
        if (!empty($param['status'])) {
            $where[] = ['status', '=', $param['status']];
        }

        //列表
        $list = LotteryLogModel::with('user')->where($where)->order('id asc')->paginate(10, false, [
            'query' => $param,//不丢失已存在的url参数
        ]);

        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        // 渲染模板输出
        $this->assign('mobile', isset($param['mobile']) ? $param['mobile'] : '');
        $this->assign('status', isset($param['status']) ? $param['status'] : '');
        return $this->fetch();
    }

    /**
     * 领奖
     */
    public function receive()
    {
        $id = input('param.id', 0, 'intval');
        LotteryLogModel::update(['status' => 1,'out_at'=>time()], ['id' => $id]);

        //增加领证次数
        Db::name('config')->where('id',1)->setInc('lottery_num');

        $this->success('操作成功!');

    }
}
