import { Image, View, Text } from "@tarojs/components";
import "./index.less";

import Taro, { useDidShow } from "@tarojs/taro";
import services from "@/services";
import { useState } from "react";
const ListItem = ({ data }: { data: API.LawDto }) => (
  <View
    className="list-item"
    onTap={() => {
      Taro.navigateTo({
        url: "/pages/Article/index" + "?desc=" + data.description,
      });
    }}
  >
    <View>
      <Image
        src={data.coverImage}
        mode="aspectFill"
        className="list-img"
      ></Image>
    </View>
    <View>
      <View className="list-title">{data.title}</View>
      <Text className="list-content">{data.digestText}</Text>
      <View className="time">{data.createTime}</View>
    </View>
  </View>
);

export default function () {
  const [dataSource, setDataSource] = useState<API.LawDto[]>([]);
  useDidShow(() => {
    services.common.getPolicyList().then((res) => {
      if (res.code === 200) {
        setDataSource(res.data);
      }
    });
  });
  return (
    <View className="law-container">
      {dataSource.map((item) => {
        return <ListItem key={item.id} data={item} />;
      })}
    </View>
  );
}
