<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class ScratchCardController extends BaseController
{
	/**
	 * 商品列表
	 */
	public function index()
	{	
		$list = M('scratch_card_goods')->where(array('admin_id' => $this->data['business_id']))->select();

		foreach ($list as $k => &$v) {
			$v['img'] = $this->url.'/Uploads/'.$v['img'];
		}
		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 添加中奖纪录
	 */
	public function add_record()
	{
		if (empty($this->data['user_id'])) apiReturn(['status' => -1, 'msg' => '用户id不能为空']);

		if (empty($this->data['bw_goods_id'])) apiReturn(['status' => -1, 'msg' => '奖品id不能为空']);

		if (empty($this->data['name'])) apiReturn(['status' => -1, 'msg' => '收货人不能为空']);

		if (empty($this->data['mobile'])) apiReturn(['status' => -1, 'msg' => '联系方式不能为空']);

		if (empty($this->data['address'])) apiReturn(['status' => -1, 'msg' => '收货地址不能为空']);

		$add = array(
				'admin_id' => $this->data['business_id'],
				'user_id' => $this->data['user_id'],
				'bw_goods_id' => $this->data['bw_goods_id'],
				'name' => $this->data['name'],
				'mobile' => $this->data['mobile'],
				'address' => $this->data['address'],
				'create_time' => time(),
			);

		$goods_info = M('scratch_card_goods')->where(array('id' => $this->data['bw_goods_id']))->find();
		if ($goods_info['status'] == 2) {
			$add['status'] = 3;
		} else {
			$add['status'] = 2;
		}

		$result = M('scratch_card_winning_record')->add($add);

		if ($result) {
			apiReturn(['status' => 1, 'msg' => '已提交数据']);
		} else {
			apiReturn(['status' => -1, 'msg' => '提交失败']);
		}
	}

	/**
	 * 开始按钮（扣积分）
	 */
	public function start()
	{
		if (empty($this->data['user_id'])) apiReturn(['status' => -1, 'msg' => '用户id不能为空']);

		$user_int = M('user_int')->where(array('user_id' => $this->data['user_id']))->find();

		$config = M('scratch_card_config')->where(array('admin_id' => $this->data['business_id']))->find();
		if (empty($config)) {
			M('scratch_card_config')->add(array('admin_id' => $this->data['business_id'], 'create_time' => time()));
			$config = M('scratch_card_config')->where(array('admin_id' => $this->data['business_id']))->find();
		}

		if ($config['status'] == 2) apiReturn(['status' => -1, 'msg' => '后台已关闭刮刮卡功能']);

		if ($user_int['int'] < $config['int']) apiReturn(['status' => -1, 'msg' => '积分不足']);

		$result = M('user_int')->where(array('user_id' => $this->data['user_id']))->save(array('int' => $user_int['int'] - $config['int']));

		if ($result) {
			// 获取中奖奖品
			$prize_list = M('scratch_card_goods')->where(array('admin_id' => $this->data['business_id']))->field('id, goods_name, probability, status')->select();
			$prize_res = $this->get_win_goods($prize_list)['yes'];

			// // 添加中奖纪录
			// $add = array(
			// 	'admin_id' => $this->data['business_id'],
			// 	'user_id' => $this->data['user_id'],
			// 	'bw_goods_id' => $prize_res['id'],
			// 	'create_time' => time(),
			// );
			// $goods_info = M('scratch_card_goods')->where(array('id' => $prize_res['id']))->find();
			// if ($goods_info['status'] == 2) {
			// 	$add['status'] = 3;
			// } else {
			// 	$add['status'] = 2;
			// }
			// M('scratch_card_winning_record')->add($add);

			apiReturn(['status' => 1, 'msg' => '获取中奖奖品', 'prize_res' => $prize_res]);
		} else {
			apiReturn(['status' => -1, 'msg' => '开始失败']);
		}
	}

	/**
	 * 中奖纪录
	 */
	public function winning_log()
	{
		if (empty($this->data['user_id'])) apiReturn(['status' => -1, 'msg' => '用户id不能为空']);

		$list = M('scratch_card_winning_record')
				->join('am_scratch_card_goods ON am_scratch_card_winning_record.bw_goods_id = am_scratch_card_goods.id')
				->where(array('am_scratch_card_winning_record.admin_id' => $this->data['business_id'], 
					'am_scratch_card_winning_record.user_id' => $this->data['user_id'], 
					'am_scratch_card_goods.status' => 1))
				->field('am_scratch_card_winning_record.*, am_scratch_card_goods.goods_name, am_scratch_card_goods.img')
				->select();

		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		foreach ($list as $key => &$value) {
			$value['img'] = $this->url.'/Uploads/'.$value['img'];
			$value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 设置
	 */
	public function config()
	{
		$config = M('scratch_card_config')->where(array('admin_id' => $this->data['business_id']))->find();
		if (empty($config)) {
			M('scratch_card_config')->add(array('admin_id' => $this->data['business_id'], 'create_time' => time()));
			$config = M('scratch_card_config')->where(array('admin_id' => $this->data['business_id']))->find();
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $config]);
	}
}