import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sefer_admin1/constant/colors.dart';
import 'package:sefer_admin1/constant/style.dart';
import 'package:sefer_admin1/controller/employee_controller/employee_controller.dart';
import 'package:sefer_admin1/controller/functions/launch.dart';
import 'package:sefer_admin1/views/widgets/elevated_btn.dart';
import 'package:sefer_admin1/views/widgets/my_scafold.dart';
import 'package:sefer_admin1/views/widgets/my_textField.dart';

import '../../../constant/links.dart';
import '../../../controller/functions/upload_image copy.dart';

class EmployeePage extends StatelessWidget {
  const EmployeePage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(EmployeeController());
    return GetBuilder<EmployeeController>(builder: (employeeController) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Employee Page'.tr),
        ),
        body: ListView.builder(
          itemCount: employeeController
              .employee.length, // Set the item count based on the employee list
          itemBuilder: (context, index) {
            // Get the employee data for the current index
            var employee = employeeController.employee[index];

            // Return a widget to display the employee information
            return Padding(
              padding: const EdgeInsets.all(3.0),
              child: Container(
                decoration: AppStyle.boxDecoration1,
                child: ListTile(
                  trailing: IconButton(
                    onPressed: () {
                      Get.to(() => EmployeeDetails(
                            index: index,
                          ));
                    },
                    icon: Icon(
                      Icons.shop_two,
                      color: employee['status'].toString().contains('ممتاز')
                          ? AppColor.greenColor
                          : AppColor.accentColor,
                    ),
                  ),
                  title: Column(
                    children: [
                      Text(employee['name']),
                      Text(
                          'Phone: ${employee['phone']}\nEducation: ${employee['education']}'),
                      Text('Status: ${employee['status']}'),
                    ],
                  ), // Display employee name
                  onTap: () {
                    // Add any action you want when the employee is tapped
                  },
                  leading: IconButton(
                    onPressed: () {
                      makePhoneCall(employee['phone'].toString());
                      // launchCommunication(
                      //     'phone', employee['phone'].toString(), '');
                    },
                    icon: const Icon(Icons.phone),
                  ),
                ),
              ),
            );
          },
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            employeeController.id = employeeController.generateRandomId(8);
            Get.to(
              employeeFields(employeeController),
            );
          }, // Icon to display
          backgroundColor: Colors.blue, // Button color (optional)
          tooltip: 'Add Employee',
          child: const Icon(Icons.add), // Tooltip text when long-pressed
        ),
      );
    });
  }

  Scaffold employeeFields(EmployeeController employeeController) {
    return Scaffold(
      appBar: AppBar(),
      body: Form(
        key: employeeController.formKey,
        child: SizedBox(
          height: 500,
          child: ListView(
            children: [
              MyElevatedButton(
                  title: 'front id',
                  onPressed: () async {
                    await ImageController().choosImage(AppLink.uploadEgypt,
                        'idFrontEmployee', employeeController.id);
                  }),
              MyElevatedButton(
                  title: 'back id',
                  onPressed: () async {
                    await ImageController().choosImage(AppLink.uploadEgypt,
                        'idbackEmployee', employeeController.id);
                  }),
              MyTextForm(
                  controller: employeeController.name,
                  label: 'name',
                  hint: 'name',
                  type: TextInputType.name),
              MyTextForm(
                  controller: employeeController.education,
                  label: 'education',
                  hint: 'education',
                  type: TextInputType.name),
              MyTextForm(
                  controller: employeeController.site,
                  label: 'site',
                  hint: 'site',
                  type: TextInputType.name),
              MyTextForm(
                  controller: employeeController.phone,
                  label: 'phone',
                  hint: 'phone',
                  type: TextInputType.phone),
              MyTextForm(
                  controller: employeeController.status,
                  label: 'status',
                  hint: 'status',
                  type: TextInputType.name),
            ],
          ),
        ),
      ),
      bottomNavigationBar: MyElevatedButton(
        title: 'upload',
        onPressed: () async {
          if (employeeController.formKey.currentState!.validate()) {
            await employeeController.addEmployee();
          }
        },
      ),
    );
  }
}

class EmployeeDetails extends StatelessWidget {
  const EmployeeDetails({super.key, required this.index});
  final int index;
  @override
  Widget build(BuildContext context) {
    return MyScafolld(
      title: 'Details',
      isleading: true,
      body: [
        GetBuilder<EmployeeController>(builder: (employeeController) {
          return Column(
            children: [
              SizedBox(
                height: 200,
                width: 400,
                child: Image.network(
                    // https: //server.sefer.click/sefer.click/sefer/card_image/idFrontEmployee-GC15188P.jpg
                    'https://server.sefer.click/sefer.click/sefer/card_image/idFrontEmployee-${employeeController.employee[index]['id']}.jpg'),
              ),
              const SizedBox(
                height: 10,
              ),
              SizedBox(
                height: 200,
                width: 400,
                child: Image.network(
                    'https://server.sefer.click/sefer.click/sefer/card_image/idFrontEmployee-${employeeController.employee[index]['id']}.jpg'),
              )
            ],
          );
        })
      ],
    );
  }
}
