import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:worker_client/models/app.dart';
import 'package:worker_client/models/entities/Job.dart';
import 'package:worker_client/services/job.dart';
import 'package:worker_client/widgets/MyCard/index.dart';
import 'package:worker_client/widgets/layout/BasicPage.dart';
import '/widgets/MyDialog.dart' as modal;

class JobDetail extends BasicPage {
  String? location = AppController.t.locationStr;
  final job = Rx<Job>(Job());
  final area = Rx<String>('');
  @override
  void beforeShow() {
    super.beforeShow();
    print(AppController.t.location.value);

    if (Get.arguments != null) {
      //请求详情接口
      getJob();
    }
  }

  getJob() {
    getJobDetail(Get.arguments['id'].toString())
        .then((value) => {job(Job.fromJson(value))});
  }

  Widget startContent() {
    return Column(
      children: [
        Image.asset(
          'images/index/positionImg.png',
          width: 40.w,
        ),
        Container(
          margin: EdgeInsets.fromLTRB(0, 26.h, 0, 0),
          child: Text(
            '当前位置在',
            style: TextStyle(
              fontSize: 17.sp,
              fontWeight: FontWeight.w800,
              color: const Color(0xff323232),
            ),
          ),
        ),
        Container(
          margin: EdgeInsets.fromLTRB(0, 26.h, 0, 0),
          child: Text(
            '南京市南京南站',
            style: TextStyle(
              fontSize: 17.sp,
              fontWeight: FontWeight.w800,
              color: const Color(0xff323232),
            ),
          ),
        )
      ],
    );
  }

  Widget endContent() {
    return Container(
        margin: EdgeInsets.fromLTRB(15.w, 0, 15.w, 0),
        padding: EdgeInsets.symmetric(vertical: 18.h, horizontal: 15.w),
        decoration: BoxDecoration(
            border: Border.all(
                width: 1.w,
                style: BorderStyle.solid,
                color: const Color(0xFFF2F2F2)),
            borderRadius: BorderRadius.all(Radius.circular(20.w))),
        child: Row(
          children: [
            Text(
              '作业面积：',
              style: TextStyle(
                fontSize: 17.sp,
                fontWeight: FontWeight.w500,
                color: const Color(0xff333333),
              ),
            ),
            Expanded(
              child: TextField(
                keyboardType: TextInputType.number,
                style: TextStyle(fontSize: 16.sp),
                decoration: const InputDecoration(
                  isCollapsed: true,
                  hintText: '请输入本次作业的面积',
                  border: InputBorder.none,
                  counterText: '', //去掉计数
                  floatingLabelBehavior: FloatingLabelBehavior.never,
                ),
                onChanged: (e) {
                  area(e);
                },
              ),
            )
          ],
        ));
  }

  void changeJob(Job item) {
    if (item.status == 0) {
      modal.showDialog(
          content: startContent(),
          onConfirm: () {
            startJob(item.jobId.toString(), {'mode': true})
                .then((value) => getJob());
          });
    } else {
      modal.showDialog(
          content: endContent(),
          onConfirm: () {
            if (area.value == '') {
              Fluttertoast.showToast(msg: '请输入正确的面积整数或最多两位小数');
              return false;
            } else {
              overJob(item.jobId.toString(), {
                ...item.toJson(),
                'area': area.value.toString()
              }).then((value) {
                getJob();
                Fluttertoast.showToast(msg: '恭喜完成作业');
              });
            }
          },);
    }
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '详情';
    return Container(
        alignment: Alignment.topCenter,
        child: Column(
          children: [
            Obx(() => MyCard(
                  item: job(),
                  job: true,
                  detail: true,
                  onClick: () {
                    changeJob(job());
                  },
                  buttomText: job().status == 0 ? '开始作业' : '完成作业',
                ))
          ],
        ));
  }
}
