<?php
//====================================
if($_GET['action']=='del'&&$_GET['id']>0)
{
	$sql="delete from ".CTEMP." where id='$_GET[id]'";
	$db->query($sql);
	msg('module.php?m=company&s=user_template.php');
}
else if(!empty($_POST['submit'])&&$_POST["submit"]==lang_show('delete')&&!empty($_POST['id']))
{	
	$id=implode(',',$_POST['id']);
	$sql="delete from ".CTEMP." where id IN($id)";
	$db->query($sql);
	msg('module.php?m=company&s=user_template.php');
}
else if(!empty($_POST['submit'])&&$_POST["submit"]=='addtpl'&&!empty($_POST['usergroup']))
{	
	$tm=time();
	$group_id=@implode(',',$_POST['usergroup']);
	$catid=@implode(',',$_POST['cat']);
	
	$sql="insert into ".CTEMP." (`tname`,`ccatid`,`point`,`group_id`,`temp_file`,`time`,`sort_order`,statu)
	 VALUES('$_POST[tname]','$catid','$_POST[point]','$group_id','$_POST[temp_file]','$tm','$_POST[sort]',1)";
	 $db->query($sql);
	msg('module.php?m=company&s=user_template.php');
}
else if($_POST["submit"]=='edit'&&!empty($_POST['usergroup']))
{
	$id=$_POST['editID'];
	$_GET['firstRow']=empty($_GET['firstRow'])?0:$_GET['firstRow'];
	if($id>0)
	{
		$tm=time();
		$group_id=@implode(',',$_POST['usergroup']);
		$catid=@implode(',',$_POST['cat']);
		
		$sql="update ".CTEMP." set 
		`tname`='$_POST[tname]',`ccatid`='$catid',`point`='$_POST[point]',`group_id`='$group_id',`temp_file`='$_POST[temp_file]',
		`sort_order`='$_POST[sort]',`statu`='$_POST[statu]' where id='$id'";
		$db->query($sql);
		msg('module.php?m=company&s=user_template.php&totalRows='.$_GET['totalRows'].'&firstRow='.$_GET['firstRow']);
	}
}
@include_once('config/module_company_config.php');
if($module_company_config['identity'])
	$identity=explode('|',$module_company_config['identity']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="main.css" rel="stylesheet" type="text/css" />
<title><?php echo lang_show('admin_system');?></title>
<script src="../script/my_lightbox.js" language="javascript"></script>
<script type="text/javascript" src="../script/jquery-1.4.4.min.js"></script>
</head>
<body>
<?php
if(empty($_GET['action'])&&$_GET['action']!="add")
{	
	$list=array();
	include($config['webroot']."/config/usergroup.php");
	
	$sql="select id,tname,ccatid,group_id,temp_file,point,sort_order,statu from ".CTEMP." where 1 order by sort_order asc";
	//================================
	include_once("../includes/page_utf_class.php");
	$page = new Page;
	$page->listRows=5;
	if (!$page->__get('totalRows')){
		$db->query($sql);
		$page->totalRows = $db->num_rows();
	}
	$sql .= "  limit ".$page->firstRow.",5";
	$pages = $page->prompt();
	//=================================
	$db->query($sql);
	$de=$db->getRows();
	
	foreach($de as $key=>$v)
	{	
		if($v['ccatid']!='')
		{
			$catid=explode(',',$v['ccatid']);
			foreach($catid as $c)
			{
				$catname[]=$identity[$c];
			}
			$v['catname']=implode(',',$catname);
			unset($catname);
		}
		
		$groud_id=explode(',',$v['group_id']);
		$group_name=array();
		foreach($groud_id as $id)
		{
			$group_name[]=$group[$id]['name'];
		}
		$v['usergroup']=implode(',',$group_name);
		$list[]=$v;
	}
?>
<script>
function do_select()
{	 
	 var box_l = document.getElementsByName("id[]").length;
	 for(var j = 0 ; j < box_l ; j++)
	 {
		if(document.getElementsByName("id[]")[j].checked==true)
		  document.getElementsByName("id[]")[j].checked = false;
		else
		  document.getElementsByName("id[]")[j].checked = true;
	 }
}
function check_sele()
{
	 var box_l = document.getElementsByName("id[]").length;
	 for(var j = 0 ; j < box_l ; j++)
	 {
		if(document.getElementsByName("id[]")[j].checked==true)
		return true;
	 }
	 alert("至少选择一项");
	 return false;
}
</script>
<div class="bigbox" <?php if($_GET['act']=='') echo 'style="display:block;"';else echo 'style="display:none;"';?> >
  <div class="bigboxhead tab" style=" width:90%"> <span class="cbox on"><a href="#"><?php echo lang_show('template_manager');?></a></span> <span class="cbox"><a href="module.php?m=company&s=user_template.php&action=add"><?php echo lang_show('add_template');?></a></span> </div>
  <div class="bigboxbody" style="margin-top:-1px;">
    <form name="user_tpl" method="post" action="" onsubmit="return check_sele()">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr class="theader">
          <td width="26" align="right"><input type="checkbox" class="checkbox" name="checkbox" onclick="do_select()" /></td>
          <td width="113" align="center"><?php echo lang_show('template');?></td>
          <td width="118"><?php echo lang_show('mail_name');?></td>
          <td width="108"><?php echo lang_show('application_member');?></td>
          <td width="144"><?php echo lang_show('ug');?></td>
          <td width="79"><?php echo lang_show('bank_g');?></td>
          <td width="90" align="center"><?php echo lang_show('use_credits');?></td>
          <td width="74" align="center"><?php echo lang_show('none');?></td>
          <td width="77" align="center"><?php echo lang_show('act');?></td>
        </tr>
        <?php
		if($list)
		{
			$statu=array(lang_show('payment_g'),lang_show('payment_f'));
			foreach($list as $t)
			{
		?>
        <tr>
          <td  align="center"><input type="checkbox" class="checkbox" name="id[]" value="<?php echo $t['id'];?>" /></td>
          <td align="center"><img style="border: 1px solid #A9BAD3; padding:4px; height:114px; width:97px;" src="../templates/<?php echo $t['temp_file'];?>/img/temp.jpg" /><br /></td>
          <td><?php echo $t['tname'];?></td>
          <td><?php echo $t['catname'];?></td>
          <td><?php echo $t['usergroup'];?></td>
          <td><?php echo $statu[$t['statu']];?></td>
          <td align="center"><?php echo $t['point'];?></td>
          <td align="center"><?php echo $t['sort_order'];?></td>
          <td align="center"><a href="module.php?m=company&s=user_template.php&action=modify&edit=<?php echo $t['id'];?>&firstRow=<?php echo $_GET['firstRow'];?>&totalRows=<?php echo $_GET['totalRows'];?>"><?php echo $editimg;?></a> <a href="module.php?m=company&s=user_template.php&action=del&id=<?php echo $t['id'];?>" onclick="return confirm('<?php echo lang_show('confirm_delete');?>')"><?php echo $delimg;?></a></td>
        </tr>
        <?php
			}
		}
		?>
        <tr>
          <td colspan="2"><input class="btn" type="submit" onclick="return confirm('<?php echo lang_show('edit_yes');?>')" name="submit" value="<?php echo lang_show('del');?>"></td>
          <td colspan="7"><div class="page">&nbsp;<?php echo $pages;?></div></td>
      </table>
    </form>
  </div>
</div>
<?php
}
?>
<?php
if(!empty($_GET['action'])&&$_GET["action"]=="modify"&&empty($_POST['submit']))
{
	//#===================================
	$templist=array();
	$handle = opendir($config['webroot']."/templates"); 
	while ($filename = readdir($handle))
	{ 
		if($filename!="."&&$filename!="..")
		{
		  if(is_dir($config['webroot']."/templates/".$filename)&&substr($filename,0,5)=="user_"&&$filename!='user_templates_default')
		  {
			$templist[]=$filename;
		  }
	   }
	}
	sort($templist);
	//==================================
	$sql="select * from ".CTEMP." where id='$_GET[edit]'";
	$db->query($sql);
	$de=$db->fetchRow();
?>
<form method="post" action="" onsubmit="return check_value()">
  <div class="bigbox">
    <div class="bigboxhead"><?php echo lang_show('template_editor');?></div>
    <div class="bigboxbody">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td>是否启用</td>
          <td><label>
              <input type="radio" class="radio" name="statu" checked value='0' >
              <?php echo lang_show('payment_g');?></label>
            <label>
              <input type="radio" class="radio" <?php if($de['statu']==1) echo "checked";?> value='1' name="statu">
              <?php echo lang_show('payment_f');?></label></td>
        </tr>
        <tr>
          <td>模板名称</td>
          <td><input class="text" type='text' name='tname' id='tname' value='<?php echo $de['tname'];?>' /></td>
        </tr>
        <?php if($module_company_config['identity']){?>
        <tr>
          <td width="10%" height="22">适用会员</td>
          <td width="90%"><?php
			foreach($identity as $key=>$v)
			{
			?>
            <input <?php if(in_array($key,explode(',',$de['ccatid']))) echo "checked='checked'"; ?> name="cat[]" type="checkbox" class="checkbox" value="<?php echo $key;?>" />
            <?php echo $v?>
            <?php
			}
			?></td>
        </tr>
        <?php } ?>
        <tr>
          <td><?php echo lang_show('ug');?></td>
          <td><?php
		include($config['webroot']."/config/usergroup.php");
		$group_id=explode(',',$de['group_id']);
		foreach($group as $v)
		{
	   ?>
            <input name="usergroup[]" type="checkbox" class="checkbox" <?php if(in_array($v['group_id'],$group_id)) echo "checked";?>  value="<?php echo $v['group_id']; ?>">
            <?php echo $v['name']; ?>&nbsp;&nbsp;
            <?php
		}
	   ?></td>
        </tr>
        <tr>
          <td>模板目录</td>
          <td><select class="select" name='temp_file'>
              <?php
				$sql="select temp_file as dir from ".CTEMP." where id!='$_GET[edit]'";
				$db->query($sql);
				$re=$db->getRows();
				foreach($re as $v)
				{
					$dirs[]=$v['dir'];
				}
				foreach($templist as $t)
				{	
					if(!in_array($t,$dirs))
					{
						echo "<option value=\"$t\" ";
						if($de['temp_file']==$t)
							echo "selected ";
						echo ">$t</option>";
					}
				}
			?>
            </select></td>
        </tr>
        <tr>
          <td>出售积分</td>
          <td><input class="text" type='text' name='point' id='point' value='<?php echo $de['point'];?>' /></td>
        </tr>
        <tr>
          <td>展示顺序</td>
          <td><input class="text" type='text' name='sort' id='sort' maxlength=4 value='<?php echo $de['sort_order'];?>' /></td>
        </tr>
        <tr>
          <td >&nbsp;</td>
          <td><input class="btn" type="submit"  value="<?php echo lang_show('confirm');?>"/>
            <input type="hidden" name="submit" value="edit"/>
            <input type="hidden" name="editID" value="<?php echo $de['id'];?>"/></td>
        </tr>
      </table>
    </div>
  </div>
</form>
<?php
}
?>
<?php
if(!empty($_GET['action'])&&$_GET["action"]=="add"&&empty($_POST['submit']))
{
	$templist=array();
	$handle = opendir($config['webroot']."/templates"); 
	while ($filename = readdir($handle))
	{ 
		if($filename!="."&&$filename!="..")
		{
		  if(is_dir($config['webroot']."/templates/".$filename)&&substr($filename,0,5)=="user_"&&$filename!='user_templates_default')
		  {
			$templist[]=$filename;
		  }
	   }
	}
	sort($templist);
?>
<form method="post" action="" onsubmit="return check_value()">
  <div class="bigbox" <?php if($_GET['action']=='add') echo 'style="display:block;"';else echo 'style="display:none;"';?> >
    <div class="bigboxhead tab" style=" width:90%"> <span class="cbox"><a href="module.php?m=company&s=user_template.php"><?php echo lang_show('template_manager');?></a></span> <span class="cbox on"><a href="#"><?php echo lang_show('add_template');?></a></span> </div>
    <div class="bigboxbody" style="margin-top:-1px;">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td><?php echo lang_show('tpl_name');?></td>
          <td><input class="text" type='text' name='tname' id='tname' value='' /></td>
        </tr>
        <?php if($module_company_config['identity']){?>
        <tr>
          <td width="10%" height="22">适用会员</td>
          <td width="90%"><?php
			$identity=explode('|',$module_company_config['identity']);
			foreach($identity as $key=>$v)
			{
			?>
            <input <?php if(in_array($key,explode(',',$de['ccatid']))) echo "checked='checked'"; ?> name="cat[]" type="checkbox" class="checkbox" value="<?php echo $key;?>" />
            <?php echo $v?>
            <?php
			}
			?></td>
        </tr>
        <?php } ?>
        <tr>
          <td><?php echo lang_show('ug');?></td>
          <td><?php
		include($config['webroot']."/config/usergroup.php");
		foreach($group as $v)
		{
	   ?>
            <input name="usergroup[]" type="checkbox" class="checkbox" value="<?php echo $v['group_id']; ?>">
            <?php echo $v['name']; ?>&nbsp;&nbsp;
            <?php
		}
	   ?></td>
        </tr>
        <tr>
          <td>模板目录</td>
          <td><select class="select" name='temp_file'>
              <?php
	   			$sql="select temp_file as dir from ".CTEMP;
				$db->query($sql);
				$re=$db->getRows();
				$dirs=array();
				foreach($re as $v)
				{
					$dirs[]=$v['dir'];
				}
				foreach($templist as $t)
				{	
					if(!in_array($t,$dirs))
						echo "<option value=\"$t\">$t</option>";
				}
			?>
            </select></td>
        </tr>
        <tr>
          <td>出售积分</td>
          <td><input class="text" type='text' name='point' id='point' value='' /></td>
        </tr>
        <tr>
          <td>展示顺序</td>
          <td><input class="text" type='text' name='sort' id='sort' maxlength=4 value='' /></td>
        </tr>
        <tr>
          <td >&nbsp;</td>
          <td><input class="btn" type="submit"  value="<?php echo lang_show('add');?>"/>
            <input type="hidden" name="submit" value="addtpl"/></td>
        </tr>
      </table>
    </div>
  </div>
</form>
<script>
function check_value()
{
	if(!$('#tname').val())
	{
		alert('模板名称不能为空');
		$('#tname').focus();
		return false;
	}
	var group=document.getElementsByName('usergroup[]');
	var chk=0;
	for(var i=0;i<group.length;i++)
	{
		if(group[i].checked==true)
		{
			chk++;
			break;
		}
	}
	if(chk<1)
	{
		alert('请选择会员组');
		return false;
	}
	var point = $('#point').val();
	if(point!=''&&!point.match(/^(:?(:?\d+.\d+)|(:?\d+))$/))
	{
		alert('积分填写错误');
		$('#point').focus();
		return false;
	} 
	var sort = $('#sort').val();
	if(sort!=''&&!point.match(/^(:?(:?\d+.\d+)|(:?\d+))$/))
	{
		alert('排序填写错误');
		$('#sort').focus();
		return false;
	} 
}
</script>
<?php
}
?>
</body>
</html>