@extends('layouts.app')
@section('title', 'デバイス管理マスタ')

@section('content')
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-lg-6"><h1 class="m-0 text-dark">{{ __('デバイス管理マスタ') }}</h1></div>
      <div class="col-lg-6">
        <ol class="breadcrumb float-sm-right text-sm">
          <li class="breadcrumb-item"><a href="{{ route('home') }}">ホーム</a></li>
          <li class="breadcrumb-item active">{{ __('デバイス管理マスタ') }}</li>
        </ol>
      </div>
    </div>
  </div>
</div>

<section class="content">
  <div class="container-fluid">

    {{-- ソート用フォーム --}}
    <form action="{{ route('devices') }}" method="post" id="list-form" class="d-none">
      @csrf
      <input type="hidden" name="sort" id="sort" value="{{ $sort }}">
      <input type="hidden" name="sort_type" id="sort_type" value="{{ $sort_type }}">
    </form>

      <!-- ツールバー -->
      <div class="container-fluid mb20 d-flex justify-content-between align-items-center">
        <div>
          <button type="button" class="btn btn-sm btn-default mr10" onclick="location.href='{{ route('devices_add') }}'">{{ __('新規') }}</button>
          <button type="button" class="btn btn-sm btn-default mr10" id="delete">{{ __('削除') }}</button>
        </div>
        <div>
          {{ $list->appends(['sort'=>$sort,'sort_type'=>$sort_type])->links('pagination') }}
        </div>
      </div>

      {{-- フラッシュ --}}
      <div class="form col-lg-12">
        @if(Session::has('success'))
          <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            {{ Session::get('success') }}
          </div>
        @elseif(Session::has('error'))
          <div class="alert alert-danger alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon fa fa-ban"></i> {{ __('入力内容に不備があります') }}:</h4>
            {!! Session::get('error') !!}
          </div>
        @elseif(isset($errorMsg))
          <div class="alert alert-danger alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon fa fa-ban"></i> {{ __('入力内容に不備があります') }}:</h4>
            {!! $errorMsg !!}
          </div>
        @endif
      </div>

      {{-- 単一テーブル --}}
      <div class="col-lg-12 mb20">
        <div class="table-responsive">
          <form action="{{ route('devices_delete') }}" method="post" id="form_delete">
            @csrf
            @php
              $TYPE = [1=>'サーバー', 2=>'プリンタ', 3=>'その他'];
              $WORK = ['1'=>'稼働', '0'=>'停止', 1=>'稼働', 0=>'停止'];
            @endphp

            <table class="table table-bordered dataTable text-nowrap">
              <thead class="thead-light">
                <tr>
                  <th style="width:120px;" class="text-left">
                    <input type="checkbox" onclick="$('input[name*=\'ids\']').prop('checked', this.checked);">
                  </th>
                  <th class="sorting @if($sort=='device_id'){{ $sort_type=='asc'?'sorting_asc':'sorting_desc' }}@endif text-right"
                      sort="device_id"><span>{{ __('デバイスID') }}</span></th>

                  <th class="sorting @if($sort=='park_id'){{ $sort_type=='asc'?'sorting_asc':'sorting_desc' }}@endif text-left"
                      sort="park_id"><span>{{ __('駐輪場ID') }}</span></th>

                  <th class="sorting @if($sort=='device_type'){{ $sort_type=='asc'?'sorting_asc':'sorting_desc' }}@endif text-left"
                      sort="device_type"><span>{{ __('デバイス種別') }}</span></th>

                  <th><span>{{ __('デバイス名') }}</span></th>

                  <th><span>{{ __('識別子') }}</span></th>

                  <th class="sorting @if($sort=='device_work'){{ $sort_type=='asc'?'sorting_asc':'sorting_desc' }}@endif text-left"
                      sort="device_work"><span>{{ __('稼働停止') }}</span></th>

                  <th class="sorting @if($sort=='device_workstart'){{ $sort_type=='asc'?'sorting_asc':'sorting_desc' }}@endif text-right"
                      sort="device_workstart"><span>{{ __('稼働開始日') }}</span></th>
                  <th class="sorting @if($sort=='device_replace'){{ $sort_type=='asc'?'sorting_asc':'sorting_desc' }}@endif text-right"
                      sort="device_replace"><span>{{ __('リプレース予約日') }}</span></th>
                  <th class="text-left"><span>{{ __('備考') }}</span></th>
                </tr>
              </thead>
              <tbody class="bg-white">
                @foreach($list as $item)
                  <tr>
                    <td class="align-middle" style="background-color:#faebd7;">
                        <div class="d-flex align-items-center">
                            <input type="checkbox" class="m-0 checkbox" name="ids[]" value="{{ $item->device_id }}">
                            <a href="{{ route('devices_edit', ['id' => $item->device_id]) }}" class="btn btn-sm btn-default ml-2">{{ __('編集') }}</a>
                        </div>
                    </td>

                    <td class="sm-item text-right">{{ $item->device_id }}</td>
                    <td class="sm-item text-left">
                      @if($item->relationLoaded('park') && $item->park)
                        {{ $item->park->park_name }}
                      @endif
                    </td>
                    <td class="sm-item text-left">{{ $TYPE[$item->device_type] ?? $item->device_type }}</td>
                    <td class="sm-item text-left">{{ $item->device_subject }}</td>
                    <td class="sm-item text-left">{{ $item->device_identifier }}</td>
                    <td class="sm-item text-left">{{ $WORK[$item->device_work] ?? $item->device_work }}</td>
                    <td class="sm-item text-right">{{ optional($item->device_workstart)->format('Y/m/d') }}</td>
                    <td class="sm-item text-right">{{ optional($item->device_replace)->format('Y/m/d') }}</td>
                    <td class="sm-item text-left">{{ $item->device_remarks }}</td>
                  </tr>
                @endforeach
              </tbody>
            </table>
          </div>
        </div>
    </form>

  </div>
</section>

@endsection
