<?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\login\admin\controller;
use \temmoku\controller;
use \temmoku\db;
use plugin\login\admin\model\index AS model;
use plugin\login\public_class\cache;
class more extends controller{
	
	public function listing(){
		$data=(new model)->get_data();
		$row=db::select('*')->from(jab.'plugin')->where("  `initial_name` LIKE 'wechat'")->row();
		if($row){
			$_row=db::select('*')->from(P_jab.'list')->where("  `initial_name` LIKE 'wechat'")->row();
			$_data['initial_name']='wechat';
			$_data['name']='微信登陆';
			$_data['description']='微信登陆需要用到微信插件，需要设置参数，请到插件->微信插件->参数设置中设置';
			$_data['version']=$row['version'];
			$_data['is_install']=	$_row ? 1 : 0;
			$data[]=array_merge((array)$_data,(array)$_row);
		}
		$this->assign('data', $data);
		$this->display(tpl('more/list'));
	}
	
	public function install(){
		$row=db::select('*')->from(P_jab.'list')->where("  `initial_name` LIKE '$_GET[dir]'")->row();
		if($row){
			$this->err('无需重复安装');
		}
		
		if($_GET['dir']=='wechat'){
			$name='微信登陆';
			$loginurl="/wechat/login/index";
			$is_son='0';
			$is_open_window=0;
			$match_field='wechat_openid';
		}else{
			$row=include_once	APP_PATH.PLUGIN."data/".$_GET['dir'].".php";
			$name=$row['name'];
			$is_son=$row['is_son'];
			$loginurl=$row['loginurl'];
			$is_open_window=$row['is_open_window'];
			$match_field=$row['match_field'];
		}
		
		$array=[
			'initial_name'=>$_GET['dir'],
			'loginurl'=>$loginurl,
			'name'=>$name,
			'is_son'=>$is_son,
			'is_open_window'=>$is_open_window,
			'match_field'=>$match_field
		];
		$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_login_",P_jab,$sql);
			$explode=sql_regular_replace($sql);
			foreach($explode as $v){
				db::query($v.';');
			}
		}
		db::insert(P_jab.'list')->cols($array)->query();
		cache::more_login_list_cache();
		$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();
		cache::more_login_list_cache();
		$this->success('卸载成功');
	}
	
	public function edit(){
		
		if($_POST['step']=='post'){
			$array=['is_open'=>$_POST['is_open'],'setting'=>serialize($_POST['setting'])];
			db::update(P_jab.'list')->cols($array)->where("id=".$_POST['id'])->query();
			cache::more_login_list_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";

		$this->assign('setting', unserialize($row['setting']));
		$this->assign('data', $data);
		$this->assign('row', $row);
		$tpl=$row['initial_name'].'/edit';
		$this->display(tpl($tpl));
	}
	
	public function edit_state(){
		db::update(P_jab.'list')->cols(['is_open'=>$_POST['is_open']])->where("id='$_POST[id]'")->query();
		cache::more_login_list_cache();
	}
	
	
	public function edit_thumb(){
		$row=db::select('*')->from(P_jab.'list')->where("  `id` = '$_POST[id]'")->row();
		if(!$row){
			$this->err('不存在的数据');
		}
		if($row['thumb'] && $row['thumb'] != $_POST['thumb'] ){
			del_file($row['thumb']);
		}
		$thumb=UploadFiles_MOVE($_POST['thumb'],P_MODULE."/icon");
		db::update(P_jab.'list')->cols(['thumb'=>$thumb])->where("id='$_POST[id]'")->query();
		cache::more_login_list_cache();
		$array=['code'=>'0','thumb'=>get_img_url($thumb)];
		echo json_encode($array);
	}
	
	
	public function vendor(){
		if(!$_GET['name'] ){
			$this->err('缺少参数');
		}
		$son_controller=$_GET['login_c'] ? strtolower($_GET['login_c']) :'index';
		$son_method=$_GET['login_m'] ? strtolower($_GET['login_m']) :'index';
		$_GET['name']=strtolower($_GET['name']);
		define('admin_login_url',admin_plugin_url.'vendor/name_'.$_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.'控制器不存在');
		}
	}
}
?>