<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>调整职级管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
        var validateForm;
        var count = 0;
        $(document).ready(function() {
            checkContentKeyUp('changeFor',255);
            if($("#changeDate").val()==null || $("#changeDate").val()==''){
                $("#changeDate").val(getNowFormatDate());
            }
            validateForm = $("#inputForm").validate({
                submitHandler: function(form){
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                rules:{
                },
                messages:{
                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });

         /*   laydate.render({
                elem: '#changeDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
                format: 'yyyy-MM-dd',
                type : 'date'
            });*/


        });

        function doSubmit(obj){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
            var idArr = $("#workChangeJobUserList tr:visible");
            if(idArr.length<1){
                parent.layer.msg("请选择调整成员!", {icon: 5});
                return false;
            }
            if(obj == 2){
                $("#inputForm").attr("action","${ctx}/workchangejob/workChangeJob/store");
            }else if(obj == 1){
                $("#inputForm").attr("action","${ctx}/workchangejob/workChangeJob/save");
            }else{
                return false;
			}
            if(validateForm.form()){
                $("#inputForm").submit();
                return true;
            }else {
                parent.layer.msg("信息未填写完整!", {icon: 5});
            }

            return false;
        }

        function deleteRow(obj,index){
            $(obj).parent().parent().remove();
            str[index] = " ";
            mon[index] = 0;

        }
        function getNowFormatDate() {
            var date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            month = (month < 10)?"0"+month:month;
            strDate = (strDate < 10)?"0"+strDate:strDate;
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }

        function addRow(list, idx, tpl, row){
            var idx1 = $("#workChangeJobUserList tr").length;
            bornTemplete(list, idx, tpl, row, idx1);
        }
        function bornTemplete(list, idx, tpl, row, idx1){
            $(list).append(Mustache.render(tpl, {
                idx: idx, delBtn: true, row: row,
                order:idx1 + 1
            }));
            $(list+idx).find("select").each(function(){
                $(this).val($(this).attr("data-value"));
            });
            $(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
                var ss = $(this).attr("data-value").split(',');
                for (var i=0; i<ss.length; i++){
                    if($(this).val() == ss[i]){
                        $(this).attr("checked","checked");
                    }
                }
            });

        }
        function delRow(obj, prefix){
            var id = $(prefix+"_id");
            var delFlag = $(prefix+"_delFlag");
            if (id.val() == ""){
                delFlag.val("1");
                $(obj).parent().parent().remove();
            }else if(delFlag.val() == "0"){
                delFlag.val("1");
                $(obj).html("&divide;").attr("title", "撤销删除");
                $(obj).parent().parent().addClass("error");
                $(obj).parent().parent().addClass("hide");
            }else if(delFlag.val() == "1"){
                delFlag.val("0");
                $(obj).html("&times;").attr("title", "删除");
                $(obj).parent().parent().removeClass("error");
            }
        }

        function setUserInfo(d){
            $.ajax({
                type:'post',
                url:'${ctx}/workchangejob/workChangeJob/getBasicInfo',
                data:{
                    "ids":d
                },
                success:function(data){
                    for(var i=0;i<data.length;i++){
                        var idArr = $("#workChangeJobUserList tr:visible .basicInfoId");
                        if(data[i].id!=''&&!hasInArr(data[i].id,idArr)){
                            addRow('#workChangeJobUserList', workChangeJobUserListRowIdx, workChangeJobUserListTpl);
                            workChangeJobUserListRowIdx = workChangeJobUserListRowIdx + 1;
                            var row = workChangeJobUserListRowIdx - 1 ;
                            $("#"+"workChangeJobUserList"+row+"_basicInfoId").val(data[i].id);
                            $("#"+"workChangeJobUserList"+row+"_userId").val(data[i].userId);
                            if(data[i].jobGrade!=null){
                                $("#"+"workChangeJobUserList"+row+"_before").val(data[i].jobGrade.id);
                                $("#"+"workChangeJobUserList"+row+"_6").val(data[i].jobGrade.name);
                            }
                            $("#"+"workChangeJobUserList"+row+"_1").val(data[i].no);
                            $("#"+"workChangeJobUserList"+row+"_2").val(data[i].name);
                            $("#"+"workChangeJobUserList"+row+"_3").val(data[i].office.name);
                            $("#"+"workChangeJobUserList"+row+"_4").val(data[i].idCard);
                            $("#"+"workChangeJobUserList"+row+"_5").val(data[i].status);
						}
                    }
                }
            });
        }

        function hasInArr(id,idArr) {
            for(var i=0;i<idArr.length;i++){
                if(id==$(idArr[i]).val()){
                    return true;
                }
            }
            return false;
        }
        function setExcelInfo(data){
            for(var i=0;i<data.length;i++){
                var idArr = $("#workChangeJobUserList tr:visible .basicInfoId");
                if(data[i].id!=''&&!hasInArr(data[i].id,idArr)){
                    addRow('#workChangeJobUserList', workChangeJobUserListRowIdx, workChangeJobUserListTpl);
                    workChangeJobUserListRowIdx = workChangeJobUserListRowIdx + 1;
                    var row = workChangeJobUserListRowIdx - 1 ;
                    $("#"+"workChangeJobUserList"+row+"_basicInfoId").val(data[i].id);
                    $("#"+"workChangeJobUserList"+row+"_userId").val(data[i].userId);
                    if(data[i].jobGrade!=null){
                        $("#"+"workChangeJobUserList"+row+"_before").val(data[i].jobGrade.id);
					}
                    $("#"+"workChangeJobUserList"+row+"_1").val(data[i].no);
                    $("#"+"workChangeJobUserList"+row+"_2").val(data[i].name);
                    $("#"+"workChangeJobUserList"+row+"_3").val(data[i].office.name);
                    $("#"+"workChangeJobUserList"+row+"_4").val(data[i].idCard);
                    $("#"+"workChangeJobUserList"+row+"_5").val(data[i].status);
                    if(data[i].jobGrade!=null){
                        $("#"+"workChangeJobUserList"+row+"_6").val(data[i].jobGrade.name);
					}
                    $("#"+"workChangeJobUserList"+row+"_afterId").val(data[i].bankName);//新职级id
                    $("#"+"workChangeJobUserList"+row+"_afterName").val(data[i].sqlStr);
                }
            }
        }
        function openDialogreUser(title,url,width,height,target){

            if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
                width='auto';
                height='auto';
            }else{//如果是PC端，根据用户设置的width和height显示。

            }
            top.layer.open({
                type: 2,
                area: ['90%','90%'],
                title:title,
                skin:"two-btns",
                name:'friend',
                content: encodeURI("${ctx}/sys/office/gridSelectUser?url=${ctx}/sys/office/gridSelectUser&isAll=true&type=null&isSingle=false") ,
                btn: ['确定', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0].contentWindow; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    var item = iframeWin.getSelectedItem();
                    if(item == "-1"){
                        return;
                    }
                    if(false){//单选
                        selectUserInfo(item.userId);
                    }else{//多选
                        var ids = [], names = [];
                        for (var i=0;i<item.length;i++){
                            ids.push(item[i].id);
                            names.push(item[i].name);
                        }
                        var arr=ids.join(",").replace(/u_/ig,"");
                        setUserInfo(arr);
                    }
                    top.layer.close(index);//关闭对话框。

                },
                cancel: function(index){
                }
            });
        }

        function openDialogreExcel(title,url,width,height,target){
            if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
                width='auto';
                height='auto';
            }else{//如果是PC端，根据用户设置的width和height显示。

            }
            top.layer.open({
                type: 1,
                area: [500, 300],
                title:"导入数据",
                skin: 'three-btns with-demo',
                content:$("#importBox").html() ,
                btn: ['下载模板','确定', '关闭'],
                btn1: function(index, layero){
                    window.location.href=url+'/template';
                },
                btn2: function(index, layero){

                    var formData = new FormData();
                    formData.append("file",top.$("#uploadFile")[0].files[0]);
                    $.ajax({
                        type: 'post',
                        url: url,
                        data: formData,
                        dataType: "json",
                        cache: false,
                        processData: false,
                        contentType: false,
                    }).success(function (result) {
                        setExcelInfo(result.data);
                        top.layer.msg(result.message);
                    }).error(function () {
                        top.layer.msg("导入文件失败！");
                    });
                    top.layer.close(index);
                },
                btn3: function(index){
                    top.layer.close(index);
                }
            });
        }

	</script>
	<style>
		/*超过5个汉字，调整label的长度，以下是配套的*/
		.layui-item .layui-form-label{
			width:90px;
		}
		.form-group .layui-item .layui-input-block,
		.query .layui-input-block {
			margin-left: 116px;
		}
	</style>
</head>
<body>
<div class="single-form">
	<div class="container">
		<form:form id="inputForm" modelAttribute="workChangeJob" action="#" onsubmit="return false;" method="post" class="form-horizontal" >
			<form:hidden path="id"/>
			<form:hidden  path="changer"/>
			<div class="form-group layui-row first">
				<div class="form-group-label"><h2>基础信息</h2></div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>调整人：</label>
					<div class="layui-input-block">
						<form:input id="userName" readonly="true" path="userName" htmlEscape="false" class="form-control   layui-input"/>

					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>调整编号：</label>
					<div class="layui-input-block">
						<div class="input-group">
							<form:input path="number" htmlEscape="false"  readonly="true"  class="form-control  layui-input"/>
							<span class="input-group-btn">
                                <label class="form-status"><c:choose><c:when test="${not empty workChangeJob.state}">${fns:getDictLabel(workChangeJob.state, 'audit_state', '')}</c:when><c:otherwise>新添</c:otherwise></c:choose></label>
                             </span>
						</div>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>调整日期：</label>
					<div class="layui-input-block">
						<input id="changeDate" name="changeDate" type="text" readonly="true"  maxlength="20" class="laydate-icondate form-control layer-date  layui-input laydate-icon"
							   value="<fmt:formatDate value="${workChangeJob.changeDate}" pattern="yyyy-MM-dd"/>"/>
					</div>
				</div>

				<div class="layui-item layui-col-sm12  with-textarea">
					<label class="layui-form-label"><span class="require-item">*</span>调整原因：</label>
					<div class="layui-input-block">
						<form:textarea path="changeFor" htmlEscape="false" rows="4" onkeyup="checkContentKeyUp('changeFor',255);"   maxlength="255" class="form-control required"/>
					</div>
					<span id="contentMsg" style="float: right"></span>
				</div>
			</div>

			<div class="form-group layui-row">
				<div class="form-group-label"><h2>调整成员</h2></div>
				<div class="layui-item nav-btns ">
					<a href="javascript:void(0)" onclick="openDialogreUser('选择用户', '${ctx}/sys/office/gridSelectUser','90%','90%')" class="nav-btn nav-btn-add" ><i class="fa fa-plus"></i>&nbsp;选择</a>
					<%--<table:importExcelChangeOffice url="${ctx}/workchangejob/workChangeJobUser/import"></table:importExcelChangeOffice>--%>
					<a href="javascript:void(0)" onclick="openDialogreExcel('导入数据', '${ctx}/workchangejob/workChangeJobUser/import','90%','90%')" class="nav-btn nav-btn-import" ><i class="fa fa-plus"></i>&nbsp;导入</a>
					<div id="importBox" class="hide">
						<input id="uploadFile" name="file" type="file" style="width:330px"/>导入文件不能超过5M，仅允许导入“xls”或“xlsx”格式文件！<br/>　　
					</div>
				</div>
				<div class="layui-item layui-col-xs12 form-table-container" >
					<table id="contentTable" class="table table-bordered table-condensed can-edit no-bottom-margin">
						<thead>
						<tr>
							<th width="200px">工号</th>
							<th width="200px">姓名</th>
							<th width="150px">部门</th>
							<th width="200px">身份证号</th>
							<th width="200px">员工状态</th>
							<th width="200px">职级</th>
							<th width="200px">调整职级</th>
							<th width="200px">操作</th>
						</tr>
						</thead>
						<tbody id="workChangeJobUserList">
						<c:if test="${not empty workChangeJob.workChangeJobUserList}">
							<c:forEach items="${workChangeJob.workChangeJobUserList}" var="workChangeJobUser" varStatus="index">
								<tr id="workChangeJobUserList${index.index}">
									<td class="hide">
										<input id="workChangeJobUserList${index.index}_id" name="workChangeJobUserList[${index.index}].id" type="hidden" value="${workChangeJobUser.id}"/>
										<input id="workChangeJobUserList${index.index}_basicInfoId" name="workChangeJobUserList[${index.index}].basicInfoId" type="hidden" value="${workChangeJobUser.basicInfoId}" class="basicInfoId"/>
										<input id="workChangeJobUserList${index.index}_userId" name="workChangeJobUserList[${index.index}].userId" type="hidden" value="${workChangeJobUser.userId}"/>
										<input id="workChangeJobUserList${index.index}_before" name="workChangeJobUserList[${index.index}].before" type="hidden" value="${workChangeJobUser.before}"/>
										<input id="workChangeJobUserList${index.index}_delFlag" name="workChangeJobUserList[${index.index}].delFlag" type="hidden" value="0"/>
									</td>
									<td>
										<input id="workChangeJobUserList${index.index}_1"  readonly="true"   type="text" value="${workChangeJobUser.workStaffBasicInfo.no}"   class="form-control "/>
									</td>
									<td>
										<input id="workChangeJobUserList${index.index}_2"  readonly="true"   type="text" value="${workChangeJobUser.workStaffBasicInfo.name}"   class="form-control "/>
									</td>
									<td>
										<input id="workChangeJobUserList${index.index}_3"  readonly="true"    type="text" value="${workChangeJobUser.workStaffBasicInfo.office.name}"   class="form-control "/>
									</td>
									<td>
										<input id="workChangeJobUserList${index.index}_4"  readonly="true"    type="text" value="${workChangeJobUser.workStaffBasicInfo.idCard}"   class="form-control "/>
									</td>
									<td>
										<input id="workChangeJobUserList${index.index}_5"  readonly="true"    type="text" value="${workChangeJobUser.workStaffBasicInfo.status}"  class="form-control "/>
									</td>
									<td>
										<input id="workChangeJobUserList${index.index}_6"  readonly="true"    type="text" value="${workChangeJobUser.beforeName}"   class="form-control "/>
									</td>
									<td>
										<sys:treeselectJob id="workChangeJobUserList${index.index}_after" name="workChangeJobUserList[${index.index}].after" value="${workChangeJobUser.after}" labelName="workChangeJobUserList[${index.index}].afterName" labelValue="${workChangeJobUser.afterName}"
																title="职级类型" url="/workjobgrade/workJobGrade/treeData"  cssClass="form-control layui-input required" allowClear="false" notAllowSelectParent="true"/>
									</td>
									<td class="text-center op-td" >
										<span class="op-btn op-btn-delete" id="workChangeJobUserList${index.index}_span" onclick="delRow(this, '#workChangeJobUserList${index.index}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>
									</td>
								</tr>
							</c:forEach>
						</c:if>
						</tbody>
					</table>
					<script type="text/template" id="workChangeJobUserListTpl">//<!--
                                    <tr id="workChangeJobUserList{{idx}}">
                                        <td class="hide">
                                            <input id="workChangeJobUserList{{idx}}_id" name="workChangeJobUserList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
                                            <input id="workChangeJobUserList{{idx}}_basicInfoId" name="workChangeJobUserList[{{idx}}].basicInfoId" type="hidden" value="{{row.workStaffBasicInfo.id}}" class="basicInfoId"/>
                                            <input id="workChangeJobUserList{{idx}}_userId" name="workChangeJobUserList[{{idx}}].userId" type="hidden" value="{{row.workStaffBasicInfo.userId}}"/>
                                            <input id="workChangeJobUserList{{idx}}_before" name="workChangeJobUserList[{{idx}}].before" type="hidden" value="{{row.before}}"/>
                                            <input id="workChangeJobUserList{{idx}}_delFlag" name="workChangeJobUserList[{{idx}}].delFlag" type="hidden" value="0"/>
                                        </td>
                                        <td>
                                            <input id="workChangeJobUserList{{idx}}_1"   name="workChangeJobUserList[{{idx}}].workStaffBasicInfo.no" type="text" value="{{row.workStaffBasicInfo.no}}"  readonly="true"   class="form-control "/>
                                        </td>
                                        <td>
                                            <input id="workChangeJobUserList{{idx}}_2"  name="workChangeJobUserList[{{idx}}].workStaffBasicInfo.name" type="text" value="{{row.workStaffBasicInfo.name}}"   readonly="true"  class="form-control "/>
                                        </td>
                                         <td>
                                            <input id="workChangeJobUserList{{idx}}_3"  name="workChangeJobUserList[{{idx}}].workStaffBasicInfo.office.name" type="text" value="{{row.workStaffBasicInfo.office.name}}"   readonly="true"  class="form-control "/>
                                        </td>
                                         <td>
                                            <input id="workChangeJobUserList{{idx}}_4"  name="workChangeJobUserList[{{idx}}].workStaffBasicInfo.idCard" type="text" value="{{row.workStaffBasicInfo.idCard}}"   readonly="true"  class="form-control "/>
                                        </td>
                                         <td>
                                            <input id="workChangeJobUserList{{idx}}_5"  name="workChangeJobUserList[{{idx}}].workStaffBasicInfo.status" type="text" value="{{row.workStaffBasicInfo.status}}"   readonly="true"  class="form-control "/>
                                        </td>
                                         <td>
                                            <input id="workChangeJobUserList{{idx}}_6"  name="workChangeJobUserList[{{idx}}].beforeName" type="text" value="{{row.beforeName}}"   readonly="true"  class="form-control "/>
                                        </td>
                                         <td>
                                            <sys:treeselectJob id="workChangeJobUserList{{idx}}_after" name="workChangeJobUserList[{{idx}}].after" value="${row.after}" labelName="workChangeJobUserList[{{idx}}].afterName" labelValue="${row.afterName}"
																title="职级类型" url="/workjobgrade/workJobGrade/treeData"  cssClass="form-control layui-input required" allowClear="true" notAllowSelectParent="true"/>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="workChangeJobUserList{{idx}}_span" onclick="delRow(this, '#workChangeJobUserList{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
					</script>
					<script type="text/javascript">
                        var workChangeJobUserListRowIdx = ${fn:length(workChangeJob.workChangeJobUserList)}, workChangeJobUserListTpl = $("#workChangeJobUserListTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                        if($("#id").val()){
                            workChangeJobUserListIdx = ${fn:length(workChangeJob.workChangeJobUserList)};
                        }
                        $(document).ready(function() {
                        });
					</script>
				</div>
			</div>
			<div class="form-group layui-row page-end"></div>
		</form:form>

	</div>
</div>
</body>
</html>