<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<form:form id="subForm" modelAttribute="workContentContractinfo" action="#" method="post" class="form-horizontal">
    <form:hidden path="id"/>
    <div class="form-group layui-row lw12">
        <div class="layui-item layui-col-sm6">
            <label class="layui-form-label">合约规划名称：</label>
            <div class="layui-input-block with-icon">
                <input htmlEscape="false" readonly="true" class="form-control layui-input" value="${workContentContractinfo.program.name}"/>
            </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 path="cnumber" htmlEscape="false"  readonly="true"  class="form-control layui-input required"/>
            </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 htmlEscape="false" readonly="true" class="form-control layui-input"
                   <c:forEach items="${fns:getDictList('content_contract_type')}" var="item">
                        <c:set var="typename" value="content_contract_type_${item.value}"></c:set>
                        <c:forEach items="${fns:getDictList(typename)}" var="item2">
                            <c:if test="${workContentContractinfo.contCate eq item2.value}">value="${item2.label}"</c:if>
                        </c:forEach>
                    </c:forEach>/>
            </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 htmlEscape="false" readonly="true" class="form-control layui-input" value="${fns:getMainDictLabel(workContentContractinfo.contAttr, 'content_contract_attr', '')}"/>
            </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 path="lettingAgent" htmlEscape="false" readonly="true" class="form-control layui-input required"/>
            </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 path="contractor" htmlEscape="false" readonly="true" class="form-control layui-input required"/>
            </div>
        </div>
    </div>

    <div class="form-group layui-row lw12">
        <div class="form-group-label"><h2>详情信息</h2></div>
        <div class="layui-item layui-col-sm12 with-textarea">
            <label class="layui-form-label">合同范围：</label>
            <div class="layui-input-block">
                <form:textarea path="scope" htmlEscape="false" rows="3" readonly="true" class="form-control "/>
            </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="text" maxlength="20" readonly="true" class="form-control layui-input"
                       value="<fmt:formatDate value="${workContentContractinfo.signDate}" pattern="yyyy-MM-dd"/>"/>
            </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="text" maxlength="20" readonly="true" class="form-control layui-input"
                       value="<fmt:formatDate value="${workContentContractinfo.effectDate}" pattern="yyyy-MM-dd"/>"/>
            </div>
        </div>
        <div class="layui-item layui-col-sm6">
            <label class="layui-form-label ">目标成本(元)：</label>
            <div class="layui-input-block">
                <input name="titularAmt" htmlEscape="false" readonly="true" value="<fmt:formatNumber value="${workContentContractinfo.titularAmt}" pattern="#,##0.00#"/>" class="form-control layui-input number"/>
            </div>
        </div>
        <div class="layui-item layui-col-sm6 double-line">
            <label class="layui-form-label"><span class="require-item">*</span>有效合同金额(元)：</label>
            <div class="layui-input-block">
                <input name="trueAmt" htmlEscape="false" readonly="true" value="<fmt:formatNumber value="${workContentContractinfo.trueAmt}" pattern="#,##0.00#"/>" class="form-control layui-input number required"/>
            </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 path="pledgePercent" htmlEscape="false" readonly="true" class="form-control layui-input required number"/>
            </div>
        </div>
        <div class="layui-item layui-col-sm6">
            <label class="layui-form-label">合同开工日期：</label>
            <div class="layui-input-block">
                <input readonly="true" type="text" maxlength="20" class="form-control layui-input"
                       value="<fmt:formatDate value="${workContentContractinfo.startWorkDate}" pattern="yyyy-MM-dd"/>"/>
            </div>
        </div>
        <div class="layui-item layui-col-sm6">
            <label class="layui-form-label">合同竣工日期：</label>
            <div class="layui-input-block">
                <input readonly="true" type="text" maxlength="20" class="form-control layui-input"
                       value="<fmt:formatDate value="${workContentContractinfo.endWorkDate}" pattern="yyyy-MM-dd"/>"/>
            </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 path="stopPercent" htmlEscape="false" readonly="true" class="form-control layui-input required number"/>
            </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 name="stopAmt" htmlEscape="false"  value="<fmt:formatNumber value="${workContentContractinfo.stopAmt}" pattern="#,##0.00#"/>" readonly="true" class="form-control layui-input required number"/>
            </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 htmlEscape="false" readonly="true" class="form-control layui-input" value="${fns:getDictLabel(workContentContractinfo.status, 'content_contract_status', '')}"/>
            </div>
        </div>
    </div>
    <div class="form-group layui-row lw12">
        <div class="form-group-label"><h2>履约保证金</h2></div>
        <div class="layui-item layui-col-sm6">
            <label class="layui-form-label">支付方式：</label>
            <div class="layui-input-block">
                <form:input path="payType" 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">
                <input name="payAmt" htmlEscape="false"  value="<fmt:formatNumber value="${workContentContractinfo.payAmt}" pattern="#,##0.00#"/>" readonly="true" class="form-control layui-input required number"/>
            </div>
        </div>
        <div class="layui-item layui-col-sm6">
            <label class="layui-form-label">开始日期：</label>
            <div class="layui-input-block">
                <input readonly="true" type="text" maxlength="20" class="form-control layui-input "
                       value="<fmt:formatDate value="${workContentContractinfo.startDate}" pattern="yyyy-MM-dd"/>"/>
            </div>
        </div>
        <div class="layui-item layui-col-sm6">
            <label class="layui-form-label">结束日期：</label>
            <div class="layui-input-block">
                <input readonly="true" type="text" maxlength="20" class=" form-control layui-input "
                       value="<fmt:formatDate value="${workContentContractinfo.endDate}" pattern="yyyy-MM-dd"/>"/>
            </div>
        </div>
    </div>
</form:form>

<form id="termForm" action="#" onsubmit="return false;" method="post" class="form-horizontal">
    <div class="form-group layui-row lw12">
        <div class="form-group-label"><h2>合同条款</h2></div>
        <table id="termsTable" class="table table-bordered table-condensed">
            <thead>
            <tr>
                <th>摘要项</th>
                <th>摘要内容</th>
                <th>对应条款序号</th>
            </tr>
            </thead>
            <tbody id="termTableList">
            </tbody>
        </table>
    </div>
</form>
<script type="text/template" id="termTpl">//<!--
        <tr id="termList{{idx}}">
            <td style='text-align:center;'>
                {{row.title}}
            </td>
            <td style='text-align:center;'>
                {{row.content}}
            </td>
            <td style='text-align:center;'>
                {{row.tnumber}}
            </td>
        </tr>//-->
</script>
<script type="text/javascript">
    var termIdx = 0, termTpl = $("#termTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
    var validateForm;
    var validateForm2;
    jQuery.validator.addMethod("percent", function(value, element) {
        var length = value.length;
        var phone = /^(100|[1-9]?\d(\.\d\d?)?)%$/;
        return this.optional(element) || (phone.test(value));
    }, "请填写正确的百分数");
    $(document).ready(function() {
        validateForm = $("#subForm").validate({
            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);
                }
            }
        });

       validateForm2 = $("#termForm").validate({
            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: '#signDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
            event: 'focus' //响应事件。如果没有传入event，则按照默认的click
        });
        laydate.render({
            elem: '#effectDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
            event: 'focus' //响应事件。如果没有传入event，则按照默认的click
        });
        laydate.render({
            elem: '#startWorkDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
            event: 'focus' //响应事件。如果没有传入event，则按照默认的click
        });
        laydate.render({
            elem: '#endWorkDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
            event: 'focus' //响应事件。如果没有传入event，则按照默认的click
        });
        laydate.render({
            elem: '#startDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
            event: 'focus' //响应事件。如果没有传入event，则按照默认的click
        });
        laydate.render({
            elem: '#endDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
            event: 'focus' //响应事件。如果没有传入event，则按照默认的click
        });

        var data = ${fns:toJson(workContentContractinfo.termList)};
        if(data!=null) {
            for (var i = 0; i < data.length; i++) {
                addRowTerm('#termTableList', termIdx, termTpl, data[i]);
            }
        }
    });

    function addRowTerm(list, idx, tpl, row){
        bornTemplete(list, idx, tpl, row, idx);
        termIdx +=1;
    }

    function bornTemplete(list, idx, tpl, row, idx1){
        var idx1 = $("#termTableList tr").length +1;
        $(list).append(Mustache.render(tpl, {
            idx: idx, delBtn: true, row: row,
            order:idx1 + 1, idx1:idx1
        }));
        $(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,idx){
        var id = $(prefix+"_id");
        var delFlag = $(prefix+"_delFlag");
        if(id.val()==null||id.val()==''){
            $(obj).parent().parent().remove();
        }else {
            delFlag.val("1");
            $(obj).parent().parent().hide();
        }
    }

    function genDetailStr(){
        var contractJson = $("#subForm").serializeJson();
        var termJson = genTermJson();
        if(termJson!=null){
            console.log(termJson);
            contractJson["termList"]=JSON.parse(termJson);
        }
        console.log(contractJson);
        return JSON.stringify(contractJson);
    }

    function genTermJson(){
        var costList = $("#termTableList tr");
        if(costList.length==0){
            return null;
        }
        var contentStr = "[";
        for(var j=0; j<costList.length;j++){
            contentStr+="{"
            var idstr = $(costList[j]).attr("id");
            var detail = $("tr[id="+idstr+"] .contentDetail");
            for(var i=0; i<detail.length;i++){
                contentStr+='"'
                contentStr+=$(detail[i]).attr("subName");
                contentStr+='"'
                contentStr+=":";
                contentStr+='"'
                contentStr+=$(detail[i]).val();
                contentStr+='"'
                if(i!=detail.length-1){
                    contentStr+=","
                }
            }
            contentStr+="}"
            if(j!=costList.length-1){
                contentStr+=","
            }
        }
        contentStr += "]";
        return contentStr;
    }
</script>