import { useState, useEffect } from "react";
import { View } from "@tarojs/components";
import RadioCardItem from "@/components/wemeta-radio-card-item/index";
export type TRadioItem = {
  text: string
  value: string
  checked: boolean
};
interface Props {
  value: string
  radioList: TRadioItem[];
  onChange: (current: string) => void;
}
export default function Index({ value, radioList, onChange }: Props) {

  const [_radioList, setRadioList] = useState(radioList)

  const onRadioClick = (item: TRadioItem)=> {
    if(item.checked) return false
    const list = _radioList.map(radioItem=> {
      return {
        ...radioItem,
        checked: item.text === radioItem.text
      }
    })
    setRadioList(list)
    onChange(item.value)
    return false;
  }
  
  useEffect(()=> {
    const list = _radioList.map(radioItem=> {
      return {
        ...radioItem,
        checked: value  === radioItem.value
      }
    })
    setRadioList(list) 
  }, [value])


  return (
      <View className="flex flex-col gap-16">
      {_radioList.map((item, index) => {
        return (
          <View
            onClick={() => {
              onRadioClick(item);
            }}
            key={index}
          >
            <RadioCardItem
              text={item.text}
              checked={item.checked}
            ></RadioCardItem>
          </View>
        );
      })}
    </View>
  );
}