<?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\admin\controller;
use \plugin\advertisement\admin\model\index as model;
use \temmoku\controller;
use \temmoku\db;
use \temmoku\lib\pages;
use \plugin\advertisement\public_class\cache;
use \plugin\advertisement\init;
class index extends controller{

	
	public function add(){
		!isset($_POST['step']) && $_POST['step']='';
		if('post'===$_POST['step']){
			
			if(!$_POST['post']['name']){
				$this->err('广告位名称不可为空');
			}
			if(!$_POST['post']['tag']){
				$_POST['post']['tag']=(new model)->tag_test();
			}else{
				if (preg_match("/[\x7f-\xff]/", $_POST['post']['tag'])) {  //判断字符串中是否有中文
					$this->err('广告位标签不可含有中文');
				}
				$_POST['post']['tag']=(new model)->tag_test(substr($_POST['post']['tag'],0,32));
			}
			
			
			if($_POST['post']['ad_type']=='images'){
				$imgurllist=[];
				foreach($_POST['post']['html']['images']['imgurl'] as $key=>$v){
					$v=trim($v);
					$imgurllist[]=$v ? UploadFiles_MOVE($v,"ad") : $v;
				}
				unset($_POST['post']['html']['images']['imgurl']);
				$_POST['post']['html']['images']['imgurl']=$imgurllist;
			}
			
			if($_POST['post']['ad_type']=='code'){//代码广告位是不可以出售的，很危险
				$_POST['post']['is_sell']=0;
				$_POST['post']['sellnum']=0;
			}
			
			$arr=[
			    'name'=>$_POST['post']['name'],
				'tag'=>$_POST['post']['tag'],
			    'ad_type'=>$_POST['post']['ad_type'],
				'is_sell'=>intval($_POST['post']['is_sell']),
				'price'=>serialize($_POST['post']['price']),
				'integral'=>intval($_POST['post']['integral']),
				'sellnum'=>intval($_POST['post']['sellnum']),
				'tpl'=>$_POST['post']['tpl'],
				'msg'=>$_POST['post']['msg'],
			    'setting'=>serialize($_POST['post']['html']),
			    'posttime'=>time()
		    ];
			db::insert(init::$table."list")->cols($arr)->query();
			
		    cache::ad_cache();
		    $this->Success('广告添加成功');
		}
		$this->display(tpl('index/add'));
	}
	public function index(){
		!isset($_GET['page']) && $_GET['page']=1;
		$rows='20';
		$data=db::select('*')->from(init::$table."list")->calcFoundRows()->orderByDESC(['id'])->setPaging($rows)->page($_GET['page'])->query();
		
		$getlistNUM=db::row(' SELECT FOUND_ROWS() AS allNUM');
		$Get_Page=(new pages)->pages('','',admin_plugin_url."/index/index/",$rows,$_GET['page'],$getlistNUM['allNUM']);
		$datadb=[];
		foreach($data as $row){
			$_row=db::select('bid,endtime , count(bid) AS num')->from(init::$table."buy_list")->where("bid ='$row[id]' AND endtime >".NOWTIME)->row();
			$row['effective']=$_row['num'];
			$_row=db::select('bid ,endtime, count(bid) AS num')->from(init::$table."buy_list")->where("bid ='$row[id]' AND endtime <".NOWTIME)->row();
			$row['invalid']=$_row['num'];
			$datadb[]=$row;
		}
		$this->assign('data', $datadb);
		$this->assign('Get_Page',$Get_Page);
		$this->display(tpl('index/index'));
	}
	
	public function edit(){
		$retrun=db::select('*')->from(init::$table."list")->where("id='$_GET[id]'")->row();
		$setting=unserialize(stripslashes($retrun['setting']));
		!isset($_POST['step']) && $_POST['step']='';
		if('post'===$_POST['step']){
			if(!$_POST['post']['name']){
				$this->err('广告位名称不可为空');
			}
			
			$imgurllist=[];
			foreach($_POST['post']['html']['images']['imgurl'] as $key=>$v){
				if(!in_array($v,$setting['images']['imgurl'])){
					$v=trim($v);
					$imgurllist[]=$v ? UploadFiles_MOVE($v,"ad") : $v;
				}else{
					$imgurllist[]=$v;
				}
			}
			unset($_POST['post']['html']['images']['imgurl']);
			$_POST['post']['html']['images']['imgurl']=$imgurllist;
			
			
			
			foreach($setting['images']['imgurl'] as $key=>$v){
				if(!in_array($v,$_POST['post']['html']['images']['imgurl'])){
					$oldfile=Temmoku_PATH.C('UPFILES_CATALOG').DS.$v;
					@unlink($oldfile);//删除这个文件
				}
			}
			if($_POST['post']['ad_type']=='code'){//代码广告位是不可以出售的，很危险
				$_POST['post']['is_sell']=0;
				$_POST['post']['sellnum']=0;
			}
			
			$arr=[
			    'name'=>$_POST['post']['name'],
				'tag'=>$_POST['post']['tag'],
			    'ad_type'=>$_POST['post']['ad_type'],
				'is_sell'=>intval($_POST['post']['is_sell']),
				'price'=>serialize($_POST['post']['price']),
				'integral'=>intval($_POST['post']['integral']),
				'sellnum'=>intval($_POST['post']['sellnum']),
				'tpl'=>$_POST['post']['tpl'],
				'msg'=>$_POST['post']['msg'],
			    'setting'=>serialize($_POST['post']['html']),
			    'posttime'=>time()
			];
		    db::update(init::$table."list")->cols($arr)->where("id='$_POST[id]'")->query();
		   
		    cache::ad_cache();
		    $this->Success('广告修改成功',PLUGIN_ACTION."/index/index");
		}
		$this->assign('setting',$setting);
		$this->assign('edit',$retrun);
		$this->display(tpl('index/add'));
	}
	public function del(){
		$retrun=db::select('*')->from(init::$table.'list')->where("id='$_GET[id]'")->row();
		$setting=unserialize($retrun['setting']);
		
		$buyloglist=db::select('*')->from(init::$table.'buy_list')->where("bid='$_GET[id]' AND ad_type='images'")->query();
		foreach($buyloglist AS $row){
			
		}
		db::delete(init::$table.'buy_list')->where("bid='$_GET[id]'")->query();

		if($setting['images']['imgurl']){
			foreach($setting['images']['imgurl'] as $v){
				$oldfile=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$v;
				del_file($oldfile);
			}
		}
		db::delete(init::$table.'list')->where("id='$_GET[id]'")->query();
		
		cache::ad_cache();
		$this->Success('广告删除成功');
	}

	
}
?>
