import React, { Component } from "react";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import Header from "@/components/Main/Header";
import DetailPT from "@/components/Main/DetailPT";
import { getLog } from "@/actions/log";
import { Row, Col } from "reactstrap";
import Timeline from "@/components/Main/Timeline";
import { getOnePT } from "@/actions/PT";
import Link from "next/link";
import Loader from "@/components/Common/Loader";
import { connect } from "react-redux";

class Pemantauan extends Component {
	constructor(props) {
		super(props);
		this.state = {
			pt: {},
			log: {},
		};
	}

	static async getInitialProps({ query }) {
		return { query };
	}

	componentDidMount = async () => {
		const { query, token } = this.props;
		const log = await getLog(token, query.ptId);
		this.setState({ log });
		const pt = await getOnePT(token, query.ptId);
		this.setState({ pt });
	};

	render() {
		const { pt, log } = this.state;

		return (
			<ContentWrapper unwrap>
				<div className="p-3">
					<div className="content-heading">
						<div>Pemantauan {pt?.data && pt.data.nama}</div>
						<div className="ml-auto">
							<Link href="/app/pemantauan">
								<button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
							</Link>
						</div>
					</div>
					<Row>
						<Col xl="9">{log?.data ? <Timeline data={log.data} /> : <Loader />}</Col>
						<Col xl="3">{pt?.data ? <DetailPT data={pt.data} /> : <Loader />}</Col>
					</Row>
				</div>
			</ContentWrapper>
		);
	}
}

const mapStateToProps = (state) => ({ user: state.user, token: state.token });
export default connect(mapStateToProps)(Pemantauan);
