mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-11 20:28:55 +03:30
102 lines
3.2 KiB
Dart
102 lines
3.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:cssd/config/urls.dart';
|
|
import 'package:cssd/config/user.dart';
|
|
|
|
class Inputs with ChangeNotifier {
|
|
|
|
List<Map<String, dynamic>> inputItems = [];
|
|
|
|
Future<bool> getInputs({String testResultId = "", String name = "", String uniqueCode = "", String testTypeId = ""}) async {
|
|
print({
|
|
'test_type_id': testTypeId,
|
|
'test_result_id': testResultId,
|
|
'name': name,
|
|
'unique_code': uniqueCode,
|
|
},);
|
|
var response = await http.post(
|
|
Url.getInputs,
|
|
body: {
|
|
'test_type_id': testTypeId,
|
|
'test_result_id': testResultId,
|
|
'name': name,
|
|
'unique_code': uniqueCode,
|
|
},
|
|
headers: {
|
|
'Authorization': User.token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
},
|
|
);
|
|
if (response.statusCode != 200) {
|
|
inputItems = [];
|
|
notifyListeners();
|
|
return false;
|
|
}
|
|
final responseData = json.decode(response.body);
|
|
|
|
// print(responseData);
|
|
|
|
Map<String, dynamic> values;
|
|
if (testResultId.isNotEmpty) {
|
|
response = await http.get(Url.testResultFormValues(testResultId),
|
|
headers: {
|
|
'Authorization': User.token
|
|
});
|
|
values = json.decode(response.body)['data'];
|
|
}
|
|
|
|
var inputs = responseData['data']['inputs'] as List<dynamic>;
|
|
var general = responseData['data']['testType'] as Map<String, dynamic>;
|
|
|
|
clearInputs();
|
|
bool result = false;
|
|
DateTime dateTime = DateTime.now();
|
|
String deviceLabel;
|
|
if (values != null && values.isNotEmpty) {
|
|
result = values['result'] == 'Accept' ? true : false;
|
|
dateTime = values['date'] == null ? DateTime.now() : DateTime.parse(values['date']);
|
|
deviceLabel = values['device_id'] ?? '';
|
|
}
|
|
|
|
inputItems.add({'type': 'Label', 'key': 'test_type_id', 'name': general['name'], 'value': general['id']});
|
|
|
|
inputItems.add({'type': 'Text', 'key': 'device_id', 'name': general['device_label'], 'value': deviceLabel});
|
|
inputItems.add({'type': 'Date', 'key': 'date' , 'name': 'date', 'value': dateTime});
|
|
inputItems.add({'type': 'Switch', 'key': 'result', 'name': 'Result', 'value': result});
|
|
|
|
setValues(inputs, values);
|
|
for (var input in inputs) {
|
|
inputItems.add(input as Map<String, dynamic>);
|
|
}
|
|
notifyListeners();
|
|
return true;
|
|
}
|
|
|
|
void setValues(inputs, values) {
|
|
for (var input in inputs) {
|
|
if (input.containsKey('children')) {
|
|
setValues(input['children'], values);
|
|
}
|
|
var key = "${input['name']}_${input['id']}";
|
|
input['key'] = key;
|
|
if(values != null && values.isNotEmpty && values.containsKey(key)) {
|
|
if (input['type'] == 'Range' || input['type'] == 'Temperature' || input['type'] == 'Pressure') {
|
|
input['value'] = double.parse(values[key]);
|
|
} else if (input['type'] == 'Switch'){
|
|
input['value'] = values[key] == "1" ? true : false;
|
|
} else if (input['type'] == 'Time'){
|
|
input['value'] = values[key] == null ? '' : values[key].substring(0, 2) + ":" + values[key].substring(2, 4);
|
|
} else {
|
|
input['value'] = values[key];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void clearInputs() {
|
|
inputItems.clear();
|
|
}
|
|
}
|