<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>采购管理</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		.btn-file{
			margin-top:8px;
		}
	</style>
	<script src="${ctxStatic}/layer-v2.3/laydate/laydate.js"></script>
	<script type="text/javascript">
		var validateForm;
		function doSubmit(){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
		  if(validateForm.form()){
			  $("#inputForm").submit();
			  return true;
		  }
		  return false;
		}
		$(document).ready(function() {
			validateForm = $("#inputForm").validate({
				submitHandler: function(form){

                    var len = $("#showPeo span").length;
                    if(len<=0){
                        parent.layer.msg("请选择审批人!",{icon:5});
                        return false;
                    }
					loading('正在提交，请稍等...');
					form.submit();
				},
				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);
					}
				}
			});
            $("#attachment_btn").click(function () {
                $("#attachment_file").click();
            });

            laydate({
                elem: '#deliverDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
                event: 'focus' //响应事件。如果没有传入event，则按照默认的click
            });
		});
        function selectPeople(objId,objName){
            var showPeo = $("#showPeo").html();
            var len = $("#showPeo span").length;
            if(len > 4){
                parent.layer.msg("最多添加5个审批人",{icon:5})
                return;
            }
            var arr = [];
            var a = $("#showPeo span").each(function(){
                var dataId = $(this).attr("data-id");
                arr.push(dataId)
            })
            if(arr.indexOf(objId) != -1){
                parent.layer.msg("该审批人已被选择，不能重复选择！！！",{icon:5})
                return ;
            }
            showPeo = showPeo + '<span class="badge badge-success fontSpan" data-id="'+objId+'">'+objName+'&nbsp;&nbsp;&nbsp;' +
                '<i onclick=deletePeo(this) class="fa fa-close"></i><input type="hidden" name=adds['+len+'] value="'+objId+'"></span>';
            $("#showPeo").html(showPeo)
        }
        //删除选择的人员
        function deletePeo(obj){
            $(obj).parent().remove()
        }
        function insertTitle(tValue){

            var files = $("#attachment_file")[0].files;
            for(var i = 0;i<files.length;i++) {
                var file = files[i];
            var attachmentId = $("#id").val();
            var attachmentFlag = "69";
            var timestamp=new Date().getTime();

            var storeAs = "attachment-file/workClientInfo/"+timestamp+"/"+file['name'];
            var uploadPath="http://gangwan-app.oss-cn-hangzhou.aliyuncs.com/"+storeAs;/*将这段字符串存到数据库即可*/
            var divId = "_attachment";
            $("#addFile"+divId).show();
            multipartUploadWithSts(storeAs, file,attachmentId,attachmentFlag,uploadPath,divId,"0");
        }}


       /* function addRow(list, idx, buyDetailsTpl, row) {
            if (idx < 1) {
                $(list).append(Mustache.render(buyDetailsTpl, {
                    idx: idx, delBtn: true, row: row
                }));
                $(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");
                        }
                    }
                });
            }
        }*/
        /**
         * 只允许输入汉字
         * @param value
         */
        function testOurBank(value){
            if(!/^[\u4e00-\u9fa5]*$/.test( value )){
                parent.layer.msg("必须输入汉字",{icon:6});
            }
        }
        /**
         * 只允许输入数字
         * @param value
         */
        function testBankNumber(value){
            if(!/^\d*$/.test( value )){
                parent.layer.msg("价格必须输入数字",{icon:6});
            }
        }
        function addRow(list, idx, tpl, row){
            var idx1 = $("#buyDetailsList tr").length;
            if(list == '#buyDetailsList' && idx1 < 100){
                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() == ""){
                $(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");
            }
            var idx1 = $("#buyDetailsList tr").length; //tr 总数
            for(var i = 0;i < idx1;i++){
                $("#buyDetailsList tr").eq(i).find("td").eq(1).html(i + 1);
            }
            var idx2 = $("#buyDetailsList tr").length; //tr 总数
            for(var i = 0;i < idx1;i++){
                $("#buyDetailsList tr").eq(i).find("td").eq(1).html(i + 1);
            }
        }
	</script>
</head>
<body class="hideScroll">
<div class="wrapper wrapper-content">
<div class="ibox">
	<div class="ibox-title" style="height: 70px;">
		<h5>
			<ul class="nav nav-tabs">
				<li><a href="${ctx}/oabuy/oaBuy/list">采购申请信息</a></li>
				<li><a href="${ctx}/oabuy/oaBuy/oaBuyTodoList">待办任务</a></li>
				<li><a href="${ctx}/oabuy/oaBuy/oaBuyHistoricList">已办任务</a></li>
				<li class="active"><a href="${ctx}/oabuy/oaBuy/form">采购申请</a></li>
			</ul>
		</h5>
		<%--<div class="ibox-tools">
			<a class="collapse-link">
				<i class="fa fa-chevron-up"></i>
			</a>
			<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
				<i class="fa fa-wrench"></i>
			</a>

		</div>--%>
	</div>

	<div class="ibox-content">
		<form:form id="inputForm" modelAttribute="oaBuy" action="${ctx}/oabuy/oaBuy/save" method="post" class="form-horizontal">
			<form:hidden path="id"/>
			<sys:message content="${message}"/>
			<div class="control-group">
				<label class="control-label"><font color="red">*</font>申请事由：</label>
				<div class="controls">
					<textarea id="applyContent" name="applyContent" maxlength="20" class="form-control required" rows="3" aria-required="true"></textarea>
					<span class="help-inline"></span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">采购类型：</label>
				<div class="controls">
				<td class="width-35">
					<form:select path="buySort" class="form-control ">
						<form:option value="" label=""/>
						<form:options items="${fns:getDictList('buy_sort')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
					</form:select>
				</td>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">交付日期：</label>
				<div class="controls">
					<input class="form-control layer-date laydate-icon required" id="deliverDate" name="deliverDate"
						    value="<fmt:formatDate value="${oaBuy.deliverDate}" pattern="yyyy-MM-dd"/>
								" onclick="laydate({istime: false, format: 'YYYY-MM-DD'})">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">采购明细：</label>
				<div class="controls">
					<tr>
						<td class="width-15 active"></td>
						<td class="width-75"  colspan="3">
							<div id="kh">
								<a class="btn btn-info btn-xs" onclick="addRow('#buyDetailsList', buyDetailsRowIdx, buyDetailsTpl);buyDetailsRowIdx = buyDetailsRowIdx + 1;" title="新增"><i class="fa fa-plus"></i> 增加明细</a>
							</div>
							<div>
								<table id="bankinfo" class="table table-striped table-bordered table-hover table-condensed dataTables-example dataTable">
								<thead>
								<tr>
									<th class="">序号</th>
									<th class="">名称</th>
									<th class="">规格</th>
									<th class="">数量</th>
									<th class="">单位</th>
									<th class="">价格</th>
									<th>操作</th>
								</tr>
								</thead>
								<tbody id="buyDetailsList">
								</tbody>
							</table>
								<script type="text/template" id="buyDetailsTpl">//<!--
                                <tr id="buyDetailsList{{idx}}">
                                    <td class="hide">
                                        <input id="buyDetailsList{{idx}}_id" name="buyDetailsList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
                                        <input id="buyDetailsList{{idx}}_delFlag" name="buyDetailsList[{{idx}}].delFlag" type="hidden" value="0"/>
                                    </td>
                                    <td>
                                        {{order}}
                                    </td>
                                    <td>
                                        <input id="buyDetailsList{{idx}}_name" name="buyDetailsList[{{idx}}].name" type="text" value="{{row.name}}"    class="form-control "/>
                                    </td>
									 <td>
                                        <input id="buyDetailsList{{idx}}_format"  name="buyDetailsList[{{idx}}].format" type="text" value="{{row.format}}"    class="form-control "/>
                                    </td>
                                    <td>
                                        <input id="buyDetailsList{{idx}}_amount" name="buyDetailsList[{{idx}}].amount" type="text" value="{{row.amount}}"    class="form-control "/>
                                    </td>
									<td>
                                        <input id="buyDetailsList{{idx}}_unit"  name="buyDetailsList[{{idx}}].unit" type="text" value="{{row._unit}}"    class="form-control "/>
                                    </td>
                                     <td>
                                        <input id="buyDetailsList{{idx}}_price" onblur="testBankNumber(this.value)" name="buyDetailsList[{{idx}}].price" type="text" value="{{row.price}}"    class="form-control "/>
                                    </td>
                                    <td class="text-center" width="10">
                                        {{#delBtn}}<span class="close" onclick="delRow(this, '#buyDetailsList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
                                    </td>
                                </tr>//-->
								</script>
							<script type="text/javascript">
								var buyDetailsRowIdx = 0, buyDetailsTpl = $("#buyDetailsTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
								$(document).ready(function() {
									var data = ${fns:toJson(oaBuy.buyDetailsList)};
									for (var i=0; i<data.length; i++){
										addRow('#buyDetailsList', buyDetailsRowIdx, buyDetailsTpl, data[i]);
										buyDetailsRowIdx = buyDetailsRowIdx + 1;
									}
								});
							</script>
						</div>
						</td>
					</tr>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">支付方式：</label>
				<div class="controls">
				<td class="width-15 active"></td>
				<td class="width-35">
					<form:select path="payStyle" class="form-control ">
						<form:option value="" label=""/>
						<form:options items="${fns:getDictList('pay_style')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
					</form:select>
				</td>
				</div>
			</div>

			<div class="control-group">
				<label class="control-label">图片：</label>
				<div class="controls">
					<div style="display: inline-block">
						<div id="addFile_attachment" style="display: none">
							<span id="fileName_attachment" ></span>
							<b><span id="baifenbi_attachment" ></span></b>
							<div class="progress">
								<div id="jindutiao_attachment" class="progress-bar" style="width: 0%" aria-valuenow="0">
								</div>
							</div>
						</div>
                        <input id="attachment_file" type="file" name="attachment_file" multiple="multiple" style="display: none;" onChange="if(this.value)insertTitle(this.value);"/>
						<a id="attachment_btn" class="btn btn-info btn-xs" ><i class="fa fa-plus"></i> 添加附件</a>
						<span id="attachment_title"></span>
					</div>
					<br><br>
					<table id="upTable" class="table table-striped table-bordered table-condensed">
						<thead>
						<tr>
							<th class="hide"></th>
							<%--<th>序号</th>--%>
							<th>文件名</th>
							<th>上传人</th>
							<th>上传时间</th>
							<th width="100">操作</th>
						</tr>
						</thead>
						<tbody id="file_attachment">
						</tbody>
					</table>
					<span class="help-inline"></span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label"><font color="red">*</font>抄送人：</label>
				<div id="CCId" class="controls">
					<sys:treeselect id="CCId" name="CCId" value="${leave.CCId}" labelName="idNames" labelValue="${leave.idNames}"
									title="用户" url="/sys/office/treeDataAll?type=3&isAll=false" cssClass="form-control required" notAllowSelectParent="true" checked="true"/>
					<span class="help-inline"></span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label"><font color="red">*</font>审批人：</label>
				<div id="addida" class="controls">
					<sys:treeselectleave id="ida" name="ida" value="${leave.ida}" labelName="idNamea" labelValue="${leave.idNamea}"
										 title="用户" url="/sys/office/treeDataAll?type=3&isAll=false" cssClass="form-control" notAllowSelectParent="true"/>
					<span class="help-inline"></span>
				</div>
				<div id="showPeo" class="controls">
				</div>

			</div>
			<div class="control-group">
				<label class="control-label">备注信息：</label>
				<div class="controls">
					<td class="width-15 active"></td>
					<td class="width-35">
						<form:textarea path="remarks" htmlEscape="false" rows="4"    class="form-control "/>
					</td>
				</div>
			</div>
			<div class="form-actions">
				<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
				<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
			</div>
		</form:form>
	</div>
</div>
</div>
</body>
</html>