import React, { useState } from 'react';
import axios from 'axios';
import './TankItem.css';
import ModalTank from './ModalTank';

const hostURL = "http://localhost:8080";
const host = hostURL + "/api/1.0/tank";

const TankItem = (data) => {

    const [tank, setTank] = useState(null);
    
    //состояние для контроля вызова модального окна
    const[modal, setModal] = useState(false);

    const getTokenForHeader = function () {
        return "Bearer " + localStorage.getItem("token");
    }

    const deleteTank = async function (id) {
        const requestParams = {
            method: "DELETE",
            headers: {
                "Content-Type": "application/json",
                "Authorization": getTokenForHeader(),
            }
        };
        const requestUrl = host + `/${id}`;
        await fetch(requestUrl, requestParams);
    }

    return (
        <div className="tank-card">
            <p className="tank-attribute"> id: {data.tankItem.id} </p>
            <p className="tank-attribute"> название: {data.tankItem.name} </p>
            <p className="tank-attribute"> уровень: {data.tankItem.level.level} </p>
            <p className="tank-attribute"> нация: {data.tankItem.nation.nation} </p>
            <p className="tank-attribute"> стоимость: {data.tankItem.cost} </p>
            <div className='tank-button-group'>
                <button className="tank-button" type="button"
                    onClick={() => setModal(true)}
                >
                    Редактировать
                </button>
                <button className="tank-button" type="button"
                    onClick={event => deleteTank(data.tankItem.id)}
                >
                    Удалить
                </button>
                <ModalTank
                    data={data.tankItem}
                    visible={modal}
                    setVisible={setModal}
                />
            </div>
        </div>
    );
}

export default TankItem
