@extends('admin.layouts.base')

@section('contents')

<div class="mt5">
    <div class="d-flex justify-content-between pager mt10">
        <p class="col-8 c-tab__title">街の人の声</p>
    </div>
    @include('admin.objects.components.regist-band', ['title' => '街の人の声'])
    <div class="search_vendor">
        <form method="POST" action="{{ route('town-voice') }}" id="test">
            {{ csrf_field() }}
            <dl class="d-flex search_vendor__con">
                <dt>店舗</dt>
                <dd>@include('admin.objects.components.selectbox', ['name' => 'store_id', 'id' =>'loadStaff', 'class' =>
                    'w200', 'options'
                    => $store])</dd>
                <dt class="ml-5">担当</dt>
                <dd>@include('admin.objects.components.selectbox', ['name' => 'staff', 'class' => 'w200', 'options'
                    => $staffs])</dd>
            </dl>
            <dl class="d-flex search_vendor__con">
                <dt>住所</dt>
                <dd>
                    @include('admin.objects.projects.form-address-town-voice')
                </dd>
            </dl>

            <div class="search_vendor__btn">
                @include('admin.objects.components.btn', ['title' => '検索', 'class'=> 'btn_search w120'])
                <a href="/admin/townVoiceCreate" class="btn_imagesave w120 store_new_btn">新規登録</a>
                <a href="javascript:void(0);" class="facility_clear">条件をクリア</a>
            </div>
        </form>
    </div>
    @if(count($voice_list) > 0)
    @foreach($voice_list as $voices)
    <div class="user_list mt-5">
        <div class="mb10">
            @include('admin.objects.components.regist-band', ['title' => !empty($voices[0]->relationStore->name) ?
            $voices[0]->relationStore->name : '店舗未分類'])
        </div>
        <table>
            <tr>
                <th class="w50">ID</th>
                <th class="w125">お客様名</th>
                <th class="w150">担当店舗</th>
                <th class="w150">担当者</th>
                <th class="w300">住所</th>
                <th class="w125">公開</th>
                <th></th>
            </tr>
            <tbody>
                @foreach($voices as $item)
                <tr data-id="{{ $item['id'] }}">
                    <td name="voice_id">{{$item->id}}</td>
                    <td>{{ $item->name }}</td>
                    <td>{{ !empty($item->relationStore->name) ? $item->relationStore->name : '' }}</td>
                    <td>{{ !empty($item->relationStaff->name) ? $item->relationStaff->name: '' }}</td>
                    <td>{{$item['pref_name']}}{{$item['city_name']}}{{$item['town_name'].$item['chome_name'].$item['koaza_name']}}</td>
                    <td class="text-center">
                        @include('admin.objects.components.hidden', ['name' => 'voice_id', 'value' => $item->id])
                        @include('admin.objects.components.switch', ['name' => 'voice_kokai', 'checked' =>
                        $item['kokai']])
                    </td>
                    @if($user->role == 1)
                    <td>
                        <div class="d-flex">
                            @include('admin.objects.components.link-btn-nostyle', ['name' => 'edit_voice', 'url' =>
                            '/admin/townVoiceEdit/'.$item->id, 'class' => 'edit_user_btn', 'title' => '編集'])
                            @include('admin.objects.components.link-btn-nostyle', ['name' => 'delete_voice', 'url' =>
                            'javascript:void(0);', 'class' => 'delete_voice del_user_btn', 'title' => '削除'])
                        </div>
                    </td>
                    @endif
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    @endforeach
</div>

@endif
</div>
@endsection

@section('footer-script')
<script type="text/javascript">
$('[name=voice_kokai]').on('change', function() {
    var id = $(this).parents('td').find('[name=voice_id]').val();
    var check = 0;
    if ($(this).prop('checked')) {
        check = 1;
    }
    $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type: 'POST',
            url: '/admin/townVoice_kokai',
            data: {
                id: id,
                check: check,
            }
        })
        .done(function(data) { //ajaxの通信に成功した場合

        })
        .fail(function(data) { //ajaxの通信に失敗した場合
            alert("error!");
        });
});

$(".delete_voice").on('click', function() {
    if (confirm('本当に削除しますか？')) {
        var id = $(this).parents('tr').find('[name=voice_id]').text();
        $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/deleteTownVoice',
                data: {
                    id: id,
                }
            })
            .done(function(data) {
                location.reload();
            })
            .fail(function(data) {
                alert("error!");
            });
    }
});
</script>
@endsection
