//随手拍
import { FC } from "react";
import { View, Text, Image } from "@tarojs/components";
import "./index.less";
import { problemList } from "@/utils";
import api from "@/services/core/api";
import take from "@/assets/icon/take.png";
import photo from "@/assets/icon/photo.png";
import video from "@/assets/icon/video.png";
import Taro from "@tarojs/taro";
import useReportStore from "@/store/report";
interface PhotoConfigDto {
  name: string;
  icon: string;
  desc: string[];
  chooseImage?: boolean; //是否选择相册
}
const TakePhoto: FC = ({}) => {
  const { updateDiscover } = useReportStore();
  //入口配置
  let photoConfig: PhotoConfigDto[] = [
    {
      name: "拍照",
      icon: take,
      desc: ["1张近照", "1张远照"],
    },
    {
      name: "相册",
      icon: photo,
      desc: ["选2-3张"],
      chooseImage: true,
    },
    {
      name: "视频",
      icon: video,
      desc: ["不超过5秒"],
    },
  ];

  //跳转拍照页
  const jumpToCameraPage = (chooseImage: boolean | undefined) => {
    if (chooseImage) {
      //选择相册
      Taro.chooseImage({
        count: 3,
        sizeType: ["original", "compressed"],
        sourceType: ["album"],
        success: (res) => {
          let filepaths: string[] = [];
          res.tempFiles?.forEach((it) => {
            Taro.uploadFile({
              url: api.uploadUrl,
              name: "file",
              filePath: it.path,
              success: (res) => {
                const resData = JSON.parse(res.data);
                if (resData.code === 200) {
                  filepaths.push(api.assetsUrl + resData.data);
                }
                Taro.setStorageSync("beforeFilePath", filepaths); //存入照片
                Taro.navigateTo({
                  url: "/pages/Event/view/miniAiIdentify/index",
                });
              },
            });
          });
        },
      });
      return;
    }
    updateDiscover("take");
    Taro.navigateTo({
      url: "/pages/Event/view/cameraComp/index",
    });
  };
  return (
    <View>
      <View className="problem">
        {problemList.map((v) => (
          <Text key={v} className="register">
            {v}
          </Text>
        ))}
      </View>
      <View className="sub-title">
        <Text className="num">180+</Text>问题都能报
      </View>
      <View className="photo-wrap">
        {photoConfig.map((it) => (
          <View
            className="photo-entry"
            key={it.name}
            onClick={() => jumpToCameraPage(it.chooseImage)}
          >
            <Image className="item-icon" src={it.icon} />
            <View className="name">{it.name}</View>
            {it.desc.map((v) => (
              <View className="desc" key={v}>
                {v}
              </View>
            ))}
          </View>
        ))}
      </View>
    </View>
  );
};
export default TakePhoto;
