<?php

namespace Shop\Lib;

use Dever;

class Coupon
{
    # 获取分享活动的优惠券列表
    public function getData($shop_id)
    {
        $data = Dever::db('shop/coupon')->getData(array('shop_id' => $shop_id));

        if ($data) {
            foreach ($data as $k => $v) {
                $coupon = Dever::db('goods/coupon')->find($v['coupon_id']);
                $data[$k]['name'] = $coupon['name'];
            }
        }

        return $data;
    }

    # 获取分享活动的优惠券列表
    public function getAll($shop_id, $act_id)
    {
        $data = Dever::db('shop/coupon_act')->getData(array('shop_id' => $shop_id, 'act_id' => $act_id));

        $result = array();
        if ($data) {
        	foreach ($data as $k => $v) {
                $shop_coupon = Dever::db('shop/coupon')->getOne($v['shop_coupon_id']);
                if ($shop_coupon && $shop_coupon['total'] > 0) {
                    $info = Dever::db('goods/coupon')->one($shop_coupon['coupon_id']);
                    if (!$info) {
                        continue;
                    }

                    $data[$k]['cash'] = $info['cash'];
                    $data[$k]['total_cash'] = $info['total_cash'];
                    $data[$k]['total'] = $shop_coupon['total'];
                    $data[$k]['shop_coupon_id'] = $v['shop_coupon_id'];
                    $data[$k]['name'] = $info['name'];

                    $result[] = $data[$k];
                } else {
                    continue;
                }
        	}
        }

        return $result;
    }

    # 验证是否可以领取
    public function getOne($uid, $shop, $shop_coupon_id, $act_id = 1, $num = 1, $act = true)
    {
        if (!$shop_coupon_id) {
            return false;
        }

        $data = Dever::db('shop/coupon')->getOne($shop_coupon_id);

        if (!$data) {
            Dever::alert('优惠券不存在');
            return false;
        }

        $coupon = Dever::db('goods/coupon')->find($data['coupon_id']);

        if (!$coupon) {
            Dever::alert('优惠券不存在');
            return false;
        }

        if ($data['total'] <= 0) {
            Dever::alert('优惠券已无可用数量');
            return false;
        }
        if ($num > $data['total']) {
            $num = $data['total'];
        }

        if ($act) {
            $act = Dever::db('shop/coupon_act')->getOne(array('shop_id' => $shop['id'], 'act_id' => $act_id, 'shop_coupon_id' => $shop_coupon_id));

            if (!$act) {
                Dever::alert('优惠券不存在');
                return false;
            }

            # 验证是否已经领取，一周内不能再次领取
            if ($coupon['method'] == 3) {
                $user = Dever::db('shop/user_coupon')->getOne(array('uid' => $uid, 'shop_coupon_id' => $shop_coupon_id));
            } else {
                $user = Dever::db('shop/user_coupon')->getOne(array('uid' => $uid, 'coupon_id' => $coupon['id']));
            }

            if ($user && time() - $user['cdate'] < 86400*7) {
                Dever::alert('您已领取过该优惠劵');
                return false;
            }
        }

        $state = Dever::load('shop/lib/coupon.take', $uid, $shop, $data, $num);
        if ($state) {
            return true;
        }
        return false;
    }

    # 领取
    public function take($uid, $shop, $shop_coupon, $num = 1)
    {
        $id = false;
        $add_num = 0;
        for ($i = 0; $i < $num; $i++) {
            $data['uid'] = $uid;
            $data['shop_id'] = $shop['id'];
            $data['city'] = $shop['city'];
            $data['coupon_id'] = $shop_coupon['coupon_id'];
            $data['shop_coupon_id'] = $shop_coupon['id'];
            $data['edate'] = time() + ($shop_coupon['day'] * 86400);
            //$data['cash'] = $cash;
            $data['t'] = $i;
            $id = Dever::db('shop/user_coupon')->insert($data);
            if ($id) {
                $add_num += 1;
            }
        }

        if ($add_num > 0) {
            Dever::db('shop/coupon')->updateNum(array('where_id' => $shop_coupon['id'], 'act_num' => $add_num));
        }

        return $id;
    }

    # 获取能用的优惠券的店铺名称
    public function getShop($data, $shop = false, $shop_list = false)
    {
        $data['shop_name'] = '门店专属';
        if ($shop) {
            $data['shop_name'] = $shop['name'];
        } elseif (isset($data['shop_id'])) {
            $shop = Dever::db('shop/info')->getOne($data['shop_id']);
            $data['shop_name'] = $shop['name'];
        }
        if ($data['method'] == 1) {
            $data['shop_name'] = '全国门店通用';
        } elseif ($data['method'] == 2) {
            $where = array('coupon_id' => $data['coupon_id'], 'city' => $data['city']);
            if ($shop_list) {
                $shop_coupon = Dever::db('shop/coupon')->select($where);
                $data['shop'] = array();
                foreach ($shop_coupon as $k1 => $v1) {
                    //$shop = Dever::db('shop/info')->getOne(array('id' => $v1['shop_id'], 'city' => $data['city']));
                    $shop = Dever::load('shop/lib/info')->fetch($v1['shop_id'], $data['city'], Dever::input('lng'), Dever::input('lat'));
                    if ($shop) {
                        $data['shop'][] = $shop;
                    }
                }
                $shop_coupon = count($data['shop']);
            } else {
                $shop_coupon = Dever::db('shop/coupon')->total($where);
            }
            if ($shop_coupon > 1) {
                $city = Dever::db('area/city')->find($data['city']);
                $data['shop_name'] = $city['name'] . '多店通用';
            }
        }

        return $data;
    }
}