<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\advertisement\home\controller;
use \temmoku\controller;
use \temmoku\db;
use \plugin\advertisement\init;
class index extends controller{
	
	public function index(){
		$data=C(MODULE.'|AD_LIST.'.$_GET['tag']);
		$data=\unserialize($data);
		$data['tpl']=htmlspecialchars_decode($data['tpl'],ENT_QUOTES);
		
		if($data){
			$ad='';
			
			if($data['ad_type']=="images"){
				foreach($data['adlist'] as $key=>$row){
					$row['weburl']=C(MODULE.'|setting.is301') ? WEBURL.'/'.MODULE."/index/click/tag/{$data['tag']}/key/{$key}".C('html')  : $row['weburl'];
					$ad.=str_replace ( ['网址','图片'] , [$row['weburl'],get_img_url($row['imgurl'])] , $data['tpl'] );
				}
			}elseif($data['ad_type']=="text"){
				foreach($data['adlist'] as $key=>$row){
					$row['weburl']=C(MODULE.'|setting.is301') ? WEBURL.'/'.MODULE."/index/click/tag/{$data['tag']}/key/{$key}".C('html')  : $row['weburl'];
					$ad.=str_replace ( ['网址','标题'] , [$row['weburl'],$row['title']] , $data['tpl'] );
				}
			}else{
				$ad=htmlspecialchars_decode($data['setting']['code'],ENT_QUOTES);
			}
			echo 'document.writeln("'.addslashes($ad).'");';
		}
	}
	
	public function click(){
		$data=C(MODULE.'|AD_LIST.'.$_GET['tag']);
		$data=\unserialize($data);
		
		header("location:".$data['adlist'][intval($_GET['key'])]['weburl']);
	}
	
	public function pay(){
		$row=db::select('*')->from(init::$table.'pay_log')->where("id= '$_GET[id]' ")->row();
		if(!$row){
			$this->err('不存在的订单');
		}
		if($row['state']){
			$this->err('已经支付过的订单');
		}
		$need_rmb=$row['rmb'] - ( $row['integral']/C('advertisement|setting.integral') );
		
		$array=[
			'transaction_code'=>$row['transaction_code'],
			'title'=>'在线购买广告',
			'content'=>'订单号：'.$row['transaction_code'],
			'payment'=>$row['payment'],
			'uid'=>C('mydb.uid'),
			'need_pay'=>$need_rmb,
			'class'=>'\\plugin\\advertisement\\public_class\\payment',
			'method'=>'run',
			'posttime'=>NOWTIME
		];
		$data=\plugin\payment\public_class\payment::init($array);
		if($data['code']=='-1'){
			$this->err($data['text']);
		}
		header("location:$data[url]");
		exit;
	}
}
?>