import { useChantier } from "@/app/ContextChantier";
import { Chantier } from "@/class/class";
import { getChantiers } from "@/services/ressourcesService";
import { useRouter } from "expo-router";
import { useEffect, useState } from "react";
import {
ActivityIndicator,
Dimensions,
Image,
Pressable,
ScrollView,
StyleSheet,
View
} from "react-native";
import Animated, {
LinearTransition
} from "react-native-reanimated";
import { ThemedButton } from "./themed-button";
import { ThemedText } from "./themed-text";
import { ThemedTextInput } from "./themed-textinput";
import { ThemedView } from "./themed-view";
const screenHeight = Dimensions.get("window").height;
const { width, height } = Dimensions.get("window");
/*
*/
export default function SelectChantier() {
const { chantier, setChantier } = useChantier();
const [search, setSearch] = useState("");
const [isOpen, setIsOpen] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);
const [chantiers, setChantiers] = useState([]);
const router = useRouter();
const AnimatedThemedView = Animated.createAnimatedComponent(ThemedView);
const AnimatedThemedText = Animated.createAnimatedComponent(ThemedText);
const AnimatedThemedButton = Animated.createAnimatedComponent(ThemedButton);
const AnimatedThemedTextInput =
Animated.createAnimatedComponent(ThemedTextInput);
async function onPressOpen(){
setIsLoaded(false);
setIsOpen(!isOpen);
if(!isOpen){
const updatedChantiers = await getChantiers();
setIsLoaded(true);
setChantiers(updatedChantiers)
}
}
function onPressAddChantier(){
router.push("/(tabs)/addChantier")
setIsOpen(false)
}
useEffect(() => {
async function loadChantiers() {
const list = await getChantiers();
setChantiers(list);
}
loadChantiers();
}, []);
function selectChantier(chantier: Chantier): void {
setChantier(chantier);
setIsOpen(false);
}
const renderChantier = (chantier: Chantier, index: number) => {
return (
selectChantier(chantier)}>
Adresse: {chantier.adresse}
Chef de chantier: {chantier.chef.last_name}{" "}{chantier.chef.name}
État: {chantier.etat}
);
};
return (
{isOpen && ( setIsOpen(false)}/>)}
onPressOpen()}>
{isOpen ? "Fermer" : (chantier!=null ? chantier.adresse : "Chantier")}
{isOpen && (
onPressAddChantier()}>
+
{isLoaded?
{chantiers.map((chantier, index) =>
renderChantier(chantier, index)
)}
: }
)}
);
}
const styles = StyleSheet.create({
windowClose: {
//backgroundColor: '#00FF0040',
//borderRadius:10,
padding: 10,
width: "50%",
height: 60,
overflow: "hidden",
zIndex:1000,
},
windowOpean: {
//backgroundColor: '#00FF0040',
//borderRadius:10,
width: "100%",
height: screenHeight * 3/4,
padding: 10,
zIndex:1000,
},
window: {
borderRadius: 15,
height: "100%",
//backgroundColor: '#00FF00',
overflow: "hidden",
},
autoClose: {
position: 'absolute',
top: -height,
left: -width,
width:width*2,
height:height*2,
//backgroundColor: 'rgba(255, 0, 0, 0.5)',
},
menu: {
padding: 5,
flex: 1,
minHeight: 0,
//backgroundColor:'#FF00FF',
},
list: {
flex: 1,
overflow: "hidden",
borderRadius: 10,
},
chantiersList: {
//padding:5,
//backgroundColor:'0000FF',
//flexDirection: 'row',
//alignItems: 'center',
gap: 8,
},
chantier: {
padding: 5,
//marginTop:5,
//margin:5,
borderRadius: 10,
//borderWidth: 1,
flexDirection: 'row',
height: 100,
},
image:{
margin:0,
width: 60,
height: 90,
borderRadius: 5,
marginRight: 10,
},
input: {
width: "100%",
borderWidth: 1,
borderRadius: 10,
padding: 10,
fontSize: 16,
marginBottom: 10,
},
buttonClose: {
width: "100%",
margin: 0,
borderRadius: 15,
padding: 10,
height: 40,
},
buttonOpen: {
width: "50%",
margin: 5,
borderRadius: 10,
padding: 10,
height: 40,
},
buttonText: {
textAlign: "center",
},
buttonAdd:{
borderRadius: 10,
marginBottom: 10,
height: 30,
alignItems: 'center',
justifyContent: 'center',
}
});