import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:worker_client/models/entities/Order.dart';
import 'package:worker_client/services/order.dart';
import 'package:worker_client/widgets/MoreList.dart';
import 'package:worker_client/widgets/MyCard/index.dart';
import 'package:worker_client/widgets/layout/BasicPage.dart';

class Order extends BasicPage {
  Order({Key? key}) : super(key: key) {
    tabIndex = 1;
    naviTitle = '订单列表';
  }

  List<OrderModel> orderList = [];
  final q = Rx<String>('');

  List<String> tabValues = [
    '待作业',
    '进行中',
    '已完成',
  ];
  final currentIndex = Rx<int>(0);

  @override
  void beforeShow() {
    super.beforeShow();
  }

  List<OrderModel> GetList(value) {
    List<OrderModel> list = [];
    value.forEach((item) => {list.add(OrderModel.fromJson(item))});
    return list;
  }

  Map<String, dynamic> map = {'pageNum': 1};

  void goDetail(OrderModel item) {
    Get.toNamed('/orderDetail', arguments: {'id': item.orderId});
  }

  @override
  Widget builder(BuildContext context) {
    return Container(
      height: 610.h,
      width: 375.w,
      decoration: const BoxDecoration(
        color: Colors.white
      ),
      child: Column(
        children: [
          Container(
            width: 345.w,
            height: 34.h,
            margin: EdgeInsets.symmetric(vertical: 15.h, horizontal: 15.w),
            padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10.w),
            decoration: BoxDecoration(
                color: const Color(0xFFf8f8f8),
                borderRadius: BorderRadius.all(Radius.circular(17.w))),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  'images/index/search.png',
                  width: 15.w,
                  height: 15.w,
                ),
                Container(
                  width: 300.w,
                  margin: EdgeInsets.fromLTRB(5.w, 0, 0, 0),
                  child: TextField(
                    style: TextStyle(fontSize: 14.sp),
                    decoration: const InputDecoration(
                      isCollapsed: true,
                      hintText: '搜索关键字查询',
                      border: InputBorder.none,
                      counterText: '', //去掉计数
                      floatingLabelBehavior: FloatingLabelBehavior.never,
                    ),
                    onChanged: (e) {
                      q(e);
                    },
                  ),
                ),
              ],
            ),
          ),
          Container(
              width: 345.w,
              decoration: BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                      color: const Color(0x14000000),
                      offset: Offset(0, 1.w),
                      blurRadius: 0)
                ],
                border: Border(
                  top: BorderSide.none,
                  bottom:
                      BorderSide(width: 1.w, color: const Color(0x14000000)),
                  left: BorderSide.none,
                  right: BorderSide.none, // 右边不设置边框
                ),
              ),
              child: Obx(() => Row(
                    children: tabValues.map((e) {
                      int index = tabValues.indexOf(e);
                      return Expanded(
                        flex: 1,
                        child: GestureDetector(
                          onTap: () {
                            currentIndex(index);
                          },
                          child: SizedBox(
                            height: 44.h,
                            child: Stack(
                              alignment: Alignment.center,
                              children: [
                                Text(
                                  e,
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                      color: currentIndex.value == index
                                          ? const Color(0xFF06b03b)
                                          : const Color(0xFF999999),
                                      fontSize: 16.sp,
                                      fontWeight: currentIndex.value == index
                                          ? FontWeight.w800
                                          : FontWeight.w500),
                                ),
                                Positioned(
                                    bottom: 0,
                                    child: Container(
                                      width: 48.h,
                                      height: 3.w,
                                      decoration: BoxDecoration(
                                          color: currentIndex.value == index
                                              ? const Color(0xFF06b03b)
                                              : Colors.white),
                                    ))
                              ],
                            ),
                          ),
                        ),
                      );
                    }).toList(),
                  ))),
          SizedBox(
            height: 500.h,
            child: MoreList(
              serves: getOrderList,
              params: map,
              getJson: (BuildContext context, List<dynamic> jsonData) {
                orderList = GetList(jsonData);
                return Container(
                  decoration: const BoxDecoration(color: Colors.white),
                  padding: EdgeInsets.all(15.w),
                  child: Column(
                    children: orderList
                        .map(
                          (item) => MyCard(
                            item: item,
                            job: false,
                            detail: false,
                            goDetail: () {
                              goDetail(item);
                            },
                            buttomText: item.status == 0 ? '开始作业' : '完成作业',
                            onClick: () {},
                          ),
                        )
                        .toList(),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
