<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>

<%@include file="/WEB-INF/page/header.jsp" %>
<script>changeMenu(3,2);//左边菜单active</script>
<div class="row wrapper border-bottom white-bg page-heading">
	<div class="col-lg-10">
		<h2>资料设置</h2>
		<ol class="breadcrumb">
			<li>主页</li>
			<li><a>资料设置</a></li>
			<li class="active"><strong>设置审核资料</strong></li>
		</ol>
	</div>
	<div class="col-lg-2">
		<div style="padding-top:25px">
		<c:if test="${sessionScope.SESSION_COMPANY.canApply}">
			<button class="btn btn-primary btn-lg" type="button" onclick="submitAll()">提交全部页面</button>
		</c:if>
		<c:if test="${sessionScope.SESSION_COMPANY.companyState==1}">
			认证申请中
		</c:if>
		</div>
	</div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">

		<div class="row">
			<div class="col-md-12">
				<div class="ibox float-e-margins">
					<div class="ibox-content lightBoxGallery">
						<form class="form-horizontal">
							<div class="form-group">
								<label class="col-sm-10 control-label">
									<h3 class="text-left">律师需要上传的资料名称</h3>
								</label>
								<div class="col-sm-2">
									<button class="btn btn-primary" type="button" onclick="$('#mod-add').modal('toggle');$('#newattach').val('');">新增资料</button>
								</div>
							</div>
							<div class="hr-line-dashed"></div>
							<div class="form-group"><div id="attachList" class="col-sm-12"></div></div>
                           <%--  <c:forEach var="item" items="${list}" varStatus="status">  
                            <div class="form-group">
                            	<label class="col-sm-2 control-label">
                            		保单审核资料${status.count}
                            	</label>
                                <div class="col-sm-5 text-left">
                                	 <input type="text" id="${item.id}" class="companyattach form-control" value="${item.name}" placeholder="输入资料名称">
								</div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            </c:forEach> --%>
							<div class="form-group">
								<label class="col-sm-2 control-label">诉讼前申请资料</label>
								<div class="col-sm-10 text-left">
									<button class="btn btn-primary btn-white checkbox-inline" type="button" onclick="uploadFile(41)">上传文件</button>
								</div>
							</div>
							<div class="form-group"><div id="attachList41" class="col-sm-12"></div></div>
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">诉讼中申请资料</label>
								<div class="col-sm-10 text-left">
									<button class="btn btn-primary btn-white checkbox-inline" type="button" onclick="uploadFile(42)">上传文件</button>
								</div>
							</div>
							<div class="form-group"><div id="attachList42" class="col-sm-12"></div></div>
							<div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-12 text-center">
                                    <button class="btn btn-primary" type="button" id="attachSave">保存本页面</button>
                                </div>
                            </div>
                        </form>
					</div>
				</div>
			</div>
		</div>

</div>

	<div id="mod-add" class="modal fade" tabindex="-1">
	  <div class="modal-dialog" style="max-width: 400px;">
	      <div class="modal-content">
	      	<div class="modal-header">
      			<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					新增资料
				</div>
	      	</div>
	        <div class="modal-body">
                 <div class="form-group">
                 	<input type="text"  id="newattach" placeholder="输入资料名称" class="form-control">
                 </div>
                 <div class="form-group">
                 	<label><input type="checkbox" id="newBefore" checked="checked"/>诉讼前</label>&nbsp;&nbsp;
                 	<label><input type="checkbox" id="newAfter" checked="checked"/>诉讼中</label>
                 	&nbsp;&nbsp;
                 	<label><input type="checkbox" id="newNeed" />是否必填</label>
                 </div>
	      	</div>
	      	<div class="modal-footer">
	      		<button class="btn  btn-primary" type="button" id="mod-add-submit"><strong><i class="fa fa-floppy-o bigger-160"></i>确定</strong></button>
                <button class="btn " data-dismiss="modal"><i class="fa fa-share bigger-160"></i> 取消</button>
			</div>
	      </div>
	  </div>
	</div>
	
<div id="mod-file" class="modal fade" tabindex="-1">
	<div class="modal-dialog" style="max-width: 500px;">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					文件上传
				</div>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12">
						<!-- PAGE CONTENT BEGINS -->
						<form id="fileform" method="post" action="${applicationScope.ServerC}/ssdbExampleUpload">
							<input  type="file" name="avatar" multiple />
							<input type="hidden" name="attachType" id="mod-file-attachType"/>
							<input type="hidden" name="companyId" value="${sessionScope.SESSION_COMPANY.id}"/>
							<div class="hr hr-12 dotted"></div>
							<div class="text-center">
							<button type="submit" class="btn btn-sm btn-primary">上传</button>
							<button type="reset" class="btn btn-sm">清除</button>
							</div>
						</form>
					</div><!-- /.col -->
				</div><!-- /.row -->
				
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div>
	
	<%@include file="/WEB-INF/page/footer.jsp" %>
	
<link rel="stylesheet" href="${contextPath}/static/ssdb/ace.fileinput.css"/>
<script type="text/javascript" src="${contextPath}/static/ssdb/ace.fileinput.js"></script>
<script type="text/javascript" src="${contextPath}/static/ssdb/fileupload.js?v=2"></script>
 
<script>
function deleteFile(id,type){
	if (confirm("确定删除吗?")) {
		$.getJSON('${contextPath}/company/setting1DeletePic?id='+id,function(result){
			if(result.success){
				loadRequestAttach(type);
			}else
				alert(result.message);
		});
	}
}

function loadAttach(){
	$.getJSON('${contextPath}/company/setting2Load',function(result){
		var sec='';
		$.each(result, function(i, v){
			sec+='<div class="form-group"><label class="col-sm-2 control-label">保单审核资料'+(i+1)+'</label>';
        	sec+='<div class="col-sm-5 text-left"><input type="text" id="'+v.id+'" class="companyattach form-control" value="'+v.name+'" placeholder="输入资料名称"></div>';
        	sec+='<div class="col-sm-5 text-left">';
        	sec+='<label><input type="checkbox" id="before' + v.id + '"';
        	if('1' == v.suitBefore){
        		sec+=' checked="checked"';
        	}
        	sec += '/>诉讼前</label>&nbsp;&nbsp;';
        	sec+='<label><input type="checkbox" id="after' + v.id + '"';
        	if('1' == v.suitAfter){
        		sec+=' checked="checked"';
        	}
        	sec += '/>诉讼中</label>&nbsp;&nbsp;';
        	
        	sec+='<label><input type="checkbox" id="need' + v.id + '"';
        	if('1' == v.need){
        		sec+=' checked="checked"';
        	}
        	sec += '/>是否必填</label>';
        	
        	sec+='</div></div>';
        	sec+='<div class="hr-line-dashed"></div>';
		 });
		$("#attachList").html(sec);
	});
}

//加载诉讼申请资料
function loadRequestAttach(type){
	var attachListDiv = '#attachList' + type;
	$(attachListDiv).html('');
	$.getJSON('${contextPath}/company/setting1GetPic?attachType='+type,function(result){
		var html = '';
		$.each(result, function(i, v){
			html += '<div class="form-group">';
			html += '<label class="col-sm-2 control-label"></label>';
			html += '<label class="col-sm-5 text-left">' + v.name + '</label>';
			html += '<div class="col-sm-5 text-left">';
			html += '<button class="btn btn-primary btn-white checkbox-inline" type="button" onclick="deleteFile(\'' + v.id + '\',\'' + v.attachType + '\')">删除</button>';
			html += '</div></div>';
		});
		$(attachListDiv).html(html);
	});
}

function submitAll(){
	$.getJSON('${contextPath}/company/submitAll',function(result){
		if(result.success){
			alert('提交成功！');
			location.reload();
		}else
			alert(result.message);
	});
}
	
jQuery(function($) {
	loadAttach();
	loadRequestAttach(41);
	loadRequestAttach(42);
	
	$('#mod-add-submit').on('click', function() {
		var newattach = $("#newattach").val();
		if(newattach==''){
			alert('请输入资料名');
			return;
		}
		
		var newBefore = document.getElementById('newBefore').checked ? 1:0;
		var newAfter = document.getElementById('newAfter').checked ? 1:0;
		var newNeed = document.getElementById('newNeed').checked ? 1:0;
		
		$.ajax({
			url : "${contextPath}/company/setting2Add",
			data : {name:newattach,before:newBefore,after:newAfter,need:newNeed},
			type : 'POST',
			dataType : 'json',
			success : function(data) {
				if(data.success){
					loadAttach();
					$("#mod-add").modal("toggle");
				}else
					alert(data.message);
				
			}
		});
	});
	
	$('#attachSave').on('click', function() {
		var map = {};
   		$(".companyattach").each(function() { 
  			  	var id = $(this).attr("id");  
  				var v = $(this).val();
  				if(v==''){
  					alert("资料名称不能为空！");
  					return;
  				}
  				var after = document.getElementById('after' + id).checked ? 1:0;
  				var before = document.getElementById('before' + id).checked ? 1:0;
  				var need = document.getElementById('need' + id).checked ? 1:0;
  				map[id]=[v,after,before,need];
  			});
		$.ajax({
			url : "${contextPath}/company/setting2Save",
			data : map,
			type : 'POST',
			dataType : 'json',
			success : function(data) {
				if(data.success){
					loadAttach();
					alert('保存成功！');
				}else
					alert(data.message);
				
			}
		});
	});
});

function uploadFile(attachType){
	$("#mod-file").modal("toggle");
	$("#mod-file-attachType").val(attachType);
}

function uploadFileSuccess(){
	document.getElementById("fileform").reset();
	$("#mod-file").modal("toggle");
	var t = $("#mod-file-attachType").val();
	loadRequestAttach(t);
}

</script>