<div class="wrap">
    <h2>客户列表</h2>
    <form method="get">
        <input type="hidden" name="page" value="customerList">
        <div id="search_form">
            <input type="hidden" name="hasSearch" value="1"/>
            <label for="status" class="hidden"></label>
            <select name="status" id="status">
                <option value="-1">状态</option>
                {% for item in status %}
                <option {{ item.id == status_id ?"selected":"" }} value="{{ item.id }}">{{ item.value }}</option>
                {% endfor %}
            </select>
            <label for="customer_type" class="hidden"></label>
            <select name="customer_type" id="customer_type">
                <option value="-1">客户类型</option>
                {% for item in customer_type %}
                <option {{ item.id == customer_type_id ?"selected":"" }} value="{{ item.id }}">{{ item.value }}</option>
                {% endfor %}
            </select>
            <label for="demand_type" class="hidden"></label>
            <select name="demand_type" id="demand_type">
                <option value="-1">需求类型</option>
                <option value="0">新房</option>
                <option value="1">二手房</option>
                <option value="2">租房</option>
            </select>
            <label for="listCity" class="hidden"></label>
            <select id="listCity" name="listCity">
                <option value="-1"> 城市</option>
                {% for item in city %}
                <option {{ item.id == cityId ?"selected":"" }} value="{{ item.id }}">{{ item.value }}</option>
                {% endfor %}
            </select>
            <label for="listDistrict" class="hidden"></label>
            <select id="listDistrict" name="listDistrict">
                <option value="-1">区域</option>
                {% if district %}
                {% for item in district %}
                <option {{ item.id == districtId ?"selected":"" }} value="{{ item.id }}">{{ item.value }}</option>
                {% endfor %}
                {% endif %}
            </select>
            <label for="listPlate" class="hidden"></label>
            <select id="listPlate" name="listPlate">
                <option value="-1">板块</option>
                {% if plate %}
                {% for item in plate %}
                <option {{ item.id == plateId ?"selected":"" }} value="{{ item.id }}">{{ item.value }}</option>
                {% endfor %}
                {% endif %}
            </select>
            <label for="totalPrice" class="hidden"></label>
            <select id="totalPrice" name="totalPrice" class="hidden">
                <option value="">总价</option>
                {% if dicTotalPrice %}
                {% for item in dicTotalPrice %}
                <option {{ item.value == totalPrice ?"selected":"" }} value="{{ item.value }}">{{ item.value }}</option>
                {% endfor %}
                {% endif %}
            </select>
            <label for="rentalPrice" class="hidden"></label>
            <select id="rentalPrice" name="rentalPrice" class="hidden">
                <option value ="">月租</option>
                {% if dicRentalPrice %}
                {% for item in dicRentalPrice %}
                <option {{ item.value == rentalPrice ?"selected":"" }} value="{{ item.value }}">{{ item.value }}</option>
                {% endfor %}
                {% endif %}
            </select>
            <label for="buildProperty" class="hidden"></label>
            <select id="buildProperty" name="buildProperty">
                <option value="-1">房型</option>
                {% for item in buildProperty %}
                <option {{ item.id == buildPropertyId ?"selected":"" }} value="{{ item.id }}">{{ item.value }}</option>
                {% endfor %}
            </select>
            <label for="acreage" class="hidden"></label>
            <select id="acreage" name="acreage">
                <option value="">面积</option>
                {% if acreage %}
                {% for item in dicArea %}
                <option {{ item.value == acreage ?"selected":"" }} value="{{ item.value }}">{{ item.value }}</option>
                {% endfor %}
                {% endif %}
            </select>
        </div>
        <div style="margin-top: 10px;">
            <input type="text" placeholder="请输入姓名" name="search_name" value="{{ search_name }}">
            <input type="text" placeholder="请输入置业顾问" name="search_consultant_name" value="{{ search_consultant_name }}">
            <input type="text" placeholder="请输入电话" name="search_phone" value="{{ search_phone }}">
        </div>
        <div style="margin-top: 10px;">
            <span>日期</span>
            <label for="search_min_time" class="hidden"></label>
            <input type="date" name="search_min_time" id="search_min_time" value="{{ search_min_time }}">
            —
            <label for="search_max_time" class="hidden"></label>
            <input type="date" name="search_max_time" id="search_max_time" value="{{ search_max_time }}">
            <input type="submit" id="submit" class="button action" value="搜索">
        </div>
    </form>
    <form method="post">
        {{ function("addCustomerTable") }}
        <input type="hidden" name="allot_consultant_id" id="allot_consultant_id">
    </form>
</div>
{% include 'recConsultant.html' %}
{{ block('recConsultant') }}
<script>
    $(document).ready(function () {
        var demand_type_select = $('#demand_type');
        demand_type_select.val('{{ demand_type }}');
        search_form_set_page();

        var acreage = $("#acreage");
        var totalPrice = $("#totalPrice");
        var rentalPrice = $("#rentalPrice");
        var listCity = $('#listCity');
        listCity.change(function () {
            var cityId = $('#listCity').val();
            setCity('listCity', 'listDistrict', 'listPlate');
            acreage.find('option:not(:first-child)').remove();
            totalPrice.find('option:not(:first-child)').remove();
            rentalPrice.find('option:not(:first-child)').remove();
            //面积
            searchAcreage(cityId, acreage);
            setTotalOrRentalPrice(demand_type_select, cityId);
        });
        //设置区域
        $('#listDistrict').change(function () {
            setArea('listCity', 'listDistrict', 'listPlate');
        });

        demand_type_select.change(function () {
            totalPrice.find('option:not(:first-child)').remove();
            rentalPrice.find('option:not(:first-child)').remove();
            setTotalOrRentalPrice($(this), listCity.val());
        });

        setTotalOrRentalPrice(demand_type_select, listCity.val());

        function setTotalOrRentalPrice(self, cityId) {
            var demand_type = self.val();
            if (demand_type == 0 || demand_type == 1) {
                totalPrice.removeClass('hidden');
                rentalPrice.addClass('hidden');
                searchTotalPrice(cityId, totalPrice);
            } else if (demand_type == 2) {
                rentalPrice.removeClass('hidden');
                totalPrice.addClass('hidden');
                searchRentalPrice(cityId, rentalPrice);
            }else{
                rentalPrice.addClass('hidden');
                totalPrice.addClass('hidden');
            }
        }

        //批量操作
        $('select[name=action]').change(function(){
            //分配置业顾问
            if ($(this).find('option:selected').val() == 'allot') {
                //选择房/客源
                var checkbox = $('input[name="customerlist[]"]');
                if(checkbox.is(':checked')){
                    var array = [];
                    $('input[name="customerlist[]"]:checked').map(function() {
                        var consultant_id = Number($(this).data('consultant'));
                        if (array.indexOf(consultant_id) == -1) {
                            array.push(consultant_id);
                        }
                    });
                    var confirmFlag = true;
                    if(array.length>1){
                        confirmFlag = confirm('当前选择的房/客源置业顾问不同，是否继续');
                    }
                    if(confirmFlag){
                        $('#myConsultant').modal('show');
                    }
                //未选择房/客源，下拉框显示批量操作
                }else{
                    $(this).find('option:first').attr('selected', 'selected');
                    alert('请选择房/客源');
                    return false;
                }
            }
        });
        init_modal_myConsultantList(function (data) {
            var consultant_id = Number(data.id);
            $('#allot_consultant_id').val(consultant_id);
        });

        $("#myConsultant").on("hide.bs.modal", function () {
            var allot_consultant_id = Number($('#allot_consultant_id').val());
            if (!allot_consultant_id) {
                $('select[name=action]').find('option:first').attr('selected', 'selected');
            }
        });
    });
</script>