<script>
  import { onMount } from 'svelte';
  import { fetchLocations, createLocation, updateLocation, deleteLocation } from '$lib/api/locations.js';
  import { success as toastSuccess, error as toastError } from '$lib/utils/toast.js';
  import DataTable from '$lib/components/DataTable.svelte';
  import Modal from '$lib/components/Modal.svelte';
  import SelectDropdown from '$lib/components/SelectDropdown.svelte';
  import { Plus, Edit2, Trash2, ArrowLeft } from 'lucide-svelte';

  let loading = $state(false);
  let locations = $state([]);
  let modalOpen = $state(false);
  let modalMode = $state('create');
  let saving = $state(false);
  let formData = $state({ LocationID: null, Code: '', Name: '', Type: '', ParentID: null });
  let deleteConfirmOpen = $state(false);
  let deleteItem = $state(null);

  const typeLabels = {
    ROOM: 'Room',
    BUILDING: 'Building',
    FLOOR: 'Floor',
    AREA: 'Area',
  };

  const columns = [
    { key: 'LocCode', label: 'Code', class: 'font-medium' },
    { key: 'LocFull', label: 'Name' },
    { key: 'LocTypeLabel', label: 'Type' },
    { key: 'actions', label: 'Actions', class: 'w-32 text-center' },
  ];

  onMount(async () => {
    await loadLocations();
  });

  async function loadLocations() {
    loading = true;
    try {
      const response = await fetchLocations();
      locations = Array.isArray(response.data) ? response.data : [];
    } catch (err) {
      toastError(err.message || 'Failed to load locations');
      locations = [];
    } finally {
      loading = false;
    }
  }

  function openCreateModal() {
    modalMode = 'create';
    formData = { LocationID: null, Code: '', Name: '', Type: '', ParentID: null };
    modalOpen = true;
  }

  function openEditModal(row) {
    modalMode = 'edit';
    formData = {
      LocationID: row.LocationID,
      Code: row.LocCode,
      Name: row.LocFull,
      Type: row.LocType || '',
      ParentID: row.Parent,
    };
    modalOpen = true;
  }

  async function handleSave() {
    saving = true;
    try {
      if (modalMode === 'create') {
        await createLocation(formData);
        toastSuccess('Location created successfully');
      } else {
        await updateLocation(formData);
        toastSuccess('Location updated successfully');
      }
      modalOpen = false;
      await loadLocations();
    } catch (err) {
      toastError(err.message || 'Failed to save location');
    } finally {
      saving = false;
    }
  }

  function confirmDelete(row) {
    deleteItem = row;
    deleteConfirmOpen = true;
  }

  async function handleDelete() {
    try {
      await deleteLocation(deleteItem.LocationID);
      toastSuccess('Location deleted successfully');
      deleteConfirmOpen = false;
      await loadLocations();
    } catch (err) {
      toastError(err.message || 'Failed to delete location');
    }
  }

  const parentOptions = $derived(
    locations
      .filter((l) => l.LocationID !== formData.LocationID)
      .map((l) => ({ value: l.LocationID.toString(), label: l.LocFull }))
  );
</script>

<div class="p-6">
  <div class="flex items-center gap-4 mb-6">
    <a href="/master-data" class="btn btn-ghost btn-circle">
      <ArrowLeft class="w-5 h-5" />
    </a>
    <div class="flex-1">
      <h1 class="text-3xl font-bold text-gray-800">Locations</h1>
      <p class="text-gray-600">Manage locations and facilities</p>
    </div>
    <button class="btn btn-primary" onclick={openCreateModal}>
      <Plus class="w-4 h-4 mr-2" />
      Add Location
    </button>
  </div>

  <div class="bg-base-100 rounded-lg shadow border border-base-200">
    <DataTable
      {columns}
      data={locations.map((l) => ({
        ...l,
        LocTypeLabel: typeLabels[l.LocType] || l.LocType || '-',
      }))}
      {loading}
      emptyMessage="No locations found"
      hover={true}
      bordered={false}
    >
      {#snippet cell({ column, row, value })}
        {#if column.key === 'actions'}
          <div class="flex justify-center gap-2">
            <button class="btn btn-sm btn-ghost" onclick={() => openEditModal(row)}>
              <Edit2 class="w-4 h-4" />
            </button>
            <button class="btn btn-sm btn-ghost text-error" onclick={() => confirmDelete(row)}>
              <Trash2 class="w-4 h-4" />
            </button>
          </div>
        {:else}
          {value}
        {/if}
      {/snippet}
    </DataTable>
  </div>
</div>

<Modal bind:open={modalOpen} title={modalMode === 'create' ? 'Add Location' : 'Edit Location'} size="md">
  <form class="space-y-5" onsubmit={(e) => { e.preventDefault(); handleSave(); }}>
    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
      <div class="form-control">
        <label class="label" for="code">
          <span class="label-text font-medium">Code</span>
          <span class="label-text-alt text-error">*</span>
        </label>
        <input
          id="code"
          type="text"
          class="input input-bordered w-full"
          bind:value={formData.Code}
          placeholder="Enter code"
          required
        />
      </div>
      <div class="form-control">
        <label class="label" for="name">
          <span class="label-text font-medium">Name</span>
          <span class="label-text-alt text-error">*</span>
        </label>
        <input
          id="name"
          type="text"
          class="input input-bordered w-full"
          bind:value={formData.Name}
          placeholder="Enter name"
          required
        />
      </div>
    </div>
    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
      <div class="form-control">
        <label class="label" for="type">
          <span class="label-text font-medium">Type</span>
          <span class="label-text-alt text-error">*</span>
        </label>
        <select
          id="type"
          name="type"
          class="select select-bordered w-full"
          bind:value={formData.Type}
          required
        >
          <option value="">Select type...</option>
          <option value="ROOM">Room</option>
          <option value="BUILDING">Building</option>
          <option value="FLOOR">Floor</option>
          <option value="AREA">Area</option>
        </select>
      </div>
      <SelectDropdown
        label="Parent Location"
        name="parent"
        bind:value={formData.ParentID}
        options={parentOptions}
        placeholder="None"
      />
    </div>
  </form>
  {#snippet footer()}
    <button class="btn btn-ghost" onclick={() => (modalOpen = false)} type="button">Cancel</button>
    <button class="btn btn-primary" onclick={handleSave} disabled={saving} type="button">
      {#if saving}
        <span class="loading loading-spinner loading-sm mr-2"></span>
      {/if}
      {saving ? 'Saving...' : 'Save'}
    </button>
  {/snippet}
</Modal>

<Modal bind:open={deleteConfirmOpen} title="Confirm Delete" size="sm">
  <div class="py-2">
    <p class="text-base-content/80">
      Are you sure you want to delete <strong class="text-base-content">{deleteItem?.LocFull}</strong>?
    </p>
    <p class="text-sm text-error mt-2">This action cannot be undone.</p>
  </div>
  {#snippet footer()}
    <button class="btn btn-ghost" onclick={() => (deleteConfirmOpen = false)} type="button">Cancel</button>
    <button class="btn btn-error" onclick={handleDelete} type="button">Delete</button>
  {/snippet}
</Modal>
