<?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'] = Dever::db('card/type')->getAll($where);

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

        # 获取选择的城市
        $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()
    {
        $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, 'status' => 2));
            if (!$data) {
                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('兑换失败，请重新兑换');
            }
        }

        $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'] = $data['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'] = 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'];
            }
        }

        if ($this->data['card']) {
            foreach ($this->data['card'] as $k => $v) {
                $v['info'] = $this->data['card'][$k]['info'] = Dever::db('card/info')->find($v['card_id']);

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

                $this->data['card'][$k]['minfo'] = '';

                if ($info['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) {
                            $this->data['card'][$k]['minfo'] = '仅限' . $city['name'] . '可用';
                        }
                    }
                } elseif ($info['type'] == 1) {
                    $config_type = Dever::db('shop/info')->config['config_type'];
                    if ($info['shop_type'] && strstr($info['shop_type'], ',')) {
                        $this->data['card'][$k]['minfo'] = '全部店铺可用';
                    } elseif ($info['shop_type'] && isset($config_type[$info['shop_type']])) {
                        $this->data['card'][$k]['minfo'] = $config_type[$info['shop_type']] . '可用';
                    }
                } elseif ($info['type'] == 2 && $v['shop_id']) {
                    $shop = Dever::db('shop/info')->find($v['shop_id']);
                    if ($shop) {
                        $this->data['card'][$k]['minfo'] = '限' . $shop['name'] . '可用';
                    }
                }
            }
        }

        return $this->data;
    }

    # 查看可用的礼品卡
    public function use_card()
    {
        $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'];
            }
        }

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

        $this->data['card'] = array();
        if ($card) {
            foreach ($card as $k => $v) {
                $v['info'] = Dever::db('card/info')->find($v['card_id']);
                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;
                }
                
                $v['type'] = $type = Dever::db('card/type')->find($v['info']['type_id']);

                $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 == $cur_city) {
                            $this->data['card'][] = $v;
                        }
                        /*
                        $type['city'] = explode(',', $type['city']);
                        if (in_array($city, $type['city'])) {
                            $this->data['card'][] = $v;
                        }
                        */
                    }
                } elseif ($v['shop_id'] && $v['shop_id'] == $this->shop_id) {
                    $this->data['card'][] = $v;
                }
            }
        }

        return $this->data;
    }
}