<?php
// +----------------------------------------------------------------------
// | yershop网店管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | 版权申明：yershop网店管理系统不是一个自由软件，是贝云网络官方推出的商业源码，严禁在未经许可的情况下
// | 拷贝、复制、传播、使用yershop网店管理系统的任意代码，如有违反，请立即删除，否则您将面临承担相应
// | 法律责任的风险。如果需要取得官方授权，请联系官方http://www.yershop.com
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
/**
 * 后台数据表控制器
 */
class Table extends Home{
	
	
     /* 数据表查询首页 */
	public function index(){     
			$dbname =config('database.database');

			$list=Db::query("show tables ");
			$sql="select 
				TABLE_NAME, 
				TABLE_COMMENT 
			from 
				INFORMATION_SCHEMA.Tables
			where 
				table_schema = '$dbname'";
			$result=Db::query($sql);

	    $this->assign('result', $result);
		$this->meta_title="数据库管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch("table/index"); 
	}
	
	     /* 数据库备份 */
	public function out(){     
	   
        include(ROOT_PATH.'extend/Backup.php'); 
		$host=config('database.hostname');
		$username=config('database.username');
		$password=config('database.password');
		$dbname =config('database.database');
		
        $Backup= new \Backup($host, $username, $password,$dbname);	
		$res=$Backup->backup();
	    if($res["code"]==1){
		   $this->success($res["msg"]);	   
	   }else{
		  $this->error($res["msg"]);  
	   }
		
	}
	
		     /* 数据表备份 */
	public function backup($table=''){     
	   
        include(ROOT_PATH.'extend/Backup.php'); 
		$host=config('database.hostname');
		$username=config('database.username');
		$password=config('database.password');
		$dbname =config('database.database');
		
        $Backup= new \Backup($host, $username, $password,$dbname);	
		$res=$Backup->backup($table);
	    if($res["code"]==1){
		   $this->success($res["msg"]);	   
	   }else{
		  $this->error($res["msg"]);  
	   }
		
	}
	
	
	public function code($table=''){
      
        $prefix=config('database.prefix');	 
        $table = str_replace($prefix,"",$table);
        $fields = getColumns($table);         
        foreach ($fields as &$v) {
               $v['comment'] =getDbColumnComment($table, $v['name']);
        }   
        $this->assign('fields', $fields);
   
 		$this->meta_title="代码生成器管理";
		$this->assign('meta_title', $this->meta_title);
		$map["pid"]=0;
        $list =db('Module')->where($map)->select(); 
	    $this->assign('list', $list);;
	    $group_list =db('group')->select(); 
            /* 获取模块信息 */
       $this->assign('group_list', $group_list);
       return $this->fetch('');

   }	
      
    public function submit($table=''){
	   if($_POST){	

         if(!$_POST["title"]){
			 $this->error("菜单名称不能为空");
			 
		 }	   
		  $searchhtml='';$html="";
		   for ($i=0;$i<count($_POST['name']);$i++){
			  		
			 if($_POST['show'][$i]==1){
				 if($_POST['tit'][$i]){
					 
					 $html=get_search_html($_POST['tit'][$i],$_POST['name'][$i]);
				      $searchhtml.=$html; 
				 
				
			    }else{
				 $html=get_search_html($_POST['name'][$i],$_POST['name'][$i]);
				 $searchhtml.=$html;
				 
			    }
		    }
		  }
		 unset($html);
		  $indexhtml='';$tdhtml='';$html="";$html1="";
		   for ($i=0;$i<count($_POST['name']);$i++){		  
			   if($_POST['show'][$i]==1){
				 if($_POST['tit'][$i]){
					  $html=get_index_html($_POST['tit'][$i],$_POST['name'][$i]);
					  $indexhtml.=$html;
					  $html1=get_td_html($_POST['tit'][$i],$_POST['name'][$i]);
					  $tdhtml.=$html1;
				 
			    }else{
					  $html=get_index_html($_POST['name'][$i],$_POST['name'][$i]);
					 $indexhtml.=$html;
					  $html1=get_td_html($_POST['name'][$i],$_POST['name'][$i]);
					  $tdhtml.=$html1;
				 
		        }
		     }
			 
		  }
		   unset($html); unset($html1);
		  $html1="";
		  $html2="";$html="";
		  for ($i=0;$i<count($_POST['name']);$i++){
			   if($_POST['tit'][$i]){
			     $html=get_type_html($_POST['type'][$i],$_POST['tit'][$i],$_POST['name'][$i]);
			   }else{
				   
				   $html=get_type_html($_POST['type'][$i],$_POST['name'][$i],$_POST['name'][$i]);
			   }
			   
			 if($_POST['tab'][$i]==1){
				 $html1.=$html;
			 }
			 if($_POST['tab'][$i]==2){
				 $html2.=$html;
			 }
		  }	   
		   $Code = new \app\common\model\Code;	   
		   $res=$Code->build($searchhtml,$indexhtml,$tdhtml,$html1,$html2);
	       if($res){
			   if($_POST["pid"]){
			   $model =input("model"); 
			   $map["id"]=safe_replace($_POST["pid"]);
			   $data["pid"]=$_POST["pid"];
               $info=Db::name("module")->where($map)->find();
			   $data["group_id"]=$info["group_id"];
			   $prefix=config('database.prefix');
		       $model=str_replace($prefix,"",$model);
			   if(strpos($model,'_') !== false){ 
                    $list=explode('_', $model);
					$str0=ucfirst($list[0]);
					$str1=ucfirst($list[1]);
					$model=$str0.$str1;
                }
			   
		       $data["title"]=$_POST["title"];
			   $data["url"]=$model."/index";
			   Db::name("module")->insert($data);}
		      $this->success("执行成功！");
		   }else{
			  $error=$Code->getError()?$Code->getError():"执行失败";
			  $this->error($error);
		   } 
			
	   } 
	   


   }	
   
   
   public function lists($table='')

   {
         $tables='';
       $tables = Db::query('SHOW TABLE STATUS');

       $table_list = array_map('array_change_key_case', $tables);

  
            $prefix=config('database.prefix');
		 
           $table = str_replace($prefix,"",$table);

           $fields = getColumns($table);

           foreach ($fields as &$v) {

               $v['comment'] =getDbColumnComment($table, $v['name']);

           }

         $this->assign('fields', $fields);
   
 		$this->meta_title="字段管理";
		$this->assign('meta_title', $this->meta_title);

       return $this->fetch('');

   }	

 

   
    /* 编辑数据表 */
	public function edit($table=''){   
	    if($_POST){
		     $prefix=config('database.prefix');
		     $table=input('table');
		     $name=input('old');
		    $name=input('name');
			$table=input('table');
			$comment=input('comment');
		   if($_POST["type"]==1){
			   $new=$_POST["new"];$value=input('value');
		       $sql="alter table {$table} change {$name}  {$new}  {$value} COMMENT '{$comment}'";
              
			   
		   }//修改字段的类型
		  if($_POST["type"]==2){
			   $new=$_POST["new"];$newvalue=input('newvalue');
		       $sql="alter table {$table}  modify column {$name} {$newvalue} COMMENT '{$comment}'";
              
			   
		   }//修改字段的备注
           if($_POST["type"]==3){
			   $new=$_POST["new"];$value=input('value');
		       $sql="alter table {$table} modify column {$name}   {$value} COMMENT '{$comment}'";
               		  
			   
		   }
		   try {

           Db::query($sql); 
		   $this->success("更新成功！",cookie("__forward__"));

          } catch (Exception $e) {

              $error=$e->getMessage()?$e->getMessage():"更新失败！";
			  $this->error($error);

              }
	     
	   }
	   else{
		   $prefix=config('database.prefix');
		   $table=input('table');
           $prefix=config('database.prefix');
		 
           $table = str_replace($prefix,"",$table);

           $fields = getColumns($table);

           foreach ($fields as &$v) {

               $v['comment'] =getDbColumnComment($table, $v['name']);

           }

         $this->assign('fields', $fields);
		 $name=input('name');
		
	     $this->assign('info',$fields[$name]);
		  //dump($fields);
		   cookie("__forward__",input('server.HTTP_REFERER'));
		 $this->meta_title="编辑字段";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("table/edit");
	   }
	}
	 /* 增加数据表 */
	public function add($id=""){  
	   if($_POST){
		   $table=input('table');
		   $comment=input('comment');$value=input('value'); $name=input('name');
		   $sql="alter table {$table} add column {$name}   {$value} COMMENT '{$comment}'";
		    try {

           Db::query($sql); 
		   $this->success("更新成功！",cookie("__forward__"));

          } catch (Exception $e) {

              $error=$e->getMessage()?$e->getMessage():"更新失败！";
			  $this->error($error);

              }
	 }
	  else{
		
		  $this->meta_title="新增字段";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("table/add");
	  }
	}
	
	 /* 增加数据表 */
	public function modify($id=""){  
	   if($_POST){
		   $table=input('table');
		   $comment=input('comment');
		   $sql="alter table {$table} COMMENT '{$comment}'";
		    try {
           Db::query($sql); 
		   $this->success("更新成功！",cookie("__forward__"));

          } catch (Exception $e) {

              $error=$e->getMessage()?$e->getMessage():"更新失败！";
			  $this->error($error);

              }
	 }
	  else{
		
		  $this->meta_title="修改表备注信息";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch();
	  }
	}
	 /* 删除数据表字段*/
	public function del(){   
	  
		 $table=input('table');
		 $name=input('name');
		$sql="ALTER TABLE {$table} DROP COLUMN {$name}";
		 try {
           Db::query($sql); 
		   $this->success("删除成功！",cookie("__forward__"));
          } catch (Exception $e) {

              $error=$e->getMessage()?$e->getMessage():"删除失败！";
			  $this->error($error);

              }
	}
	 /* 删除数据表*/
	public function drop(){   
	   
		 $table=input('table');
		
		$sql="drop TABLE {$table}";
		 try {
           Db::query($sql); 
		   $this->success("删除成功！",cookie("__forward__"));
          } 
		  catch (Exception $e) {

              $error=$e->getMessage()?$e->getMessage():"删除失败！";
			  $this->error($error);

              }
	}
	 /* 删除数据表*/
	public function alert(){   
	    if($_POST){
		  $sql=input('sql');
		 if($sql){
			 $this->error("sql语句不能为空");
		 }
		 try {
           Db::query($sql); 
		   $this->success("删除成功！",cookie("__forward__"));
          } catch (Exception $e) {

              $error=$e->getMessage()?$e->getMessage():"删除失败！";
			  $this->error($error);

              }
		}else{
		
		  $this->meta_title="mysql命令";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch();
	  }	  
	}
}
