<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
  <title>SEO Keyword Update : {{config('dashboard.config.dashboard_name')}}</title>
  <meta name="csrf-token" content="{{ csrf_token() }}"><!-- for onchange/update-status -->
  @includeif('dashboard.partials.header')
</head>
<body class="{{config('dashboard.config.dashboard_css_body_class')}}"><!-- hold-transition dark-mode sidebar-mini layout-fixed layout-navbar-fixed layout-footer-fixed -->
<div class="wrapper">
    <!-- Preloader -->
    <div class="preloader flex-column justify-content-center align-items-center">
      <img class="animation__wobble" src="{{config('site.config.logo')}}" alt="{{config('site.config._sitename')}}" height="60" width="60">
    </div>

  <!-- Navbar -->
  <nav class="main-header navbar navbar-expand navbar-white navbar-light">
      @includeif('dashboard.partials.topnav')
  </nav>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
  <aside class="main-sidebar sidebar-dark-primary elevation-4">
      @includeif('dashboard.partials.sidebar')
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Your Domain List</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Domain List</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

      <div class="card">
        <div class="card-body">
          <div class="row">
            <div class="col-12 col-md-12 col-lg-12 order-2 order-md-1">

                <div class="card card-info">
                  <div class="card-header">
                    <h3 class="card-title">Edit your Keyword</h3>
                  </div>
                  <!-- /.card-header -->
                    @if ($errors->has('seoKeywordtxt') || $errors->has('websiteList'))
                    <div class="card-body">
                        <ul>
                            @foreach ($errors->all() as $error)
                                <li style="color:red; font-weight:bold;">{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                    @endif
                  <!-- form start -->
                  <form class="form-horizontal" action="{{ localized_route('dashboard.keywords.post') }}" method="post">
                    @csrf
                    <input type="hidden" name="userRole" value="{{ $userRole->id }}">
                    <input type="hidden" name="mode" value="update">
                    <input type="hidden" name="seoKeywordID" value="{{ $editKeywordInfo->seoKeywordID }}">
                    <div class="card-body">
                      <div class="form-group row">
                        <div class="col-sm-2">
                            <label for="inputEmail3" class="col-sm-1=2 col-form-label">Keyword : </label>
                        </div>
                        <div class="col-sm-8">
                          <input type="text" name="seoKeywordtxt" id="seoKeywordtxt" class="form-control" value="{{ $editKeywordInfo->seoKeywordtxt }}" placeholder="e.g. SEO Ezy Pro tool" required>
                        </div>
                      </div>
                      <div class="form-group row">
                        <div class="col-sm-2">
                            <label for="inputEmail3" class="col-sm-1=2 col-form-label">Status : </label>
                        </div>
                        <div class="col-sm-8">
                          <input name="status_flg" id="status_flg_main_0" class="statusSwitch" data-id="{{$editKeywordInfo->seoKeywordID}}" type="checkbox" {{ $editKeywordInfo->status_flg === 'A' ? 'checked' : '' }} data-bootstrap-switch data-off-color="danger" data-on-color="success"> (Disabling the keyword status will remove all keyword settings from the website.)
                        </div>
                      </div>
                      <div class="form-group row">
                        <div class="col-sm-2">
                            <label>Domain : </label>
                        </div>
                        <div class="col-sm-8">
                          <select name="websiteList[]" class="select2" multiple="multiple" data-placeholder="Select Domain" style="width: 100%;">
                                Select Keyword with Domain: (Please pick at least one domain to continue.)
                                @if($websitesList)
                                    @php
                                        $urlRelSelected = array_column($seoUrlRel ?? [], 'domain');
                                    @endphp
                                    @foreach($websitesList as $websiteKey=>$websiteInfo)
                                        <option value="{{ $websiteInfo->domain }}" {{ in_array($websiteInfo->domain, $urlRelSelected) ? 'selected' : '' }}>{{ $websiteInfo->domain }}</option>
                                    @endforeach
                                @else
                                    Please add your domain. Click <a href="{{ dashboard.websites }}">Add website</a>.
                                @endif
                          </select>
                        </div>
                      </div>
                    </div>
                    <!-- /.card-body -->
                    <div class="card-footer">
                      <button type="submit" class="btn btn-info">Submit</button>
                      <button type="reset" class="btn btn-danger">Reset</button>
                    </div>
                    <!-- /.card-footer -->
                  </form>
                </div>
              
              <!-- /.card-header -->
              <div class="card-body">
                <table id="keywordTable" class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <th>No.</th>
                    <th>Keyword</th>
                    <th>Domain</th>
                    <th>Created Date</th>
                    <th>Last usage</th>
                    <th>Status</th>
                    <th>Action</th>
                  </tr>
                  </thead>
                  <tbody>
                        @php
                            $indCnt = 1;
                        @endphp
                        @foreach($keywordList as $index=>$keywordInfo)
                        
                            <tr>
                              <td>{{ $indCnt }}</td>
                              <td>{{ $keywordInfo->seoKeywordtxt }}</td>
                              <td>
                                @if($keywordInfo->websiteRel !== null)
                                      @foreach($keywordInfo->websiteRel as $indexWebRel=>$websiteRel)
                                          {{
                                            $websiteRel->domain
                                          }}
                                      @endforeach
                                @else
                                  No website selected.
                                @endif
                              </td>
                              <td>{{ \App\Helpers\DateHelper::convertTimesamp2Datetime($keywordInfo->created_at, 'Normal') }}</td>
                              <td>{{ \App\Helpers\DateHelper::convertTimesamp2Datetime($keywordInfo->updated_at) }}</td>
                              <td>
                                  <input name="status_flg" id="status_flg_{{$indCnt}}" class="statusSwitch" data-id="{{$keywordInfo->seoKeywordID}}" type="checkbox" {{ $keywordInfo->status_flg === 'A' ? 'checked' : '' }} data-bootstrap-switch data-off-color="danger" data-on-color="success">
                              </td>
                              <td>
                                  <!-- Edit Button -->
                                    <a href="{{ localized_route('dashboard.keywordUpdate', ['keyword' => $keywordInfo->seoKeywordID]) }}" class="btn btn-sm btn-primary">
                                        <i class="fas fa-edit"></i>
                                    </a>
    
                                    <!-- Delete Button -->
                                    <form action="{{ localized_route('dashboard.keywordDelete') }}" method="POST" style="display:inline;">
                                        @csrf
                                        <input type="hidden" name="websiteId" value="{{ $keywordInfo->seoKeywordID }}">
                                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </form>    
                              </td>
                            </tr>

                            @php
                                $indCnt++;
                            @endphp
                        @endforeach

                  </tbody>
                  <tfoot>
                  <tr>
                    <th>No.</th>
                    <th>Keyword</th>                    
                    <th>Domain</th>
                    <th>Created Date</th>
                    <th>Last usage</th>
                    <th>Status</th>
                    <th>Action</th>
                  </tr>
                  </tfoot>
                </table>
              </div>

            </div>
          </div>
        </div>
        <!-- /.card-body -->
      </div>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  <footer class="main-footer">
    <strong>Copyright &copy; {{config('site.config.copyright')}} <a href="{{config('site.config._live_site')}}">{{config('site.config._sitename')}}</a>.</strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
      <b>Version</b> {{config('dashboard.config.dashboard_Veresion')}} Template {{config('dashboard.config._dashboard_template_version')}}
    </div>
  </footer>

  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/jquery/jquery.min.js"></script>
<!-- Select2 -->
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/select2/js/select2.full.min.js"></script>
<!-- Tempusdominus Bootstrap 4 -->
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
<!-- Bootstrap Switch -->
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<!-- Bootstrap 4 -->
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- DataTables  & Plugins -->
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables/jquery.dataTables.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/jszip/jszip.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/pdfmake/pdfmake.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/pdfmake/vfs_fonts.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="{{config('dashboard.config._dashboard_main_path')}}plugins/datatables-buttons/js/buttons.colVis.min.js"></script>

<!-- AdminLTE App -->
<script src="{{config('dashboard.config._dashboard_main_path')}}dist/js/adminlte.min.js"></script>
<!-- Page specific script -->
<script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
    
    $("[name='status_flg']").bootstrapSwitch({
        onText: "ON",
        offText: "OFF",
        inverse: true // flips the switch direction
    });
    $("[name='automatic_tracking_status']").bootstrapSwitch({
        onText: "ON",
        offText: "OFF",
        inverse: true // flips the switch direction
    });

    $("#keywordTable").DataTable({
      "responsive": true, "lengthChange": false, "autoWidth": false,
      "lengthMenu": [ [10, 25, 50, 100], [10, 25, 50, 100] ],
      "pageLength": 15,
      "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"]
    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');

    $("input[data-bootstrap-switch]").each(function(){
      $(this).bootstrapSwitch('state', $(this).prop('checked'));
    });
    
    // Handle change for ALL switches
    $(document).on('switchChange.bootstrapSwitch', '.statusSwitch', function (event, state) {
        let id = $(this).data('id');
        let newState = state ? 1 : 0;
        let checkboxName = $(this).attr('name');
        let StatusColName = (checkboxName === 'automatic_tracking_status') 
            ? 'automatic_tracking_status' 
            : 'status_flg';
//alert(id);
        $.ajax({
            url: '/onchange/update-status',
            type: 'POST',
            data: {
                dbTbName:'SeoKeywordUrlRel',//this is model file name
                dbTbColName:'seoKeywordUrlRelID',
                dbTbStatusColName: StatusColName,
                id: {id: id },
                status: newState,
                _token: $('meta[name="csrf-token"]').attr('content')
            },
            success: function (response) {
//console.log(response.message); // "string"
                if (response.success) {
                    //success from Laravel
                    $('#statusModalLabel').text('Success');
                    $('#statusModalBody').text(response.message);
                } else {
                    //Laravel returned success=false (logical error, not HTTP error)
                    $('#statusModalLabel').text('Error');
                    $('#statusModalBody').text(response.message);
                }
                $('#statusModal').modal('show');
            },
            error: function (xhr) {
//console.log(errorMsg); // "string"
                //HTTP-level error (404, 500, etc.)
                let errorMsg = xhr.responseJSON?.message || 'Unknown server error';
                $('#statusModalLabel').text('Error');
                $('#statusModalBody').text(errorMsg);
                $('#statusModal').modal('show');
            }
        });
    });
  });
</script>

</body>
</html>
