<?php
namespace member\controller;

/**
 * 106云盘自动发卡平台网盘系统
 * ============================================================================
 * 版权所有 2016-2028 扎兰屯市姝妍计算机服务有限公司，并保留所有权利。
 * 网站地址: http://www.106ym.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */
class WangpanLog extends Member {

	// 管理列表
	public function index(){
		$this->display();
    }
	
	// 购买
	public function buy(){
		$this->display('Wangpan_log_buy');
    }
	
	// 升级
	public function shengji(){
		$id=I('id',0);
		$uid=session("user_auth.id");
		$userconfig=userconfig_get($uid);
		$this->userconfig=$userconfig;
		$wangpan_type_old=$userconfig['wangpan_id'];
		$wangpan_type_new=I('id');
		//终身
		$wangpan_config=M('wangpan_config')->find($id);
		if($wangpan_config['iszhongshen']==1){
			$this->wangpan=$wangpan_config;
		    $this->amount = M('wangpan_price')->where(array('type_id'=>$id))->getField('price');
		}else{
			//距离过期天数
			$edays = floor(($userconfig['wangpan_etime']-time())/86400);
			if( $edays > 0){
				//升级网盘的类型
				$wangpan_days = M('wangpan_price')->where(array('type_id'=>$wangpan_type_new))->getField('days',true);
				
				$count=count($wangpan_days);
				$arr=array();
				for ($i=0; $i <$count ; $i++) { 
					$arr[$i]=abs($edays-$wangpan_days[$i]);
				}
				$min= min($arr);
				for ($i=0; $i <$count ; $i++) { 
					if ($min==$arr[$i]) {
						$days=$wangpan_days[$i];
					}
				}
				$price_old=M('wangpan_price')->where(array('days'=>$days,'type_id'=>$wangpan_type_old))->find();
				$price_old=$price_old['price']/$price_old['days'];
				$price_new=M('wangpan_price')->where(array('days'=>$days,'type_id'=>$wangpan_type_new))->find();
				$price_new=$price_new['price']/$price_new['days'];
				$amount=($price_new-$price_old)*$edays;
				//差价
				$this->amount=sprintf("%.2f",$amount);
				
				
			}
		}
		
		$this->display('Wangpan_log_shengji');
    }
    
    // 续费
	public function xufei(){
		$this->display('Wangpan_log_xufei');
    }
	
	//降级
	public function jiangji($id){
		$uid=session("user_auth.id");
		M('member')->save(array('wangpan_type'=>$id,'id'=>$uid));
		$wangpan_config=M('wangpan_config')->find($id);
		foreach ($wangpan_config as $k => $v) {
			$attr['wangpan_'.$k]=$v;
		}
		$re=userconfig_set($uid, $attr);
		
		$this->ajaxReturn(array(
			'status'=>1
		));
    }

}