<?php
namespace Manage\Src;

use Dever;
use Manage\Src\Lib\Input as Html;

class Opt
{
	# 建立的索引名
	const INDEX = 'i1';
	
	public function push($param = false)
	{
		$param = $param ? $param : json_decode(base64_decode(Dever::input('param')), true);
		
		if ($param) {
			foreach ($param as $k => $v) {
				$k = explode('.', $k);
				$time = $v['time'];
				unset($v['time']);
				$this->update($k[0], $k[1], implode(',', $v), false, $time);
			}
		}
		
		return 1;
	}
	
	public function get($id)
	{
		$opt = is_array($id) ? $id : Dever::db('manage/opt')->one($id);
		
		$db = Dever::db($opt['project'] . '/' . $opt['table']);

		if (empty($db->config['lang'])) {
			return;
		}

		$result['table'] = $db->config['lang'] . '<br />' . $opt['table'];
		
		$result['total'] = Dever::load($opt['project'] . '/' . $opt['table'] . '-total');
		
		$table = '<b>[表结构]</b>：';
		
		if ($result['total'] > 0) {
			$table .= '数据总数（'.$result['total'].'）';
		}
		
		$table .= '<table border=1 width=100% height=100%>';
		$table .= Dever::tbody(array('字段', '类型-长度', '名称'));
		foreach ($db->config['struct'] as $k => $v) {
			if (isset($v['type'])) {
				$temp = Html::cue(array('name' => $v['name']));
				$v['name'] = $temp['name'];
				$table .= Dever::tbody(array($k, $v['type'], $v['name']));
			}
		}
		$table .= '</table>';
		
		$table .= '<br /><b>[常用字段]</b>：<table border=1 width=100% height=100%>';
		$table .= Dever::tbody(array('字段', '说明'));
		$col = explode(',', $opt['col']);
		
		$index = array();
		foreach ($col as $k => $v) {
			if (isset($db->config['struct'][$v])) {
				$struct = $db->config['struct'][$v];
				if ($v == 'id') {
					$struct['name'] = '<font style="color:red">主键</font>，无需索引';
				} elseif ((isset($struct['option']) && is_array($struct['option']) && count($struct['option']) <= 2) || strpos($struct['type'], 'tinyint') !== false) {
					$struct['name'] = '<font style="color:red">不同值较少</font>，无需索引';
				} else {
					$index[$v] = $v;
				}
				$table .= Dever::tbody(array($v, $struct['name']));
			}
		}
		$table .= '</table>';
		
		if ($opt['col_index']) {
			$table .= '<br /><b style="color:red">已为('.$opt['col_index'].')建立索引，版本号：<input type="text" id="version_'.$opt['id'].'" class="form-control " value="'.$opt['version'].'"></b>';
		} else {
			$table .= '<input type="hidden" id="version_'.$opt['id'].'" class="form-control " value="0">';
		}
		
		if ($index) {
			$index = implode(',', $index);
			
			$result['index'] = $index;
			
			$table .= '<br /><br /><b style="color:blue">建议索引：'.self::INDEX.'-<input type="text" style="width:300px;" id="index_'.$opt['id'].'" class="form-control " value="'.$index.'"></b>';
			
			$url = Dever::url('opt.index?id=' . $opt['id']);
			
			$table .= '&nbsp;&nbsp;&nbsp;&nbsp;<a class="btn btn-default" onclick="load(\''.$url.'\' + \'&version=\' + $(\'#version_'.$opt['id'].'\').val() + \'&index=\' + $(\'#index_'.$opt['id'].'\').val())">点此更新索引</a>';
		}
		
		$result['text'] = $table;
		
		return $result;
	}
	
	public function index_api()
	{
		$id = Dever::input('id');
		$config = Dever::load('manage/opt-one', $id);
		if ($config) {
			$info = $this->get($config);
			
			if (isset($info['index'])) {
				$index = array();
				
				$info['index'] = Dever::input('index', $info['index']);
				
				$index['version'] = Dever::input('version', 0);
				
				$index['version'] += 1;
				
				$index[$index['version']] = array
				(
					self::INDEX => $info['index']
				);
				
				Dever::load($config['project'] . '/' . $config['table'] . '-index', $index);
				
				Dever::load('manage/opt-update', array('set_status' => 2, 'set_version' => $index['version'], 'set_col_index' => $info['index'], 'where_id' => $id));
			}
		}
		
		Dever::out('yes');
	}
	
	private function update($project, $table, $col, $method = false, $time = 0)
	{
		$param['option_project'] = $project;
		$param['option_table'] = $table;
		if ($method) $param['option_method'] = $method;

		$id = -1;
		
		if ($col) {
			$info = Dever::db('manage/opt')->one($param);

			if ($info) {
				$param['where_id'] = $info['id'];
				$param['set_col'] = array();
				$col = explode(',', $col);
				$info['col'] = explode(',', $info['col']);

				foreach ($info['col'] as $v) {
					$param['set_col'][$v] = $v;
				}
				foreach ($col as $v) {
					$param['set_col'][$v] = $v;
				}

				$param['set_col'] = implode(',', $param['set_col']);
				$param['set_time'] = $time;
				Dever::db('manage/opt')->updateNum($param);
				$id = $info['id'];
			} else {
				$param['add_project'] = $param['option_project'];
				$param['add_table'] = $param['option_table'];
				if($method) $param['add_method'] = $param['option_method'];
				$param['add_col'] = $col;
				$param['add_time'] = $time;
				$param['add_num'] = 1;
				$param['add_status'] = 1;

				$id = Dever::db('manage/opt')->insert($param);
			}
		}
		
		return $id;
	}
}