<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>发票管理</title>
	<meta name="decorator" content="default"/>
	<%--<script src="${ctxStatic}/layer-v2.3/laydate/laydate.js"></script>--%>
	<style>
		#contractTypeDoc-error{
			top:80px;
			left:0;
		}
	</style>
	<script type="text/javascript">
        var validateForm;
        function doSubmit(obj){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
            //debugger
            if(validateForm.form()){
                if(obj == 2){
                    $("#inputForm").attr("action","${ctx}/workinvoice/workInvoice/store");
                }else{
                    $("#inputForm").attr("action","${ctx}/workinvoice/workInvoice/save");
                }
                $("#inputForm").submit();
                return true;
            }
            return false;
        }
        $(document).ready(function() {
            /*$('#money').keyup(function(){
                var c=$(this);
                if(/[^\d]/.test(c.val())){//替换非数字字符
                    var temp_amount=c.val().replace(/[^\d]/g,'');
                    $(this).val(temp_amount);
                }
            })*/
            jQuery.validator.addMethod("isUscCode", function(value, element) {
                if (this.optional(element)){
                    return true;
                }
                var uscCode = /^((\w{15})|(\w{18})|(\w{20}))$/;
                return uscCode.test(value);
            }, "请填写正确的纳税人识别号");

            /* $("input[name='ext']").on('ifChecked', function(event){
                 var radioVal = $(this).val();
                 if(radioVal == 0){
                     $("#td1").removeClass("hide");
                     $("#td2").addClass("hide");
                     $("#tr1").show()
                 }else{
                     $("#td1").addClass("hide");
                     $("#td2").removeClass("hide");
                     $("#tr1").hide()
                 }
             });*/
            if("${workInvoice.ext}" == "1"){
                $("#ext1").iCheck('check')
            }else{
                $("#ext").iCheck('check')
            }
            if("${workInvoice.invoiceType}" == "1"){
                $("#invoiceType1").iCheck('check');
                $(".invoicetype").show();
            }else{
                $("#invoiceType").iCheck('check');
                $(".invoicetype").hide();
            }
            validateForm = $("#inputForm").validate({
                submitHandler: function(form){
                    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);
                    }
                }
            });

            $("input[name='invoiceType']").on('ifChecked', function(event){
                var radioVal = $(this).val();
                //专票必填
                if(radioVal == 1){
                    $(".invoicetype").show();
                    $("#bank").attr("class","form-control required layui-input");
                    $("#bankNumber").attr("class","form-control number required layui-input");
                    $("#orUnicode").attr("class","form-control isUscCode required layui-input");
                    $("#address").attr("class","form-control required layui-input");
                    $("#telephone").attr("class","form-control isTel required layui-input");
                }else if(radioVal == 2){
                    $(".invoicetype").hide();
                    $("#bank").attr("class","form-control layui-input");
                    $("#bankNumber").attr("class","form-control number layui-input");
                    $("#orUnicode").attr("class","form-control isUscCode layui-input");
                    $("#address").attr("class","form-control layui-input");
                    $("#telephone").attr("class","form-control isTel layui-input");
                }
            });
            /*--------------*/

        });
        function setValuee(obj){
            $.ajax({
                type:'post',
                url:'${ctx}/workinvoice/workInvoice/getWorkClientInfo',
                data:{
                    "obj":obj
                },
                success:function(data){
                    var d = JSON.parse(data);
                    $("#bank").val(d.bank);
                    $("#orUnicode").val(d.orUnicode);
                    $("#bankNumber").val(d.bankNumber);
                    $("#telephone").val(d.telephone);
                    $("#address").val(d.adress);
                    $("#ids").val(d.ids);
                }
            })
        }
        function setDp(obj){
            $.ajax({
                type:'post',
                url:'${ctx}/workinvoice/workInvoice/getUserDp',
                data:{
                    "obj":obj
                },
                success:function(data){
                    var d = JSON.parse(data);
                    $("#dp").val(d.dp);
                }
            })
        }
        function setPNumber(obj){
            $.ajax({
                type:'post',
                url:'${ctx}/workinvoice/workInvoice/getPNumber',
                data:{
                    "obj":obj
                },
                success:function(data){
                    var d = JSON.parse(data);
                    $("#pNumber").val(d.pNumber);
                    $("#cInfoName").val(d.cInfoName);
                    $("#cName").val(d.cName);
                    $("#officeName").val(d.officeName);
                    $("#officeId").val(d.officeId);
                }
            })
        }

        function addRow(list, idx, tpl, row){
            var idx1 = $("#workAccountList tr").length;
            if(list == '#workAccountList' && 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 = $("#workAccountList tr").length; //tr 总数
            // for (var i = 0; i < idx1; i++) {
            //     $("#workAccountList tr").eq(i).find("td").eq(1).html(i + 1);
            // }
        }*/
        function provinceForShort(province){
            if(province == "北京市")
                return "京";
            else if(province == "天津市")
                return "津";
            else if(province == "重庆市")
                return "渝";
            else if(province == "上海市")
                return "沪";
            else if(province == "河北省")
                return "冀";
            else if(province == "山西省")
                return "晋";
            else if(province == "辽宁省")
                return "辽";
            else if(province == "吉林省")
                return "吉";
            else if(province == "黑龙江省")
                return "黑";
            else if(province == "江苏省")
                return "苏";
            else if(province == "浙江省")
                return "浙";
            else if(province == "安徽省")
                return "皖";
            else if(province == "福建省")
                return "闽";
            else if(province == "江西省")
                return "赣";
            else if(province == "山东省")
                return "鲁";
            else if(province == "河南省")
                return "豫";
            else if(province == "湖北省")
                return "鄂";
            else if(province == "湖南省")
                return "湘";
            else if(province == "广东省")
                return "粤";
            else if(province == "海南省")
                return "琼";
            else if(province == "四川省")
                return "川/蜀";
            else if(province == "贵州省")
                return "黔/贵";
            else if(province == "云南省")
                return "云/滇";
            else if(province == "陕西省")
                return "陕/秦";
            else if(province == "甘肃省")
                return "甘/陇";
            else if(province == "青海省")
                return "青";
            else if(province == "台湾省")
                return "台";
            else if(province == "内蒙古自治区")
                return "内蒙古";
            else if(province == "广西壮族自治区")
                return "桂";
            else if(province == "宁夏回族自治区")
                return "宁";
            else if(province == "新疆维吾尔自治区 ")
                return "新";
            else if(province == "西藏自治区")
                return "藏";
            else if(province == "香港特别行政区")
                return "港";
            else if(province == "澳门特别行政区")
                return "澳";
        }

        function setVs(obj){
            var a = $(obj).attr("id");
            var b = a.replace('allPrice','proportion');
            var value = obj.value;
            var p = $("#price").val() || 0;
            if(!p || p == 0){
                parent.layer.msg("请先填写造价指标,且不能为零",{icon:5});
                return;
            }
            var num = value/p ;
            if(!/^\d+(\.\d{1,2})?$/.test( value )){
                parent.layer.msg("合价必须输入数字",{icon:5});
            }else{
                $("#" +  b ).val(parseFloat(num.toFixed(2)));
            }
        }
        function setTaxMoney(obj){
            var a = $(obj).attr("id");
            var b = a.replace('tax','taxMoney');
            var t = a.replace('tax','totalMoney');
            var tr = a.replace('tax','taxRate');
            var value = obj.value || 0;
            console.log("-value----"+value);
            var tax = 1 + value/100;
            console.log("-tax----"+tax);
            var p = $("#" +  t ).val() || 0;//获取价税合计
            console.log("-----"+p);
            if(!p || p == 0){
                parent.layer.msg("请先填写价税合计,且不能为零",{icon:5});
                return;
            }
            var num = p/tax || 0;
            console.log("-----"+num);
            if(!/^\d+(\.\d{1,2})?$/.test( tax )){
                parent.layer.msg("税率必须输入数字",{icon:5});
            }else{
                $("#" +  b ).val(parseFloat(num.toFixed(2)));
                var taxRate = p - num;
                $("#" +  tr ).val(parseFloat(taxRate.toFixed(2)));
            }
        }

        /*function setV(obj){
            var a =  $(obj).attr("id");
            var b = $("#province").val();
            var c = provinceForShort(b);//开票单位所在省/直辖市简称
            var date=new Date;
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            month =(month<10 ? "0"+month:month);
            var mydate = (year.toString()+month.toString());
            var timestamp="["+mydate+"]";//获取固定格式年月
			var number = c+timestamp;
            $("#" +  a ).val(number);
        }*/
        function setV(obj){
            var a =  $(obj).attr("id");
            var c = "京";
            var date=new Date;
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            month =(month<10 ? "0"+month:month);
            var mydate = (year.toString()+month.toString());
            var timestamp="["+mydate+"]";//获取固定格式年月
            var number = c+timestamp;
            $("#" +  a ).val(number);
        }
	</script>
</head>
<body>
<div class="single-form">
	<div class="container">
		<form:form id="inputForm" modelAttribute="workInvoice" action="${ctx}/workinvoice/workInvoice/save" method="post" class="form-horizontal">
			<form:hidden path="id"/>
			<form:hidden path="invoiceDate"/>
			<form:hidden path="province" id="province"/>

			<div class="form-group layui-row first lw14">
				<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  with-icon">
						<input type="hidden" value="" id ="ids">
						<sys:gridselectcallprojectt url="${ctx}/workinvoice/workInvoice/selectproject" id="project" name="project.id"  value="${workInvoice.project.id}"  title="选择所属项目" labelName="workInvoice.project.projectName"
													labelValue="${workInvoice.project.projectName}" cssClass="form-control required layui-input" fieldLabels="项目" fieldKeys="projectName" searchLabel="项目名称" searchKey="projectName" ></sys:gridselectcallprojectt>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">发票申请编号：</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="${ empty workInvoice.invoiceState}">新添</c:when><c:otherwise>${fns:getDictLabel(workInvoice.invoiceState, 'audit_state','')}</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">
						<form:input id ="cInfoName" path="project.workContractInfo.name" htmlEscape="false"  readonly="true" 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">
						<form:input id ="pNumber" path="project.projectId" htmlEscape="false"  readonly="true" 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">
						<form:input id ="cName" path="project.workContractInfo.client.name" htmlEscape="false"  readonly="true" 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">--%>
						<%--<form:input id ="officeName" path="office.name" htmlEscape="false"  readonly="true" class="form-control layui-input"/>--%>
						<%--<form:hidden path="office.id" id ="officeId" />--%>
					<%--</div>--%>
				<%--</div>--%>
			</div>
			<div class="form-group layui-row first lw14">
				<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">
						<c:choose>
							<c:when test="${workInvoice.invoiceState == 4}">
								<c:choose>
									<c:when test="${workInvoice.ext ==0}">
										<input value = "本公司开票" readonly="true" class="form-control layui-input"/>
									</c:when>
									<c:otherwise>
										<input value = "上级公司开票" readonly="true"  class="form-control layui-input"/>
									</c:otherwise>
								</c:choose>
							</c:when>
							<c:otherwise>
								<input type="radio" class="i-checks" name="ext" checked id="ext" value="0">
								<label for="ext">本公司开票</label>
								<input type="radio" class="i-checks" name="ext" id="ext1" value="1">
								<label for="ext1">上级公司开票</label>
							</c:otherwise>
						</c:choose>
					</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 type="radio" class="i-checks" name="invoiceType" checked id="invoiceType1" value="1">
						<label for="invoiceType1">专票</label>
						<input type="radio" class="i-checks" name="invoiceType" id="invoiceType" value="2">
						<label for="invoiceType">普票</label>
					</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 with-icon">
						<sys:gridselect url="${ctx}/workinvoice/workInvoice/selectclient" id="client" name="client.id"  value="${workInvoice.client.id}"  title="选择开票单位" labelName="workInvoice.client.name"
										labelValue="${workInvoice.client.name}" cssClass="form-control required layui-input" fieldLabels="单位" fieldKeys="name" searchLabel="客户名称" searchKey="name" ></sys:gridselect>
						</td>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item invoicetype">*</span>纳税人识别号：</label>
					<div class="layui-input-block">
						<form:input id="orUnicode" path="orUnicode" htmlEscape="false"    class="form-control isUscCode layui-input"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item invoicetype">*</span>地址：</label>
					<div class="layui-input-block">
						<form:input id="address" path="address" 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 invoicetype">*</span>电话：</label>
					<div class="layui-input-block">
						<form:input id="telephone" path="telephone" htmlEscape="false"    class="form-control isTel layui-input"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item invoicetype">*</span>开户银行：</label>
					<div class="layui-input-block">
						<form:input id ="bank"  path="bank" 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 invoicetype">*</span>银行账号：</label>
					<div class="layui-input-block">
						<form:input id="bankNumber" path="bankNumber" htmlEscape="false"   class="form-control number layui-input"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm12">
					<label class="layui-form-label"><span class="require-item">*</span>价税合计(元)：</label>
					<div class="layui-input-block">
							<%--<form:input id=""  path="money" htmlEscape="false" class="form-control number required layui-input"/>--%>
						<input name="money" htmlEscape="false"  value="${workInvoice.money}" class="form-control required number layui-input"/>
					</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="content" id="contractTypeDoc" htmlEscape="false" rows="4"    class="form-control required"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">开票人：</label>
					<div class="layui-input-block">
						<form:input id=""  path="" htmlEscape="false" readonly="true"  class="form-control layui-input"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">开票时间：</label>
					<div class="layui-input-block">
						<form:input id=""  path="" htmlEscape="false" readonly="true" class="form-control layui-input"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">领票时间：</label>
					<div class="layui-input-block">
						<form:input id=""  path="" htmlEscape="false" readonly="true" class="form-control layui-input"/>
					</div>
				</div>
			</div>
			<div class="form-group layui-row">
				<div class="form-group-label"><h2>发票明细</h2></div>
				<div class="layui-item layui-col-xs12 form-table-container">
					<table id="contentTable" class="table table-bordered table-condensed can-edit">
						<thead>
						<tr>
							<th><span class="require-item">*</span>发票代码</th>
							<th><span class="require-item">*</span>发票号</th>
							<th><span class="require-item">*</span>价税合计</th>
							<th><span class="require-item">*</span>税率</th>
							<th><span class="require-item">*</span>金额</th>
							<th><span class="require-item">*</span>税额</th>
							<th><span class="require-item">*</span>累计登记金额</th>
							<th width="150px">操作</th>
						</tr>
						</thead>
						<tbody id="workAccountList">
						</tbody>
					</table>
					<script type="text/template" id="workAccountListTpl">//<!--
                                    <tr id="workAccountList{{idx}}">
                                        <td class="hide">
                                            <input id="workAccountList{{idx}}_id" name="workAccountList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
                                            <input id="workAccountList{{idx}}_delFlag" name="workAccountList[{{idx}}].delFlag" type="hidden" value="0"/>
                                        </td>
                                    	<td>
                                        	<input id="workAccountList{{idx}}_code" name="workAccountList[{{idx}}].code" type="text" value="{{row.code}}"    class="form-control required"/>
                                   		 </td>
                                    	 <td>
                                        	<input id="workAccountList{{idx}}_number" name="workAccountList[{{idx}}].number" type="text" value="{{row.number}}"    class="form-control required"/>
                                   		 </td>
                                    	<td>
                                            <input id="workAccountList{{idx}}_totalMoney"  name="workAccountList[{{idx}}].totalMoney" type="text" value="{{row.totalMoney}}"    class="form-control required"/>
                                        </td>
                                        <td>
                                        <div class="input-group">
                                            <input id="workAccountList{{idx}}_tax" onblur="setTaxMoney(this)" name="workAccountList[{{idx}}].tax" type="text" value="{{row.tax}}"    class="form-control number required"/><span class="input-group-addon">%</span>
                                            </div>

                                        </td>
                                        <td>
                                            <input id="workAccountList{{idx}}_taxMoney" name="workAccountList[{{idx}}].taxMoney" type="text" value="{{row.taxMoney}}"    class="form-control required"/>
                                        </td>
										<td>
                                            <input id="workAccountList{{idx}}_taxRate" name="workAccountList[{{idx}}].taxRate" type="text" value="{{row.taxRate}}"    class="form-control required"/>
                                        </td>
                                        <td>
                                            <input id="workAccountList{{idx}}_incomeMoney" name="workAccountList[{{idx}}].incomeMoney" type="text" value="0"  readonly="true"  class="form-control required"/>
                                        </td>

                                            <input id="workAccountList{{idx}}_state" name="workAccountList[{{idx}}].state"  type="hidden" value="正常"   class="form-control"/>

                                            <input id="workAccountList{{idx}}_backSign" name="workAccountList[{{idx}}].backSign"  type="hidden" value="正常"  readonly="true"  class="form-control required"/>

                                            <input id="workAccountList{{idx}}_backNumber" name="workAccountList[{{idx}}].backNumber"  type="hidden" value=""  readonly="true"  class="form-control required"/>


                                        <td class="text-center op-td">
                                            {{#delBtn}}<span class="op-btn op-btn-delete" onclick="delRow(this, '#workAccountList{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
					</script>
					<script type="text/javascript">
                        var workAccountListRowIdx = 0, workAccountListTpl = $("#workAccountListTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                        $(document).ready(function() {
                            var data = ${fns:toJson(workInvoice.workAccountList)};
                            for (var i=0; i<data.length; i++){
                                addRow('#workAccountList', workAccountListRowIdx, workAccountListTpl, data[i]);
                                workAccountListRowIdx = workAccountListRowIdx + 1;
                            }
                        });
					</script>
				</div>
			</div>
			<div class="form-group layui-row page-end"></div>
		</form:form>
	</div>
</div>

</body>
</html>