Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.7 kB
1
Indexable
Never
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';
import 'package:tailorx/src/data/api/dio_client.dart';
import 'package:tailorx/src/view/booking/models/search_customer_response_model.dart';

import '../../data/api/dio_constants.dart';
import '../../data/api/dio_result.dart';
import '../../utils/colors.dart';
import '../login/login_reponse_model.dart';
import '../edit_order/models/order_drop_down_data_response.dart';

class EditOrderController extends GetxController {
  final DioClient dioClient = DioClient();
  late List<SearchCustomerResult> customerList = [];
  final selectedPaymentValue = "".obs;
  final selectedMasterValue = "".obs;
  final selectedTailorValue = "".obs;
  final selectedStatusValue = "".obs;
  var isLoaded = false;
  late List<OrderDropDownData> dropDownData;
  List<OrderDropDownData> tailors = List.empty(growable: true);
  List<OrderDropDownData> masters = List.empty(growable: true);
  late TextEditingController discountController, previousBalanceController;

  @override
  void onInit() {
    super.onInit();

    discountController = TextEditingController();
    previousBalanceController = TextEditingController();
    getDropDownData();
  }

  Future<void> getDropDownData() async {
    var loginUser = await getLoginData();
    var cid = loginUser.result?.cID ?? "";

    var requestQuery = "?CID=$cid";

    DioResult result =
        await dioClient.get(DioConstants.apiOrderDropDownData + requestQuery);

    if (result is SuccessState) {
      var jsonData = OrderDropDownDataResponse.fromJson(result.value);
      if (jsonData.statusCode == 1) {
        dropDownData = jsonData.result!;
        setupDropDownData();
      }
      update();
    } else if (result is ErrorState) {
      debugPrint("Error occurred! $result");
    }
  }

  void setupDropDownData() {
    for (final data in dropDownData) {
      var designation = data.designation!;
      if (designation.toLowerCase().contains("tailor")) {
        tailors.add(data);
      } else if (designation.toLowerCase().contains("master")) {
        masters.add(data);
      }
    }

    isLoaded = true;
  }

  Future<LoginResponseModel> getLoginData() async {
    const secureStorage = FlutterSecureStorage();
    var readLoginData = await (secureStorage.read(
          key: "save_login_data",
        )) ??
        "";

    debugPrint("Json occurred!$readLoginData");
    if (readLoginData.isNotEmpty) {
      Map<String, dynamic> json = jsonDecode(readLoginData);

      return LoginResponseModel.fromJson(json);
    } else {
      return LoginResponseModel();
    }
  }

  void setPaymentValue(String? value) {
    selectedPaymentValue.value = value ?? "";
  }

  void setMasterValue(String? value) {
    selectedMasterValue.value = value ?? "";
  }

  void setTailorValue(String? value) {
    selectedTailorValue.value = value ?? "";
  }

  void setStatusValue(String? value) {
    selectedStatusValue.value = value ?? "";
  }

  void updateTheOrder() async {
    showLoading();
    var postMap = {
      "Discount": discountController.text,
      "NetAmount": "500",
      "Paid": "200",
      "PaidBalance": previousBalanceController.text,
      "Balance": 250,
      "ModeOfPayment": selectedPaymentValue.value,
      "Status": selectedStatusValue.value,
      "Tailor": selectedTailorValue.value,
      "Master": selectedMasterValue.value,
      "SalesMeters": "20",
      "ProcessBy": "test",
      "QRCode": "",
      "UpdatedBy": "Test",
      "CID": "SA0001",
      "RowID": "10022"
    };

    debugPrint("Post Map $postMap");

    DioResult result =
        await dioClient.post(DioConstants.apiUpdatOrder, postMap);

    if (result is SuccessState) {
      debugPrint("Success");

      hideLoading();
      Get.back();
    } else if (result is ErrorState) {
      debugPrint("Error occurred!");
    }
  }

  showLoading() {
    Get.dialog(
      Dialog(
        elevation: 0.0,
        backgroundColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: const [
              SpinKitRing(
                color: AppColors.primaryColor,
                size: 50.0,
              ),
            ],
          ),
        ),
      ),
    );
  }

  hideLoading() {
    if (Get.isDialogOpen!) Get.back();
  }

  @override
  void onClose() {
    super.onClose();
    discountController.dispose();
    previousBalanceController.dispose();
  }
}