<?php
include_once("htui_header.php");
include_once("../classes/images.php");  //生成缩略图
include_once("../common/watermark.php");//图片添加水印

if (!empty($_POST)){

	if (!check_permission($_SESSION["a_uid"],'userlist.php',2)){
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
		echo json_encode($response_arr);
        exit;
	}

    $scuser_id  = intval($_POST["scuser_id"]);
	$rolelistid = flc_request($_POST["rolelistid"]);
    $Money      = flc_request($_POST["Money"]);
	$AdminType  = intval($_POST["AdminType"]);
	$UserType   = intval($_POST["UserType"]);
	$isCheck    = intval($_POST["isCheck"]);
	$jfmsign    = intval($_POST["jfmsign"]);
	$endtime    = flc_request($_POST["endtime"]);
    
	//修改用户表与用户角色表
	try {
           
        $db->startTrans();

		$sql = "update {$tablepre}scuser set Money='".$Money."',AdminType='".$AdminType."',UserType='".$UserType."',
				isCheck='".$isCheck."',jfmsign='".$jfmsign."',endtime='".$endtime."' where ID='".$scuser_id."'";
		$db->exec($sql);

		$db->exec("delete from {$tablepre}scuser_role where scuser_id='".$scuser_id."'");

		$rolearr=explode(",",$rolelistid);
		foreach ($rolearr as $roleid){
            $db->exec("insert into {$tablepre}scuser_role (role_id,scuser_id) values('".$roleid."','".$scuser_id."')");
		}

		$db->commit();

		$response_arr = array("status" => 1,"message"  => '修改成功!');
		echo json_encode($response_arr);
		exit;

	} catch (Exception $e) {
		$db->rollback();
		$response_arr = array("status" => -1,"message"  => $e->getMessage());
		echo json_encode($response_arr);
		exit;
	}

}

$id = intval($_GET["id"]);
$sql = "select * from {$tablepre}scuser where ID='".$id."'";
$row = $db->get_row($sql);

$sql = "select r.*,sr.scuser_id from {$tablepre}role r 
        inner join {$tablepre}scuser_role sr on sr.role_id=r.role_id 
		where sr.scuser_id='".$id."'";
$role = $db->get_all($sql);

foreach($role as $v){
   $rolearr[]=$v['role_id'];
}

$rolestr = implode(',',$rolearr);
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>后台管理中心-飞天素材网</title>
<meta name="author" content="ftsucai.com">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/htstyle.css" rel="stylesheet">
<link href="../js/bootstrap-datepicker/bootstrap-datepicker3.min.css" rel="stylesheet">
<link href="../js/bootstrap-colorpicker/css/bootstrap-colorpicker.min.css" rel="stylesheet">
</head>
  
<body>

    <!--页面主要内容-->
    <main class="ftdms-layout-content">
      
      <div class="container-fluid" style="margin-bottom:90px;">
        
        <div class="row" style="margin-top:15px;">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-header"><h4>修改用户帐号信息</h4>
			     <ul class="card-actions">
                    <li> <span><a href="userlist.php">返回列表</a></span> </li>
                 </ul>
			  </div>
              <div class="card-body" style="padding:5px 20px;">
                
                <form class="form-horizontal" action="" method="post" id="form">

				  <div class="form-group">
                    <label class="col-xs-12" for="UserName">用户帐号</label>
                    <div class="col-xs-12">
                      <input class="form-control" type="text" id="UserName" name="UserName" value="<?php echo $row['UserName'];?>" disabled>
                    </div>
                  </div>

				  <div class="form-group">
                    <label class="col-xs-12" for="Money">当前积分</label>
                    <div class="col-xs-12">
                      <input class="form-control" type="text" id="Money" name="Money" value="<?php echo $row['Money'];?>">
                    </div>
                  </div>

				  <div class="form-group">
				    <label class="col-xs-12" for="cateid">所属角色</label>
				    <select class="form-control" id="sel" multiple="multiple" size="1">
					<?php
	                  $sql="select * from {$tablepre}role order by role_id asc"; 
	                  $role_arr = $db->get_all($sql);
					  foreach ($role_arr as $k => $v){
                    ?>
						<option value="<?php echo $v['role_id'];?>" <?php if (in_array($v['role_id'],$rolearr)){?>selected<?php }?>>
						  <?php echo $v['role_name'];?>
						</option>
					<?php
				      }		
				    ?>
					</select>
					<input type="hidden" name="roleid" id="roleid" value="<?php echo $rolestr;?>">
                  </div>

				  <div class="form-group">
                    <label class="col-xs-12" for="AdminType">是否为管理员</label>
					<div class="col-xs-12">
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="AdminType" value="0" <?php if ($row['AdminType']==0){?>checked<?php }?>><span>否</span>
						</label>
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="AdminType" value="1" <?php if ($row['AdminType']==1){?>checked<?php }?>><span>是</span>
						</label>
					</div>
                  </div>
                  
                  <div class="form-group">
                    <label class="col-xs-12" for="UserType">付费状态</label>
					<div class="col-xs-12">
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="UserType" value="0" <?php if ($row['UserType']==0){?>checked<?php }?>><span>未付费</span>
						</label>
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="UserType" value="1" <?php if ($row['UserType']==1){?>checked<?php }?>><span>已付费</span>
						</label>
					</div>
                  </div>
                  
                  <div class="form-group">
	                   <label class="col-xs-12" for="yxsj">有效时间</label>
	                   <div class="input-daterange input-group" style="padding:5px 16px;">
	                      <input class="form-control js-datepicker" data-date-format="yyyy-mm-dd" type="text" id="endtime" name="endtime" value="<?php echo $row['endtime'];?>">
	                   </div>
                  </div>

                  <div class="form-group">
                    <label class="col-xs-12" for="isCheck">审核状态</label>
					<div class="col-xs-12">
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="isCheck" value="1" <?php if ($row['isCheck']==1){?>checked<?php }?>><span>已审核</span>
						</label>
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="isCheck" value="0" <?php if ($row['isCheck']==0){?>checked<?php }?>><span>未审核</span>
						</label>
					</div>
                  </div>
				  
				  <div class="form-group">
                    <label class="col-xs-12" for="jfmsign">激活状态</label>
					<div class="col-xs-12">
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="jfmsign" value="1" <?php if ($row['jfmsign']==1){?>checked<?php }?>><span>已激活</span>
						</label>
						<label class="ftdms-radio radio-inline radio-primary">
						<input type="radio" name="jfmsign" value="0" <?php if ($row['jfmsign']==0){?>checked<?php }?>><span>未激活</span>
						</label>
					</div>
                  </div><br>
                  
                  <div class="form-group">
                    <div class="col-xs-12">
                       <input type="hidden" name="scuser_id" id="scuser_id" value="<?php echo $id;?>">
					  <button class="btn btn-primary" type="button" id="submit">确认修改</button>
					  <button type="button" class="btn btn-default" onclick="javascript:history.back(-1);return false;">返回</button>
                    </div>
                  </div><br><br>
                </form>
                
              </div>
            </div>
          </div>
          
        </div>
        
      </div>
      
    </main>
    <!--End 页面主要内容-->
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.js"></script>
<script src="../js/perfect-scrollbar.min.js"></script>
<script src="../js/main.min.js"></script>
<script src="../js/fSelect.js"></script>
<script src="../xheditor/xheditor-1.2.2.min.js"></script>
<script src="../xheditor/zh-cn.js"></script>
<script src="../js/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
<script src="../js/bootstrap-datepicker/bootstrap-datepicker.zh-CN.min.js"></script>
<script src="../js/bootstrap-colorpicker/bootstrap-colorpicker.js"></script>
<script src="../js/bootstrap-notify.min.js"></script>
<script src="../js/tips.js"></script>
<script>
$(function(){
	//载入多选分类值
	$('#sel').fSelect();
	
});
</script>
<script type="text/javascript">
$("#submit").click(function(){

    var formData  = new FormData();
	var rolelistid    = $("#sel").GetValues(",");

    var Money     = $("#Money").val();
    var scuser_id = $("#scuser_id").val();
	var AdminType = $("input[name='AdminType']:checked").val();
	var UserType  = $("input[name='UserType']:checked").val();
	var isCheck   = $("input[name='isCheck']:checked").val();
	var endtime   = $("#endtime").val();

	if (rolelistid==''){
		tips.notify('用户角色至少要选一个', 'warning', 200);
		return false;
	}

	formData.append("scuser_id", scuser_id);
	formData.append("rolelistid", rolelistid);
	formData.append("Money", Money);
	formData.append("AdminType", AdminType);
	formData.append("UserType", UserType);
	formData.append("isCheck", isCheck);
	formData.append("endtime", endtime);

    $.ajax({
        url: "edit_user.php",
		type: "post",
		data: formData,
		processData: false,
		contentType: false,
		dataType: 'text',
		//执行中
		beforeSend:function() {
			tips.loading('show');
		},
		success: function(data) {

			tips.loading('hide');
			var rs  = eval('('+data+')');
			var msg = rs.message;

			if(rs.status==1){
			   tips.notify(msg, 'success', 500, 'ftsucai-name', 'top', 'center');
			   window.location.href='userlist.php';
			}else{
			   tips.notify(msg, 'danger', 200);
			}
		},
		error: function(data) {
			tips.notify('服务器异常,请稍后再试~', 'danger', 200);
		}
	});
	
});
</script>
</body>
</html>