@extends('admin.layouts.base')

@section("styles")
    <script src="/admin/js/search.js"></script>
@endsection

@section('contents')
    @if (false)
<script type="text/javascript">
$(document).on('change', '[name^=address2]', function(){
        var cityId = $(this).val();
        var obj = $(this);
        var prefId = $('[name^=address1]').val();
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type: 'POST',
            datatype: 'json',
            url: '/admin/city_search',
            data: {
                prefId: prefId,
                cityId: cityId
            }
        })
        .done(function(data){ //ajaxの通信に成功した場合
            // alert("success!");
            $(obj).next('[name^=address3]').children('option').remove();
            $(obj).next('[name^=address3]').append($('<option>').text('町村を選択').attr('value', ''));
            $.each(data[0], function(row, value){
                $(obj).next('[name^=address3]').append($('<option>').text(value['name']).attr('value', value['code']));
            });

            $('[name=primary_school]').children('option').remove();
            $('[name=primary_school]').append($('<option>').text('小学校区を選択').attr('value', ''));
            $.each(data[1], function(row, value){
                $('[name=primary_school]').append($('<option>').text(value['name']).attr('value', value['code']));
            });

            $('[name=secondary_school]').children('option').remove();
            $('[name=secondary_school]').append($('<option>').text('中学校区を選択').attr('value', ''));
            $.each(data[2], function(row, value){
                $('[name=secondary_school]').append($('<option>').text(value['name']).attr('value', value['code']));
            });

        })
        .fail(function(data){ //ajaxの通信に失敗した場合
            alert("error!");
        });
    });
</script>
    @endif
<div class="mt5">
    @include('admin.objects.components.hp-tab', ['title' => 'HP管理ーおすすめ物件', 'type' => 'recommend'])
    @include('admin.objects.components.regist-band', ['title' => 'おすすめ物件一覧'])
    <form method="POST" action="{{ route('recommend-regist') }}#recommend_list" enctype="multipart/form-data" id="form-forsale">
    {{ csrf_field() }}
        @include('admin.objects.projects.search-common-multi', ['isForSale' => true, 'isregistRecommend'=> true])
        @include('admin.objects.projects.search-footer-recommend')
        @include('admin.objects.components.hidden', ['name' => 'recommend', 'value' => 2])

@if(isset($recommend))
    <div id="recommend_list" class="recommend_list">

        @include('admin.objects.components.hidden', ['name' => 'remaining', 'value' => $remaining])
        <div class="d-flex justify-content-between pager mt10">
            <div class="m-2"><span class="pager-total">検索結果&nbsp;{{$recommend->total()}}</span><span>件中&nbsp; {{$recommend->first}}〜{{$recommend->last}}件表示</span></div>
            <div class="mt-2">{!!$recommend->appends($con)->fragment("recommend_list")->render()!!}</div>

        </div>
        <div>
            <a href="javascript:void(0);" name="recommend_save" class="recommend_save_btn">チェックした物件を登録する</a>
        </div>
        <table>
            <tr>
                <th class="" rowspan="2"></th>
                <th class="w150">
                    <input type="hidden" name="sort_type" value="{{ !empty(request("sort_type")) ? request("sort_type") : "desc"}}">
                    <div class="sort-link" sort="id">物件番号
                        @if (!empty(request("sort_recommend")["building_id"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["building_id"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[building_id]">
                    </div>
                </th>
                <th class="" colspan="2">
                    <div class="sort-link" sort="name">物件名
                        @if (!empty(request("sort_recommend")["name"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["name"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[name]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="location">所在地
                        @if (!empty(request("sort_recommend")["location"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["location"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[location]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="price">価格
                        @if (!empty(request("sort_recommend")["price"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["price"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[price]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="land_area">土地面積
                        @if (!empty(request("sort_recommend")["land_area_val"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["land_area_val"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[land_area_val]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="school">小学校区/中学校区
                        @if (!empty(request("sort_recommend")["school"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["school"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[school]">
                    </div>
                </th>
            </tr>
            <tr>
                <th class="">
                    <div class="sort-link" sort="registration_date">物件登録日
                        @if (!empty(request("sort_recommend")["created_at"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["created_at"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[created_at]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="property">物件種別
                        @if (!empty(request("sort_recommend")["property"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["property"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[property]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="floor_plan">間取り
                        @if (!empty(request("sort_recommend")["floor_plan"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["floor_plan"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[floor_plan]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="traffic">交通
                        @if (!empty(request("sort_recommend")["traffic"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["traffic"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[traffic]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="year_month">築年月
                        @if (!empty(request("sort_recommend")["year_month"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["year_month"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[year_month]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="total_area">延床面積
                        @if (!empty(request("sort_recommend")["total_area_val"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["total_area_val"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[total_area_val]">
                    </div>
                </th>
                <th class="">
                    <div class="sort-link" sort="whereabouts">階/階建
                        @if (!empty(request("sort_recommend")["whereabouts"]) && request("sort_type") == "desc" )
                            <i class="fa fa-long-arrow-down" aria-hidden="true"></i>
                        @endif
                        @if (!empty(request("sort_recommend")["whereabouts"]) && request("sort_type") == "asc" )
                            <i class="fa fa-long-arrow-up" aria-hidden="true"></i>
                        @endif
                        <input type="hidden" name="sort_recommend[whereabouts]">
                    </div>
                </th>
            </tr>
            @foreach($recommend as $item)
            <tr class="@if($loop->iteration % 2 == 0) even @else odd @endif">
                <td rowspan="2">
                @include('admin.objects.components.checkbox', ['name' => 'recommend_target[]', 'checkbox' => [['code' => $item->building_id, 'name' => '']], 'nocheck' => true])
                </td>
                <td>物件番号<br><a class="target_link_id" href="
                    @if($item->property == null)
                    /admin/editRoom/{{$item->mansion_id}}/{{$item->building_id}}
                    @elseif($item->property < 4)
					/admin/editLand/{{$item->building_id}}
                    @elseif($item->property < 6)
                    /admin/editHouse/{{$item->building_id}}
                    @elseif($item->property < 8)
                    /admin/editMansion/{{$item->building_id}}
                    @elseif($item->property == 99)
                    /admin/editLot/{{$item->building_id}}
					@endif
                ">{{$item->building_id}}</a></td>
                <td colspan="2">{{$item->name}}</td>
                <td>{{$item->address_info }}</td>
                <td><span class="price_text">{{number_format($item->price, 0, ",", ",")}}</span><span>万円</span><br />{{ $item->tax == 2 ? ' (税込)' : null }}</td>
                <td>{{$item->land_area_val?? "-"}}m&sup2;</td>
                <td>{{$item->primary_school_name?? "-"}}/{{$item->secondary_school_name?? "-"}}</td>
            </tr>
            <tr class="@if($loop->iteration % 2 == 0) even @else odd @endif">
                <td>物件登録日<br />{{date('Y/m/d', strtotime($item->created_at))}}</td>
                <td>
                    @if($item->property != 1 && $item->property != 2 && $item->property != 3)
                        @if(isset($item->property_sub) && $item->property_sub == 1)
                            新築
                        @elseif(isset($item->property_sub) && $item->property_sub == 2)
                            中古
                        @endif
                    @endif

                    @if(isset($item->property) && $item->property == 1)
                        土地
                    @elseif(isset($item->property) && $item->property == 2)
                        土地（借地権）
                    @elseif(isset($item->property) && $item->property == 3)
                        土地（底地権）
                    @elseif(isset($item->property) && $item->property == 4)
                        戸建
                    @elseif(isset($item->property) && $item->property == 5)
                        テラスハウス
                    @elseif(isset($item->property) && $item->property == 6)
                        マンション
                    @elseif(isset($item->property) && $item->property == 7)
                        タウンハウス
                    @elseif(isset($item->m_property) && $item->m_property == 7)
                        タウンハウス
                    @else
                        マンション
                    @endif
                </td>
                <td>{{$item->floor_plan_text?? "-"}}</td>
                <td>{{$item->main_traffic_line}}「{{$item->main_traffic_station}}」徒歩{{$item->main_traffic_time??'-'}}分</td>
                <td>{{$item->age_year?? "-"}}年{{$item->age_month?? "-"}}月</td>
                <td>{{$item->total_area_val?? "-"}}m&sup2;</td>
                <td>
                    {{$item->whereabouts}}階/{{$item->floor}}階建
                </td>
            </tr>
            @endforeach
        </table>
        <div>
            <a href="javascript:void(0);" name="recommend_save" class="recommend_save_btn">チェックした物件を登録する</a>
        </div>
        <div class="d-flex justify-content-between pager mt10">
            <div class="m-2"><span class="pager-total">検索結果&nbsp;{{$recommend->total()}}</span><span>件中&nbsp; {{$recommend->first}}〜{{$recommend->last}}件表示</span></div>
            <div class="mt-2">{!!$recommend->appends($con)->fragment("recommend_list")->render()!!}</div>

        </div>
    </div>
@endif
    </form>
</div>
<input type="hidden" id="address3" value="{{ isset($con["address3"]) ? array_first($con["address3"]) : "" }}">
<input type="hidden" id="primary_school" value="{{ isset($con["primary_school"]) ? array_first($con["primary_school"]) : "" }}">
<input type="hidden" id="secondary_school" value="{{ isset($con["secondary_school"]) ? array_first($con["secondary_school"]) : "" }}">
<input type="hidden" id="access2" value="{{ isset($con["access2"]) ? array_first($con["access2"]) : "" }}">
<input type="hidden" id="access3" value="{{ isset($con["access3"]) ? array_first($con["access3"]) : "" }}">
<script src="{{ asset("admin/js/fillable_search.js") }}"></script>
<script src="{{ asset("admin/js/forsale_address.js") }}"></script>

<script>
    $(".sort-link").click(function () {
        var action = $("#form-forsale").attr("action");
        var url = window.location.href;
        action = action.replace("#recommend_list", "");
        $(this).find("input").val($(this).attr("sort"));
        if (url.indexOf("sort="+$(this).attr("sort")) !== -1) {
            var type = $("input[name='sort_type']").val();
            if (type == "desc") {
                $("input[name='sort_type']").val("asc");
            } else {
                $("input[name='sort_type']").val("desc");
            }
        } else {
            $("input[name='sort_type']").val("desc")
        }
        action = action + "?sort=" + $(this).attr("sort") + "&" + $("input[name='sort_type']").val();
        action = action + "#recommend_list";

        $("#form-forsale").attr("action", action);
        $("#form-forsale").submit();
    });
</script>
@endsection
