import 'package:flutter/material.dart';
import '../../../../app/theme/app_colors.dart';

/// 多门店管理页面
///
/// 门店层级：总部 → 区域 → 门店
/// 门店员工管理：收银员/店长/管理员
class StoreManagementPage extends StatelessWidget {
  const StoreManagementPage({super.key});

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('门店管理'),
          bottom: const TabBar(
            tabs: [
              Tab(text: '门店列表'),
              Tab(text: '员工管理'),
            ],
          ),
        ),
        body: const TabBarView(
          children: [
            _StoreListTab(),
            _EmployeeListTab(),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            // TODO: Show add store / add employee dialog
          },
          backgroundColor: AppColors.primary,
          child: const Icon(Icons.add_rounded, color: Colors.white),
        ),
      ),
    );
  }
}

class _StoreListTab extends StatelessWidget {
  const _StoreListTab();

  @override
  Widget build(BuildContext context) {
    final stores = [
      _Store('总部', 'headquarters', '上海市黄浦区', 15, true),
      _Store('华东区', 'regional', '上海/杭州/南京', 8, true),
      _Store('徐汇门店', 'store', '上海市徐汇区xxx路', 3, true),
      _Store('静安门店', 'store', '上海市静安区xxx路', 2, true),
      _Store('杭州西湖店', 'store', '杭州市西湖区xxx路', 2, false),
    ];

    return ListView.builder(
      padding: const EdgeInsets.all(20),
      itemCount: stores.length,
      itemBuilder: (context, index) {
        final store = stores[index];
        return Container(
          margin: EdgeInsets.only(
            left: store.level == 'regional' ? 20 : store.level == 'store' ? 40 : 0,
            bottom: 10,
          ),
          padding: const EdgeInsets.all(14),
          decoration: BoxDecoration(
            color: AppColors.surface,
            borderRadius: BorderRadius.circular(12),
            border: Border.all(color: AppColors.borderLight),
          ),
          child: Row(
            children: [
              Container(
                width: 40,
                height: 40,
                decoration: BoxDecoration(
                  color: _levelColor(store.level).withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Icon(_levelIcon(store.level), color: _levelColor(store.level), size: 20),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Text(store.name, style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
                        const SizedBox(width: 8),
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1),
                          decoration: BoxDecoration(
                            color: store.isActive ? AppColors.successLight : AppColors.gray100,
                            borderRadius: BorderRadius.circular(999),
                          ),
                          child: Text(
                            store.isActive ? '营业中' : '休息中',
                            style: TextStyle(fontSize: 10, color: store.isActive ? AppColors.success : AppColors.textTertiary),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 2),
                    Text(store.address, style: const TextStyle(fontSize: 12, color: AppColors.textSecondary)),
                  ],
                ),
              ),
              Text('${store.staffCount}人', style: const TextStyle(fontSize: 12, color: AppColors.textTertiary)),
              const SizedBox(width: 4),
              const Icon(Icons.chevron_right_rounded, size: 18, color: AppColors.textTertiary),
            ],
          ),
        );
      },
    );
  }

  Color _levelColor(String level) {
    switch (level) {
      case 'headquarters': return AppColors.primary;
      case 'regional': return AppColors.info;
      default: return AppColors.success;
    }
  }

  IconData _levelIcon(String level) {
    switch (level) {
      case 'headquarters': return Icons.business_rounded;
      case 'regional': return Icons.location_city_rounded;
      default: return Icons.store_rounded;
    }
  }
}

class _EmployeeListTab extends StatelessWidget {
  const _EmployeeListTab();

  @override
  Widget build(BuildContext context) {
    final employees = [
      ('张经理', '管理员', '总部', Icons.admin_panel_settings_rounded, AppColors.primary),
      ('李店长', '店长', '徐汇门店', Icons.manage_accounts_rounded, AppColors.info),
      ('王店长', '店长', '静安门店', Icons.manage_accounts_rounded, AppColors.info),
      ('赵收银', '收银员', '徐汇门店', Icons.point_of_sale_rounded, AppColors.success),
      ('钱收银', '收银员', '徐汇门店', Icons.point_of_sale_rounded, AppColors.success),
      ('孙收银', '收银员', '静安门店', Icons.point_of_sale_rounded, AppColors.success),
    ];

    return ListView.separated(
      padding: const EdgeInsets.all(20),
      itemCount: employees.length,
      separatorBuilder: (_, __) => const Divider(height: 1),
      itemBuilder: (context, index) {
        final (name, role, store, icon, color) = employees[index];
        return ListTile(
          contentPadding: const EdgeInsets.symmetric(vertical: 4),
          leading: Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: color.withValues(alpha: 0.1),
              borderRadius: BorderRadius.circular(10),
            ),
            child: Icon(icon, color: color, size: 20),
          ),
          title: Text(name, style: const TextStyle(fontWeight: FontWeight.w500)),
          subtitle: Text('$role · $store', style: const TextStyle(fontSize: 12, color: AppColors.textSecondary)),
          trailing: PopupMenuButton<String>(
            itemBuilder: (ctx) => [
              const PopupMenuItem(value: 'edit', child: Text('编辑')),
              const PopupMenuItem(value: 'delete', child: Text('移除')),
            ],
          ),
        );
      },
    );
  }
}

class _Store {
  final String name;
  final String level;
  final String address;
  final int staffCount;
  final bool isActive;

  _Store(this.name, this.level, this.address, this.staffCount, this.isActive);
}
