<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改合同发票管理')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-contractInvoice-edit" th:object="${crmContractInvoice}">
            <input name="invoiceId" th:field="*{invoiceId}" type="hidden">
            <input name="contractId" type="hidden" id="contractId" th:field="*{contractId}" />
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">合同ID：</label>
                <div class="col-sm-8">
                    <div class="input-group">   
                    	<input name="contractName" onclick="selectContract()" th:field="*{contract.contractName}" class="form-control"
						type="text" placeholder="请选择所属合同 " id="contractName" required readonly>		
						<span class="input-group-addon"><i class="fa fa-search"></i></span>
					</div>	
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">发票号码：</label>
                <div class="col-sm-8">
                    <input name="invoiceNo" th:field="*{invoiceNo}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">发票金额：</label>
                <div class="col-sm-8">
                    <input name="invoiceMoney" id="invoiceMoney" th:field="*{invoiceMoney}" class="form-control" type="number" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发票类型：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('invoice_type')}">
                        <input type="radio" th:id="${'invoiceType_' + dict.dictCode}" name="invoiceType" th:value="${dict.dictValue}" th:field="*{invoiceType}" required>
                        <label th:for="${'invoiceType_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">发票税率/%：</label>
                <div class="col-sm-8">
                    <input name="invoiceFate" th:field="*{invoiceFate}" class="form-control" type="number" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发票日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="invoiceDate" th:value="${#dates.format(crmContractInvoice.invoiceDate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                    </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="submit()" shiro:hasPermission="crm:contractInvoice:submit">
				<i class="fa fa-location-arrow"></i>提 交
			</button>
			&nbsp;
			<button type="button" class="btn btn-sm btn-success" 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 :: datetimepicker-js" />
    <script type="text/javascript">
        var prefix = ctx + "crm/contractInvoice";
        var prefixContract = ctx + "crm/contract";
        $("#form-contractInvoice-edit").validate({
            focusCleanup: true
        });

        //保存
        function submitHandler() {
            if ($.validate.form()) {
            	$.ajax({
					type : "get",
					contentType : "json",
					url : prefixContract + "/checkInvoiceMoney",
					data : {
						"contractId" : $("#contractId").val(),
						"invoiceMoney" : $("#invoiceMoney").val()
					},
					success : function(response) {
						if (!response) {
							$.modal.alertError("发票金额不能大于合同金额！");
							return;
						} else {
							$.operate.saveTab(prefix + "/edit", $('#form-contractInvoice-edit').serialize());
						}
					},
					error : function(e) {
					}
				});
            }
        }
        
      //提交
		function submit() {
			if ($.validate.form()) {
				$.modal.confirm("确认提交吗？", function() {
					$.ajax({
						type : "get",
						contentType : "json",
						url : prefixContract + "/checkInvoiceMoney",
						data : {
							"contractId" : $("#contractId").val(),
							"invoiceMoney" : $("#invoiceMoney").val()
						},
						success : function(response) {
							if (!response) {
								$.modal.alertError("发票金额不能大于合同金额！");
								return;
							} else {
								$.operate.saveTab(prefix + "/submit", $('#form-contractInvoice-edit').serialize());
							}
						},
						error : function(e) {
						}
					});
				});
			}
		}

        $("input[name='invoiceDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        
        /*  合同发票管理-修改-选择合同 */
        function selectContract() {
        	 var url = ctx + "crm/contract/selectContractInvoice";
        	 $.modal.open("选择合同", url);
		}
    </script>
</body>
</html>