<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>

<div class="form-group layui-row lw12">
    <div class="layui-item layui-col-sm12">
        <label class="layui-form-label"><span class="require-item">*</span>清单类型:</label>
        <div class="layui-input-block">
            <select id="inventoryType" subName="inventoryType" class="form-control layui-input required">
                <option value=""></option>
                <option value="01" <c:if test="${ctrlpricelist[0].inventoryType == '01'}">selected</c:if>>工程量清单</option>
                <option value="02" <c:if test="${ctrlpricelist[0].inventoryType == '02'}">selected</c:if>>工程量清单计价</option>
            </select>
        </div>
    </div>
</div>
<form id="subForm"  action="#" onsubmit="return false;" method="post" class="form-horizontal">
    <div class="layui-item nav-btns" style="padding-left:0px;">
        <a class="nav-btn nav-btn-add" onclick="addRow('#ctrlpriceList',ctrlpriceIdx,ctrlpriceTpl)" title="新增"><i class="fa fa-plus"></i> 新增</a>

        <table:importExcelCost url="${ctx}/workcontent/workContentCtrlprice/import"></table:importExcelCost><!-- 导入按钮 -->
    </div>

	<!-- 表格 -->
	<table id="contentTable" class="table table-bordered table-condensed can-edit">
		<thead>
			<tr>
				<th class="hide"></th>
				<th>编号</th>
				<th>工程费用名称</th>
				<th>金额</th>
				<th>安全文明施工费</th>
				<th>规费</th>
				<th>暂估价</th>
				<th>节点类型</th>
                <th>备注</th>
				<th width="120">操作</th>
			</tr>
		</thead>
		<tbody id="ctrlpriceList">
		</tbody>
	</table>

        <script type="text/template" id="ctrlpriceTpl">//<!--
        <tr id="ctrlpriceList{{idx}}">
            <td class="hide">
                <input id="ctrlpriceList{{idx}}_id" subName="id" type="hidden" value="{{row.id}}" class="contentDetail"/>
                <input id="ctrlpriceList{{idx}}_delFlag" subName="delFlag" type="hidden" value="0" class="contentDetail"/>
            </td>
            <td>
                <input id="ctrlpriceList{{idx}}_ctrlNum" subName="ctrlNum" value="{{row.ctrlNum}}" type="text" class="form-control contentDetail"/>
            </td>
            <td>
                <input id="ctrlpriceList{{idx}}_costsubName" subName="costName" value="{{row.costName}}" type="text" class="form-control contentDetail"/>
            </td>
            <td>
                <input id="ctrlpriceList{{idx}}_sum" subName="sum" type="text" value="{{row.sum}}" onchange="formatNum(this);" class="form-control number contentDetail money"/>
            </td>
            <td>
                <input id="ctrlpriceList{{idx}}_constructionCost" subName="constructionCost" type="text" value="{{row.constructionCost}}" onchange="formatNum(this);" class="form-control number contentDetail money"/>
            </td>
            <td>
                <input id="ctrlpriceList{{idx}}_fees" subName="fees" type="text" value="{{row.fees}}" onchange="formatNum(this);" class="form-control number contentDetail money"/>
            </td>
            <td>
                <input id="ctrlpriceList{{idx}}_estimate" subName="estimate" type="text" value="{{row.estimate}}" onchange="formatNum(this);" class="form-control number contentDetail money"/>
            </td>
            <td>
             <select style="width:80px;" id="ctrlpriceList{{idx}}_nodeTypes" subName="nodeTypes" class="form-control contentDetail">
			 <option value =""></option>
             <c:if test="${ not empty nodeList}">
				<c:forEach items="${nodeList}" var="nodeType" varStatus="index">
				<option value="${nodeType.value}">${nodeType.label}</option>
				</c:forEach>
			</c:if>
			</select>
            </td>
            <td>
                <input id="ctrlpriceList{{idx}}_remarks" subName="remarks" type="text"  value="{{row.remarks}}" class="form-control contentDetail"/>
            </td>
            <td class="text-center op-td">
                        <a href="#" onclick="delRow(this, '#ctrlpriceList{{idx}}')"   class="op-btn op-btn-delete"><i class="fa fa-trash"></i> 取消</a>
            </td>
        </tr>//-->
        </script>
        <script type="text/javascript">
            var ctrlpriceIdx = 0, ctrlpriceTpl = $("#ctrlpriceTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
            $(function(){
                    var data = ${fns:toJson(ctrlpricelist)};
                    if(data!=null) {
                        for (var i = 0; i < data.length; i++) {
                            addRow('#ctrlpriceList', ctrlpriceIdx, ctrlpriceTpl, data[i]);
                        }
                    }
                $(":input.money").change();
            });

            function addRow(list, idx, tpl, row){
                bornTemplete(list, idx, tpl, row, idx);
                ctrlpriceIdx +=1;
            }

            function bornTemplete(list, idx, tpl, row, idx1){
                var idx1 = $("#ctrlpriceList 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 genRow(data) {
                for (var i = 0; i < data.length; i++) {
                    addRow('#ctrlpriceList',ctrlpriceIdx,ctrlpriceTpl,data[i]);
                    $("#ctrlpriceList"+i+"_nodeTypes").val(data[i].nodeTypes);
                }
                $(":input.money").change();
            }

            function genDetailStr(){
                var inventoryType = $("#inventoryType").val();
                var costList = $("#ctrlpriceList tr");
                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+="'"
                        contentStr+=","
                    }
                    contentStr+="'inventoryType':'";
                    contentStr+=inventoryType;
                    contentStr+="'"
                    contentStr+="}"
                    if(j!=costList.length-1){
                        contentStr+=","
                    }
                }
                contentStr += "]";
                return contentStr;
            }
        </script>
</form>