import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/presentation/setting/pages/setting_tile.dart';
import 'package:flutter/material.dart';
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
import 'package:enaklo_pos/presentation/sales/pages/sales_page.dart';
import 'package:enaklo_pos/presentation/setting/pages/discount_page.dart';
import 'package:enaklo_pos/presentation/setting/pages/product_page.dart';
import 'package:enaklo_pos/presentation/setting/pages/server_key_page.dart';
import 'package:enaklo_pos/presentation/setting/pages/sync_data_page.dart';
import 'package:enaklo_pos/presentation/setting/pages/tax_page.dart';

import '../../../core/constants/colors.dart';

class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  int currentIndex = 0;
  String? role;

  void indexValue(int index) {
    currentIndex = index;
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    setRole();
  }

  void setRole() {
    AuthLocalDataSource().getAuthData().then((value) {
      setState(() {
        role = value.user!.role;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.background,
      body: Row(
        children: [
          // LEFT CONTENT
          Expanded(
            flex: 2,
            child: Align(
              alignment: Alignment.topCenter,
              child: Material(
                color: AppColors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 16.0,
                        vertical: 12.0,
                      ),
                      width: double.infinity,
                      height: context.deviceHeight * 0.1,
                      decoration: const BoxDecoration(
                        color: AppColors.white,
                        border: Border(
                          bottom: BorderSide(
                            color: AppColors.background,
                            width: 1.0,
                          ),
                        ),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const Text(
                            'Pengaturan',
                            style: TextStyle(
                              color: AppColors.black,
                              fontSize: 20,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          Text(
                            'Kelola pengaturan aplikasi',
                            style: TextStyle(
                              color: AppColors.grey,
                              fontSize: 14,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: SingleChildScrollView(
                        child: Column(
                          children: [
                            role != null && role! != 'admin'
                                ? const SizedBox()
                                : SettingTile(
                                    index: 0,
                                    currentIndex: currentIndex,
                                    title: 'Kelola Produk',
                                    subtitle: 'Kelola produk anda',
                                    icon: Icons.inventory_outlined,
                                    onTap: () => indexValue(0),
                                  ),
                            SettingTile(
                              index: 1,
                              currentIndex: currentIndex,
                              title: 'Kelola Diskon',
                              subtitle: 'Kelola diskon pelanggan',
                              icon: Icons.discount_outlined,
                              onTap: () => indexValue(1),
                            ),
                            SettingTile(
                              index: 2,
                              currentIndex: currentIndex,
                              title: 'Riwayat Transaksi',
                              subtitle: 'Lihat riwayat transaksi',
                              icon: Icons.receipt_long_outlined,
                              onTap: () => indexValue(2),
                            ),
                            SettingTile(
                              index: 3,
                              currentIndex: currentIndex,
                              title: 'Perhitungan Biaya',
                              subtitle: 'Kelola biaya diluar biaya modal',
                              icon: Icons.attach_money_outlined,
                              onTap: () => indexValue(3),
                            ),
                            SettingTile(
                              index: 4,
                              currentIndex: currentIndex,
                              title: 'Sinkronisasi Data',
                              subtitle: 'Sinkronisasi data dari dan ke server',
                              icon: Icons.sync_outlined,
                              onTap: () => indexValue(4),
                            ),
                            SettingTile(
                              index: 6,
                              currentIndex: currentIndex,
                              title: 'Qr Key Setting',
                              subtitle: 'Kelola QR Key',
                              icon: Icons.qr_code_2_outlined,
                              onTap: () => indexValue(6),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),

          // RIGHT CONTENT
          Expanded(
            flex: 4,
            child: Align(
              alignment: AlignmentDirectional.topStart,
              child: IndexedStack(
                index: currentIndex,
                children: [
                  role != null && role! != 'admin' ? SizedBox() : ProductPage(),
                  DiscountPage(),
                  SalesPage(),
                  TaxPage(),
                  SyncDataPage(),
                  ProductPage(),
                  ServerKeyPage()
                  // Text('tax'),
                  // ManageDiscount(),
                  // ManagePrinterPage(),
                  // ManageTax(),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
