@extends('admin.layouts.app')

@section('title', '商品分类')
@section('page_title', '商品分类')

@section('content')
<div class="page-header mb-20" data-page="admin.product_categories.index">
    <div class="page-header-main">
        <div>
            <div class="page-header-title">商品分类</div>
            <div class="page-header-subtitle">总台视角的商品分类巡检与维护入口，可跨站点查看分类结构，并为商品巡检/导入提供基础数据承接。</div>
        </div>
        <div class="page-header-actions">
            <span class="muted muted-xs">缓存：{{ $cacheMeta['store'] }} / TTL {{ $cacheMeta['ttl'] }}</span>
        </div>
    </div>
</div>

<details class="card mb-20 collapsible filters-card" data-role="collapsible" data-storage-key="admin.product_categories.filters" open>
    <summary class="collapsible-summary filters-summary">
        <div class="flex-between items-center">
            <div>
                <h3 class="mb-0">新增分类</h3>
                <div class="muted muted-xs">在总台为指定站点创建分类；后续可在列表内快速编辑/删除。</div>
            </div>
            <span class="muted muted-xs">点击收起/展开</span>
        </div>
    </summary>

    <div class="collapsible-body filters-body">
        <h3 class="mt-0">新增分类</h3>
        <form method="post" action="/admin/product-categories" class="filters-grid">
            @csrf
            <div class="grid-3">
                <select name="merchant_id">
                    @foreach($merchants as $merchant)
                        <option value="{{ $merchant->id }}" @selected((string) old('merchant_id', $merchants->first()?->id) === (string) $merchant->id)>{{ $merchant->id }} / {{ $merchant->name }}</option>
                    @endforeach
                </select>
                <input name="name" placeholder="分类名称" value="{{ old('name') }}">
                <input name="slug" placeholder="slug" value="{{ old('slug') }}">
                <select name="status">
                    <option value="active" @selected(old('status', 'active') === 'active')>active</option>
                    <option value="inactive" @selected(old('status') === 'inactive')>inactive</option>
                </select>
                <input name="sort" placeholder="排序" value="{{ old('sort', 0) }}">
                <input name="description" placeholder="分类说明" class="span-2" value="{{ old('description') }}">
            </div>
            <div class="mt-12"><button type="submit" class="btn btn-sm">创建分类</button></div>
        </form>
    </div>
</details>

<div class="card list-card">
    <div class="list-card-header">
        <div>
            <h3 class="list-card-title">分类列表</h3>
        </div>
    </div>

    <div class="list-card-body">
        <table class="list-card-table">
            <thead><tr><th>ID</th><th>商家</th><th>名称</th><th>Slug</th><th>状态</th><th>排序</th><th>说明</th><th>操作</th></tr></thead>
            <tbody>
            @forelse($categories as $category)
                <tr>
                    <td>{{ $category->id }}</td>
                    <td>{{ $category->merchant?->name ?? ('商家#'.$category->merchant_id) }}</td>
                    <td>
                        <form method="post" action="/admin/product-categories/{{ $category->id }}" class="actions">
                            @csrf
                            <input name="name" value="{{ $category->name }}">
                    </td>
                    <td><input name="slug" value="{{ $category->slug }}"></td>
                    <td>
                            <select name="status">
                                <option value="active" @selected($category->status==='active')>active</option>
                                <option value="inactive" @selected($category->status==='inactive')>inactive</option>
                            </select>
                    </td>
                    <td><input name="sort" value="{{ $category->sort }}" class="w-90"></td>
                    <td><input name="description" value="{{ $category->description }}"></td>
                    <td>
                            <button type="submit" class="btn btn-secondary btn-sm">更新</button>
                        </form>
                        <form method="post" action="/admin/product-categories/{{ $category->id }}/delete" class="inline" onsubmit="return confirm('确定删除这个分类？');">
                            @csrf
                            <button type="submit" class="btn btn-danger btn-sm">删除</button>
                        </form>
                    </td>
                </tr>
            @empty
                <tr><td colspan="8" class="muted table-empty">暂无分类</td></tr>
            @endforelse
            </tbody>
        </table>

        {{ $categories->links('pagination.admin') }}
    </div>
</div>
@endsection
