<?php
if (!defined('IN_CONTEXT')) die('access violation error!');
?>
<link rel="stylesheet" href="../script/jquery.cluetip.css" type="text/css" />
<script type="text/javascript" src="../script/jquery.cluetip.min.js"></script>
<script type="text/javascript" language="javascript">
<!--
$(document).ready(function(){
	$('#answer').cluetip({splitTitle: '|',width: '300px',height:'68px'});
});
function on_del_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("adminprdlst_stat");
    if (o_result.result == "ERROR") {
        stat.innerHTML = o_result.errmsg;
        return false;
    } else if (o_result.result == "OK") {
	    stat.innerHTML = "<?php _e('OK, refreshing...'); ?>";
	    reloadPage();
    } else {
        return on_failure(response);
    }
}

function on_del_failure(response) {
    document.getElementById("adminprdlst_stat").innerHTML = "<?php _e('Request failed!'); ?>";
    return false;
}

function delete_product(p_id) {
	if (confirm("<?php _e('Delete the selected product?'); ?>")) {
	    var stat = document.getElementById("adminprdlst_stat");
	    stat.style.display = "block";
	    stat.innerHTML = "<?php _e('Deleting selected product...'); ?>";
		_ajax_request("mod_product", 
			"admin_delete", 
	        {
	            p_id:p_id
	        }, 
			on_del_success, 
			on_del_failure);
	}
}
function delete_products(){
	var arr = document.getElementsByName("product");
	var str="";
	for (var i = 0; i < arr.length; i++){
		var e = arr[i];
		if (e.checked){
			str = e.value + "_" + str;
		}
	}
	if(str.length < 1) {
		alert("<?php _e('Please select items to be deleted!'); ?>");
	} else {
		delete_product(str);
	}
}
function ck_select(){
	try{
		var el=document.getElementById('ckselect');
		var arr = document.getElementsByName("product");
		if(el.checked){
			for(i=0;i<arr.length;i++){arr[i].checked=true;}
		}else{
			for(i=0;i<arr.length;i++){arr[i].checked=false;}
		}
	}catch(e){
		return false;
	}
}
//-->
</script>
<div class="status_bar">
	<span id="adminprdlst_stat" class="status" style="display:none;"></span>
</div>
<ul style="margin-left:1px;">
	<li><a class="iconfl" href="index.php?<?php echo Html::xuriquery('mod_category_p', 'admin_list'); ?>" title=""><?php _e('Manage Categories'); ?></a></li>
	<li><a class="icontj" href="index.php?<?php echo Html::xuriquery('mod_product', 'admin_add'); ?>" title=""><?php _e('Add Product');?></a></li>
	<li><a class="iconsc" href="javascript:void(0)" onclick="delete_products();"><?php _e('Delete Selected'); ?></a></li>
	<li style="margin-top:14px;"><?php include_once(dirname(__FILE__).'/_category_switch.php'); ?></li>
	<li style="margin-top:14px;"><?php include_once(P_TPL.'/common/language_switch.php'); if(SessionHolder::get('_LOCALE', DEFAULT_LOCALE) != 'en'){ ?></li><li style="margin-top:0;_margin-top:16px;">
	<img id="answer" class="title" src="<?php echo P_TPL_WEB; ?>/images/answer1.gif" alt="help" title="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;此处语言切换后可以使您在中文后台即时编辑、修改英文版网站内容，而不需切换到英文后台，方便快捷！"/>
	<?php } ?></li>
<?php
$product_order_form = new Form('index.php', 'productorderform', 'check_login_info');
$product_order_form->p_open('mod_product', $next_action, '_ajax');
?>
	<li><input style="_margin-top:14px;" type="submit" value="<?php _e('Save Order');?>" id="submit" name="submit"/></li>
</ul>

<table class="list_table" id="admin_product_list" width="100%" border="0" cellspacing="1" cellpadding="2" style="line-height:24px;">
	<thead>
		<tr>
		    <td width="10%"><?php echo Html::input('checkbox', 'ckselect','','onclick=ck_select()'); ?></td>
            <td><?php _e('Name'); ?></td>
            <td><?php _e('Category');?></td>
			<td><?php _e('Order');?></td>
            <td><?php _e('Status'); ?></td>
            <td><?php _e('Create Time'); ?></td>
            <td><?php _e('Edit'); ?></td>
            <td><?php _e('Delete'); ?></td>
        </tr>
    </thead>
    <tbody>
    <?php
    if (sizeof($products) > 0) {
        $row_idx = 0;
        foreach ($products as $product) {
        	$product->loadRelatedObjects(REL_PARENT, array('ProductCategory'));
    ?>
        <tr class="row_style_<?php echo $row_idx; ?>" valign="middle">
        	<td><?php echo Html::input('checkbox', 'product', $product->id); ?></td>
        	<td><?php echo $product->name; ?></td>
        	<td><?php echo $product->masters['ProductCategory']->name; ?></td>
			<td><?php echo Html::input('text', 'i_order['.$product->id.']', $product->i_order, 'size=1'); ?></td>
        	<td><?php //echo Product::explain_publish($product);
    				echo Toolkit::validateYesOrNo($product->published,$product->id,"index.php?_m=mod_product&_a=admin_pic&_r=ajax&_id=".$product->id); 
    			?></td>
        	<td><?php echo date('y-n-j g:i', $product->create_time); ?></td>
        	<td>
        		<a href="index.php?<?php echo Html::xuriquery('mod_product', 'admin_edit', array('p_id' => $product->id)); ?>" title="<?php _e('Edit'); ?>"><img style="border:none;" alt="<?php _e('Edit');?>" src="<?php echo P_TPL_WEB; ?>/images/edit.gif"/></a>
        	</td>
        	<td>
        		<a href="#" onclick="delete_product(<?php echo $product->id; ?>);return false;" title="<?php _e('Delete'); ?>"><img style="border:none;" alt="<?php _e('Delete');?>" src="<?php echo P_TPL_WEB; ?>/images/cross.gif"/></a>
        	</td>
        </tr>
    <?php
            $row_idx = 1 - $row_idx;
        }
    } else {
    ?>
    	<tr class="row_style_0">
    		<td colspan="8"><?php _e('No Records!'); ?></td>
    	</tr>
    <?php
    }
    ?>
    </tbody>
</table>
<?php
$product_order_form->close();
$running_msg = __('Saving product order...');
$custom_js = <<<JS
$("#adminprdlst_stat").css({"display":"block"});
$("#adminprdlst_stat").html("$running_msg");
_ajax_submit(thisForm, on_success, on_failure);
return false;

JS;
$product_order_form->addCustValidationJs($custom_js);
$product_order_form->writeValidateJs();
?>
<?php
include_once(P_TPL.'/common/pager.php');
?>
