<?php
!defined('EMLOG_ROOT') && exit('error');
addAction('save_log', 'Ixc_field_create');
function Ixc_field_create($logid){
	$DB = Database::getInstance();
	$row = $DB->once_fetch_array("SELECT * FROM ".DB_PREFIX."Ixc WHERE gid ='$logid' AND (type = '0' or type is NULL)");
	if($row==0)$DB->query("INSERT INTO ".DB_PREFIX."Ixc (`gid`,`type`) VALUES ('$logid','0')");
    $Ixc_field = isset($_POST['Ixc_field']) ? $_POST['Ixc_field'] : '' ;
	if($Ixc_field){
		foreach($Ixc_field as $name => $value){
			$DB->query("UPDATE ".DB_PREFIX."Ixc SET $name='".addslashes($value)."' WHERE gid='$logid' AND (type = '0' or type is NULL)");
		}
	}
    $field = isset($_POST['field']) ? $_POST['field'] : '' ;
	if($field){
		$row = isset($row['value']) ? unserialize($row['value']) : [];
		//$Data = array_merge($row,$field);
		$Data = $field;
		$DB->query("UPDATE ".DB_PREFIX."Ixc SET value='".serialize($Data)."' WHERE gid='$logid' AND (type = '0' or type is NULL)");
	}
}
addAction('adm_writelog_head', 'Ixc_field_option');
function Ixc_field_option(){
	$id = isset($_GET['gid']) ? intval($_GET['gid']) : 0 ;
	$DB = Database::getInstance();
	$Ixc = $DB->once_fetch_array("SELECT * FROM ".DB_PREFIX."Ixc WHERE gid ='$id' AND (type = '0' or type is NULL)");
	if($Ixc)$row = unserialize($Ixc['value']);
	$plugin_storage = Storage::getInstance('Ixc_field');
	$article = $plugin_storage->getValue('article');
	$Ixc_title = $plugin_storage->getValue('Ixc')['title'];
	if(!is_array($article))$article = [];?>
<a href="#Ixc_field" class="text-muted small my-3" data-toggle="modal" data-target="#Ixc_field"><i class="icofont-options"></i> <?= empty($Ixc_title) ? '字段选项' : $Ixc_title ?></a>
<div class="modal" id="Ixc_field" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel"><?= empty($Ixc_title) ? '字段选项' : $Ixc_title ?></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
<?php 
foreach($article as $value){
	if(isset($value['true'])){
		$field_name = 'Ixc_field';
		$Ixc_Field_value = isset($Ixc['field_'.$value['value']]) ? $Ixc['field_'.$value['value']] : '';
	}else{
		$field_name = 'field';
		$Ixc_Field_value = isset($row['field_'.$value['value']]) ? $row['field_'.$value['value']] : '';
	}
	if($value['sort']==7){ ?><input type="hidden" name="<?=$field_name?>[field_<?=$value['value']?>]" value="<?=$Ixc_Field_value?>"><?php continue; } ?>
				<div class="form-group" id="field_<?=$value['value']?>">
					<label><?=$value['title']?>：</label>
		<?php if(!empty($value['array'])){
			$Ixc_Field_value = is_array($Ixc_Field_value) ? $Ixc_Field_value : [''];
			switch($value['sort']){
				case '1': 
					$option = explode(',',$value['other']);
					echo '<a href="javascript:$(\'#field_'.$value['value'].'\').append(\'<select name='.$field_name.'[field_'.$value['value'].'][] class=form-control><option value= >请选择...</option>';
					foreach($option as $val){
						echo '<option value='.$val.'>'.$val.'</option>';
					}
					echo '\');">+</a>';
					foreach($Ixc_Field_value as $Field_value){
						echo '<select name="'.$field_name.'[field_'.$value['value'].'][]" class="form-control"><option value="">请选择...</option>';
						foreach($option as $val){
							echo '<option value="'.$val.'"'.($Field_value==$val ? 'selected' : '').'>'.$val.'</option>';
						}
						echo '</select>';
					}
					break;
				case '2': 
					$option = explode(',',$value['other']);
					echo '<a href="javascript:$(\'#field_'.$value['value'].'\').append(\'|&emsp;';
					foreach($option as $val){
						echo '<input type=radio name='.$field_name.'[field_'.$value['value'].']['.(count($Ixc_Field_value) + 1).'][] value='.$val.'><label>'.$val.'&emsp;</label>';
					}
					echo '\');">+</a>&emsp;';
					foreach($Ixc_Field_value as $i => $Field_value){
						foreach($option as $val){
							echo '<input type="radio" name="'.$field_name.'[field_'.$value['value'].']['.$i.'][]" '.($val==$Field_value ? 'checked' : '').' value="'.$val.'"><label>'.$val.'&emsp;</label>';
						}
					}
					break;
				case '3': 
					$option = explode(',',$value['other']);
					/*echo '<a href="javascript:$(\'#field_'.$value['value'].'\').append(\'|&emsp;';
					foreach($option as $val){
						echo '<input type=checkbox name='.$field_name.'[field_'.$value['value'].'][][] value='.$val.'><label>'.$val.'&emsp;</label>';
					}
					echo '\');">+</a>&emsp;';*/
					foreach($Ixc_Field_value as $ii => $Field_value){
						$Field_value = is_array($Field_value) ? $Field_value : [];
						foreach($option as $i => $val){
							echo '<input type="checkbox" name="'.$field_name.'[field_'.$value['value'].'][]" id="field_'.$value['value'].$ii.$i.'" '.(in_array($val,$Field_value) ? 'checked' : '').' value="'.$val.'"><label for="field_'.$value['value'].$ii.$i.'">'.$val.'&emsp;</label>';
						}
						
					}
					break;
				case '4': 
					echo '<a href="javascript:$(\'#field_'.$value['value'].'\').append(\'<input type=color name='.$field_name.'[field_'.$value['value'].'][] placeholder='.$value['other'].' value='.(!empty($value['default']) ? $value['default'] : '#000000').'>\');">+</a>&emsp;';
					foreach($Ixc_Field_value as $Field_value){
						echo '<input type="color" name="'.$field_name.'[field_'.$value['value'].'][]" placeholder="'.$value['other'].'" value="'.($Field_value ? $Field_value : (!empty($value['default']) ? $value['default'] : '#000000')).'">';
					}
					break;
				case '5': 
					echo '<a href="javascript:$(\'#field_'.$value['value'].'\').append(\'<input type=number name='.$field_name.'[field_'.$value['value'].'][] placeholder='.$value['other'].' maxlength=5 style=width:100px; class=form-control>\');">+</a>';
					foreach($Ixc_Field_value as $Field_value){
						echo '<input type="number" name="'.$field_name.'[field_'.$value['value'].'][]" placeholder="'.$value['other'].'" value="'.intval($Field_value ? $Field_value : $value['default']).'" maxlength="5" style="width:100px;" class="form-control">';
					}
					break;
				case '6':
					echo '<a href="javascript:$(\'#field_'.$value['value'].'\').append(\'<textarea name='.$field_name.'[field_'.$value['value'].'][] rows=3 placeholder='.$value['other'].' class=form-control></textarea>\');">+</a>';
					foreach($Ixc_Field_value as $Field_value){
						echo '<textarea name="'.$field_name.'[field_'.$value['value'].'][]" rows="3" placeholder="'.$value['other'].'" class="form-control">'.($Field_value ? $Field_value : $value['default']).'</textarea>';
					}
					break;
				default:
					echo "<a href=\"javascript:$('#field_".$value['value']."').append('<input type=text name=".$field_name."[field_".$value['value']."][] placeholder=".$value['other']." class=form-control>\ ');\">+</a>";
					foreach($Ixc_Field_value as $Field_value){
						echo '<input type="text" name="'.$field_name.'[field_'.$value['value'].'][]" placeholder="'.$value['other'].'" value="'.($Field_value ? $Field_value : $value['default']).'" class="form-control">';
					}
			}
		}else{
			switch($value['sort']){
				case '1': 
					echo '<select name="'.$field_name.'[field_'.$value['value'].']" class="form-control"><option value="">请选择...</option>';
					$option = explode(',',$value['other']);
					foreach($option as $val){
						echo '<option value="'.$val.'"'.($Ixc_Field_value ? ($Ixc_Field_value==$val ? 'selected' : '') : ($value['default']==$val ? 'selected' : '')).'>'.$val.'</option>';
					}
					echo '</select>';
					break;
				case '2': 
					$option = explode(',',$value['other']);
					foreach($option as $i => $val){
						echo '<input type="radio" name="'.$field_name.'[field_'.$value['value'].']" id="field_'.$value['value'].$i.'" '.($Ixc_Field_value ? ($Ixc_Field_value==$val ? 'checked' : '') : ($value['default']==$val ? 'checked' : '')).' value="'.$val.'"><label for="field_'.$value['value'].$i.'">'.$val.'&emsp;</label>';
					}
					break;
				case '3': 
					$option = explode(',',$value['other']);
					$Ixc_Field_value = is_array($Ixc_Field_value) ? $Ixc_Field_value : [];
					foreach($option as $i => $val){
						echo '<input type="checkbox" name="'.$field_name.'[field_'.$value['value'].'][]" id="field_'.$value['value'].$i.'" '.(in_array($val,$Ixc_Field_value) ? 'checked' : '').' value="'.$val.'"><label for="field_'.$value['value'].$i.'">'.$val.'&emsp;</label>';
					}
					break;
				case '4': 
					echo '<input type="color" name="'.$field_name.'[field_'.$value['value'].']" placeholder="'.$value['other'].'" value="'.($Ixc_Field_value ? $Ixc_Field_value : (!empty($value['default']) ? $value['default'] : '#000000')).'">';
					break;
				case '5': 
					echo '<input type="number" name="'.$field_name.'[field_'.$value['value'].']" placeholder="'.$value['other'].'" value="'.intval($Ixc_Field_value ? $Ixc_Field_value : $value['default']).'" maxlength="5" style="width:100px;" class="form-control">';
					break;
				case '6':
					echo '<textarea name="'.$field_name.'[field_'.$value['value'].']" rows="3" placeholder="'.$value['other'].'" class="form-control">'.($Ixc_Field_value ? $Ixc_Field_value : $value['default']).'</textarea>';
					break;
				default:
					echo '<input type="text" name="'.$field_name.'[field_'.$value['value'].']" placeholder="'.$value['other'].'" value="'.($Ixc_Field_value ? $Ixc_Field_value : $value['default']).'" class="form-control"/>';
			}
		} ?>
				</div>
<?php 
}
 ?>	
				<a href="#" data-dismiss="modal" class="btn btn-sm btn-success shadow-sm mb-3">确定</a>
            </div>
        </div>
    </div>
</div>
<script>
    // 载入资源列表
    $('#Ixc_field').on('show.bs.modal', function (e) {
        var button = $(e.relatedTarget);
        var modal = $(this);
    });
</script>
<?php
}
//数据获取文章值
function Ixc_field($logid,$data = ''){
	$DB = Database::getInstance();
	$Ixc = $DB->once_fetch_array("SELECT * FROM ".DB_PREFIX."Ixc WHERE gid ='$logid' AND (type = '0' or type is NULL)");
	if($Ixc){$row = unserialize($Ixc['value']);}else{$row = [];}
	$value = array();
	if(is_array($data)){
		if(empty($data['value'])){
			foreach($row as $key => $val){
				if(strpos($key,'field_')!==false){
					$key = str_ireplace('field_','',$key);
					$value[$key] = $val;
				}
			}
			return $value;
		}else{
			if(!array_key_exists('msg',$data))$data['msg'] = '';
			if(!array_key_exists('inside',$data))$data['inside'] = ',';
			
			if(isset($Ixc['field_'.$data['value']])){
				$val = $Ixc['field_'.$data['value']];
			}else if(isset($row['field_'.$data['value']])){
				$val = $row['field_'.$data['value']];
			}else{
				return false;
			}
			
			if(is_array($val))$val = implode($data['inside'],$val);
			return isset($val) ? $val : $data['msg'];
		}
	}else{
		foreach($row as $key => $val){
			if(strpos($key,'field_')!==false){
				$key = str_ireplace('field_','',$key);
				$value[$key] = $val;
			}
		}
		return $value;
	}
	
}
//数据获取分类值
function Ixc_sort($sid,$data = ''){
	$DB = Database::getInstance();
	$Ixc = $DB->once_fetch_array("SELECT * FROM ".DB_PREFIX."Ixc WHERE gid ='$sid' AND type = '1'");
	if($Ixc){$row = unserialize($Ixc['value']);}else{$row = [];}
	$value = array();
	if(is_array($data)){
		if(empty($data['value'])){
			foreach($row as $key => $val){
				if(strpos($key,'sort_')!==false){
					$key = str_ireplace('sort_','',$key);
					$value[$key] = $val;
				}
			}
			return $value;
		}else{
			if(!array_key_exists('msg',$data))$data['msg'] = '';
			if(!array_key_exists('inside',$data))$data['inside'] = ',';
			if(isset($Ixc['sort_'.$data['value']])){
				$val = $Ixc['sort_'.$data['value']];
			}else if(isset($row['sort_'.$data['value']])){
				$val = $row['sort_'.$data['value']];
			}else{
				return false;
			}
			if(is_array($val))$val = implode($data['inside'],$val);
			return isset($val) ? $val : $data['msg'];
		}
	}else{
		foreach($row as $key => $val){
			if(strpos($key,'sort_')!==false){
				$key = str_ireplace('sort_','',$key);
				$value[$key] = $val;
			}
		}
		return $value;
	}
	
}
//数据获取用户值
function Ixc_user($uid,$data = ''){
	$DB = Database::getInstance();
	$Ixc = $DB->once_fetch_array("SELECT * FROM ".DB_PREFIX."Ixc WHERE gid ='$uid' AND type = '2'");
	if($Ixc){$row = unserialize($Ixc['value']);}else{$row = [];}
	$value = array();
	if(is_array($data)){
		if(empty($data['value'])){
			foreach($row as $key => $val){
				if(strpos($key,'user_')!==false){
					$key = str_ireplace('user_','',$key);
					$value[$key] = $val;
				}
			}
			return $value;
		}else{
			if(!array_key_exists('msg',$data))$data['msg'] = '';
			if(!array_key_exists('inside',$data))$data['inside'] = ',';
			
			if(isset($Ixc['user_'.$data['value']])){
				$val = $Ixc['user_'.$data['value']];
			}else if(isset($row['user_'.$data['value']])){
				$val = $row['user_'.$data['value']];
			}else{
				return false;
			}
			
			if(is_array($val))$val = implode($data['inside'],$val);
			return isset($val) ? $val : $data['msg'];
		}
	}else{
		foreach($row as $key => $val){
			if(strpos($key,'user_')!==false){
				$key = str_ireplace('user_','',$key);
				$value[$key] = $val;
			}
		}
		return $value;
	}
}
//数据设置值
function Ixc_field_set($id,$type,$field,$value='') {
	$DB = Database::getInstance();
	if($type==0)$val = 'field_'.$field;
	if($type==1)$val = 'sort_'.$field;
	if($type==2)$val = 'user_'.$field;
	if(empty($id) || empty($field))return false;
	$row = $DB->once_fetch_array("SELECT * FROM ".DB_PREFIX."Ixc WHERE gid ='$id' AND type = '$type'");
	if($row==0)$DB->query("INSERT INTO ".DB_PREFIX."Ixc (`gid`,`type`) VALUES ('$id','$type')");
	if($DB->num_rows($DB->query("show columns from ".DB_PREFIX."Ixc like '$val'"))==0){
		$row = isset($row['value']) ? unserialize($row['value']) : [];
		$row[$val] = $value;
		$DB->query("UPDATE ".DB_PREFIX."Ixc SET value='".serialize($row)."' WHERE gid='$id' AND type = '$type';");
	}else{
		$row = isset($row[$val]) ? $row[$val] : '';
		$DB->query("UPDATE ".DB_PREFIX."Ixc SET $val='".addslashes($row)."' WHERE gid='$id' AND type = '$type';");
	}
	return $value;
}
//搜索数据值
function Ixc_field_search(){
}
