<?php

namespace Shop\Src;

use Dever;
use Main\Lib\Core;

class My extends Core
{
	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}

	public function getInfo()
	{
		$this->data['user'] = $this->user;

		# 获取拥有的优惠券数量
		$this->data['coupon_num'] = Dever::db('shop/user_coupon')->total(array('uid' => $this->uid, 'status' => 1));

		$this->data['card_num'] = Dever::db('card/code')->total(array('dh_uid' => $this->uid));

		$this->data['score'] = Dever::load('score/lib/core')->getUserScore($this->uid);

		$this->data['msg'] = Dever::load('message/lib/data')->num($this->uid);

		$this->data['level'] = 'Lv1';

		# 获取代理商绑定信息
		$bind = Dever::db('agent/user_bind')->find(array('uid' => $this->uid));
		if ($bind && $bind['status'] == 2 && $bind['mid']) {
			$this->data['bind_status'] = $bind['status'];
			$this->data['agent'] = Dever::load('agent/lib/member')->getInfo($bind['mid']);
		} elseif ($bind && $bind['status'] != 2) {
			$this->data['bind_status'] = $bind['status'];
		} else {
			$this->data['bind_status'] = 0;
		}

		return $this->data;
	}

	# 我的优惠券列表
	public function coupon()
	{
		$this->data = Dever::db('shop/user_coupon')->getAllPage(array('uid' => $this->uid));

		if ($this->data) {
			$time = time();
			foreach ($this->data as $k => $v) {
				if ($time > $v['edate']) {
					$this->data[$k]['status'] = 3;
				}
				$this->data[$k]['edate'] = date('Y-m-d', $v['edate']);
				$this->data[$k]['info'] = Dever::db('goods/coupon')->find($v['coupon_id']);
				$this->data[$k]['shop'] = array();
				$this->data[$k]['method'] = $this->data[$k]['info']['method'];
				$this->data[$k] = Dever::load('shop/lib/coupon')->getShop($this->data[$k], false, true);
			}
		}

		return $this->data;
	}

	# 我的礼品卡列表 废弃
	public function card()
	{
		$this->data['card'] = Dever::db('goods/card_code')->select_page(array('uid' => $this->uid));

		if ($this->data['card']) {
			$status = Dever::db('goods/card_code')->config['status'];
			$time = time();
			foreach ($this->data['card'] as $k => $v) {

				if ($v['status'] == 1 && $time > $v['edate']) {
					$v['status'] = 4;
					Dever::db('goods/card_code')->update(array('where_id' => $v['id'], 'status' => 4));
				}
				$this->data['card'][$k]['status_name'] = $status[$v['status']];
				$this->data['card'][$k]['edate'] = date('Y-m-d', $v['edate']);
				$this->data['card'][$k]['goods'] = array();
				$info = Dever::db('goods/card')->find($v['card_id']);
				$goods = Dever::array_decode($info['goods']);
				foreach ($goods as $k1 => $v1) {
					$this->data['card'][$k]['goods'][$k1] = Dever::db('goods/info')->find($v1['goods_id']);
					$this->data['card'][$k]['goods'][$k1]['num'] = $v1['num'];
				}

				if (($v['status'] == 2 || $v['status'] == 3) && $v['order_id'] > 0) {
					# 查找订单
					$order = Dever::db('shop/sell_order')->find($v['order_id']);
					$this->data['card'][$k]['order_num'] = $order['order_num'];
				} else {
					$this->data['card'][$k]['order_num'] = '';
				}
			}
		}

		return $this->data;
	}

	# 检测礼品卡 废弃
	public function dh_card()
	{
		$city = Dever::input('city');
        $lng = Dever::input('lng');
        $lat = Dever::input('lat');

		$data['shop'] = Dever::load('shop/lib/info')->get($city, $lng, $lat);
		if (!$data['shop']) {
			Dever::alert('没有门店');
		}

		$data['card'] = Dever::load('shop/lib/sell')->set(1, 1)->card($this->uid, $data['shop']);
		
		return $data;
	}

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

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

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

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

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

	# 用户退款
	public function order_tui_commit()
	{
		$order_id = Dever::input('order_id');

        return Dever::load('shop/lib/refund')->set('sell')->apply(1, $this->uid, $order_id, false, 1, 0, '用户自行取消退款');
	}

	# 获取订单退款记录
	public function order_tui_log()
	{
		$order_id = Dever::input('order_id');

        return Dever::load('shop/lib/refund')->set('sell')->getList(1, $this->uid, $order_id);
	}

	# 我的消息列表
	public function msg()
	{
		$data = Dever::load('message/lib/data')->read($this->uid, false, true);

		if ($data) {
			foreach ($data as $k => $v) {
				$data[$k]['cdate_string'] = Dever::ddate($v['cdate']);
				if ($v['param']) {
					$data[$k]['param'] = Dever::json_decode($v['param']);
				}
			}
		}
		return $data;
	}

	# 删除消息
	public function msg_delete()
	{
		$id = Dever::input('id');

		Dever::db('message/inbox')->delete(array('id' => $id, 'uid' => $this->uid));

		return $this->msg();
	}

	# 查看我的消息 废弃
	public function view_msg()
	{
		$id = Dever::input('id');
		$data = Dever::load('message/lib/data')->view($this->uid, $id);

		if ($data) {
			$data['cdate_string'] = Dever::ddate($data['cdate']);
			$type = Dever::db('message/type')->one($data['type']);
			$data['type_name'] = $type['name'];
		}

		return $data;
	}

	# 我的地址列表
	public function address()
	{
		return Dever::load('passport/address')->getAddress($this->uid);
	}

	# 获取单个地址信息
	public function getAddress()
	{
		$id = Dever::input('id');
		return Dever::load('passport/address')->getOne($this->uid, $id);
	}

	# 删除单个地址信息
	public function delAddress()
	{
		$id = Dever::input('id');
		return Dever::load('passport/address')->del($this->uid, $id, 2);
	}

	# 我的发票列表
	public function invoice()
	{
		return Dever::load('passport/invoice')->get($this->uid);
	}

	# 获取单个发票信息
	public function getInvoice()
	{
		$id = Dever::input('id');
		return Dever::load('passport/invoice')->getOne($this->uid, $id);
	}

	# 删除单个发票信息
	public function delInvoice()
	{
		$id = Dever::input('id');
		return Dever::load('passport/invoice')->del($this->uid, $id, 2);
	}

	# 提交反馈
	public function feedback()
	{
		$data['uid'] = $this->uid;
		$data['shop_id'] = Dever::input('shop_id');
		$data['mobile'] = Dever::input('mobile');
		$data['truename'] = Dever::input('truename');
		$data['content'] = Dever::input('content');

		return Dever::db('shop/feedback')->insert($data);
	}

	# 领取优惠券
	public function take_coupon()
	{
		
	}

	# 绑定代理商
	public function bind()
	{
		$data['type'] = Dever::input('type', 1);
		$data['mobile'] = Dever::input('mobile');
        $data['name'] = Dever::input('name');
        $data['idcard'] = Dever::input('idcard');
        $data['idcard_front'] = Dever::input('idcard_front');
        $data['idcard_back'] = Dever::input('idcard_back');
        $data['company_name'] = Dever::input('company_name');
        $data['company_pic'] = Dever::input('company_pic');
        $data['company_number'] = Dever::input('company_number');

        if (!$data['name']) {
            Dever::alert('姓名不能为空');
        }
        if (!$data['mobile']) {
            Dever::alert('手机号不能为空');
        }
        Dever::load('agent/lib/member')->checkMcode($data['mobile']);

        $member = Dever::db('agent/member')->find(array('mobile' => $data['mobile']));
        if (!$member) {
            Dever::alert('代理商还未注册');
        }
        if ($member['status'] != 2) {
            Dever::alert('代理商还未审核通过或已被禁用');
        }
        $data['mid'] = $member['id'];
        if (!$data['idcard']) {
            Dever::alert('身份证号不能为空');
        }

        if (!$data['idcard_front']) {
            Dever::alert('身份证正面不能为空');
        }
        if (!$data['idcard_back']) {
            Dever::alert('身份证反面不能为空');
        }

        if ($data['type'] == 2) {
        	if (!$data['company_name']) {
                Dever::alert('公司名称不能为空');
            }
            if (!$data['company_pic']) {
                Dever::alert('营业执照照片不能为空');
            }
            if (!$data['company_number']) {
                Dever::alert('营业执照号码不能为空');
            }
        }

		$info = Dever::db('agent/user_bind')->find(array('uid' => $this->uid));

		if (!$info) {
			$data['uid'] = $this->uid;
			Dever::db('agent/user_bind')->insert($data);

			return 'ok';
		} else {
			$data['where_id'] = $info['id'];
			$data['status'] = 1;
			Dever::db('agent/user_bind')->update($data);
			return 'ok';
		}
	}
}