<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('修改用户')" />
	<th:block th:include="include :: select2-css" />
</head>
<body>
    <div class="main-content">
        <form class="form-horizontal" id="form-user-edit" th:object="${user}">
            <input name="userId"  type="hidden"  th:field="*{userId}" />
			<input name="deptId"  type="hidden"  th:field="*{deptId}" id="treeId"/>
            <h4 class="form-header h4">基本信息</h4>
            <div class="row">
            	<div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>用户名称：</label>
                        <div class="col-sm-8">
                            <input name="userName" placeholder="请输入用户名称" class="form-control" type="text" maxlength="30" th:field="*{userName}" required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>归属部门：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input class="form-control" type="text" name="deptName" onclick="selectDeptTree()" id="treeName" th:field="*{dept.deptName}" required>
                            	<span class="input-group-addon"><i class="fa fa-search"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>手机号码：</label>
                        <div class="col-sm-8">
                            <input name="phonenumber" placeholder="请输入手机号码" class="form-control" type="text" maxlength="11" th:field="*{phonenumber}" required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>邮箱：</label>
                        <div class="col-sm-8">
                            <input name="email" class="form-control email" type="text" maxlength="50" placeholder="请输入邮箱" th:field="*{email}" required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>登录账号：</label>
                        <div class="col-sm-8">
                            <input class="form-control" type="text" readonly="true" th:field="*{loginName}"/>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">用户状态：</label>
                        <div class="col-sm-8">
                            <label class="toggle-switch switch-solid">
	                            <input type="checkbox" id="status" th:checked="${user.status == '0' ? true : false}">
	                            <span></span>
	                        </label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">岗位：</label>
                        <div class="col-sm-8">
                            <select id="post" class="form-control select2-multiple" multiple>
								<option th:each="post:${posts}" th:value="${post.postId}" th:text="${post.postName}" th:selected="${post.flag}" th:disabled="${post.status == '1'}"></option>
							</select>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">用户性别：</label>
                        <div class="col-sm-8">
                            <select name="sex" class="form-control m-b" th:with="type=${@dict.getType('sys_user_sex')}">
				                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{sex}"></option>
				            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
            	<div class="col-sm-12">
                    <div class="form-group">
                        <label class="col-xs-2 control-label">角色：</label>
                        <div class="col-xs-10">
                            <label th:each="role:${roles}" class="check-box">
								<input name="role" type="checkbox" th:value="${role.roleId}" th:text="${role.roleName}" th:checked="${role.flag}" th:disabled="${role.status == '1'}">
							</label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">是否同步：</label>
                        <div class="col-sm-8">
                            <label class="toggle-switch switch-solid">
                                <input type="checkbox" id="isSync" th:checked="${user.isSync == '0' ? true : false}">
                                <span></span>
                            </label>
                        </div>
                    </div>
                </div>
            </div>
            <h4 class="form-header h4" style="line-height: 2.1;">扩展信息
                <div style="float: right" shiro:hasPermission="system.user.editExt">
                    <button type="button" class="btn btn-sm btn-primary" onclick="seletDiyExt()"><i class="fa fa-check"></i>设置为自定义</button>&nbsp;
                    <button type="button" class="btn btn-sm btn-primary" onclick="selectExt()"><i class="fa fa-check"></i>选择扩展信息</button>&nbsp;
                </div>
            </h4>

            <input  type="hidden" readonly="true" th:field="*{sysUserExt.isChangeExt}" />
            <!--<input  type="hidden" readonly="true" th:field="*{sysUserExt.sapEmployeeId}" />-->
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">用户类别：</label>
                        <input type="hidden" th:field="*{sysUserExt.salesLevel}" onchange="changeLevelText">
                        <input type="hidden" th:field="*{sysUserExt.identity}" >
                        <div class="col-sm-8">
                            <input class="form-control" type="text" readonly="true" id="salesText" />
                        </div>
                    </div>
                </div>
                <div class="col-sm-6" id="sapEmployeeId">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">员工编号：</label>
                        <div class="col-sm-8">
                            <input class="form-control" type="text"  th:field="*{sysUserExt.sapEmployeeId}" value="sysUserExt.sapEmployeeId" name="sapEmployeeId"/>
                        </div>
                    </div>
                </div>
            </div>
            <div id="diyDiv">
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">岗位名称：</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.postName}" />
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">组织Code：</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.orgCode}" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div id="salesDiv">
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">岗位Code：</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.postCode}"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">组织名称：</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.orgName}" />
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">领导名称：</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.bossName}"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">领导id：</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.bossEmployeeId}"/>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
            <div class="row" id="customerDiv">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">经销商Code：</label>
                        <div class="col-sm-8">
                            <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.customerCode}" />
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <!--<label class="col-sm-4 control-label">是否是经理：</label>
                        <div class="col-sm-8">
                            <input class="form-control" type="text" readonly="true" th:field="*{sysUserExt.isCustomerManager}" />
                        </div>-->
                        <label class="col-sm-4 control-label">经销商职位：</label>
                        <div class="col-sm-8">
                            <input class="form-control" id="customerManagerValue" type="text" readonly="true" />
                            <!--<div th:if="${user.sysUserExt.isCustomerManager eq '0'}">
                                <input class="form-control" type="text" readonly="true" value="导购" />
                            </div>
                            <div th:if="${user.sysUserExt.isCustomerManager eq '1'}">
                                <input class="form-control" type="text" readonly="true" value="经理" />
                            </div>
                            <div th:if="${user.sysUserExt.isCustomerManager eq '2'}">
                                <input class="form-control" type="text" readonly="true" value="财务" />
                            </div>
                            <div th:if="${user.sysUserExt.isCustomerManager eq '3'}">
                                <input class="form-control" type="text" readonly="true" value="仓管" />
                            </div>-->
                        </div>
                        <!--<div class="col-sm-8" th:switch="${user.sysUserExt.isCustomerManager}">
                            <div th:case="0">
                                <input class="form-control" type="text" readonly="true" value="导购" />
                            </div>
                            <div th:case="1">
                                <input class="form-control" type="text" readonly="true" value="经理" />
                            </div>
                            <div th:case="2">
                                <input class="form-control" type="text" readonly="true" value="财务" />
                            </div>
                            <div th:case="3">
                                <input class="form-control" type="text" readonly="true" value="仓管" />
                            </div>
                        </div>-->
                    </div>
                </div>
            </div>
            <h4 class="form-header h4">其他信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <div class="form-group">
                        <label class="col-xs-2 control-label">备注：</label>
                        <div class="col-xs-10">
                            <textarea name="remark" maxlength="500" class="form-control" rows="3">[[*{remark}]]</textarea>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: select2-js" />
	<script th:inline="javascript">
        var prefix = ctx + "system/user";
        var salesLevelDict = [[${@dict.getType('sales_level')}]];//销售人员等级
        var post = [[${user.sysUserExt.isCustomerManager}]];
        var postValue = "";

        if (post.indexOf('1') != -1){
            postValue += "经销商负责人 ";
        }
        if(post.indexOf('2') != -1){
            postValue += " 财务 ";
        }
        if(post.indexOf('3') != -1){
            postValue += " 仓管 ";
        }
        if(post.indexOf('4') != -1){
            postValue += " 厨卫老板 ";
        }
        if(post.indexOf('5') != -1){
            postValue += " 厨卫经理 ";
        }
        if(post.indexOf('6') != -1){
            postValue += " 厨卫业务人员 ";
        }
        if (post.indexOf('0') != -1){
            postValue += " 导购 ";
        }
        if(post.indexOf('7') != -1){
            postValue += " 厨卫仓管 ";
        }
        $("#customerManagerValue").val(postValue);

        $("#form-user-edit").validate({
        	onkeyup: false,
        	rules:{
        		email:{
                    email:true,
                    remote: {
                        url: prefix + "/checkEmailUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                        	"userId": function() {
                                return $("#userId").val();
                            },
                			"email": function() {
                                return $.common.trim($("#email").val());
                            }
                        },
                        dataFilter: function (data, type) {
                        	return $.validate.unique(data);
                        }
                    }
        		},
        		phonenumber:{
        			isPhone:true,
                    remote: {
                        url: prefix + "/checkPhoneUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                        	"userId": function() {
                        		return $("#userId").val();
                            },
                			"phonenumber": function() {
                                return $.common.trim($("#phonenumber").val());
                            }
                        },
                        dataFilter: function (data, type) {
                        	return $.validate.unique(data);
                        }
                    }
        		},
        	},
        	messages: {
        		"email": {
                    remote: "Email已经存在"
                },
        		"phonenumber":{
                	remote: "手机号码已经存在"
        		}
            },
            focusCleanup: true
        });
        changeLevel();
        changeLevelText();
        function submitHandler() {
	        if ($.validate.form()) {
	        	var data = $("#form-user-edit").serializeArray();
	        	var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
	        	var isSync = $("input[id='isSync']").is(':checked') == true ? 0 : 1;
	        	var roleIds = $.form.selectCheckeds("role");
	        	var postIds = $.form.selectSelects("post");
	        	data.push({"name": "status", "value": status});
	        	data.push({"name": "isSync", "value": isSync});
	        	data.push({"name": "roleIds", "value": roleIds});
	        	data.push({"name": "postIds", "value": postIds});
	        	$.operate.saveTab(prefix + "/edit", data);
	        }
	    }

        /*用户管理-修改-选择部门树*/
        function selectDeptTree() {
        	var deptId = $.common.isEmpty($("#treeId").val()) ? "100" : $("#treeId").val();
            var url = ctx + "system/dept/selectDeptTree/" + deptId;
		    var options = {
				title: '选择部门',
				width: "380",
				url: url,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
    			var body = layer.getChildFrame('body', index);
    			$("#treeId").val(body.find('#treeId').val());
    			$("#treeName").val(body.find('#treeName').val());
    			layer.close(index);
			}
		}
		function changeLevel(){
            var level = $("input[name='sysUserExt.salesLevel']").val();
            if(level=="customer_level"){
                $("#diyDiv").show();
                $("#sapEmployeeId").show();
                $("#customerDiv").show();
                $("#salesDiv").hide();
                // $("input[name='sysUserExt.sapEmployeeId']").attr("readonly","readonly");
            }else if(level=="diy_level"){
                $("#diyDiv").hide();
                // $("#sapEmployeeId").hide();
                // $("input[name='sysUserExt.sapEmployeeId']").removeAttr("readonly");
                $("#customerDiv").hide();
                $("#salesDiv").hide();
            }else{
                $("#diyDiv").show();
                $("#sapEmployeeId").show();
                $("#customerDiv").hide();
                $("#salesDiv").show();
                // $("input[name='sysUserExt.sapEmployeeId']").attr("readonly","readonly");
            }


        }

        /*用户管理-修改-选择其他用户扩展信息*/
        function selectExt() {
            var url = ctx + "system/user/selectUser/";
            var options = {
                title: '选择部门',
                url: url,
                callBack: doExtSubmit
            };
            $.modal.openOptions(options);

        }

        function doExtSubmit(index, layero){
        var body = layer.getChildFrame('body', index);
            var userId = body.find('#selectUserId').val();
            if(userId==null||userId==""){
                alert("请选择一个用户！");
                return;
            }
            $.ajax({
                url: ctx + "/system/user/getExt/"+userId,
                type: "GET",
                success: function(result) {
                    if(result.code==0){
                        $("input[name='sysUserExt.salesLevel']").val(result.data.salesLevel);

                        changeLevelText();
                        changeLevel();
                        $("input[name='sysUserExt.postName']").val(result.data.postName);
                        $("input[name='sysUserExt.orgCode']").val(result.data.orgCode);
                        $("input[name='sysUserExt.postCode']").val(result.data.postCode);
                        $("input[name='sysUserExt.orgName']").val(result.data.orgName);
                        $("input[name='sysUserExt.bossName']").val(result.data.bossName);
                        $("input[name='sysUserExt.bossEmployeeId']").val(result.data.bossEmployeeId);
                        $("input[name='sysUserExt.customerCode']").val(result.data.customerCode);
                        $("input[name='sysUserExt.sapEmployeeId']").val(result.data.sapEmployeeId);
                        $("input[name='sysUserExt.isCustomerManager']").val(result.data.isCustomerManager);

                        $("input[name='sysUserExt.identity']").val(result.data.identity);

                        $("input[name='sysUserExt.isChangeExt']").val(1);

                    }else{
                        $.modal.alertWarning(result.msg);
                    }

                }
            })
                layer.close(index);
        }

        function changeLevelText(){
           var v = $("input[name='sysUserExt.salesLevel']").val();
            console.log(salesLevelDict);
            for(var i =0;i<salesLevelDict.length;i++){
                if(salesLevelDict[i].dictValue==v){

                    $("#salesText").val(salesLevelDict[i].dictLabel);
                    break;
                }

            }
        }
        function seletDiyExt(){
            $("input[name='sysUserExt.salesLevel']").val("diy_level");

            changeLevelText();
            changeLevel();
            $("input[name='sysUserExt.postName']").val("");
            $("input[name='sysUserExt.orgCode']").val("");
            $("input[name='sysUserExt.postCode']").val("");
            $("input[name='sysUserExt.orgName']").val("");
            $("input[name='sysUserExt.bossName']").val("");
            $("input[name='sysUserExt.bossEmployeeId']").val("");
            $("input[name='sysUserExt.customerCode']").val("");
            // $("input[name='sysUserExt.sapEmployeeId']").val("");
            // $("input[name='sysUserExt.sapEmployeeId']").removeAttr("readonly");
            $("input[name='sysUserExt.isCustomerManager']").val("");
            $("input[name='sysUserExt.isChangeExt']").val(1);

            $("input[name='sysUserExt.identity']").val(1);
        }

		$(function() {
            $('#post').select2({
                placeholder:"请选择岗位",
                allowClear: true
            });
        })
    </script>
</body>
</html>