@extends('layouts.ownerLayout')

@section('title', 'Pagos')

@section('vendor-style')
@vite([
  'resources/assets/vendor/libs/datatables-bs5/datatables.bootstrap5.scss',
  'resources/assets/vendor/libs/datatables-responsive-bs5/responsive.bootstrap5.scss',
  'resources/assets/vendor/libs/datatables-buttons-bs5/buttons.bootstrap5.scss',
  'resources/assets/vendor/libs/notyf/notyf.scss',
  'resources/assets/vendor/libs/sweetalert2/sweetalert2.scss'
])
@endsection

@section('vendor-script')
@vite([
  'resources/assets/vendor/libs/datatables-bs5/datatables-bootstrap5.js',
  'resources/assets/vendor/libs/notyf/notyf.js',
  'resources/assets/vendor/libs/sweetalert2/sweetalert2.js'
])
@endsection

@section('page-script')
@vite('resources/assets/js/owner-payments.js')
<script>
  window.__paymentPageData = {
    canManage: @json($canManagePayments),
    openCreateModal: @json(old('form_type') === 'create'),
    openEditModal: @json(old('form_type') === 'edit'),
    editPaymentId: @json(old('payment_method_id')),
    toast: {
      success: @json(session('status')),
      errors: @json($errors->all())
    }
  };
</script>
@endsection

@section('content')
@if (!$canManagePayments)
  <div class="alert alert-warning mb-6">
    Tu usuario no tiene organización asignada. Solicita al administrador asociarte a una organización para gestionar pagos.
  </div>
@endif

<div class="card mb-6">
  <div class="card-widget-separator-wrapper">
    <div class="card-body card-widget-separator">
      <div class="row gy-4 gy-sm-1">
        <div class="col-sm-6 col-lg-4">
          <div class="d-flex justify-content-between align-items-start card-widget-1 border-end pb-4 pb-sm-0">
            <div>
              <p class="mb-1">Total métodos</p>
              <h4 class="mb-1">{{ number_format($stats['total']) }}</h4>
              <p class="mb-0 text-body-secondary">Registrados</p>
            </div>
            <span class="avatar me-sm-6">
              <span class="avatar-initial rounded bg-label-primary"><i class="icon-base ti tabler-credit-card icon-28px"></i></span>
            </span>
          </div>
        </div>
        <div class="col-sm-6 col-lg-4">
          <div class="d-flex justify-content-between align-items-start card-widget-2 border-end pb-4 pb-sm-0">
            <div>
              <p class="mb-1">Activos</p>
              <h4 class="mb-1">{{ number_format($stats['active']) }}</h4>
              <p class="mb-0 text-body-secondary">Disponibles para cobrar</p>
            </div>
            <span class="avatar p-2 me-lg-6">
              <span class="avatar-initial rounded bg-label-success"><i class="icon-base ti tabler-checks icon-28px"></i></span>
            </span>
          </div>
        </div>
        <div class="col-sm-6 col-lg-4">
          <div class="d-flex justify-content-between align-items-start">
            <div>
              <p class="mb-1">Por defecto</p>
              <h4 class="mb-1">{{ number_format($stats['default']) }}</h4>
              <p class="mb-0 text-body-secondary">Método principal</p>
            </div>
            <span class="avatar p-2">
              <span class="avatar-initial rounded bg-label-info"><i class="icon-base ti tabler-star icon-28px"></i></span>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="card">
  <div class="card-header border-bottom">
    <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-4">
      <h5 class="card-title mb-0">Métodos de pago</h5>
      <button type="button" class="btn btn-primary" id="btnOpenCreatePayment" @disabled(!$canManagePayments)>
        <i class="icon-base ti tabler-plus icon-xs me-1"></i>
        Agregar método
      </button>
    </div>
    <div class="d-flex justify-content-between align-items-center row pt-4 gap-6 gap-md-0 g-md-6">
      <div class="col-md-4 payment_status_filter"></div>
      <div class="col-md-4 payment_default_filter"></div>
      <div class="col-md-4 payment_order_filter"></div>
    </div>
  </div>
  <div class="card-datatable">
    <table class="datatables-owner-payments table border-top">
      <thead>
        <tr>
          <th>ID</th>
          <th>Método</th>
          <th>Datos de pago</th>
          <th>Estado</th>
          <th>Por defecto</th>
          <th>Orden</th>
          <th>Acciones</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($methods as $method)
          <tr>
            <td>{{ $method->id }}</td>
            <td class="fw-medium text-heading">{{ $method->name }}</td>
            <td>
              <div class="d-flex flex-column">
                <span>{{ $method->account_holder ?: 'Sin titular' }}</span>
                <small>{{ $method->account_reference ?: 'Sin referencia' }}</small>
              </div>
            </td>
            <td><span class="badge {{ $method->is_active ? 'bg-label-success' : 'bg-label-secondary' }}">{{ $method->is_active ? 'Activo' : 'Inactivo' }}</span></td>
            <td><span class="badge {{ $method->is_default ? 'bg-label-primary' : 'bg-label-secondary' }}">{{ $method->is_default ? 'Sí' : 'No' }}</span></td>
            <td>{{ $method->sort_order }}</td>
            <td>
              <div class="d-flex align-items-center">
                <button
                  type="button"
                  class="btn btn-text-secondary rounded-pill waves-effect btn-icon btn-edit-payment"
                  title="Editar"
                  data-id="{{ $method->id }}"
                  data-name="{{ $method->name }}"
                  data-account-holder="{{ $method->account_holder }}"
                  data-account-reference="{{ $method->account_reference }}"
                  data-payment-details="{{ $method->payment_details }}"
                  data-is-active="{{ $method->is_active ? 1 : 0 }}"
                  data-is-default="{{ $method->is_default ? 1 : 0 }}"
                  data-sort-order="{{ $method->sort_order }}"
                  data-update-url="{{ route('owner.settings.payments.update', $method) }}"
                >
                  <i class="icon-base ti tabler-edit icon-22px"></i>
                </button>

                <form id="delete-payment-form-{{ $method->id }}" action="{{ route('owner.settings.payments.destroy', $method) }}" method="POST">
                  @csrf
                  @method('DELETE')
                  <button
                    type="button"
                    class="btn btn-text-secondary rounded-pill waves-effect btn-icon btn-delete-payment"
                    data-form-id="delete-payment-form-{{ $method->id }}"
                    data-name="{{ $method->name }}"
                    title="Eliminar">
                    <i class="icon-base ti tabler-trash icon-22px"></i>
                  </button>
                </form>
              </div>
            </td>
          </tr>
        @endforeach
      </tbody>
    </table>
  </div>
</div>

<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasCreatePayment" aria-labelledby="offcanvasCreatePaymentLabel">
  <div class="offcanvas-header border-bottom">
    <h5 id="offcanvasCreatePaymentLabel" class="offcanvas-title">Nuevo método de pago</h5>
    <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body mx-0 flex-grow-0 p-6 h-100">
    <form id="createPaymentForm" method="POST" action="{{ route('owner.settings.payments.store') }}" class="h-100 d-flex flex-column">
      @csrf
      <input type="hidden" name="form_type" value="create">
      <div class="mb-4">
        <label class="form-label" for="create_name">Nombre</label>
        <input type="text" class="form-control" id="create_name" name="name" value="{{ old('form_type') === 'create' ? old('name') : '' }}" required>
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_account_holder">Titular</label>
        <input type="text" class="form-control" id="create_account_holder" name="account_holder" value="{{ old('form_type') === 'create' ? old('account_holder') : '' }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_account_reference">Referencia / cuenta / número</label>
        <input type="text" class="form-control" id="create_account_reference" name="account_reference" value="{{ old('form_type') === 'create' ? old('account_reference') : '' }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_payment_details">Detalles de pago</label>
        <textarea class="form-control" id="create_payment_details" name="payment_details" rows="4">{{ old('form_type') === 'create' ? old('payment_details') : '' }}</textarea>
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_sort_order">Orden</label>
        <input type="number" min="0" class="form-control" id="create_sort_order" name="sort_order" value="{{ old('form_type') === 'create' ? old('sort_order', 100) : 100 }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_is_active">Estado</label>
        <select class="form-select" id="create_is_active" name="is_active">
          <option value="1" @selected(old('form_type') === 'create' ? old('is_active', '1') === '1' : true)>Activo</option>
          <option value="0" @selected(old('form_type') === 'create' && old('is_active') === '0')>Inactivo</option>
        </select>
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_is_default">Usar por defecto</label>
        <select class="form-select" id="create_is_default" name="is_default">
          <option value="1" @selected(old('form_type') === 'create' && old('is_default') === '1')>Sí</option>
          <option value="0" @selected(old('form_type') === 'create' ? old('is_default', '0') === '0' : true)>No</option>
        </select>
      </div>
      <div class="mt-auto pt-4 d-flex gap-3">
        <button type="button" class="btn btn-label-secondary" data-bs-dismiss="offcanvas">Cancelar</button>
        <button type="submit" class="btn btn-primary" @disabled(!$canManagePayments)>Guardar</button>
      </div>
    </form>
  </div>
</div>

<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasEditPayment" aria-labelledby="offcanvasEditPaymentLabel">
  <div class="offcanvas-header border-bottom">
    <h5 id="offcanvasEditPaymentLabel" class="offcanvas-title">Editar método de pago</h5>
    <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body mx-0 flex-grow-0 p-6 h-100">
    <form id="editPaymentForm" method="POST" action="#" class="h-100 d-flex flex-column">
      @csrf
      @method('PUT')
      <input type="hidden" name="form_type" value="edit">
      <input type="hidden" name="payment_method_id" id="edit_payment_method_id" value="{{ old('payment_method_id') }}">
      <div class="mb-4">
        <label class="form-label" for="edit_name">Nombre</label>
        <input type="text" class="form-control" id="edit_name" name="name" value="{{ old('form_type') === 'edit' ? old('name') : '' }}" required>
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_account_holder">Titular</label>
        <input type="text" class="form-control" id="edit_account_holder" name="account_holder" value="{{ old('form_type') === 'edit' ? old('account_holder') : '' }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_account_reference">Referencia / cuenta / número</label>
        <input type="text" class="form-control" id="edit_account_reference" name="account_reference" value="{{ old('form_type') === 'edit' ? old('account_reference') : '' }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_payment_details">Detalles de pago</label>
        <textarea class="form-control" id="edit_payment_details" name="payment_details" rows="4">{{ old('form_type') === 'edit' ? old('payment_details') : '' }}</textarea>
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_sort_order">Orden</label>
        <input type="number" min="0" class="form-control" id="edit_sort_order" name="sort_order" value="{{ old('form_type') === 'edit' ? old('sort_order', 100) : 100 }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_is_active">Estado</label>
        <select class="form-select" id="edit_is_active" name="is_active">
          <option value="1" @selected(old('form_type') === 'edit' ? old('is_active', '1') === '1' : true)>Activo</option>
          <option value="0" @selected(old('form_type') === 'edit' && old('is_active') === '0')>Inactivo</option>
        </select>
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_is_default">Usar por defecto</label>
        <select class="form-select" id="edit_is_default" name="is_default">
          <option value="1" @selected(old('form_type') === 'edit' && old('is_default') === '1')>Sí</option>
          <option value="0" @selected(old('form_type') === 'edit' ? old('is_default', '0') === '0' : true)>No</option>
        </select>
      </div>
      <div class="mt-auto pt-4 d-flex gap-3">
        <button type="button" class="btn btn-label-secondary" data-bs-dismiss="offcanvas">Cancelar</button>
        <button type="submit" class="btn btn-primary" @disabled(!$canManagePayments)>Actualizar</button>
      </div>
    </form>
  </div>
</div>
@endsection
