<?php
$SID = $cst->get("SID");
$mode = $cst->get("mode");
$lo = $cst->get("locale_obj");
$item_id = $cst->get("item_id");
$data = $cst->get("data");
$pfile = $cst->get("pfile");
//速查用
$mask_q_PRNAME  = array("PRINTFORM.PRINTPAPERNO","PRINTFORM.PRINTFORMNAME");
$mask_r_PRNAME  = array("PRNAME_","PRCNAME_");
$mask_q_MPRNAME = array("PRINTFORM.PRINTPAPERNO","PRINTFORM.PRINTFORMNAME");
$mask_r_MPRNAME = array("MPRNAME_","MPRCNAME_");
//下拉選單用
$FORMAT_v_select = array("","PDF","HTML");
$FORMAT_o_select = array("","PDF","HTML");
?>

&nbsp;&nbsp;
<input type="button" id="btn_prev" name="prev" value="<?= $lo->text("PREV_STEP") ?>">
<input type="button" id="btn_next" name="next" value="<?= $lo->text("NEXT_STEP") ?>">

<br></br>

&nbsp;&nbsp;
<input type="button" id="btn_save" name="save" value="<?= $lo->text("SAVE") ?>">
&nbsp;&nbsp;
<input type="button" id="btn_xml_out" name="btn_xml_out" value="<?= $lo->text("XML_OUT") ?>">
<input type="button" id="btn_xml_in" name="btn_xml_in" value="<?= $lo->text("XML_IN") ?>">
<div id="setup_detail" style="overflow: auto; ">
<!--<input type="button" id="btn_d_new" value="<?= $lo->text("ADD_ONE") ?>">-->
<form id="frm_menu">
<table id="WEBRPTCTRL" border="0" cellspacing="1" cellpadding="2" style="border: 1px dashed #777777; ">
	<tr style="background-color: #0066FF; white-space: nowrap; ">
		<th style="color: #FFFFFF; text-align: center;" colspan="8">報表預設設定</th>
	</tr>

	<tr style="background-color: #0066FF; white-space: nowrap; ">
		<!--<th style="text-align: center; color: #FFFFFF; width: 22px;">&nbsp;</th>-->
		<th style="color: #FFFFFF; text-align: center;">系統別</th>
		<th style="color: #FFFFFF; text-align: center;">選單名稱</th>
		<th style="color: #FFFFFF; text-align: center;">報表項名稱</th>
		<th style="color: #FFFFFF; text-align: center;">報表項中文名稱</th>
		<th style="color: #FFFFFF; text-align: center;">報表檔名</th>
		<th style="color: #FFFFFF; text-align: center;">報表格式</th>
	</tr>
<?php
if (!empty($data)) {
	for ($i = 0; $i < count($data); $i++) {
?>
	<tr style="background-color: #FFCC66; white-space: nowrap; ">
		<!--刪除圖示
		<td style="text-align: center;"><a href="#" onClick="can_delete_detail($(this),'<?= $item_id ?>')"><img src="<?= PATH_IMAGES ?>delete-16x16.png" border="0"></a>
		-->
		<!-- 順序 -->
			<input type="hidden" id="ERI_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_ERI[]" value="<?= $data[$i]["ERI"] ?>">
			<input type="hidden" id="SERNO_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_SERNO[]" value="<?= $i ?>">
		</td>
		<td>
		<!-- 系統別 -->
			<?= $data[$i]["CSYS"] ?>
			<input type="hidden" id="CSYS_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_CSYS[]" value="<?= $data[$i]["CSYS"] ?>">
		</td>
		<td>
		<!-- 選單名稱 -->
			<?= $data[$i]["PRCSYS"] ?>
			<input type="hidden" id="PRCSYS_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_PRCSYS[]" value="<?= $data[$i]["PRCSYS"] ?>">
		</td>
		<td>
		<!-- 報表項名稱 -->
			<?= $data[$i]["PRNAME"] ?>
			<input type="hidden" id="PRNAME_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_PRNAME[]" value="<?= $data[$i]["PRNAME"] ?>">
		</td>
		<td>
			<?= $data[$i]["PRCNAME"] ?>
			<input type="hidden" id="PRCNAME_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_PRCNAME[]" value="<?= $data[$i]["PRCNAME"] ?>">
		</td>
		<!-- 報表檔名 -->
		<td>
			<select id="RPTFILE_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_RPTFILE[]" onchange="load_qtydisc_RPTFILE('<?= $data[$i]["ERI"] ?>'); ">
			 <option value="" <?= (($data[$i]["RPTFILE"]) == "" || ($data[$i]["RPTFILE"]) == " " )? " selected" : "" ?>></option>
<?php
			for ($k = 0 ; $k < count($pfile) ; $k++ ) {
				if(preg_match("/".$data[$i]["PRNAME"]."/i", $pfile[$k])){
?>
					<option value="<?= $pfile[$k] ?>" <?= (isset($data[$i]["RPTFILE"]) && $data[$i]["RPTFILE"] == $pfile[$k] )? " selected" : "" ?>><?= $pfile[$k] ?></option>
<?php
				}
			}
?>			</select>
		</td>
		<!-- 報表格式 -->
		<td>
			<select id="FORMAT_<?= $data[$i]["ERI"] ?>" name="<?= $item_id ?>_FORMAT[]">
				<option value="" <?= (($data[$i]["FORMAT"]) == "" || ($data[$i]["FORMAT"]) == " " )? " selected" : "" ?>></option>
				<option value="PDF" <?= (isset($data[$i]["FORMAT"]) && $data[$i]["FORMAT"] == "PDF" )? " selected" : "" ?>>PDF</option>
				<option value="HTML" <?= (isset($data[$i]["FORMAT"]) && $data[$i]["FORMAT"] == "HTML" )? " selected" : "" ?>>HTML</option>
			</select>
		</td>
	</tr>
<?php
	}
}
?>
</table>
</form>
</div>

<script>
/*if ($('#content').height() <= 550){
	$('#page_body').height(585);
}else{
	$('#page_body').height($('#content').height() + 35);
}*/
if ($('.ui-mobile-viewport').height() > 300) {
	$('#setup_detail').height($('.ui-mobile-viewport').height()- 120);
	$('#page_body').css('height', '100%');
}

$('#btn_save').click(function() {
	//var param = $('#frm_menu').serialize();
	var url = 'manage/<?= $mode ?>/index.php?sid=<?= $SID ?>&mode=<?= $mode ?>&act=save';
	var djson = (from_output_json('#frm_menu')).replace(/'/g, "''");
	var verlen = djson.length;
	url += '&verlen='+verlen.toString();
	//alert(param);
	if (confirm('<?= $lo->text("CONFIRM_SUBMIT") ?>?')) {
		$.post(url, 'htmljson='+EncodeURLutf8(djson), function(msg) {
			var ret_msg = JSON.parse(msg);
			if (ret_msg[0].result == 'false') {
				alert(ret_msg[0].message);
				return false;
			} else {
				alert('<?= $lo->text("DATA_SAVED") ?>!');
			}
		});
	}
});

//--XML匯出------------------
$('#btn_xml_out').click(function() {
	var param = $('#frm_menu').serialize();
	var url = 'manage/<?= $mode ?>/index.php?sid=<?= $SID ?>&mode=<?= $mode ?>&act=xml_out';
	
	//alert(param);
	if (confirm('<?= $lo->text("CONFIRM_SUBMIT") ?>?')) {
		$.post(url, param, function(msg) {
		//console.log(msg);
			var ret_msg = JSON.parse(msg);
			if (ret_msg[0].result == 'false') {
				alert(ret_msg[0].message);
				return false;
			} else {
				alert('<?= $lo->text("XML_OUT") ?>!');
			}
		});
	}
});

//--XML匯入------------------
$('#btn_xml_in').click(function() {
	var param = $('#frm_menu').serialize();
	var url = 'manage/<?= $mode ?>/index.php?sid=<?= $SID ?>&mode=<?= $mode ?>&act=xml_in';
	
	//alert(param);
	if (confirm('<?= $lo->text("CONFIRM_SUBMIT") ?>?')) {
		$.post(url, param, function(msg) {
			var ret_msg = JSON.parse(msg);
			if (ret_msg[0].result == 'false') {
				alert(ret_msg[0].message);
				return false;
			} else {
				alert('<?= $lo->text("XML_IN") ?>!');
				load_content('content','manage/<?= $mode ?>/index.php?sid=<?= $SID ?>&mode=<?= $mode ?>');
			}
		});
	}
});

$('#btn_next').click(function() {
	load_content('content','manage/RWCALENDAR/index.php?sid=<?= $SID ?>&mode=RWCALENDAR');
});

$('#btn_prev').click(function() {
	load_content('content','manage/RWRPTTREE/index.php?sid=<?= $SID ?>&mode=RWRPTTREE');
});
</script>