<div id="preview">
    <div class="row">
        <div class="col-md-2">
            <label for="traffic" style="margin-left: 30px">主力房源:</label>
        </div>
        <div class="col-md-10">
            {% set exists_ids = "" %}
            {% for item in mainImage %}
            {% if exists_ids != "" %}
            {% set exists_ids = exists_ids~"," %}
            {% endif %}
            {% set exists_ids = exists_ids~item.id %}
            <div class="row" style="margin-top:5px">
                <div class="col-md-3">
                    <img src="{{siteUrl}}{{item.path}}" height="90" width="140">
                </div>
                <div class="col-md-3">
                    <select name="exists[{{item.id}}][buildProperty]" class="form-control" style="height: 33px;width: 80%">
                        {% for i in buildProperty %}
                        <option {{ i.id == item.buildproperty_id?"selected":"" }} value="{{i.id}}">{{i.value}}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <input type="text" placeholder="面积" name="exists[{{item.id}}][housearea]" class="form-control" value="{{item.area}}" style="width: 80%">
                </div>
                <div class="col-md-3">
                    <input type="button" value="删除" class="button action cancel existsCancel">
                </div>
            </div>
            {% endfor %}
            <input type="file" name="files[0]" property="0" class = "files form-control"multiple style="width: 30%;margin-top: 5px">
            <div id="mainHouseDiv"></div>
            <input type="hidden" name="exists_ids" value="{{exists_ids}}" >
        </div>
    </div>
</div>

<script>
    (function($){
        $(document).ready(function(){
            $("form").on("change",".files",function(){
                mainHouse(this,i);
                $(this).hide();
                i++;
            });

            $("#preview").on("click",".cancel",function(){
              $(this).parent().parent().remove();
            });
        });
        //显示主力户型
        function mainHouse(input,i){
            var reader = new FileReader();
            reader.onload = function (e) {
                var div1 = $("<div>").addClass("col-md-3");
                var img = $("<img>").attr({"id":"target","src":e.target.result,"height":90,"width":140});
                div1.append(img);
                var div2 = $("<div>").addClass("col-md-3");
                var select = $("<select>").attr({"name":"data["+i+"][buildProperty]","style":"height:33px;width:80%"}).addClass("form-control");
                div2.append(select);
                var div3 = $("<div>").addClass("col-md-3");
                var areatext = $("<input>").attr({"type":"text","placeholder":"面积","name":"data["+i+"][housearea]","style":"width:80%"}).addClass("form-control");
                div3.append(areatext);
                var div4 = $("<div>").addClass("col-md-3");
                var button = $("<input>").attr({"type":"button","value":"删除","property":+i,"id":+i}).addClass("button action cancel");
                div4.append(button);
                var type = $("<input>").attr({"type":"hidden","name":"data["+i+"][type]","value":4,"property":+i});
                var mainHousePic = $("<input>").attr({"type":"hidden","name":"data["+i+"][mainHouse]","value":0,"property":+i});
                var file = $("<input>").attr({"type":"file","name":"files["+(i+1)+"]","property":+(i+1)}).addClass("files form-control").css({"width":"30%","margin-top":"10px"});

                {% for item in buildProperty %}
                    select.append($("<option>").attr("value",{{item.id}}).append('{{item.value}}'));
                {% endfor%}

                var div = $("<div>").attr({"property":+i,"style":"margin-top:5px"}).addClass("row").append(div1).append(div2).append(div3).append(div4).append(type).append(mainHousePic);
                $("form").find("#mainHouseDiv").after(file);
                $("#mainHouseDiv").append(div);

            }
            reader.readAsDataURL(input.files[0]);
        }
    })(jQuery);
</script>