<?php
/**
 * GiftController.php UTF-8
 * 礼包处理类
 *
 * @date    : 2018/1/19 16:25
 *
 * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 * @author  : linjiebin <ljb@huosdk.com>
 * @version : HUOSDK 8.0
 */
namespace web\pc\controller\v8;

use think\Session;
use web\common\controller\WebBaseController;
use web\pc\logic\GiftLogic;

class GiftController extends WebBaseController {
    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $_web_info = $this->web_info;
        $this->set_seo($_web_info['web_basic']['company_name']);
        $_gift_class = new GiftLogic();
        $_name = $this->request->param('giftname', '');
        $_page = $this->request->param('page', 1);
        $_row = $this->request->param('row', 8);
        $_map = [];
        if (!empty($_name)) {
            $_map['gf.title|g.name'] = ['like', "%$_name%"];
        }
        $_rdata['gift'] = $_gift_class->getGifts(0, $_map, $_page.','.$_row);
        if($this->request->isAjax()){
            return $_rdata;
        }
//        print_r($_rdata);
        $this->assign($_rdata);

        return $this->fetch('Gift/index');
    }

    public function set_gift() {
        if ($this->request->isAjax()) {
            if (!$this->isLogin()) {
                return ['error' => 1, 'msg' => '未登录'];
            }
            $_giftid = $this->request->param('giftid');
            $_gift_class = new GiftLogic();
            $_mem_id = Session::get('user.id');
            $_res = $_gift_class->setGift($_mem_id, $_giftid);
            if (601 == $_res) {
                return ['error' => 2, 'msg' => '礼包已下架'];
            }

            if (602 == $_res) {
                return ['error' => 3, 'msg' => '已经领取过了,请到个人中心查看'];
            }


            return ['error' => 0, 'msg' => '领取成功', 'data' => $_res];
        }
    }
}