<?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\sms\admin\controller;
use plugin\sms\admin\model\index as model;
use \temmoku\controller;
use \temmoku\db;
use plugin\sms\public_class\cache;
class index extends controller{
	
	public function alllist(){
		$data=(new model)->get_data();
		$this->assign('data', $data);
		$this->display(tpl('index/list'));
	}
	
	public function setdefault(){
		$row=db::select('*')->from(P_jab.'list')->where("id=$_GET[id]")->row();
		if(!$row){
			$this->err('不存在的数据');
		}
		db::update(P_jab.'list')->cols(['is_default'=>'0'])->where('1')->query();
		db::update(P_jab.'list')->cols(['is_default'=>'1'])->where("id=$_GET[id]")->query();
		cache::sms_default_cache();
		$this->success('设置成功');
	}
	

	
	public function install(){
		$row=db::select('*')->from(P_jab.'list')->where("  `initial_name` LIKE '$_GET[dir]'")->row();
		if($row){
			$this->err('无需重复安装');
		}
		$array=['initial_name'=>$_GET['dir']];
		$sql_dir =APP_PATH.PLUGIN.'vendor/'.$_GET['dir'].'/data.sql';
		if(is_file($sql_dir)){
			$sql=fils_content($sql_dir);
			$sql=str_replace("temmoku_plug_sms_",P_jab,$sql);
			$explode=sql_regular_replace($sql);
			foreach($explode as $v){
				db::query($v.';');
			}
		}
		db::insert(P_jab.'list')->cols($array)->query();
		$this->success('安装完成');
	}
	
	public function uninstall(){
		$data=db::select('*')->from(P_jab.'list')->where("  `id` = '$_GET[id]'")->row();
		if(!$data){
			$this->err('不存在的数据');
		}
		//获取所有表
		$table=db::query("SHOW TABLE STATUS");
		foreach($table as $row){
			//进行正则匹配。不能胡乱删除
			$regular='/'.P_jab.$data['initial_name'].'/';
			preg_match_all($regular, $row['Name'], $matches);
			if($matches){
				if($matches[0]){
					$sql="DROP TABLE ".$row['Name'];
					db::query($sql);
				}
			}
		}
		db::delete(P_jab.'list')->where("  `id` = '$_GET[id]'")->query();
		$this->success('卸载成功');
	}
	
	
	public function edit(){
		
		if($_POST['step']=='post'){
			$array=['setting'=>serialize($_POST['setting'])];
			db::update(P_jab.'list')->cols($array)->where("id=".$_POST['id'])->query();
			cache::sms_default_cache();
			$this->success('编辑完成');
		}
		
		$row=db::select('*')->from(P_jab.'list')->where("  `id` = '$_GET[id]'")->row();
		$data =include_once APP_PATH.PLUGIN.'data/'.$row['initial_name'].".php";
		$code= include_once APP_PATH.PLUGIN.'code.php';
		
		$this->assign('setting', unserialize($row['setting']));
		$this->assign('code', $code);
		$this->assign('data', $data);
		$tpl=$row['initial_name'].'/edit';
		$this->display(tpl($tpl));
	}
	
	public function vendor(){
		if(!$_GET['name'] ){
			$this->err('缺少参数');
		}
		$son_controller=$_GET['c'] ? strtolower($_GET['c']) :'index';
		$son_method=$_GET['m'] ? strtolower($_GET['m']) :'index';
		$_GET['name']=strtolower($_GET['name']);
		define('admin_sms_url',admin_plugin_url.$_GET['name']);
		$file= APP_PATH.PLUGIN."vendor/".$_GET['name']."/admin/".$son_controller.".php";
	    if(!is_file($file)){
	    		$this->err('不存在的控制器文件<br>'.$file);
	    }
	    @include_once $file;
		$Modular_Class= "plugin\\".P_MODULE."\\vendor\\".$_GET['name']."\\admin\\".$son_controller;
		if (class_exists($Modular_Class)) {
		    //加载类
		    $Class=new $Modular_Class();
		    //加载方法
		    Load_Method($Class,$son_method,$Modular_Class);
		}else{
			E($Modular_Class.'控制器不存在');
		}
	}
	
	
	
	
}
?>