<?php 
require_once 'library/top_application.php';
require_once 'library/logged.php';

if (isset($_GET['action'])) {
  $action=$_GET['action'];
}

if (isset($_GET['id'])) {
  $id=$_GET['id'];
}

if ($action != 'add' && $action != 'edit'){
	$action='add';
}
//權限 
$sql_right = mysql_query('SELECT `manage_product` FROM `'.TBL_USER.'` WHERE `uid` = '.$_SESSION['Logged_Uid']);
$row_right = mysql_fetch_assoc($sql_right);

if($row_right['manage_product'] != 1){
	header("Location: listUser.php");
}

$sql_lang = mysql_query('SELECT `first_language` FROM `general_setting` WHERE `id` = 1');
$row_lang = mysql_fetch_assoc($sql_lang);

if ($row_lang['first_language'] == 1){
	//eng
	$lang_table = 'en_table';
	$must_subject = 'ep_option_name';
	$choice = 'ep_choice_value';
	$lang_btn = 'btn_lang_switch_en';
} elseif ($row_lang['first_language'] == 2){
	//tc
	$lang_table = 'tc_table';
	$must_subject = 'cp_option_name';
	$choice = 'cp_choice_value';
	$lang_btn = 'btn_lang_switch_tc';
} elseif ($row_lang['first_language'] == 3){
	//sc
	$lang_table = 'sc_table';
	$must_subject = 'sp_option_name';
	$choice = 'sp_choice_value';
	$lang_btn = 'btn_lang_switch_sc';
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "fHtmlEditor")){
	$option_type = isNumber(trim($_POST['option_type']));
	$ep_option_name = txt_only($_POST['ep_option_name']);
	$cp_option_name = txt_only($_POST['cp_option_name']);
	$sp_option_name = txt_only($_POST['sp_option_name']);
	$ep_choice_value = htmlPurify($_POST['ep_choice_value']);
	$cp_choice_value = htmlPurify($_POST['cp_choice_value']);
	$sp_choice_value = htmlPurify($_POST['sp_choice_value']);
	$ep_note = htmlPurify($_POST['ep_note']);
	$cp_note = htmlPurify($_POST['cp_note']);
	$sp_note = htmlPurify($_POST['sp_note']);
	
	if(!get_magic_quotes_gpc()){ 
		$ep_option_name = mysql_real_escape_string($ep_option_name);
		$cp_option_name = mysql_real_escape_string($cp_option_name);
		$sp_option_name = mysql_real_escape_string($sp_option_name);
		$ep_choice_value = mysql_real_escape_string($ep_choice_value);
		$cp_choice_value = mysql_real_escape_string($cp_choice_value);
		$sp_choice_value = mysql_real_escape_string($sp_choice_value);
		$ep_note = mysql_real_escape_string($ep_note);
		$cp_note = mysql_real_escape_string($cp_note);
		$sp_note = mysql_real_escape_string($sp_note);
	}
	$sql_rank = mysql_query('SELECT `rank` FROM `options` ORDER BY `rank` DESC LIMIT 0, 1');
	$row_rank = mysql_fetch_assoc($sql_rank);
	$insert_rank = $row_rank['rank']+1;
	
	mysql_query("INSERT INTO `options` (option_type, ep_option_name, cp_option_name, sp_option_name, ep_choice_value, cp_choice_value, sp_choice_value, ep_note, cp_note, sp_note, last_update, rank) VALUES ('$option_type', '$ep_option_name', '$cp_option_name', '$sp_option_name', '$ep_choice_value', '$cp_choice_value', '$sp_choice_value', '$ep_note', '$cp_note', '$sp_note', NOW(), '$insert_rank')", $connePaper) or die(mysql_error());
	header("Location: close_iframe.html");
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "fHtmlEditor")){
	$option_id = isNumber(trim($_POST['option_id']));
	$option_type = isNumber(trim($_POST['option_type']));
	$ep_option_name = txt_only($_POST['ep_option_name']);
	$cp_option_name = txt_only($_POST['cp_option_name']);
	$sp_option_name = txt_only($_POST['sp_option_name']);
	$ep_choice_value = htmlPurify($_POST['ep_choice_value']);
	$cp_choice_value = htmlPurify($_POST['cp_choice_value']);
	$sp_choice_value = htmlPurify($_POST['sp_choice_value']);
	$ep_note = htmlPurify($_POST['ep_note']);
	$cp_note = htmlPurify($_POST['cp_note']);
	$sp_note = htmlPurify($_POST['sp_note']);
	
	if(!get_magic_quotes_gpc()){ 
		$ep_option_name = mysql_real_escape_string($ep_option_name);
		$cp_option_name = mysql_real_escape_string($cp_option_name);
		$sp_option_name = mysql_real_escape_string($sp_option_name);
		$ep_choice_value = mysql_real_escape_string($ep_choice_value);
		$cp_choice_value = mysql_real_escape_string($cp_choice_value);
		$sp_choice_value = mysql_real_escape_string($sp_choice_value);
		$ep_note = mysql_real_escape_string($ep_note);
		$cp_note = mysql_real_escape_string($cp_note);
		$sp_note = mysql_real_escape_string($sp_note);
	}

	mysql_query("UPDATE `options` SET `option_type` = '$option_type', `ep_option_name` = '$ep_option_name', `cp_option_name` = '$cp_option_name', `sp_option_name` = '$sp_option_name', `ep_choice_value` = '$ep_choice_value', `cp_choice_value` = '$cp_choice_value', `sp_choice_value` = '$sp_choice_value', `ep_note` = '$ep_note', `cp_note` = '$cp_note', `sp_note` = '$sp_note', `last_update` = NOW() WHERE `option_id` = '$option_id'", $connePaper) or die(mysql_error());
	header("Location: close_iframe.html");
}

if ($action == 'edit'){
	$sql = mysql_query("SELECT * FROM `options` WHERE `option_id` = ".$id);
	$row = mysql_fetch_assoc($sql);
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Artech Creative</title>

<link href="css/css_admin.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-min.js"></script>
<!--[if IE]>
    <script src="../js/html5.js"></script>
<![endif]--> 
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>

<script type="text/javascript">
$(document).ready(function(){
  $('.lang_content_table').css('display', 'none');
  $('#<?php echo $lang_table;?>').css('display', 'block');
  $('#<?php echo $lang_btn;?>').addClass('selected');
	
	$('#btn_lang_switch_content a').click(function(){
		$('#btn_lang_switch_content a').removeClass('selected');
		$(this).addClass('selected');
	})
})

function switch_lang(id){
	$('.lang_content_table:not(#'+id+'_table)').css('display', 'none');
	$('#'+id+'_table').css('display', 'block');
}
function OnFormSubmit(){
	var must_subject = $.trim($('#<?php echo $must_subject;?>').val());
	var option_type = parseInt($('#option_type').val());
	var choice_value = $.trim($('#<?php echo $choice;?>').val());
	var error_text = '';
	var error = false;
	/****/
	if(option_type > 2 && choice_value == ''){
		error = true;
		error_text = '必須輸入選項<br>';
	}
	
	if (!must_subject){
		error = true;
		error_text +='必須輸入標題<br>';
		
		
		$('.lang_content_table').css('display', 'none')
		$('#<?php echo $lang_table;?>').css('display', 'block')
		$('#btn_lang_switch_content a').removeClass('selected');
		$('#<?php echo $lang_btn;?>').addClass('selected');
		$('#<?php echo $must_subject;?>').focus();
	} 
	if (error == true){
		$('.er_msg').html(error_text);
		$('.er_msg').css('display', 'block');
		return
	} else {
	  if(confirm("This Document is about to be submitted\nAre you sure you have finished editing?")){
		document.fHtmlEditor.submit();
	  }
	}
}
</script>
</head>
<body  style="background:#fff">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="padding:20px;" >
  <tr>
    <td><h2><?php if ($action=='add'){ echo '新增屬性';} else { echo '修改屬性';}?></h2></td>
  </tr>
  <tr>
    <td><hr></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellpadding="4" cellspacing="0" >
      <tr>
        <td>
          <form name="fHtmlEditor" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table width="100%"  border="0" cellpadding="2" cellspacing="1" class="content">
              <tr>
                <td width="120">屬性輸入類別:</td>
                <td>
                  
                  <select name="option_type" id="option_type" style="width:200px">
                   	<option value="1" <?php if ($row['option_type']==1){echo 'selected';}?>>文字輸入</option>
                    <option value="2" <?php if ($row['option_type']==2){echo 'selected';}?>>數字輸入</option>
                    <option value="3" <?php if ($row['option_type']==3){echo 'selected';}?>>下拉選單</option>
                    <option value="4" <?php if ($row['option_type']==4){echo 'selected';}?>>多項選擇</option>
                  </select>
                  </td>
              </tr>
              <tr>
                <td width="120" valign="top">選擇修改語言:</td>
                <td>
                  <div id="btn_lang_switch_content"><a href="javascript:switch_lang('en')" id="btn_lang_switch_en">ENG</a> | <a href="javascript:switch_lang('tc')" id="btn_lang_switch_tc">繁</a> | <!--a href="javascript:switch_lang('sc')" id="btn_lang_switch_sc">簡</a--></div>
<span class="er_msg">必須輸入標題</span></td>
              </tr>
              <tr>
                <td valign="top" bgcolor="#CCCCCC" height="1"></td>
                <td bgcolor="#CCCCCC" height="1"></td>
              </tr>
              </table>
            <table width="100%" border="0" id="tc_table" class="lang_content_table">
              <tr>
                <td><table width="100%" border="0">
                  <tr class="content">
                    <td width="120">屬性名稱：<br>
                    (建議最多12字)
                    </td>
                    <td><input name="cp_option_name" type="text" id="cp_option_name" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['cp_option_name'];}?>"> </td>
                    </tr>
                  <tr class="content">
                    <td>屬性選項：<br>
                    (下拉選單 / 多項選擇使用)
					</td>
                    <td><input name="cp_choice_value" type="text" id="cp_choice_value" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['cp_choice_value'];}?>"> (請用 "," 分隔. e.g. 選項1,選項2 <b style="color:red">不能有其他符號</b>)</td>
                  </tr>
                  <tr class="content">
                    <td>屬性註備：</td>
                    <td><input name="cp_note" type="text" id="cp_note" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['cp_note'];}?>"></td>
                  </tr>
                  </table></td>
              </tr>
            </table>
            <table width="100%" border="0" id="sc_table" class="lang_content_table">
              <tr>
                <td><table width="100%" border="0">
                  <tr class="content">
                    <td width="120">属性名称：<br>(建议最多12字)</td>
                    <td><input name="sp_option_name" type="text" id="sp_option_name" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['sp_option_name'];}?>"></td>
                    </tr>
                  <tr class="content">
                    <td>属性选项：<br>
                    (下拉选单 / 多项选择使用)</td>
                    <td><input name="sp_choice_value" type="text" id="sp_choice_value" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['cp_choice_value'];}?>"> (请用 "," 分隔. e.g. 选项1,选项2 <b style="color:red">不能有其他符号</b>)</td>
                  </tr>
                  <tr class="content">
                    <td>属性注备：</td>
                    <td><input name="sp_note" type="text" id="sp_note" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['sp_note'];}?>"></td>
                  </tr>
                  </table></td>
              </tr>
            </table>
            
            
            <table width="100%" border="0" id="en_table" class="lang_content_table">
              <tr>
                <td><table width="100%" border="0">
                  <tr class="content">
                    <td width="120">Feature name:<br>(Suggesting <br>Max. 30 words)</td>
                    <td><input name="ep_option_name" type="text" id="ep_option_name" size="50"  value="<? if ($action=='add'){ echo '';} else {echo $row['ep_option_name'];}?>">
                    </td>
                    </tr>
                  <tr class="content">
                    <td valign="top">Feature options:</td>
                    <td><input name="ep_choice_value" type="text" id="ep_choice_value" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['ep_choice_value'];}?>"><br>
(Please seperate the options by ",". e.g. option1,opton2 <b style="color:red">No other symbols or special characters</b>)</td>
                  </tr>
                  <tr class="content">
                    <td>Note:</td>
                    <td><input name="ep_note" type="text" id="ep_note" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['ep_note'];}?>"></td>
                  </tr>
                  </table></td>
              </tr>
            </table>
           
          <table width="100%" border="0" cellpadding="0" cellspacing="0" class="content">
            <tr>
              <td height="15"></td>
            </tr>
          </table>
          	<?php if ($action =='edit'){?><input type="hidden" name="option_id" value="<?php echo $row['option_id'];?>"><?php }?>
           <input type="hidden" name="<? if ($action =='add'){ echo 'MM_insert';} else { echo 'MM_update';}?>" value="fHtmlEditor">
            <input type="button" name="Submit"  onClick="OnFormSubmit()" id="btn_submit">
          </form>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
<?php include 'library/bottom.php'; ?>
</body>
</html>
