import React, { FC, useState } from "react";
import { Text } from "react-native";
import { Dialog, Input, Button } from "react-native-elements";
import { trainDataType } from "../../../lib/trainPositionTextArray";
import { useCurrentTrain } from "../../../stateBox/useCurrentTrain";
type Props = {
  dialog: boolean;
  setDialog: (dialog: boolean) => void;
  currentTrainData: trainDataType;
  stationInput: string;
  stationNumberInput: string;
  posInput: string;
  descInput: string;
  setPosInput: (pos: string) => void;
  setDescInput: (desc: string) => void;
  station: {
    Station_JP: string;
    StationNumber: string;
  };
};
export const TrainPositionDataPush: FC<Props> = ({
  dialog,
  setDialog,
  currentTrainData,
  stationInput,
  stationNumberInput,
  posInput,
  descInput,
  setPosInput,
  setDescInput,
  station,
}) => {
  const sendPlatformData = () => {
    fetch(`https://n8n.haruk.in/webhook/JR-shikoku-PosID`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        PosId: currentTrainData?.PosNum,
        lineName: currentTrainData?.Line,
        PlatformNum: parseInt(posInput),
        Description: descInput,
        StationName: station.Station_JP,
        StationId: station.StationNumber,
      }),
    })
      .then(() => {
        alert("位置情報データを送信しました。");
        setDialog(false);
        setPosInput("");
        setDescInput("");
      })
      .catch(() => {
        alert("位置情報データの送信に失敗しました。");
      });
  };
  return (
    <Dialog isVisible={dialog} onBackdropPress={() => setDialog(false)}>
      <Text>路線: {currentTrainData?.Line}</Text>
      <Text>地点ID: {currentTrainData?.PosNum}</Text>
      <Text>駅名: {stationInput}</Text>
      <Text>駅ナンバー: {stationNumberInput}</Text>
      <Input
        label="番線"
        inputMode="numeric"
        value={posInput}
        onChangeText={setPosInput}
      />
      <Input
        label="参考情報"
        inputMode="text"
        value={descInput}
        onChangeText={setDescInput}
      />
      <Button title="送信" onPress={sendPlatformData} />
    </Dialog>
  );
};
