<?php

namespace Card\Src;

use Dever;
use Main\Lib\Core;

class Api extends Core
{
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->shop_id = Dever::input('shop_id');

        $this->shop = array();

        if ($this->shop_id) {
            $lng = Dever::input('lng');
            $lat = Dever::input('lat');
            if ($lng && $lat) {
                $this->shop = Dever::load('shop/lib/info')->fetch($this->shop_id, false, $lng, $lat, false);
            } else {
                $this->shop = Dever::db('shop/info')->getOne($this->shop_id);
            }

            if (!$this->shop) {
                Dever::alert('门店不存在');
            }
        }
    }

    # 获取礼品卡
    public function getCard()
    {
        $where['type'] = 1;
        $this->data['card'] = array();
        $card = Dever::db('card/type')->getAll($where);

        if ($card) {
            foreach ($card as $k => $v) {
                $w['type_id'] = $v['id'];
                $w['status'] = 1;
                $w['create_type'] = 1;
                $w['buy'] = 1;
                $list = Dever::db('card/info')->select($w);
                if (!$list) {
                    unset($card[$k]);
                } else {
                    $card[$k]['value'] = $list;
                    $this->data['card'][] = $card[$k];
                }
            }
        }

        # 获取选择的城市
        $this->data['city'] = Dever::load('shop/lib/info')->getCity();

        $config = Dever::db('main/sell_config')->find();
        $this->data['card_name'] = $config['card_name'];

        return $this->data;
    }

    # 确认订单页面
    public function confirm()
    {
        $this->data['uid'] = $this->uid;
        $this->data['user'] = $this->user;
        $this->data['shop'] = $this->shop;
        Dever::load('card/lib/buy')->getCard($this->data);
        return $this->data;
    }

    # 开始下单
    public function pay_commit()
    {
        $refer = Dever::input('refer');
        $address_id = Dever::input('address_id');

        $this->data['uid'] = $this->uid;
        $this->data['user'] = $this->user;
        $this->data['shop'] = $this->shop;

        Dever::load('card/lib/buy')->getCard($this->data);

        if (!$address_id) {
            Dever::alert('收货地址不能为空');
        }

        $pay = Dever::load('card/lib/buy')->pay($this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $address_id, 5, $refer);
        return $pay;
    }

    # 再次付款
    public function r_pay()
    {
        $refer = Dever::input('refer');
        $order_id = Dever::input('order_id');

        $pay = Dever::load('card/lib/buy')->rpay($order_id, 5, $refer);

        return $pay;
    }

    # 查询是否支付
    public function c_pay()
    {
        $refer = Dever::input('refer');
        $order_id = Dever::input('order_id');

        $pay = Dever::load('card/lib/buy')->cpay($order_id, 5, $refer);

        return $pay;
    }

    # 我的订单列表
    public function order()
    {
        return Dever::load('card/lib/buy')->set(1)->getList($this->uid);
    }

    # 查看订单详情
    public function order_view()
    {
        $id = Dever::input('order_id');

        return Dever::load('card/lib/buy')->set(2)->getView($this->uid, $id);
    }

    # 取消订单
    public function order_cancel_commit()
    {
        $order_id = Dever::input('order_id');

        return Dever::load('card/lib/buy')->set(2)->cancel($this->uid, $order_id);
    }

    # 兑换
    public function dh_card_commit()
    {
        $card = Dever::input('card');
        if (!$card) {
            Dever::alert('请输入正确的卡号');
        }

        $test = Dever::input('test');

        if ($test == 1) {
            $data = Dever::db('card/code')->find(array('card' => $card));
            if (!$data) {
                Dever::alert('礼品卡卡号错误或礼品卡已兑换');
            }
        } else {
            $data = Dever::db('card/code')->find(array('card' => $card));
            if (!$data) {
                Dever::alert('礼品卡卡号错误或礼品卡已兑换');
            }
            if ($data['status'] > 2) {
                Dever::alert('礼品卡卡号错误或礼品卡已兑换');
            }

            $check = Dever::load('card/lib/code')->check($data);
            if ($check == -1) {
                Dever::alert('兑换失败，礼品卡已下架');
            } elseif ($check == -2) {
                Dever::alert('兑换失败，礼品卡已下架');
            } elseif ($check == -3) {
                Dever::alert('礼品卡已过兑换时间');
            } 

            $update['where_id'] = $data['id'];
            $update['dh_uid'] = $this->uid;
            $update['status'] = 3;
            $update['ddate'] = time();
            $state = Dever::db('card/code')->update($update);

            if (!$state) {
                Dever::alert('兑换失败，请重新兑换');
            }

            # 验证代理商权益
            if ($data['type'] == 2) {
                $where['sku_id'] = $data['id'];
                $where['type'] = 2;
                $where['type_id'] = $data['card_id'];
                $agent = Dever::db('agent/member_goods')->find($where);
                if ($agent) {
                    Dever::db('agent/member_goods')->update(array('where_id' => $agent['id'], 'sell_num' => $agent['total_num']));
                    Dever::load('agent/lib/member_set')->log($agent['mid'], '', $agent['type'], $agent['type_id'], $agent['sku_id'], 1, $agent['total_num'], 0, $agent['name'], '礼品卡' . $data['card'] . '已兑换，兑换人手机号：' . $this->user['mobile']);
                }
            }
        }

        $info = array();
        $config = Dever::db('main/sell_config')->find();
        $info['card_name'] = $config['card_name'];
        $info['info'] = Dever::db('card/info')->find($data['card_id']);
        if ($info['info']) {
            $info['type'] = Dever::db('card/type')->find($info['info']['type_id']);
        }

        if (!$data['total_cash']) {
            $data['total_cash'] = $info['info']['value'];
        }
        if (!$data['use_cash']) {
            $data['use_cash'] = 0;
        }
        $data['cash'] = $data['total_cash'] - $data['use_cash'];
        $info['card'] = $data;

        return $info;
    }

    # 查看兑换列表
    public function card()
    {
        $config = Dever::db('main/sell_config')->find();
        $this->data['card_pic'] = $config['card'];
        $this->data['card_name'] = $config['card_name'];
        $this->data['card'] = array();
        $card = Dever::db('card/code')->getPageData(array('dh_uid' => $this->uid));

        $city = Dever::input('city');
        if ($city) {
            $county = Dever::db('area/county')->find($city);
            if ($county) {
                $city = $county['city_id'];
            }
        }
        $time = time();
        if ($card) {
            foreach ($card as $k => $v) {
                $check = Dever::load('card/lib/code')->check($v);
                if ($check < 0) {
                    continue;
                }

                if (!$v['total_cash']) {
                    $v['total_cash'] = $v['info']['value'];
                }
                if (!$v['use_cash']) {
                    $v['use_cash'] = 0;
                }
                $v['cash'] = $v['total_cash'] - $v['use_cash'];
                $v['type'] = Dever::db('card/type')->find($v['info']['type_id']);
                $v['minfo'] = '';

                if ($v['type']['city_type'] == 2) {
                    $city = $v['city'];
                    if (!$city) {
                        $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
                        if ($card_order && $card_order['city']) {
                            $city = $card_order['city'];
                        }
                    }

                    if ($city) {
                        $city = Dever::db('area/city')->find($city);
                        if ($city) {
                            $v['minfo'] = '仅限' . $city['name'] . '可用';
                        }
                    }
                } elseif ($v['type']['type'] == 2 && $v['shop_id']) {
                    $shop = Dever::db('shop/info')->find($v['shop_id']);
                    if ($shop) {
                        $v['minfo'] = '限' . $shop['name'] . '可用';
                    }
                } else {
                    $config_type = Dever::db('shop/info')->config['config_type'];

                    /*
                    $v['type']['shop_type'] = explode(',', $v['type']['shop_type']);
                    $v['minfo'] = array();
                    foreach ($v['type']['shop_type'] as $tv) {
                        if (isset($config_type[$tv])) {
                            $v['minfo'][] = $config_type[$tv];
                        }
                    }
                    $v['minfo'] = implode('、', $v['minfo']) . '可用';
                    */

                    
                    if ($v['type']['shop_type'] && strstr($v['type']['shop_type'], ',')) {
                        $v['minfo'] = '多店铺可用';
                    } elseif ($v['type']['shop_type'] && isset($config_type[$v['type']['shop_type']])) {
                        $v['minfo'] = $config_type[$v['type']['shop_type']] . '可用';
                    }
                }

                $v['cinfo'] = '';
                if ($v['info']['category']) {
                    $v['cinfo'] = '限部分要求商品可用';
                } else {
                    $goods = Dever::db('card/info_goods')->find(array('card_id' => $v['info']['id']));
                    if ($goods) {
                        $v['cinfo'] = '限部分要求商品可用';
                    }
                }
                $this->data['card'][] = $v;
            }
        }

        return $this->data;
    }

    # 查看可用的礼品卡
    public function use_card()
    {
        # 对商品进行处理
        $goods_id = Dever::input('goods_id');
        if ($goods_id) {
            $data = array();
            $data['shop'] = $this->shop;
            Dever::load('shop/lib/sell')->goods($data);
        }

        $config = Dever::db('main/sell_config')->find();
        $this->data['card_name'] = $config['card_name'];

        $shop_type = $this->shop['type'];
        $cur_city = Dever::input('city');
        $cur_city = $this->shop['city'];
        if ($cur_city) {
            $county = Dever::db('area/county')->find($cur_city);
            if ($county) {
                $cur_city = $county['city_id'];
            }
        }
        $config_type = Dever::db('shop/info')->config['config_type'];

        $card = Dever::db('card/code')->getData(array('dh_uid' => $this->uid));

        $this->data['card'] = array();
        $this->data['no_card'] = array();
        if ($card) {
            $time = time();
            foreach ($card as $k => $v) {
                $check = Dever::load('card/lib/code')->check($v);
                if ($check < 0) {
                    continue;
                }
                if (!$v['total_cash']) {
                    $v['total_cash'] = $v['info']['value'];
                }
                if (!$v['use_cash']) {
                    $v['use_cash'] = 0;
                }
                $v['cash'] = $v['total_cash'] - $v['use_cash'];
                if ($v['cash'] <= 0) {
                    continue;
                }
                # 验证是否符合当前商品
                $card_goods = array();
                $check = false;
                $xian = false;
                if ($this->shop['type'] == 1 && $v['info']['shop_set'] == 1) {
                    $check = false;
                    $xian = false;
                } else {
                    $xian = true;
                    $goods = Dever::db('card/info_goods')->getData(array('card_id' => $v['info']['id']));
                    if ($goods || $v['info']['category']) {
                        $check = true;
                    }
                }
                
                $v['minfo'] = '';
                $v['cinfo'] = '';

                if ($check) {
                    $v['cinfo'] = '限部分要求商品可用';
                    $price = 0;
                    $v['info']['category'] = explode(',', $v['info']['category']);
                    if ($goods) {
                        $goods = array_keys($goods);
                    }
                    # 检测当前商品是否符合该卡的限制
                    if ($goods_id && isset($data['list']) && $data['list']) {
                        foreach ($data['list'] as $k1 => $v1) {
                            if (!isset($v1['cash'])) {
                                $v1['cash'] = $v1['price'];
                            }
                            if ($goods) {
                                if (in_array($v1['id'], $goods)) {
                                    $price += ($v1['cash'] * $v1['buy_num']);
                                }
                            } elseif ($v['info']['category']) {
                                if (in_array($v1['top_category_id'], $v['info']['category'])) {
                                    $price += ($v1['cash'] * $v1['buy_num']);
                                }
                            }
                        }
                    }
                } else {
                    $price = $data['price'];
                }

                if ($xian && ($price <= 0 || ($v['info']['min'] > 0 && $price < $v['info']['min']))) {
                    # 不满足限额 不可用
                    $no = true;
                } else {
                    # 可用
                    $no = false;
                }
                $v['type'] = $type = Dever::db('card/type')->find($v['info']['type_id']);

                if ($no) {
                    $this->data['no_card'][] = $v;
                } else {
                    /*
                    $type['shop_type'] = explode(',', $type['shop_type']);
                    $v['minfo'] = array();
                    foreach ($type['shop_type'] as $tv) {
                        if (isset($config_type[$tv])) {
                            $v['minfo'][] = $config_type[$tv];
                        }
                    }
                    $v['minfo'] = implode('、', $v['minfo']) . '可用';
                    */

                    if ($type['shop_type'] && strstr($type['shop_type'], ',')) {
                        $v['minfo'] = '多店铺可用';
                    } elseif ($type['shop_type'] && isset($config_type[$type['shop_type']])) {
                        $v['minfo'] = $config_type[$type['shop_type']] . '可用';
                    }
                    $type['shop_type'] = explode(',', $type['shop_type']);
                    if (in_array($shop_type, $type['shop_type'])) {
                        
                        if ($type['city_type'] == 1) {
                            $this->data['card'][] = $v;
                        } else {
                            $city = $v['city'];
                            if (!$city) {
                                $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
                                if ($card_order && $card_order['city']) {
                                    $city = $card_order['city'];
                                }
                            }
                            if ($city) {
                                $city_info = Dever::db('area/city')->find($city);
                                $v['minfo'] = '仅限' . $city_info['name'] . '可用';
                                if ($city == $cur_city) {
                                    $this->data['card'][] = $v;
                                } else {
                                    $this->data['no_card'][] = $v;
                                }
                            } else {
                                $this->data['no_card'][] = $v;
                            }
                            
                            /*
                            $type['city'] = explode(',', $type['city']);
                            if (in_array($city, $type['city'])) {
                                $this->data['card'][] = $v;
                            }
                            */
                        }
                    } elseif ($v['shop_id']) {
                        $shop = Dever::db('shop/info')->find($v['shop_id']);
                        $v['minfo'] = '限' . $shop['name'] . '可用';
                        if ($v['shop_id'] == $this->shop_id) {
                            $this->data['card'][] = $v;
                        } else {
                            $this->data['no_card'][] = $v;
                        }
                    } else {
                        $this->data['no_card'][] = $v;
                    }
                }
            }
        }

        $this->data['card_num'] = count($this->data['card']);
        $this->data['no_card_num'] = count($this->data['no_card']);

        return $this->data;
    }
}